Search in sources :

Example 76 with Atom

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

the class AtomTest method setterTest.

@Test
public void setterTest() {
    Predicate predicate = new Predicate("pred", 3);
    Term[] terms = new Term[3];
    terms[0] = new DefaultVariable("X");
    terms[1] = new DefaultConstant("a");
    terms[2] = new DefaultConstant("b");
    Atom atom = new DefaultAtom(predicate, Arrays.asList(terms));
    Term newTerm = new DefaultConstant("new");
    Predicate newPredicate = new Predicate("newPred", 3);
    atom.setPredicate(newPredicate);
    Assert.assertTrue(atom.getPredicate().equals(newPredicate));
    atom.setTerm(2, newTerm);
    Assert.assertTrue(atom.getTerm(2).equals(newTerm));
}
Also used : 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) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Test(org.junit.Test)

Example 77 with Atom

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

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

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

the class FrontierRestrictedChaseHaltingCondition method apply.

@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
    Set<Term> fixedVars = substitution.getValues();
    if (ruleIndex.get(rule) == null) {
        ruleIndex.put(rule, _currentRuleIndex++);
    }
    final int index = ruleIndex.get(rule).intValue();
    StringBuilder frontierSb = new StringBuilder();
    SortedSet<Variable> frontierSet = new TreeSet<Variable>(rule.getFrontier());
    for (Term t : frontierSet) {
        frontierSb.append("_");
        frontierSb.append(t.getLabel());
        frontierSb.append(substitution.createImageOf(t).getLabel());
    }
    String frontier = frontierSb.toString();
    for (Variable t : rule.getExistentials()) {
        substitution.put(t, DefaultTermFactory.instance().createConstant("f_" + index + "_" + t.getIdentifier() + frontier));
    }
    InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
    ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, fixedVars);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Fixed Query:" + query);
    }
    try {
        if (SmartHomomorphism.instance().execute(query, data).hasNext()) {
            return new CloseableIteratorAdapter<Atom>(Collections.<Atom>emptyList().iterator());
        }
    } catch (IteratorException e) {
        throw new HomomorphismException("An errors occurs while iterating results", e);
    }
    return newFacts.iterator();
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables) Atom(fr.lirmm.graphik.graal.api.core.Atom) TreeSet(java.util.TreeSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 80 with Atom

use of fr.lirmm.graphik.graal.api.core.Atom 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)

Aggregations

Atom (fr.lirmm.graphik.graal.api.core.Atom)269 Test (org.junit.Test)97 Term (fr.lirmm.graphik.graal.api.core.Term)86 Rule (fr.lirmm.graphik.graal.api.core.Rule)64 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)41 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)41 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)40 LinkedList (java.util.LinkedList)40 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)36 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)35 Theory (org.junit.experimental.theories.Theory)35 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)33 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)31 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)29 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)28 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)27 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)14 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)14 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)13