use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class AtomicQueryHomomorphism method execute.
@Override
public CloseableIterator<Substitution> execute(ConjunctiveQuery q, AtomSet a, RulesCompilation rc) throws HomomorphismException {
try {
List<CloseableIterator<Substitution>> iteratorsList = new LinkedList<CloseableIterator<Substitution>>();
Atom atom = q.getAtomSet().iterator().next();
for (Pair<Atom, Substitution> im : rc.getRewritingOf(atom)) {
iteratorsList.add(new ConverterCloseableIterator<Atom, Substitution>(a.match(im.getLeft()), new Atom2SubstitutionConverter(im.getLeft(), q.getAnswerVariables(), im.getRight())));
}
return new CloseableIteratorAggregator<Substitution>(new CloseableIteratorAdapter<CloseableIterator<Substitution>>(iteratorsList.iterator()));
} catch (AtomSetException e) {
throw new HomomorphismException(e);
}
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class BacktrackIterator method existNegParts.
private boolean existNegParts() throws BacktrackException {
Substitution s = currentSubstitution(this.vars);
s.put(initialSubstitution);
for (PreparedExistentialHomomorphism negPart : this.currentVar().shared.negatedPartsToCheck) {
try {
if (negPart.exist(s)) {
this.data.bj.success();
return true;
}
} catch (HomomorphismException e) {
throw new BacktrackException("Error while checking anegated part: ", e);
}
}
return false;
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class StatBootstrapper method exec.
// /////////////////////////////////////////////////////////////////////////
//
// /////////////////////////////////////////////////////////////////////////
@Override
public CloseableIterator<Term> exec(final VarSharedData v, Collection<Atom> preAtoms, Collection<Atom> postAtoms, final AtomSet data, RulesCompilation rc) throws BacktrackException {
if (!(data instanceof Store)) {
return fallback.exec(v, preAtoms, postAtoms, data, rc);
}
Store store = (Store) data;
Set<Term> terms = null;
if (this.getProfiler() != null) {
this.getProfiler().start("BootstrapTime");
this.getProfiler().start("BootstrapTimeFirstPart");
}
Iterator<Atom> it;
Collection<Constant> constants = null;
Atom aa = null;
it = postAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
Atom a = it.next();
if (constants == null || constants.isEmpty()) {
constants = a.getConstants();
aa = a;
}
}
try {
if (constants != null && !constants.isEmpty()) {
terms = new HashSet<Term>();
for (Pair<Atom, Substitution> im : rc.getRewritingOf(aa)) {
int pos = im.getLeft().indexOf(im.getRight().createImageOf(v.value));
CloseableIterator<Atom> match = data.match(im.getLeft());
while (match.hasNext()) {
terms.add(match.next().getTerm(pos));
}
}
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTimeFirstPart");
}
if (terms == null) {
Atom a = null, tmp;
double probaA = 1.1;
it = postAtoms.iterator();
while (it.hasNext()) {
tmp = it.next();
double p = ProbaUtils.computeProba(tmp, store, rc);
if (p < probaA) {
a = tmp;
p = probaA;
}
}
it = preAtoms.iterator();
while (it.hasNext()) {
tmp = it.next();
double p = ProbaUtils.computeProba(tmp, store, rc);
if (p < probaA) {
a = tmp;
p = probaA;
}
}
terms = BootstrapperUtils.computeCandidatesOverRewritings(a, v, data, rc);
}
if (this.getProfiler() != null) {
this.getProfiler().stop("BootstrapTime");
}
if (terms == null) {
return data.termsIterator();
} else {
return new CloseableIteratorAdapter<Term>(terms.iterator());
}
} catch (AtomSetException e) {
throw new BacktrackException(e);
} catch (IteratorException e) {
throw new BacktrackException(e);
}
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class DefaultKnowledgeBaseQueryTest method testQueryWithTimeout0.
/**
* Test method for
* {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#query(fr.lirmm.graphik.graal.api.core.Query, long)}.
* @throws AtomSetException
* @throws KnowledgeBaseException
* @throws ParseException
* @throws IteratorException
* @throws TimeoutException
*/
@Test
public void testQueryWithTimeout0() throws AtomSetException, ParseException, KnowledgeBaseException, IteratorException, TimeoutException {
KnowledgeBase kb = new DefaultKnowledgeBase(new DlgpParser("p(X) :- q(X). q(X) :- r(X). r(X) :- s(X). s(a)."));
CloseableIterator<Substitution> res = kb.query(DlgpParser.parseQuery("? :- p(a)."), 0);
Assert.assertTrue(res.hasNext());
res.close();
kb.close();
}
use of fr.lirmm.graphik.graal.api.core.Substitution in project graal by graphik-team.
the class DefaultKnowledgeBaseQueryTest method testQuery.
/**
* Test method for
* {@link fr.lirmm.graphik.graal.kb.DefaultKnowledgeBase#query(fr.lirmm.graphik.graal.api.core.Query)}.
* @throws AtomSetException
* @throws KnowledgeBaseException
* @throws ParseException
* @throws IteratorException
*/
@Test
public void testQuery() throws AtomSetException, ParseException, KnowledgeBaseException, IteratorException {
KnowledgeBase kb = new DefaultKnowledgeBase(new DlgpParser("p(X) :- q(X). q(X) :- r(X). r(X) :- s(X). s(a)."));
CloseableIterator<Substitution> res = kb.query(DlgpParser.parseQuery("? :- p(a)."));
Assert.assertTrue(res.hasNext());
res.close();
kb.close();
}
Aggregations