use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class FrontierRestrictedChaseHaltingCondition method apply.
@Override
public CloseableIterator<Atom> apply(Rule rule, Substitution substitution, AtomSet data) throws HomomorphismFactoryException, HomomorphismException {
Set<Term> fixedVars = substitution.getValues();
if (ruleIndex.get(rule) == null) {
ruleIndex.put(rule, _currentRuleIndex++);
}
final int index = ruleIndex.get(rule).intValue();
StringBuilder frontierSb = new StringBuilder();
SortedSet<Variable> frontierSet = new TreeSet<Variable>(rule.getFrontier());
for (Term t : frontierSet) {
frontierSb.append("_");
frontierSb.append(t.getLabel());
frontierSb.append(substitution.createImageOf(t).getLabel());
}
String frontier = frontierSb.toString();
for (Variable t : rule.getExistentials()) {
substitution.put(t, DefaultTermFactory.instance().createConstant("f_" + index + "_" + t.getIdentifier() + frontier));
}
InMemoryAtomSet newFacts = substitution.createImageOf(rule.getHead());
ConjunctiveQuery query = new ConjunctiveQueryWithFixedVariables(newFacts, fixedVars);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Fixed Query:" + query);
}
try {
if (SmartHomomorphism.instance().execute(query, data).hasNext()) {
return new CloseableIteratorAdapter<Atom>(Collections.<Atom>emptyList().iterator());
}
} catch (IteratorException e) {
throw new HomomorphismException("An errors occurs while iterating results", e);
}
return newFacts.iterator();
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class Utils method getSafeCopy.
public static InMemoryAtomSet getSafeCopy(InMemoryAtomSet atomSet) {
Substitution substitution = new TreeMapSubstitution();
for (Variable t : atomSet.getVariables()) {
substitution.put(t, varGen.getFreshSymbol());
}
InMemoryAtomSet safe = new LinkedListAtomSet();
substitution.apply(atomSet, safe);
return safe;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class IDConditionImpl method homomorphism.
@Override
public Substitution homomorphism(List<Term> head, List<Term> to) {
if (!checkBody(to)) {
return null;
}
Pair<List<Term>, Substitution> ret = this.generateBody(head);
if (ret == null) {
return null;
}
Substitution s = ret.getRight();
Substitution homo = DefaultSubstitutionFactory.instance().createSubstitution();
List<Term> generatedBody = ret.getLeft();
// check for a simple homomorphism from generated body into 'to'
Iterator<Term> itFrom = generatedBody.iterator();
Iterator<Term> itTo = to.iterator();
while (itFrom.hasNext() && itTo.hasNext()) {
Term termFrom = itFrom.next();
Term termTo = itTo.next();
if (termFrom.isConstant()) {
if (!termFrom.equals(termTo)) {
return null;
}
} else {
if (!homo.put((Variable) termFrom, termTo)) {
return null;
}
}
}
if (itFrom.hasNext() || itTo.hasNext()) {
throw new Error("Wrong term number");
}
// homo
for (Variable t : s.getTerms()) {
homo.put(t, homo.createImageOf(s.createImageOf(t)));
}
return homo;
}
use of fr.lirmm.graphik.graal.api.core.Variable in project graal by graphik-team.
the class DefaultRule method computeFrontierAndExistentials.
// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void computeFrontierAndExistentials() {
this.frontier = new TreeSet<Variable>();
this.existentials = new TreeSet<Variable>();
Collection<Variable> body = this.getBody().getVariables();
for (Variable termHead : this.getHead().getVariables()) {
boolean isExistential = true;
for (Variable termBody : body) {
if (termBody.equals(termHead)) {
this.frontier.add(termHead);
isExistential = false;
}
}
if (isExistential) {
this.existentials.add(termHead);
}
}
}
use of fr.lirmm.graphik.graal.api.core.Variable 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