Search in sources :

Example 91 with Term

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);
}
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 92 with Term

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));
}
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 93 with Term

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);
}
Also used : DefaultURI(fr.lirmm.graphik.util.DefaultURI) Literal(org.eclipse.rdf4j.model.Literal) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultURI(fr.lirmm.graphik.util.DefaultURI) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 94 with Term

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

Example 95 with Term

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);
    }
}
Also used : OWL2Parser(fr.lirmm.graphik.graal.io.owl.OWL2Parser) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Aggregations

Term (fr.lirmm.graphik.graal.api.core.Term)173 Atom (fr.lirmm.graphik.graal.api.core.Atom)86 LinkedList (java.util.LinkedList)41 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)36 Test (org.junit.Test)35 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)32 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)27 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)26 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)23 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)15 Rule (fr.lirmm.graphik.graal.api.core.Rule)15 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)14 TreeSet (java.util.TreeSet)12 Constant (fr.lirmm.graphik.graal.api.core.Constant)9 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9 TreeMap (java.util.TreeMap)8