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