Search in sources :

Example 6 with Variable

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();
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) Term(fr.lirmm.graphik.graal.api.core.Term) CloseableIteratorAdapter(fr.lirmm.graphik.util.stream.CloseableIteratorAdapter) ConjunctiveQueryWithFixedVariables(fr.lirmm.graphik.graal.core.ConjunctiveQueryWithFixedVariables) Atom(fr.lirmm.graphik.graal.api.core.Atom) TreeSet(java.util.TreeSet) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)

Example 7 with Variable

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;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution)

Example 8 with Variable

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;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) MethodNotImplementedError(fr.lirmm.graphik.util.MethodNotImplementedError) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 9 with Variable

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);
        }
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable)

Example 10 with Variable

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;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term)

Aggregations

Variable (fr.lirmm.graphik.graal.api.core.Variable)57 Atom (fr.lirmm.graphik.graal.api.core.Atom)33 Term (fr.lirmm.graphik.graal.api.core.Term)32 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)25 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)19 LinkedList (java.util.LinkedList)15 Test (org.junit.Test)15 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)8 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)8 Constant (fr.lirmm.graphik.graal.api.core.Constant)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)6 Rule (fr.lirmm.graphik.graal.api.core.Rule)6 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 VarSharedData (fr.lirmm.graphik.graal.homomorphism.VarSharedData)5 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)4 DefaultConjunctiveQueryWithNegatedParts (fr.lirmm.graphik.graal.core.DefaultConjunctiveQueryWithNegatedParts)4 HashMapSubstitution (fr.lirmm.graphik.graal.core.HashMapSubstitution)4 DefaultInMemoryGraphStore (fr.lirmm.graphik.graal.core.atomset.graph.DefaultInMemoryGraphStore)4