Search in sources :

Example 21 with AtomSet

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

the class MFAProperty method check.

/**
 */
@Override
public int check(AnalyserRuleSet ruleSet) {
    RuleSet R = translateToMFA(ruleSet);
    AtomSet A = Rules.criticalInstance(ruleSet);
    Chase chase = new ChaseWithGRD<AtomSet>(new DefaultGraphOfRuleDependencies(R), A, new DefaultRuleApplier<AtomSet>(new FrontierRestrictedChaseHaltingCondition()));
    DefaultConjunctiveQuery Q = new DefaultConjunctiveQuery();
    DefaultAtom q = new DefaultAtom(C);
    q.setTerm(0, FAKE);
    Q.getAtomSet().add(q);
    try {
        while (chase.hasNext()) {
            chase.next();
            if (SmartHomomorphism.instance().exist(Q, A)) {
                return -1;
            }
        }
    } catch (ChaseException e) {
        LOGGER.warn("An error occurs during the chase: ", e);
        return 0;
    } catch (HomomorphismException e) {
        LOGGER.warn("An error occurs during the homomorphism: ", e);
        return 0;
    }
    return 1;
}
Also used : AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) FrontierRestrictedChaseHaltingCondition(fr.lirmm.graphik.graal.forward_chaining.halting_condition.FrontierRestrictedChaseHaltingCondition) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ChaseWithGRD(fr.lirmm.graphik.graal.forward_chaining.ChaseWithGRD) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase)

Example 22 with AtomSet

use of fr.lirmm.graphik.graal.api.core.AtomSet 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 23 with AtomSet

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

the class MSAProperty method check.

@Override
public int check(AnalyserRuleSet ruleSet) {
    RuleSet R = translateToMSA(ruleSet);
    AtomSet A = Rules.criticalInstance(ruleSet);
    try {
        StaticChase.executeChase(A, R);
    } catch (ChaseException e) {
        LOGGER.warn("An error occurs during the chase: ", e);
        return 0;
    }
    DefaultConjunctiveQuery Q = new DefaultConjunctiveQuery();
    DefaultAtom q = new DefaultAtom(C);
    q.setTerm(0, FAKE);
    Q.getAtomSet().add(q);
    try {
        if (SmartHomomorphism.instance().exist(Q, A))
            return -1;
        return 1;
    } catch (HomomorphismException e) {
        LOGGER.warn("An error occurs during the homomorphism: ", e);
        return 0;
    }
}
Also used : AnalyserRuleSet(fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet) RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) DefaultConjunctiveQuery(fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)

Example 24 with AtomSet

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

