Search in sources :

Example 1 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException in project graal by graphik-team.

the class BasicChase method next.

// /////////////////////////////////////////////////////////////////////////
// PUBLICS METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    try {
        this.hasNext = false;
        for (Rule rule : this.ruleSet) {
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().start("saturationTime");
            }
            String key = null;
            if (this.getProfiler().isProfilingEnabled()) {
                key = "Rule " + rule.getLabel() + " application time";
                this.getProfiler().clear(key);
                this.getProfiler().trace(rule.toString());
                this.getProfiler().start(key);
            }
            boolean val = this.getRuleApplier().apply(rule, this.atomSet);
            this.hasNext = this.hasNext || val;
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().stop(key);
            }
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().stop("saturationTime");
            }
        }
    } catch (Exception e) {
        throw new ChaseException("An error occured during saturation step.", e);
    }
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)

Example 2 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException in project graal by graphik-team.

the class BreadthFirstChase method next.

// /////////////////////////////////////////////////////////////////////////
// PUBLICS METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    this.rulesToCheck = this.nextRulesToCheck;
    this.nextRulesToCheck = new TreeMap<Rule, AtomSet>();
    try {
        if (!this.rulesToCheck.isEmpty()) {
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().start("saturationTime");
            }
            for (Entry<Rule, AtomSet> e : this.rulesToCheck.entrySet()) {
                String key = null;
                Rule rule = e.getKey();
                AtomSet data = e.getValue();
                if (this.getProfiler().isProfilingEnabled()) {
                    key = "Rule " + rule.getLabel() + " application time";
                    this.getProfiler().clear(key);
                    this.getProfiler().trace(rule.toString());
                    this.getProfiler().start(key);
                }
                CloseableIterator<Atom> it = this.getRuleApplier().delegatedApply(rule, data, this.atomSet);
                while (it.hasNext()) {
                    tmpData.add(it.next());
                }
                it.close();
                if (this.getProfiler().isProfilingEnabled()) {
                    this.getProfiler().stop(key);
                }
            }
            this.dispatchNewData(this.tmpData);
            this.atomSet.addAll(new CloseableIteratorAdapter<Atom>(this.tmpData.iterator()));
            this.tmpData.clear();
            if (this.getProfiler().isProfilingEnabled()) {
                this.getProfiler().stop("saturationTime");
            }
        }
    } catch (Exception e) {
        throw new ChaseException("An error occured during saturation step.", e);
    }
}
Also used : AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) CloseableIteratorWithoutException(fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)

Example 3 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException in project graal by graphik-team.

the class ChaseWithGRD method next.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    Queue<Rule> newQueue = new LinkedList<Rule>();
    List<Atom> newAtomSet = new LinkedList<Atom>();
    try {
        while (!queue.isEmpty()) {
            Rule rule = queue.poll();
            if (rule != null) {
                CloseableIterator<Atom> it = this.getRuleApplier().delegatedApply(rule, this.atomSet);
                if (it.hasNext()) {
                    while (it.hasNext()) {
                        newAtomSet.add(it.next());
                    }
                    for (Rule triggeredRule : this.grd.getTriggeredRules(rule)) {
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("-- -- Dependency: " + triggeredRule);
                        }
                        if (!newQueue.contains(triggeredRule)) {
                            newQueue.add(triggeredRule);
                        }
                    }
                }
                it.close();
            }
        }
        queue = newQueue;
        atomSet.addAll(new CloseableIteratorAdapter<Atom>(newAtomSet.iterator()));
    } catch (Exception e) {
        throw new ChaseException("An error occur pending saturation step.", e);
    }
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)

Example 4 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException in project graal by graphik-team.

the class SccChase method next.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public void next() throws ChaseException {
    ++this.level;
    tmpAtom = new LinkedList<Atom>();
    for (Integer scc : layers[level]) {
        Set<Rule> component = this.sccg.getComponent(scc);
        GraphOfRuleDependencies subGraph = this.grd.getSubGraph(component);
        if (component.size() == 1 && !subGraph.hasCircuit()) {
            try {
                CloseableIterator<Atom> it = this.getRuleApplier().delegatedApply(component.iterator().next(), atomSet);
                while (it.hasNext()) {
                    tmpAtom.add(it.next());
                }
                it.close();
            } catch (RuleApplicationException e) {
                throw new ChaseException("", e);
            } catch (IteratorException e) {
                throw new ChaseException("", e);
            }
        } else {
            Chase chase = new ChaseWithGRD<T>(subGraph, atomSet, this.getRuleApplier());
            chase.execute();
        }
    }
    try {
        atomSet.addAll(new CloseableIteratorAdapter<Atom>(tmpAtom.iterator()));
    } catch (AtomSetException e) {
        throw new ChaseException("", e);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase) AbstractChase(fr.lirmm.graphik.graal.api.forward_chaining.AbstractChase) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) GraphOfRuleDependencies(fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies)

Example 5 with ChaseException

use of fr.lirmm.graphik.graal.api.forward_chaining.ChaseException in project graal by graphik-team.

the class RdbmsStoreTest method SQLRuleApplierTest.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Theory
public void SQLRuleApplierTest(RdbmsStore store) throws AtomSetException, IteratorException, ParseException {
    RuleSet ruleSet = new LinkedListRuleSet();
    ruleSet.add(DlgpParser.parseRule("<Q>(X,Y) :- <P>(X)."));
    ruleSet.add(DlgpParser.parseRule("<R>(Y) :- <Q>(X,Y)."));
    Atom a = DlgpParser.parseAtom("<P>(a).");
    store.add(a);
    Chase chase = new BasicChase<RdbmsStore>(ruleSet, store, new SQLRuleApplier(SqlHomomorphism.instance()));
    try {
        chase.execute();
    } catch (ChaseException e) {
        Assert.fail(e.getMessage());
    }
    CloseableIterator<Atom> it = store.iterator();
    int count = Iterators.count(it);
    Assert.assertEquals(3, count);
}
Also used : RuleSet(fr.lirmm.graphik.graal.api.core.RuleSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) BasicChase(fr.lirmm.graphik.graal.forward_chaining.BasicChase) SQLRuleApplier(fr.lirmm.graphik.graal.store.rdbms.rule_applier.SQLRuleApplier) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) ChaseException(fr.lirmm.graphik.graal.api.forward_chaining.ChaseException) Atom(fr.lirmm.graphik.graal.api.core.Atom) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase) BasicChase(fr.lirmm.graphik.graal.forward_chaining.BasicChase) Theory(org.junit.experimental.theories.Theory)

Aggregations

ChaseException (fr.lirmm.graphik.graal.api.forward_chaining.ChaseException)13 Atom (fr.lirmm.graphik.graal.api.core.Atom)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)6 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)5 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)5 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)5 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)5 Chase (fr.lirmm.graphik.graal.api.forward_chaining.Chase)4 KnowledgeBaseException (fr.lirmm.graphik.graal.api.kb.KnowledgeBaseException)4 AnalyserRuleSet (fr.lirmm.graphik.graal.rulesetanalyser.util.AnalyserRuleSet)4 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)3 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)3 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)3 DefaultConjunctiveQuery (fr.lirmm.graphik.graal.core.DefaultConjunctiveQuery)3 DefaultGraphOfRuleDependencies (fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies)3 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)3 GraphOfRuleDependencies (fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies)2 UnionOfConjunctiveQueries (fr.lirmm.graphik.graal.api.core.UnionOfConjunctiveQueries)2 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)2 PureRewriter (fr.lirmm.graphik.graal.backward_chaining.pure.PureRewriter)2