Search in sources :

Example 76 with Term

use of fr.lirmm.graphik.graal.api.core.Term 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 77 with Term

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

the class Rules method generateCriticalInstance.

private static void generateCriticalInstance(InMemoryAtomSet A, Set<Term> terms, Predicate p, int position, DefaultAtom a) {
    if (position >= p.getArity()) {
        A.add(a);
        return;
    }
    for (Term t : terms) {
        DefaultAtom a2 = new DefaultAtom(a);
        a2.setTerm(position, t);
        generateCriticalInstance(A, terms, p, position + 1, a2);
    }
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term)

Example 78 with Term

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

the class DefaultInMemoryGraphStore method add.

@Override
public boolean add(Atom atom) {
    List<TermVertex> atomTerms = new LinkedList<TermVertex>();
    PredicateVertex atomPredicate;
    for (Term t : atom.getTerms()) {
        atomTerms.add(this.addTermVertex(TermVertexFactory.instance().createTerm(t)));
    }
    atomPredicate = this.addPredicateVertex(new PredicateVertex(atom.getPredicate()));
    AtomEdge atomEdge = new AtomEdge(atomPredicate, atomTerms);
    return this.addAtomEdge(atomEdge);
}
Also used : Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList)

Example 79 with Term

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

the class HierarchicalCompilation method homomorphism.

@Override
public Collection<Substitution> homomorphism(Atom father, Atom son) {
    LinkedList<Substitution> res = new LinkedList<Substitution>();
    if (isMappable(father.getPredicate(), son.getPredicate())) {
        Substitution sub = DefaultSubstitutionFactory.instance().createSubstitution();
        Iterator<Term> fatherTermsIt = father.getTerms().iterator();
        Iterator<Term> sonTermsIt = son.getTerms().iterator();
        Term fatherTerm, sonTerm;
        while (fatherTermsIt.hasNext() && sonTermsIt.hasNext()) {
            fatherTerm = fatherTermsIt.next();
            sonTerm = sonTermsIt.next();
            if (fatherTerm.isConstant()) {
                if (!fatherTerm.equals(sonTerm)) {
                    return res;
                }
            } else if (!sub.getTerms().contains(fatherTerm))
                sub.put((Variable) fatherTerm, sonTerm);
            else if (!sub.createImageOf(fatherTerm).equals(sonTerm))
                return res;
        }
        res.add(sub);
    }
    return res;
}
Also used : Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Term(fr.lirmm.graphik.graal.api.core.Term) LinkedList(java.util.LinkedList)

Example 80 with Term

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

the class AbstractMapBasedSubstitution method aggregate.

@Override
public boolean aggregate(Variable term, Term substitut) {
    Term termSubstitut = this.createImageOf(term);
    Term substitutSubstitut = this.createImageOf(substitut);
    if (!termSubstitut.equals(substitutSubstitut)) {
        if (termSubstitut.isConstant()) {
            if (substitutSubstitut.isConstant()) {
                return substitutSubstitut.equals(termSubstitut);
            } else {
                Term tmp = termSubstitut;
                termSubstitut = substitutSubstitut;
                substitutSubstitut = tmp;
            }
        }
        for (Variable t : this.getTerms()) {
            Term image = this.createImageOf(t);
            if (termSubstitut.equals(image) && !t.equals(substitutSubstitut)) {
                this.getMap().put(t, substitutSubstitut);
            }
        }
        this.getMap().put((Variable) termSubstitut, substitutSubstitut);
    }
    return true;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term)

Aggregations

Term (fr.lirmm.graphik.graal.api.core.Term)173 Atom (fr.lirmm.graphik.graal.api.core.Atom)86 LinkedList (java.util.LinkedList)41 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)36 Test (org.junit.Test)35 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)34 Variable (fr.lirmm.graphik.graal.api.core.Variable)32 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)27 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)26 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)23 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)18 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)15 Rule (fr.lirmm.graphik.graal.api.core.Rule)15 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)14 TreeSet (java.util.TreeSet)12 Constant (fr.lirmm.graphik.graal.api.core.Constant)9 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9 TreeMap (java.util.TreeMap)8