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;
}
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);
}
}
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);
}
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;
}
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;
}
Aggregations