use of fr.lirmm.graphik.graal.api.store.Store in project graal by graphik-team.
the class KBBuilderTest method testSetStore.
/**
* Test method for {@link fr.lirmm.graphik.graal.kb.KBBuilder#setStore(fr.lirmm.graphik.graal.api.store.Store)}.
*/
@Test
public void testSetStore() {
// Given
KBBuilder kbb = new KBBuilder();
Store store = new DefaultInMemoryGraphStore();
// When
kbb.setStore(store);
KnowledgeBase kb = kbb.build();
// Then
Assert.assertTrue(kb.getFacts() == store);
}
use of fr.lirmm.graphik.graal.api.store.Store 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.store.Store 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.store.Store in project graal by graphik-team.
the class StoreTest method getDomainSizeTest.
@Theory
public void getDomainSizeTest(AtomSet atomset) throws ParseException, AtomSetException {
Assume.assumeTrue(atomset instanceof Store);
Store store = (Store) atomset;
// given
store.add(DlgpParser.parseAtom("<P>(b,a)."));
store.add(DlgpParser.parseAtom("<P>(a,a)."));
store.add(DlgpParser.parseAtom("<P>(b,b)."));
store.add(DlgpParser.parseAtom("<P>(a,c)."));
store.add(DlgpParser.parseAtom("<Q>(a,a)."));
store.add(DlgpParser.parseAtom("<Q>(a,b)."));
// when
int domainSize = store.getDomainSize();
// then
Assert.assertTrue(3 <= domainSize);
}
use of fr.lirmm.graphik.graal.api.store.Store in project graal by graphik-team.
the class StoreTest method sizeTest.
@Theory
public void sizeTest(AtomSet atomset) throws ParseException, AtomSetException {
Assume.assumeTrue(atomset instanceof Store);
Store store = (Store) atomset;
// given
store.add(DlgpParser.parseAtom("<P>(b,a)."));
store.add(DlgpParser.parseAtom("<P>(a,a)."));
store.add(DlgpParser.parseAtom("<P>(b,b)."));
store.add(DlgpParser.parseAtom("<P>(a,c)."));
store.add(DlgpParser.parseAtom("<Q>(a,a)."));
store.add(DlgpParser.parseAtom("<Q>(a,b)."));
// when
int sizeP = store.size(new Predicate("P", 2));
int sizeQ = store.size(new Predicate("Q", 2));
// then
Assert.assertTrue(4 <= sizeP);
Assert.assertTrue(2 <= sizeQ);
}
Aggregations