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);
}
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));
}
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));
}
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));
}
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);
}
Aggregations