Search in sources :

Example 1 with Store

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);
}
Also used : KnowledgeBase(fr.lirmm.graphik.graal.api.kb.KnowledgeBase) Store(fr.lirmm.graphik.graal.api.store.Store) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) DefaultInMemoryGraphStore(fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore) Test(org.junit.Test)

Example 2 with 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;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HashMap(java.util.HashMap) Store(fr.lirmm.graphik.graal.api.store.Store) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) DefaultHyperGraph(fr.lirmm.graphik.util.graph.DefaultHyperGraph) HyperGraph(fr.lirmm.graphik.util.graph.HyperGraph)

Example 3 with Store

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);
    }
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Constant(fr.lirmm.graphik.graal.api.core.Constant) Store(fr.lirmm.graphik.graal.api.store.Store) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) Atom(fr.lirmm.graphik.graal.api.core.Atom) BacktrackException(fr.lirmm.graphik.graal.homomorphism.BacktrackException) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 4 with Store

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);
}
Also used : RdbmsStore(fr.lirmm.graphik.graal.store.rdbms.RdbmsStore) Store(fr.lirmm.graphik.graal.api.store.Store) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) Theory(org.junit.experimental.theories.Theory)

Example 5 with Store

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);
}
Also used : RdbmsStore(fr.lirmm.graphik.graal.store.rdbms.RdbmsStore) Store(fr.lirmm.graphik.graal.api.store.Store) TripleStore(fr.lirmm.graphik.graal.api.store.TripleStore) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Aggregations

Store (fr.lirmm.graphik.graal.api.store.Store)6 TripleStore (fr.lirmm.graphik.graal.api.store.TripleStore)3 RdbmsStore (fr.lirmm.graphik.graal.store.rdbms.RdbmsStore)3 Theory (org.junit.experimental.theories.Theory)3 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)2 Term (fr.lirmm.graphik.graal.api.core.Term)2 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)2 Atom (fr.lirmm.graphik.graal.api.core.Atom)1 Constant (fr.lirmm.graphik.graal.api.core.Constant)1 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)1 Rule (fr.lirmm.graphik.graal.api.core.Rule)1 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)1 Variable (fr.lirmm.graphik.graal.api.core.Variable)1 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)1 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)1 ParseException (fr.lirmm.graphik.graal.api.io.ParseException)1 KnowledgeBase (fr.lirmm.graphik.graal.api.kb.KnowledgeBase)1 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)1 DefaultRuleApplier (fr.lirmm.graphik.graal.forward_chaining.rule_applier.DefaultRuleApplier)1