Search in sources :

Example 31 with Variable

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

the class Atom2SubstitutionConverterTest method wrongUsage.

@Test
public void wrongUsage() throws ParseException {
    // given
    Predicate p = DefaultPredicateFactory.instance().create("p", 1);
    Variable x = DefaultTermFactory.instance().createVariable("X");
    Atom queryAtom = new DefaultAtom(p, x, x);
    List<Term> ansList = new LinkedList<>();
    ansList.add(x);
    // when
    Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList);
    Substitution s = null;
    try {
        s = converter.convert(DlgpParser.parseAtom("p(a, b)."));
    } catch (ConversionException e) {
        fail();
    }
    // then
    Constant a = DefaultTermFactory.instance().createConstant("a");
    System.out.println(s);
}
Also used : ConversionException(fr.lirmm.graphik.util.stream.converter.ConversionException) Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Constant(fr.lirmm.graphik.graal.api.core.Constant) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 32 with Variable

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

the class Atom2SubstitutionConverterTest method githubIssue2variantWithConstant.

@Test
public void githubIssue2variantWithConstant() throws ParseException {
    // given
    Predicate p = DefaultPredicateFactory.instance().create("p", 1);
    Variable x = DefaultTermFactory.instance().createVariable("X");
    Constant b = DefaultTermFactory.instance().createConstant("b");
    Atom queryAtom = new DefaultAtom(p, x);
    List<Term> ansList = new LinkedList<>();
    ansList.add(x);
    ansList.add(b);
    // when
    Converter<Atom, Substitution> converter = new Atom2SubstitutionConverter(queryAtom, ansList);
    Substitution s = null;
    try {
        s = converter.convert(DlgpParser.parseAtom("p(a)."));
    } catch (ConversionException e) {
        fail();
    }
    // then
    Constant a = DefaultTermFactory.instance().createConstant("a");
    assertEquals(a, s.createImageOf(x));
    assertEquals(b, s.createImageOf(b));
}
Also used : ConversionException(fr.lirmm.graphik.util.stream.converter.ConversionException) Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Constant(fr.lirmm.graphik.graal.api.core.Constant) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 33 with Variable

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

the class EqualityUtilsTest method test3.

@Test
public void test3() throws ParseException {
    ConjunctiveQuery q = DlgpParser.parseQuery("?(X,Y) :- p(X,Y), X=Y.");
    Pair<ConjunctiveQuery, Substitution> pair = EqualityUtils.processEquality(q);
    q = pair.getLeft();
    Substitution s = pair.getRight();
    // check substitution
    Set<Variable> terms = new HashSet<Variable>(s.getTerms());
    terms.remove(x);
    terms.remove(y);
    Assert.assertTrue(terms.isEmpty());
    Assert.assertEquals(s.createImageOf(x), s.createImageOf(y));
    Assert.assertTrue(s.createImageOf(y).isVariable());
    // check query ans part
    Assert.assertEquals(1, q.getAnswerVariables().size());
    Assert.assertEquals(s.createImageOf(x), q.getAnswerVariables().get(0));
    // check query atomset
    Assert.assertEquals(1, Iterators.count(q.getAtomSet().iterator()));
    Atom atom = q.getAtomSet().iterator().next();
    Assert.assertEquals(p, atom.getPredicate());
    Assert.assertEquals(s.createImageOf(x), atom.getTerm(0));
    Assert.assertEquals(s.createImageOf(x), atom.getTerm(1));
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 34 with Variable

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

the class EqualityUtilsTest method test4.

@Test
public void test4() throws ParseException {
    ConjunctiveQuery q = DlgpParser.parseQuery("?(X,Y) :- p(X,Y), X=a, X=Y.");
    Pair<ConjunctiveQuery, Substitution> pair = EqualityUtils.processEquality(q);
    q = pair.getLeft();
    Substitution s = pair.getRight();
    // check substitution minimality
    Set<Variable> terms = new HashSet<Variable>(s.getTerms());
    terms.remove(x);
    terms.remove(y);
    Assert.assertTrue(terms.isEmpty());
    // check substitution completude
    Assert.assertEquals(a, s.createImageOf(y));
    Assert.assertEquals(a, s.createImageOf(y));
    // check query ans part
    Assert.assertEquals(0, q.getAnswerVariables().size());
    // check query atomset
    Assert.assertEquals(1, Iterators.count(q.getAtomSet().iterator()));
    Atom atom = q.getAtomSet().iterator().next();
    Assert.assertEquals(p, atom.getPredicate());
    Assert.assertEquals(a, atom.getTerm(0));
    Assert.assertEquals(a, atom.getTerm(1));
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with Variable

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

the class SparqlConjunctiveQueryParser method execute.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void execute(String queryString) {
    this.prefixes = new LinkedList<Prefix>();
    List<Term> ans = new LinkedList<Term>();
    Query sparql = QueryFactory.create(queryString);
    for (Map.Entry<String, String> e : sparql.getPrefixMapping().getNsPrefixMap().entrySet()) {
        this.prefixes.add(new Prefix(e.getKey(), e.getValue()));
    }
    if (sparql.isSelectType()) {
        for (String v : sparql.getResultVars()) {
            ans.add(DefaultTermFactory.instance().createVariable(v));
        }
    }
    ElementVisitorImpl visitor = new ElementVisitorImpl(DefaultAtomSetFactory.instance().create());
    sparql.getQueryPattern().visit(visitor);
    // check if answer variables appear in the query body
    Set<Variable> bodyVars = visitor.getAtomSet().getVariables();
    for (Term t : ans) {
        if (t.isVariable() && !bodyVars.contains(t)) {
            throw new ParseError("The variable [" + t + "] of the answer list does not appear in the query body.");
        }
    }
    this.query = DefaultConjunctiveQueryFactory.instance().create(visitor.getAtomSet(), ans);
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Query(com.hp.hpl.jena.query.Query) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Prefix(fr.lirmm.graphik.util.Prefix) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) ParseError(fr.lirmm.graphik.graal.api.io.ParseError) Map(java.util.Map)

Aggregations

Variable (fr.lirmm.graphik.graal.api.core.Variable)57 Atom (fr.lirmm.graphik.graal.api.core.Atom)33 Term (fr.lirmm.graphik.graal.api.core.Term)32 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)25 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)19 LinkedList (java.util.LinkedList)15 Test (org.junit.Test)15 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)8 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)8 Constant (fr.lirmm.graphik.graal.api.core.Constant)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)6 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 VarSharedData (fr.lirmm.graphik.graal.homomorphism.VarSharedData)5 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)4 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)4 HashMapSubstitution (fr.lirmm.graphik.graal.core.HashMapSubstitution)4 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)4