Skip to content

Commit

Permalink
Merge pull request #58 from Adrian-Bielefeldt/53-unit-test-reasoner-t…
Browse files Browse the repository at this point in the history
…imeout

53: Added unit test for reasoner timeout
  • Loading branch information
irina-dragoste authored Nov 26, 2018
2 parents a38d486 + 99190ec commit abb1adf
Showing 1 changed file with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
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;
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.After;
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.
*/
private static int timeout = 1;

/**
* A list of facts to be used in multiple test runs.
*/
private static List<Atom> facts = new ArrayList<>();
/**
* A list of rules to be used in multiple test runs.
*/
private static List<Rule> rules = new ArrayList<>();

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.
*/
@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);
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 infinite_IDB_yz = makeAtom(infinite_IDB, y, z);
Rule infinite_rule = makeRule(infinite_IDB_yz, infinite_IDB_xy);
rules.add(infinite_rule);
}

@Before
public void setUp() throws ReasonerStateException {
reasoner = Reasoner.getInstance();

reasoner.addFacts(facts);
reasoner.addRules(rules);
}

@Test
public void skolem() throws EdbIdbSeparationException, IncompatiblePredicateArityException, IOException, ReasonerStateException {
reasoner.setReasoningTimeout(timeout);
reasoner.setAlgorithm(Algorithm.SKOLEM_CHASE);

reasoner.load();

reasoner.reason();
}

@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();
}

@After
public void tearDown() {
reasoner.close();
}
}

0 comments on commit abb1adf

Please sign in to comment.