Search in sources :

Example 36 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class OWL2ParserTest method equivalentProperty.

@Test
public void equivalentProperty() {
    // q(X, Y) <-> p(X, Y).
    try {
        OWL2Parser parser = new OWL2Parser(PREFIXES + ":p rdf:type owl:ObjectProperty . " + ":q rdf:type owl:ObjectProperty . " + ":p owl:equivalentProperty :q .");
        int nbRules = 0;
        while (parser.hasNext()) {
            Object o = parser.next();
            if (o instanceof Rule) {
                ++nbRules;
                Rule r = (Rule) o;
                Atom subProperty = (Atom) r.getBody().iterator().next();
                Atom property = (Atom) r.getHead().iterator().next();
                Assert.assertTrue(Q.equals(property.getPredicate()) || P.equals(property.getPredicate()));
                Assert.assertTrue(Q.equals(subProperty.getPredicate()) || P.equals(subProperty.getPredicate()));
                Assert.assertEquals(property.getTerm(0), subProperty.getTerm(0));
                Assert.assertEquals(property.getTerm(1), subProperty.getTerm(1));
            }
        }
        parser.close();
        Assert.assertEquals("Number of assertions found:", 2, nbRules);
    } catch (Exception e) {
        Assert.assertFalse(e.getMessage(), true);
    }
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Atom(fr.lirmm.graphik.graal.api.core.Atom) OWL2ParserException(fr.lirmm.graphik.graal.io.owl.OWL2ParserException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Test(org.junit.Test)

Example 37 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class SparqlRuleTest method testStringLiteral.

@Test
public void testStringLiteral() {
    String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "PREFIX : <" + PREFIX + ">" + "CONSTRUCT" + "{" + "  ?x :q 'toto' " + "}" + "WHERE" + "{" + "	?x :p 'toto' ." + "}";
    Rule rule = new SparqlRuleParser(query).getRule();
    CloseableIteratorWithoutException<Atom> it = rule.getBody().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        Assert.assertEquals(P, a.getPredicate());
        Assert.assertEquals(STRING, a.getTerm(1));
    }
    it = rule.getHead().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        Assert.assertEquals(Q, a.getPredicate());
        Assert.assertEquals(STRING, a.getTerm(1));
    }
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 38 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class SparqlRuleTest method testRDFType.

@Test
public void testRDFType() {
    String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "PREFIX : <" + PREFIX + ">" + "CONSTRUCT" + "{" + "  ?x rdf:type :B " + "}" + "WHERE" + "{" + "	?x a :A  ." + "}";
    Rule rule = new SparqlRuleParser(query).getRule();
    CloseableIteratorWithoutException<Atom> it = rule.getBody().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        Assert.assertEquals(A, a.getPredicate());
    }
    it = rule.getHead().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        Assert.assertEquals(B, a.getPredicate());
    }
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 39 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class DefaultKnowledgeBaseTest method testGetRuleNames.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#getRuleNames()}.
 * @throws ParseException
 */
@Test
public void testGetRuleNames() throws ParseException {
    Rule r1 = DlgpParser.parseRule("[R1] p(x) :- q(X).");
    Rule r2 = DlgpParser.parseRule("[R2] q(x) :- r(X).");
    AtomSet store = new DefaultInMemoryGraphStore();
    RuleSet ruleset = new LinkedListRuleSet();
    ruleset.add(r1);
    ruleset.add(r2);
    KnowledgeBase kb = new DefaultKnowledgeBase(store, ruleset);
    Assert.assertTrue(kb.getRuleNames().contains("R1"));
    Assert.assertTrue(kb.getRuleNames().contains("R2"));
    Assert.assertEquals(r1, kb.getRule("R1"));
    Assert.assertEquals(r2, kb.getRule("R2"));
    kb.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Test(org.junit.Test)

Example 40 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class DefaultKnowledgeBaseTest method testDefaultKnowledgeBaseParserOfObject.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#DefaultKnowledgeBase(fr.lirmm.graphik.graal.api.io.Parser)}.
 *
 * @throws ParseException
 * @throws AtomSetException
 */
@Test
public void testDefaultKnowledgeBaseParserOfObject() throws ParseException, AtomSetException {
    Atom aa = DlgpParser.parseAtom("q(a).");
    Atom ab = DlgpParser.parseAtom("q(b).");
    Atom ac = DlgpParser.parseAtom("q(c).");
    Rule r = DlgpParser.parseRule("[R] p(X) :- q(X).");
    NegativeConstraint nc = DlgpParser.parseNegativeConstraint("[NC] ! :- q(X), p(X).");
    KnowledgeBase kb = new DefaultKnowledgeBase(new DlgpParser("[R] p(X) :- q(X). q(a), q(b). q(c). [NC] ! :- q(X), p(X)."));
    Assert.assertTrue(kb.getOntology().contains(r));
    Assert.assertTrue(kb.getOntology().contains(nc));
    Assert.assertTrue(kb.getFacts().contains(aa));
    Assert.assertTrue(kb.getFacts().contains(ab));
    Assert.assertTrue(kb.getFacts().contains(ac));
    kb.close();
}
Also used : KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) DlgpParser(fr.lirmm.graphik.graal.io.dlp.DlgpParser) Rule(fr.lirmm.graphik.graal.api.core.Rule) Atom(fr.lirmm.graphik.graal.api.core.Atom) NegativeConstraint(fr.lirmm.graphik.graal.api.core.NegativeConstraint) Test(org.junit.Test)

Aggregations

Rule (fr.lirmm.graphik.graal.api.core.Rule)141 Test (org.junit.Test)87 Atom (fr.lirmm.graphik.graal.api.core.Atom)64 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)52 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)49 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)26 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)23 LinkedList (java.util.LinkedList)19 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)16 Term (fr.lirmm.graphik.graal.api.core.Term)15 Prefix (fr.lirmm.graphik.util.Prefix)14 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)14 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)13 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)9 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)9 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)7 KnowledgeBase (fr.lirmm.graphik.graal.api.kb.KnowledgeBase)7 DefaultRule (fr.lirmm.graphik.graal.core.DefaultRule)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 Variable (fr.lirmm.graphik.graal.api.core.Variable)6