use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class BCCScheduler method execute.
@Override
public VarSharedData[] execute(InMemoryAtomSet query, Set<Variable> preAffectedVars, List<Term> ans, AtomSet data, RulesCompilation rc) {
InMemoryAtomSet fixedQuery = (preAffectedVars.isEmpty()) ? query : computeFixedQuery(query, preAffectedVars);
// Term index
Set<Variable> variables = fixedQuery.getVariables();
Map<Term, Integer> map = new HashMap<Term, Integer>();
this.inverseMap = new Term[variables.size() + 1];
{
// init indexes
int i = 0;
for (Variable t : variables) {
inverseMap[++i] = t;
map.put(t, i);
}
}
HyperGraph graph = constructHyperGraph(fixedQuery, variables.size(), this.inverseMap, map, ans);
double[] proba;
if (data instanceof Store) {
proba = this.computeProba(fixedQuery, (Store) data, variables.size(), map, rc);
} else {
proba = new double[variables.size() + 1];
Arrays.fill(proba, 1);
}
// bias proba of answer variables
for (Term t : ans) {
if (t.isVariable()) {
int idx = map.get(t);
proba[idx] *= ansVariableFactor;
}
}
this.varComparator = new IntegerComparator(proba);
TmpData d = biconnect(graph, this.varComparator);
VarSharedData[] vars = new VarSharedData[variables.size() + 2];
this.BCC.varData = new VarData[variables.size() + 2];
vars[0] = new VarSharedData(0);
this.BCC.varData[0] = new VarData();
int lastAnswerVariable = -1;
for (int i = 1; i < d.vars.length; ++i) {
VarSharedData v = d.vars[i];
vars[v.level] = v;
this.BCC.varData[v.level] = d.ext[i];
v.value = (Variable) this.inverseMap[i];
v.nextLevel = v.level + 1;
v.previousLevel = v.level - 1;
if (this.withForbiddenCandidate && this.BCC.varData[v.level].isAccesseur) {
this.BCC.varData[v.level].forbidden = new HashSet<Term>();
}
if (ans.contains(v.value)) {
if (v.level > lastAnswerVariable)
lastAnswerVariable = v.level;
}
}
int level = variables.size() + 1;
vars[level] = new VarSharedData(level);
this.BCC.varData[level] = new VarData();
// if an homomorphism is found, go to the last answer variable
vars[level].previousLevel = lastAnswerVariable;
// Profiling
if (this.getProfiler().isProfilingEnabled()) {
StringBuilder sb = new StringBuilder();
for (VarSharedData v : vars) {
sb.append(v.value);
sb.append(" > ");
}
this.getProfiler().put("BCCOrder", sb.toString());
}
return vars;
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class BacktrackIterator method toString.
// /////////////////////////////////////////////////////////////////////////
// OBJECT METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{\n").append("\t{query: ").append(data.query);
for (InMemoryAtomSet negPart : this.data.negParts) {
sb.append("\u2227\u00AC").append(negPart);
}
sb.append("},\n\t{level: ").append(level).append("},\n\t{\n");
int i = 0;
for (Var v : this.vars) {
sb.append("\t\t");
sb.append((i == level) ? '*' : ' ');
String s = v.toString();
sb.append(s.substring(0, s.length() - 1)).append("->").append(v.image);
sb.append(v.shared.negatedPartsToCheck.isEmpty() ? " " : " \u00AC ");
sb.append("\tFC{");
this.data.fc.append(sb, i).append("}");
this.data.bj.append(sb, i).append(" ");
sb.append(this.data.scheduler.getInfos(v));
sb.append("\n");
++i;
}
sb.append("\t}\n}\n");
return sb.toString();
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class ChaseTest method test.
@Test
public void test() throws ParseException, ChaseException {
Ontology onto = new DefaultOntology();
onto.add(DlgpParser.parseRule("p(X) :- q(X)."));
onto.add(DlgpParser.parseRule("q(X) :- p(X)."));
InMemoryAtomSet store = new DefaultInMemoryGraphStore();
store.add(DlgpParser.parseAtom("p(a)."));
Chase chase = new BreadthFirstChase(onto, store);
Assert.assertTrue(chase.hasNext());
chase.next();
Assert.assertTrue(chase.hasNext());
chase.next();
Assert.assertFalse(chase.hasNext());
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class UnifierTest method mostGeneralTest1.
@Test
public void mostGeneralTest1() throws IteratorException {
Rule rule = DefaultRuleFactory.instance().create();
rule.getBody().add(TestUtils.pXY);
rule.getHead().add(TestUtils.qXY);
InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create();
atomset.add(TestUtils.qUV);
CloseableIteratorWithoutException<Substitution> unifiers = DefaultUnifierAlgorithm.instance().computePieceUnifier(rule, atomset);
Assert.assertEquals(1, Iterators.count(unifiers));
}
use of fr.lirmm.graphik.graal.api.core.InMemoryAtomSet in project graal by graphik-team.
the class UnifierTest method unificationExistentialWithFrontier.
@Test
public void unificationExistentialWithFrontier() {
Rule rule = DefaultRuleFactory.instance().create();
rule.getBody().add(TestUtils.sX);
rule.getHead().add(TestUtils.pXY);
InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create();
atomset.add(TestUtils.pUU);
CloseableIteratorWithoutException<Substitution> unifiers = DefaultUnifierAlgorithm.instance().computePieceUnifier(rule, atomset);
Assert.assertEquals(0, Iterators.count(unifiers));
}
Aggregations