use of fr.lirmm.graphik.graal.api.core.Term 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.Term 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.Term in project graal by graphik-team.
the class AbstractRDFListener method handleStatement.
@Override
public void handleStatement(Statement st) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(st.toString());
}
Predicate predicate = new Predicate(new DefaultURI(st.getPredicate().toString()), 2);
Term subject = DefaultTermFactory.instance().createConstant(new DefaultURI(st.getSubject().toString()));
Term object;
fr.lirmm.graphik.util.URI datatype;
if (st.getObject() instanceof Literal) {
Literal l = (Literal) st.getObject();
if (l.getDatatype() == null) {
datatype = URIUtils.RDF_LANG_STRING;
} else {
datatype = new fr.lirmm.graphik.util.DefaultURI(l.getDatatype().getNamespace(), l.getDatatype().getLocalName());
}
String value = l.getLabel();
if (datatype.equals(URIUtils.RDF_LANG_STRING)) {
value += "@" + l.getLanguage();
}
object = DefaultTermFactory.instance().createLiteral(datatype, value);
} else {
object = DefaultTermFactory.instance().createConstant(new DefaultURI(st.getObject().toString()));
}
DefaultAtom a = new DefaultAtom(predicate, subject, object);
this.createAtom(a);
}
use of fr.lirmm.graphik.graal.api.core.Term 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);
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class OWL2ParserTest method complexAssertionWithExistential.
@Test
public void complexAssertionWithExistential() throws OWL2ParserException {
try {
OWL2Parser parser = new OWL2Parser(PREFIXES + ":A a owl:Class . " + ":B a owl:Class . " + ":p a owl:ObjectProperty . " + "_:x1 a :A. " + "_:y1 a :B. " + "_:x1 :p _:y1. " + "_:x2 a :A. " + "_:y2 a :B. " + "_:x2 :p _:y2. ");
int nbFacts = 0;
int nbAtoms = 0;
while (parser.hasNext()) {
Object o = parser.next();
if (o instanceof AtomSet) {
Term a0 = null, b0 = null, p0 = null, p1 = null;
++nbFacts;
CloseableIterator<Atom> it = ((AtomSet) o).iterator();
while (it.hasNext()) {
Atom a = it.next();
++nbAtoms;
if (a.getPredicate().equals(P)) {
p0 = a.getTerm(0);
p1 = a.getTerm(1);
} else if (a.getPredicate().equals(A)) {
a0 = a.getTerm(0);
} else if (a.getPredicate().equals(B)) {
b0 = a.getTerm(0);
}
}
Assert.assertEquals(p0, a0);
Assert.assertEquals(p1, b0);
}
}
parser.close();
Assert.assertEquals("Number of facts found:", 2, nbFacts);
Assert.assertEquals("Number of atoms found:", 6, nbAtoms);
} catch (Throwable e) {
Assert.fail("An exception was found: " + e);
}
}
Aggregations