Search in sources :

Example 66 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class ChaseTest method restrictedChaseTestWithGrd.

@Theory
public void restrictedChaseTestWithGrd(InMemoryAtomSet atomSet) throws IOException, ChaseException, ParseException, AtomSetException, IteratorException {
    atomSet.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
    LinkedList<Rule> ruleSet = new LinkedList<>();
    ruleSet.add(DlgpParser.parseRule("<Q>(X,Z) :- <P>(X,X)."));
    ruleSet.add(DlgpParser.parseRule("<R>(X,Z) :- <Q>(X,Y)."));
    ruleSet.add(DlgpParser.parseRule("<Q>(X,Z) :- <R>(X,Y)."));
    ruleSet.add(DlgpParser.parseRule("<S>(X,X) :- <Q>(Y,X)."));
    GraphOfRuleDependencies grd = new DefaultGraphOfRuleDependencies(ruleSet);
    Chase chase = new ChaseWithGRDAndUnfiers<AtomSet>(grd, atomSet);
    chase.execute();
    int size = 0;
    for (CloseableIterator<Atom> it = atomSet.iterator(); it.hasNext(); it.next()) {
        ++size;
    }
    Assert.assertEquals(4, size);
}
Also used : DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) Rule(fr.lirmm.graphik.graal.api.core.Rule) GraphOfRuleDependencies(fr.lirmm.graphik.graal.api.core.GraphOfRuleDependencies) DefaultGraphOfRuleDependencies(fr.lirmm.graphik.graal.core.grd.DefaultGraphOfRuleDependencies) LinkedList(java.util.LinkedList) Chase(fr.lirmm.graphik.graal.api.forward_chaining.Chase) Atom(fr.lirmm.graphik.graal.api.core.Atom) Theory(org.junit.experimental.theories.Theory)

Example 67 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class IDCompilation method appendTo.

public void appendTo(StringBuilder sb) {
    for (Rule r : this.getSaturation()) {
        r.appendTo(sb);
        sb.append('\n');
    }
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule)

Example 68 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class Rules method computeAtomicHead.

/**
 * Generate a set of atomic head rules equivalent of the specified rule.
 *
 * @param rule
 * @return a Collection of Rule which is a decomposition of the specified rule to atomic head rules.
 */
public static Collection<Rule> computeAtomicHead(Rule rule) {
    String label = rule.getLabel();
    Collection<Rule> atomicHead = new LinkedList<Rule>();
    if (rule.getHead().isEmpty() || hasAtomicHead(rule)) {
        return Collections.<Rule>singleton(rule);
    } else {
        Predicate predicate = new Predicate("aux_" + ++auxIndex, rule.getTerms().size());
        Atom aux = DefaultAtomFactory.instance().create(predicate, rule.getTerms().toArray(new Term[rule.getTerms().size()]));
        if (label.isEmpty()) {
            atomicHead.add(DefaultRuleFactory.instance().create(rule.getBody(), new LinkedListAtomSet(aux)));
            CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
            while (it.hasNext()) {
                Atom atom = it.next();
                atomicHead.add(DefaultRuleFactory.instance().create(aux, atom));
            }
        } else {
            int i = -1;
            atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, rule.getBody(), new LinkedListAtomSet(aux)));
            CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
            while (it.hasNext()) {
                Atom atom = it.next();
                atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, aux, atom));
            }
        }
    }
    return atomicHead;
}
Also used : LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Example 69 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class AbstractRulesCompilation method extractCompilable.

/**
 * Remove compilable rule from ruleSet and return a List of compilable
 * rules.
 *
 * @param ruleSet
 * @return a List containing the compilable rules.
 */
protected final LinkedList<Rule> extractCompilable(Iterator<Rule> ruleSet) {
    LinkedList<Rule> compilable = new LinkedList<Rule>();
    Rule r;
    while (ruleSet.hasNext()) {
        r = ruleSet.next();
        if (this.isCompilable(r)) {
            compilable.add(r);
            ruleSet.remove();
        }
    }
    if (this.getProfiler() != null) {
        this.getProfiler().put("Compiled rules", compilable.size());
    }
    return compilable;
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) LinkedList(java.util.LinkedList)

Example 70 with Rule

use of fr.lirmm.graphik.graal.api.core.Rule in project graal by graphik-team.

the class HierarchicalCompilation method computeIndex.

private void computeIndex(Iterable<Rule> ruleset) {
    for (Rule rule : ruleset) {
        // count the number of new pred in r
        CloseableIteratorWithoutException<Predicate> it = rule.getBody().predicatesIterator();
        while (it.hasNext()) {
            this.addPredicate(it.next());
        }
        it = rule.getHead().predicatesIterator();
        while (it.hasNext()) {
            this.addPredicate(it.next());
        }
    }
    int nbPred = this.indexPredicate.size();
    Atom father, son;
    this.order = new byte[nbPred][nbPred];
    for (Rule ru : ruleset) {
        father = ru.getHead().iterator().next();
        son = ru.getBody().iterator().next();
        this.addRule(father, son);
    }
    // reflexivity
    for (int i = 0; i < this.order.length; ++i) {
        this.order[i][i] = 1;
    }
}
Also used : Rule(fr.lirmm.graphik.graal.api.core.Rule) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate)

Aggregations

Rule (fr.lirmm.graphik.graal.api.core.Rule)141 Test (org.junit.Test)87 Atom (fr.lirmm.graphik.graal.api.core.Atom)64 OWL2Parser (fr.lirmm.graphik.graal.io.owl.OWL2Parser)52 DefaultNegativeConstraint (fr.lirmm.graphik.graal.core.DefaultNegativeConstraint)49 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)26 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)23 LinkedList (java.util.LinkedList)19 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)16 Term (fr.lirmm.graphik.graal.api.core.Term)15 Prefix (fr.lirmm.graphik.util.Prefix)14 CloseableIteratorWithoutException (fr.lirmm.graphik.util.stream.CloseableIteratorWithoutException)14 OWL2ParserException (fr.lirmm.graphik.graal.io.owl.OWL2ParserException)13 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)9 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)9 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)7 KnowledgeBase (fr.lirmm.graphik.graal.api.kb.KnowledgeBase)7 DefaultRule (fr.lirmm.graphik.graal.core.DefaultRule)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 Variable (fr.lirmm.graphik.graal.api.core.Variable)6