Search in sources :

Example 41 with Variable

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

the class RuleMLWriter method write.

@Override
public RuleMLWriter write(Rule rule) throws IOException {
    Set<Variable> existVar = rule.getExistentials();
    Set<Variable> universalVar = rule.getVariables();
    universalVar.removeAll(existVar);
    CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
    if (it.hasNext()) {
        it.next();
    }
    boolean isAtomicHead = !it.hasNext();
    this.openBalise("Assert");
    this.writeLabel(rule.getLabel());
    this.openBalise("Forall");
    for (Term t : universalVar) {
        this.writeTerm(t);
    }
    this.openBalise("Implies");
    this.openBalise("if");
    this.openBalise("And");
    this.writeAtomSet(rule.getBody().iterator());
    this.closeBaliseWithReturnLine("And");
    this.closeBaliseWithReturnLine("if");
    this.openBalise("then");
    if (!existVar.isEmpty()) {
        this.openBalise("Exists");
        for (Term t : existVar) {
            this.writeTerm(t);
        }
    }
    if (!isAtomicHead) {
        this.openBalise("And");
    }
    this.writeAtomSet(rule.getHead().iterator());
    if (!isAtomicHead) {
        this.closeBaliseWithReturnLine("And");
    }
    if (!existVar.isEmpty()) {
        this.closeBaliseWithReturnLine("Exists");
    }
    this.closeBaliseWithReturnLine("then");
    this.closeBaliseWithReturnLine("Implies");
    this.closeBaliseWithReturnLine("Forall");
    this.closeBaliseWithReturnLine("Assert");
    return this;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 42 with Variable

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

the class AbstractAffectedPositionSet method step1.

/**
 * for each rule and for each existentially quantified variable occuring at
 * position p[i] in its head, p[i] is affected;
 */
protected void step1() {
    int i;
    Set<Variable> existentials;
    PredicatePosition predicatePosition;
    for (Rule rule : ruleSet) {
        existentials = rule.getExistentials();
        CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
        while (it.hasNext()) {
            Atom atom = it.next();
            i = -1;
            for (Term t : atom) {
                ++i;
                if (existentials.contains(t)) {
                    predicatePosition = new PredicatePosition(atom.getPredicate(), i);
                    this.affectedPosition.add(predicatePosition);
                }
            }
        }
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) PredicatePosition(fr.lirmm.graphik.graal.rulesetanalyser.util.PredicatePosition) Rule(fr.lirmm.graphik.graal.api.core.Rule) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 43 with Variable

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

the class FrontierGuardedProperty method check.

@Override
public int check(Rule rule) {
    Set<Variable> frontier = rule.getFrontier();
    boolean isGuarded = true;
    CloseableIteratorWithoutException<Atom> it = rule.getBody().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        isGuarded = true;
        for (Term v : frontier) {
            if (!a.getTerms().contains(v)) {
                isGuarded = false;
                break;
            }
        }
        if (isGuarded) {
            break;
        }
    }
    if (isGuarded)
        return 1;
    return -1;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 44 with Variable

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

the class GraphPositionDependencies method init.

private void init() {
    int bodyTermIndex, headTermIndex;
    Set<Variable> existentials;
    for (Rule r : this.rules) {
        existentials = r.getExistentials();
        CloseableIteratorWithoutException<Atom> bodyIt = r.getBody().iterator();
        while (bodyIt.hasNext()) {
            Atom bodyAtom = bodyIt.next();
            bodyTermIndex = -1;
            for (Term bodyTerm : bodyAtom) {
                ++bodyTermIndex;
                if (r.getHead().getTerms().contains(bodyTerm)) {
                    CloseableIteratorWithoutException<Atom> headIt = r.getHead().iterator();
                    while (headIt.hasNext()) {
                        Atom headAtom = headIt.next();
                        headTermIndex = -1;
                        for (Term headTerm : headAtom) {
                            ++headTermIndex;
                            if (bodyTerm.equals(headTerm)) {
                                this.addEdge(new PredicatePosition(bodyAtom.getPredicate(), bodyTermIndex), new PredicatePosition(headAtom.getPredicate(), headTermIndex));
                            } else if (existentials.contains(headTerm)) {
                                this.addSpecialEdge(new PredicatePosition(bodyAtom.getPredicate(), bodyTermIndex), new PredicatePosition(headAtom.getPredicate(), headTermIndex));
                            }
                        }
                    }
                } else {
                    if (!this.graph.containsVertex(new PredicatePosition(bodyAtom.getPredicate(), bodyTermIndex))) {
                        this.graph.addVertex(new PredicatePosition(bodyAtom.getPredicate(), bodyTermIndex));
                    }
                }
            }
        }
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) PredicatePosition(fr.lirmm.graphik.graal.rulesetanalyser.util.PredicatePosition) Rule(fr.lirmm.graphik.graal.api.core.Rule) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 45 with Variable

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

the class IDConditionImpl method generateBody.

@Override
public Pair<List<Term>, Substitution> generateBody(List<Term> head) {
    Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
    Set<Variable> toRemoveFromPartition = new TreeSet<Variable>();
    for (int i = 0; i < condHead.length; i++) {
        Variable v = DefaultTermFactory.instance().createVariable(condHead[i]);
        toRemoveFromPartition.add(v);
        if (!s.aggregate(v, head.get(i))) {
            return null;
        }
    }
    List<Term> body = new ArrayList<Term>(condBody.length);
    for (int i = 0; i < condBody.length; i++) {
        Variable v = DefaultTermFactory.instance().createVariable(condBody[i]);
        toRemoveFromPartition.add(v);
        body.add(s.createImageOf(v));
    }
    for (Variable v : toRemoveFromPartition) {
        s.remove(v);
    }
    return new ImmutablePair<List<Term>, Substitution>(body, s);
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) 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