Search in sources :

Example 71 with Substitution

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

the class ConjunctiveQueryFixedBugTest method GraphAtomSetQuery.

/**
 * Query using an DefaultInMemoryGraphAtomSet.
 *
 * @param h
 * @param store
 */
@Theory
public void GraphAtomSetQuery(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        InMemoryAtomSet atomset = new DefaultInMemoryGraphStore();
        atomset.add(DlgpParser.parseAtom("<P>(X)."));
        ConjunctiveQuery query = new DefaultConjunctiveQuery(atomset);
        CloseableIterator<Substitution> subReader;
        subReader = h.execute(query, store);
        Assert.assertFalse(subReader.hasNext());
        subReader.close();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage(), false);
    }
}
Also used : DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Theory(org.junit.experimental.theories.Theory)

Example 72 with Substitution

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

the class ConjunctiveQueryWithCompilation method issue35.

@Theory
public void issue35(Homomorphism<ConjunctiveQuery, AtomSet> hh, RulesCompilationFactory factory, AtomSet store) throws Exception {
    Assume.assumeFalse(store instanceof TripleStore);
    Assume.assumeTrue(hh instanceof HomomorphismWithCompilation);
    HomomorphismWithCompilation<ConjunctiveQuery, AtomSet> h = (HomomorphismWithCompilation<ConjunctiveQuery, AtomSet>) hh;
    store.addAll(DlgpParser.parseAtomSet("<P>(a,a), <R>(a,b,b)."));
    RuleSet rules = new LinkedListRuleSet();
    rules.add(DlgpParser.parseRule("<Q>(X,Y,X) :- <P>(X,Y)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("? :- <Q>(X,Y,Y), <R>(X,Y,Z).");
    RulesCompilation comp = factory.create();
    comp.compile(rules.iterator());
    StaticChase.executeChase(store, rules);
    CloseableIterator<Substitution> results = h.execute(new DefaultConjunctiveQuery(query), store, comp);
    Assert.assertFalse(results.hasNext());
    results.close();
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) RulesCompilation(fr.lirmm.graphik.graal.api.core.RulesCompilation) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismWithCompilation(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation) Theory(org.junit.experimental.theories.Theory)

Example 73 with Substitution

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

the class MSAProperty method translateRuleToMSA.

public static List<Rule> translateRuleToMSA(final Rule r) {
    List<Rule> result = new LinkedList<Rule>();
    Substitution s = buildMSASubstitution(r);
    DefaultRule r2 = new DefaultRule(r);
    /*r2.setBody(r.getBody());
		r2.setHead(r.getHead());*/
    for (Term yi : r2.getExistentials()) {
        Predicate Fir = GraalConstant.freshPredicate(1);
        DefaultAtom f = new DefaultAtom(Fir);
        f.setTerm(0, yi);
        r2.getHead().add(f);
        for (Term xj : r2.getFrontier()) {
            DefaultAtom ss = new DefaultAtom(S);
            ss.setTerm(0, xj);
            ss.setTerm(1, yi);
            r2.getHead().add(ss);
        }
        DefaultRule r3 = new DefaultRule();
        DefaultAtom f1 = new DefaultAtom(Fir);
        f1.setTerm(0, DefaultTermFactory.instance().createVariable("X1"));
        DefaultAtom f2 = new DefaultAtom(Fir);
        f2.setTerm(0, DefaultTermFactory.instance().createVariable("X2"));
        DefaultAtom d = new DefaultAtom(D);
        d.setTerm(0, DefaultTermFactory.instance().createVariable("X1"));
        d.setTerm(1, DefaultTermFactory.instance().createVariable("X2"));
        r3.getBody().add(f1);
        r3.getBody().add(d);
        r3.getBody().add(f2);
        DefaultAtom c = new DefaultAtom(C);
        c.setTerm(0, FAKE);
        r3.getHead().add(c);
        result.add(r3);
    }
    r2.setHead(s.createImageOf(r2.getHead()));
    result.add(r2);
    return result;
}
Also used : DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultRule(fr.lirmm.graphik.graal.core.DefaultRule) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 74 with Substitution

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

the class ResultSet2SubstitutionConverter method convert.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public Substitution convert(ResultSet result) throws ConversionException {
    try {
        Substitution substitution = new TreeMapSubstitution();
        if (!ans.isEmpty()) {
            for (Term t : ans) {
                if (t.isVariable()) {
                    int i = result.findColumn(t.getLabel());
                    int type = result.getMetaData().getColumnType(i);
                    String value = result.getString(i);
                    Term substitut = this.queryTranslator.createTermFromColumnType(type, value);
                    substitution.put((Variable) t, substitut);
                }
            }
        }
        return substitution;
    } catch (SQLException e) {
        throw new ConversionException(e);
    } catch (AtomSetException e) {
        throw new ConversionException(e);
    }
}
Also used : ConversionException(fr.lirmm.graphik.util.stream.converter.ConversionException) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) SQLException(java.sql.SQLException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution)

Example 75 with Substitution

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

the class ConjunctiveQueryTest method noAnswerQueryTest3.

/**
 * Test a query without answer
 */
@Theory
public void noAnswerQueryTest3(Homomorphism<ConjunctiveQuery, AtomSet> h, AtomSet store) {
    try {
        store.addAll(DlgpParser.parseAtomSet("<P>(a,b), <R>(c,c)."));
        ConjunctiveQuery query = DlgpParser.parseQuery("?(Y,X) :- <P>(a,X), <Q>(X,Y).");
        CloseableIterator<Substitution> subReader;
        subReader = h.execute(query, store);
        Assert.assertFalse(subReader.hasNext());
        subReader.close();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage(), false);
    }
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) Theory(org.junit.experimental.theories.Theory)

Aggregations

Substitution (fr.lirmm.graphik.graal.api.core.Substitution)158 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)58 Test (org.junit.Test)55 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)46 Theory (org.junit.experimental.theories.Theory)44 Atom (fr.lirmm.graphik.graal.api.core.Atom)41 Term (fr.lirmm.graphik.graal.api.core.Term)36 LinkedList (java.util.LinkedList)27 Variable (fr.lirmm.graphik.graal.api.core.Variable)25 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)24 Rule (fr.lirmm.graphik.graal.api.core.Rule)23 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)19 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)18 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)16 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)15 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)14 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)14 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)13 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)13 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)13