the class NaturalConjunctiveQueryTranslator method translate.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public SQLQuery translate(ConjunctiveQuery cquery) throws AtomSetException {
    if (cquery.getAtomSet().isEmpty()) {
        return SQLQuery.emptyInstance();
    }
    AtomSet atomSet = cquery.getAtomSet();
    StringBuilder fields = new StringBuilder();
    StringBuilder tables = new StringBuilder();
    StringBuilder where = new StringBuilder();
    HashMap<Atom, String> tableAsNames = new HashMap<Atom, String>();
    HashMap<Atom, String> tableNames = new HashMap<Atom, String>();
    HashMap<Atom, List<DBColumn>> tableColumns = new HashMap<Atom, List<DBColumn>>();
    HashMap<String, String> lastOccurrence = new HashMap<String, String>();
    ArrayList<String> constants = new ArrayList<String>();
    ArrayList<String> equivalences = new ArrayList<String>();
    TreeMap<Term, String> columns = new TreeMap<Term, String>();
    int count = -1;
    CloseableIterator<Atom> it = atomSet.iterator();
    try {
        while (it.hasNext()) {
            Atom atom = it.next();
            DBTable table = this.store.getPredicateTableIfExist(atom.getPredicate());
            if (table != null) {
                String tableName = "atom" + ++count;
                tableAsNames.put(atom, tableName);
                tableNames.put(atom, table.getName());
                tableColumns.put(atom, table.getColumns());
            } else {
                return SQLQuery.hasSchemaErrorInstance();
            }
        }
        // Create WHERE clause
        it = atomSet.iterator();
        while (it.hasNext()) {
            Atom atom = it.next();
            String currentAtom = tableAsNames.get(atom) + ".";
            List<DBColumn> columnsInfo = tableColumns.get(atom);
            int position = 0;
            for (Term term : atom.getTerms()) {
                String thisTerm = currentAtom + columnsInfo.get(position).getName();
                if (term.isConstant()) {
                    constants.add(thisTerm + " = '" + term.getIdentifier().toString() + "'");
                } else {
                    if (lastOccurrence.containsKey(term.getIdentifier().toString())) {
                        equivalences.add(lastOccurrence.get(term.getIdentifier().toString()) + " = " + thisTerm);
                    }
                    lastOccurrence.put(term.getIdentifier().toString(), thisTerm);
                    if (cquery.getAnswerVariables().contains(term))
                        columns.put(term, thisTerm + " as " + term.getIdentifier().toString());
                }
                ++position;
            }
        }
        for (String equivalence : equivalences) {
            if (where.length() != 0)
                where.append(" AND ");
            where.append(equivalence);
        }
        for (String constant : constants) {
            if (where.length() != 0)
                where.append(" AND ");
            where.append(constant);
        }
        // Create FROM clause
        for (Map.Entry<Atom, String> entries : tableAsNames.entrySet()) {
            if (tables.length() != 0)
                tables.append(", ");
            tables.append(tableNames.get(entries.getKey()));
            tables.append(" as ");
            tables.append(entries.getValue());
        }
        // Create SELECT clause
        for (Term t : cquery.getAnswerVariables()) {
            if (fields.length() != 0)
                fields.append(", ");
            if (t.isConstant()) {
                fields.append("'");
                fields.append(t.getIdentifier());
                fields.append("'");
            } else {
                fields.append(columns.get(t));
            }
        }
        StringBuilder query = new StringBuilder("SELECT DISTINCT ");
        if (fields.length() > 0)
            query.append(fields);
        else
            query.append("1");
        query.append(" FROM ");
        if (tables.length() > 0)
            query.append(tables);
        if (where.length() > 0)
            query.append(" WHERE ").append(where);
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Generated SQL query :" + cquery + " --> " + query.toString());
        return new SQLQuery(query.toString());
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) HashMap(java.util.HashMap) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) ArrayList(java.util.ArrayList) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) ArrayList(java.util.ArrayList) List(java.util.List) DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 25 with AtomSet

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

the class ForwardCheckingTest method simpleFCTest1.

@Test
public void simpleFCTest1() throws HomomorphismException, IteratorException, ParseException, AtomSetException {
    Profiler profiler = new CPUTimeProfiler();
    InMemoryAtomSet data = new DefaultInMemoryGraphStore();
    data.addAll(DlgpParser.parseAtomSet("p(a,b), q(b,c)."));
    ConjunctiveQuery query = DlgpParser.parseQuery("?(X,Y,Z) :- p(X,Y), q(Y,Z).");
    Homomorphism<ConjunctiveQuery, AtomSet> h = new BacktrackHomomorphism(new SimpleFC());
    h.setProfiler(profiler);
    CloseableIterator<Substitution> results = h.execute(query, data);
    while (results.hasNext()) {
        results.next();
    }
    results.close();
    Assert.assertEquals(7, profiler.get("#calls"));
}
Also used : SimpleFC(fr.lirmm.graphik.graal.homomorphism.forward_checking.SimpleFC) Profiler(fr.lirmm.graphik.util.profiler.Profiler) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) CPUTimeProfiler(fr.lirmm.graphik.util.profiler.CPUTimeProfiler) 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) Test(org.junit.Test)

Aggregations

AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)32 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)14 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)13 Test (org.junit.Test)13 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)11 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)11 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)11 Atom (fr.lirmm.graphik.graal.api.core.Atom)9 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)9 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)9 Rule (fr.lirmm.graphik.graal.api.core.Rule)7 RulesCompilation (fr.lirmm.graphik.graal.api.core.RulesCompilation)7 HomomorphismWithCompilation (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismWithCompilation)7 Theory (org.junit.experimental.theories.Theory)7 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)5 Term (fr.lirmm.graphik.graal.api.core.Term)5 ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)5 CPUTimeProfiler (fr.lirmm.graphik.util.profiler.CPUTimeProfiler)5 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)4