From 441695c30dab9f6cdb748eb25ddd797511e81425 Mon Sep 17 00:00:00 2001 From: Adrian Bielefeldt Date: Sun, 25 Nov 2018 15:22:13 +0100 Subject: [PATCH 1/7] 53: Added unit test for reasoner timeout Added a unit test to check the reasoner timeout as per issue 53. The test checks both skolem and restricted chase using a small set of existential rules generating infinite data. A small overhead of one second is accepted to allow for setup and tear down of reasoner resources. --- .../core/reasoner/ReasonerTimeoutTest.java | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java diff --git a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java new file mode 100644 index 000000000..ed86374ea --- /dev/null +++ b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java @@ -0,0 +1,99 @@ +package org.semanticweb.vlog4j.core.reasoner; + +import static org.semanticweb.vlog4j.core.model.implementation.Expressions.makeAtom; +import static org.semanticweb.vlog4j.core.model.implementation.Expressions.makeConstant; +import static org.semanticweb.vlog4j.core.model.implementation.Expressions.makePredicate; +import static org.semanticweb.vlog4j.core.model.implementation.Expressions.makeRule; +import static org.semanticweb.vlog4j.core.model.implementation.Expressions.makeVariable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.rules.Timeout; +import org.semanticweb.vlog4j.core.model.api.Atom; +import org.semanticweb.vlog4j.core.model.api.Predicate; +import org.semanticweb.vlog4j.core.model.api.Rule; +import org.semanticweb.vlog4j.core.model.api.Variable; +import org.semanticweb.vlog4j.core.reasoner.exceptions.EdbIdbSeparationException; +import org.semanticweb.vlog4j.core.reasoner.exceptions.IncompatiblePredicateArityException; +import org.semanticweb.vlog4j.core.reasoner.exceptions.ReasonerStateException; + +/** + * Test case ensuring {@link Reasoner#setReasoningTimeout(Integer)} works as expected and terminates reasoning after the given {@link #timeout}. + * Results are accepted within one second to account for setup and tear down of reasoning resources. + * @author Adrian Bielefeldt + * + */ +public class ReasonerTimeoutTest { + + /** + * The timeout after which reasoning should be completed in seconds. + */ + static int timeout = 1; + + static List facts = new ArrayList<>(); + static List rules = new ArrayList<>(); + + Reasoner reasoner; + + /** + * The timeout after which reasoning should be completed. One second is added to account for setup and tear down of reasoning resources. + */ + @org.junit.Rule + public Timeout globalTimeout = Timeout.seconds(timeout + 1); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + Predicate infinite_EDB = makePredicate("infinite_EDB", 2); + Predicate infinite_IDB = makePredicate("infinite_IDB", 2); + + facts.add(makeAtom(infinite_EDB, makeConstant("A"), makeConstant("B"))); + + Variable x = makeVariable("x"); + Variable y = makeVariable("y"); + + Atom infinite_IDB_xy = makeAtom(infinite_IDB, x, y); + Atom infinite_EDB_xy = makeAtom(infinite_EDB, x, y); + + Rule import_rule = makeRule(infinite_IDB_xy, infinite_EDB_xy); + rules.add(import_rule); + + Variable z = makeVariable("z"); + + Atom further_yz = makeAtom(infinite_IDB, y, z); + Rule infinite_rule = makeRule(further_yz, infinite_IDB_xy); + rules.add(infinite_rule); + } + + @Before + public void setUp() throws Exception { + reasoner = Reasoner.getInstance(); + + reasoner.addFacts(facts); + reasoner.addRules(rules); + + reasoner.setReasoningTimeout(timeout); + } + + @Test + public void skolem() throws EdbIdbSeparationException, IncompatiblePredicateArityException, IOException, ReasonerStateException { + reasoner.setAlgorithm(Algorithm.SKOLEM_CHASE); + + reasoner.load(); + + reasoner.reason(); + } + + @Test + public void restricted() throws EdbIdbSeparationException, IncompatiblePredicateArityException, IOException, ReasonerStateException { + reasoner.setAlgorithm(Algorithm.RESTRICTED_CHASE); + + reasoner.load(); + + reasoner.reason(); + } +} From 953f95b17128923c3f16c8c2cd0c890a50be432c Mon Sep 17 00:00:00 2001 From: Adrian Bielefeldt Date: Mon, 26 Nov 2018 12:02:43 +0100 Subject: [PATCH 2/7] Added test setting timeout after load Added two tests which set the reasoner timeout after loading. --- .../core/reasoner/ReasonerTimeoutTest.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java index ed86374ea..d643e0540 100644 --- a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java +++ b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java @@ -75,12 +75,11 @@ public void setUp() throws Exception { reasoner.addFacts(facts); reasoner.addRules(rules); - - reasoner.setReasoningTimeout(timeout); } @Test public void skolem() throws EdbIdbSeparationException, IncompatiblePredicateArityException, IOException, ReasonerStateException { + reasoner.setReasoningTimeout(timeout); reasoner.setAlgorithm(Algorithm.SKOLEM_CHASE); reasoner.load(); @@ -90,10 +89,33 @@ public void skolem() throws EdbIdbSeparationException, IncompatiblePredicateArit @Test public void restricted() throws EdbIdbSeparationException, IncompatiblePredicateArityException, IOException, ReasonerStateException { + reasoner.setReasoningTimeout(timeout); + reasoner.setAlgorithm(Algorithm.RESTRICTED_CHASE); + + reasoner.load(); + + reasoner.reason(); + } + + @Test + public void skolemAfterLoad() throws EdbIdbSeparationException, IncompatiblePredicateArityException, IOException, ReasonerStateException { + reasoner.setAlgorithm(Algorithm.SKOLEM_CHASE); + + reasoner.load(); + + reasoner.setReasoningTimeout(timeout); + + reasoner.reason(); + } + + @Test + public void restrictedAfterLoad() throws EdbIdbSeparationException, IncompatiblePredicateArityException, IOException, ReasonerStateException { reasoner.setAlgorithm(Algorithm.RESTRICTED_CHASE); reasoner.load(); + reasoner.setReasoningTimeout(timeout); + reasoner.reason(); } } From f0df43709bd427ba4915be94acff7ba347ec3ebf Mon Sep 17 00:00:00 2001 From: Adrian Bielefeldt Date: Mon, 26 Nov 2018 12:03:17 +0100 Subject: [PATCH 3/7] Added closing the reasoner Added closing the reasoner after each test run. --- .../vlog4j/core/reasoner/ReasonerTimeoutTest.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java index d643e0540..cf1c3d9d2 100644 --- a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java +++ b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.List; +import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -118,4 +119,9 @@ public void restrictedAfterLoad() throws EdbIdbSeparationException, Incompatible reasoner.reason(); } + + @After + public void tearDown() { + reasoner.close(); + } } From e8f4e24f46a51b82959527b38ed911bc99d7b395 Mon Sep 17 00:00:00 2001 From: Adrian Bielefeldt Date: Mon, 26 Nov 2018 12:03:53 +0100 Subject: [PATCH 4/7] Added license to ReasonerTimeoutTest --- .../core/reasoner/ReasonerTimeoutTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java index cf1c3d9d2..d8e9333bd 100644 --- a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java +++ b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java @@ -1,5 +1,25 @@ package org.semanticweb.vlog4j.core.reasoner; +/*- + * #%L + * VLog4j Core Components + * %% + * Copyright (C) 2018 VLog4j Developers + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + import static org.semanticweb.vlog4j.core.model.implementation.Expressions.makeAtom; import static org.semanticweb.vlog4j.core.model.implementation.Expressions.makeConstant; import static org.semanticweb.vlog4j.core.model.implementation.Expressions.makePredicate; From 23cb21e0075d6b0b911ea06983d21900ead6e1a4 Mon Sep 17 00:00:00 2001 From: Adrian Bielefeldt Date: Mon, 26 Nov 2018 14:02:51 +0100 Subject: [PATCH 5/7] Set class variables to private --- .../vlog4j/core/reasoner/ReasonerTimeoutTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java index d8e9333bd..da8d7b922 100644 --- a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java +++ b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java @@ -54,12 +54,12 @@ public class ReasonerTimeoutTest { /** * The timeout after which reasoning should be completed in seconds. */ - static int timeout = 1; + private static int timeout = 1; - static List facts = new ArrayList<>(); - static List rules = new ArrayList<>(); + private static List facts = new ArrayList<>(); + private static List rules = new ArrayList<>(); - Reasoner reasoner; + private Reasoner reasoner; /** * The timeout after which reasoning should be completed. One second is added to account for setup and tear down of reasoning resources. From 369dc088fcde4660a7c8ce91cabd73b43d47ae31 Mon Sep 17 00:00:00 2001 From: Adrian Bielefeldt Date: Mon, 26 Nov 2018 14:04:43 +0100 Subject: [PATCH 6/7] Changed to more specific exception --- .../semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java index da8d7b922..0d6dfc885 100644 --- a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java +++ b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java @@ -68,7 +68,7 @@ public class ReasonerTimeoutTest { public Timeout globalTimeout = Timeout.seconds(timeout + 1); @BeforeClass - public static void setUpBeforeClass() throws Exception { + public static void setUpBeforeClass() { Predicate infinite_EDB = makePredicate("infinite_EDB", 2); Predicate infinite_IDB = makePredicate("infinite_IDB", 2); @@ -91,7 +91,7 @@ public static void setUpBeforeClass() throws Exception { } @Before - public void setUp() throws Exception { + public void setUp() throws ReasonerStateException { reasoner = Reasoner.getInstance(); reasoner.addFacts(facts); From 99190eca0897e1f69efbcab56985542ea52b2dc5 Mon Sep 17 00:00:00 2001 From: Adrian Bielefeldt Date: Mon, 26 Nov 2018 14:11:39 +0100 Subject: [PATCH 7/7] Documentation and clearer variable names Documented the facts and rules used for the ReasonerTimeoutTest Changed a variable name to be clearer about its content --- .../core/reasoner/ReasonerTimeoutTest.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java index 0d6dfc885..3c8f707e4 100644 --- a/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java +++ b/vlog4j-core/src/test/java/org/semanticweb/vlog4j/core/reasoner/ReasonerTimeoutTest.java @@ -56,7 +56,13 @@ public class ReasonerTimeoutTest { */ private static int timeout = 1; + /** + * A list of facts to be used in multiple test runs. + */ private static List facts = new ArrayList<>(); + /** + * A list of rules to be used in multiple test runs. + */ private static List rules = new ArrayList<>(); private Reasoner reasoner; @@ -67,6 +73,15 @@ public class ReasonerTimeoutTest { @org.junit.Rule public Timeout globalTimeout = Timeout.seconds(timeout + 1); + /** + * This method provides the {@link #facts} and {@link #rules} to be used in all test runs. + * To test if the timeout works as expected, a small set of facts and rules is used that results in an infinite chase. + * Facts: + * infinite_EDB(A, B) + * Rules: + * infinite_IDB(?x, ?y) :- infinite_EDB(?x, ?y) + * infinite_IDB(?y, ?z) :- infinite_IDB(?x, ?y) + */ @BeforeClass public static void setUpBeforeClass() { Predicate infinite_EDB = makePredicate("infinite_EDB", 2); @@ -85,8 +100,8 @@ public static void setUpBeforeClass() { Variable z = makeVariable("z"); - Atom further_yz = makeAtom(infinite_IDB, y, z); - Rule infinite_rule = makeRule(further_yz, infinite_IDB_xy); + Atom infinite_IDB_yz = makeAtom(infinite_IDB, y, z); + Rule infinite_rule = makeRule(infinite_IDB_yz, infinite_IDB_xy); rules.add(infinite_rule); }