Search in sources :

Example 56 with Atom

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

the class SparqlConjunctiveQueryWriter method write.

@Override
public SparqlConjunctiveQueryWriter write(ConjunctiveQuery query) throws IOException {
    this.write("SELECT DISTINCT ");
    for (Term t : query.getAnswerVariables()) {
        this.write(t);
        this.write(' ');
    }
    this.write("\nWHERE\n{\n");
    boolean isFirst = true;
    CloseableIteratorWithoutException<Atom> it = query.getAtomSet().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (!isFirst) {
            this.write(" .\n");
        } else {
            isFirst = false;
        }
        this.writeAtom(a);
    }
    this.write("\n}\n");
    return this;
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 57 with Atom

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

the class SparqlRuleWriter method writeAtomSet.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void writeAtomSet(InMemoryAtomSet atomset) throws IOException {
    this.write(" {\n");
    boolean isFirst = true;
    CloseableIteratorWithoutException<Atom> it = atomset.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (!isFirst) {
            this.write(" .\n");
        } else {
            isFirst = false;
        }
        this.writeAtom(a);
    }
    this.write("\n }\n");
}
Also used : Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 58 with Atom

use of fr.lirmm.graphik.graal.api.core.Atom 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 59 with Atom

use of fr.lirmm.graphik.graal.api.core.Atom 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 60 with Atom

use of fr.lirmm.graphik.graal.api.core.Atom 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

Atom (fr.lirmm.graphik.graal.api.core.Atom)269 Test (org.junit.Test)97 Term (fr.lirmm.graphik.graal.api.core.Term)86 Rule (fr.lirmm.graphik.graal.api.core.Rule)64 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)41 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)41 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)40 LinkedList (java.util.LinkedList)40 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)36 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)35 Theory (org.junit.experimental.theories.Theory)35 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)33 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)31 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)29 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)28 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)27 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)14 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)14 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)13