Search in sources :

Example 21 with Variable

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

the class EqualityUtils method processEquality.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * This method produces a conjunctive query based on the specified one where
 * equality atoms are removed and affected variables are replaced or merged.
 * It returns the new query and a substitution which allow to rebuild
 * answers to the original query based on answers to the returned one by
 * composition of each answer with the returned substitution.
 *
 * @param q
 *            a conjunctive query
 * @return a pair composed of the computed conjunctive query and the
 *         substitution which allow to rebuild answers.
 */
public static Pair<ConjunctiveQuery, Substitution> processEquality(ConjunctiveQuery q) {
    LinkedList<Atom> toRemove = new LinkedList<Atom>();
    Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
    CloseableIteratorWithoutException<Atom> it = q.getAtomSet().iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        if (Predicate.EQUALITY.equals(a.getPredicate())) {
            if (a.getTerm(0).isVariable()) {
                if (!updateSubstitution(s, (Variable) a.getTerm(0), a.getTerm(1))) {
                    return generateBottomResult();
                }
                toRemove.add(a);
            } else if (a.getTerm(1).isVariable()) {
                if (!updateSubstitution(s, (Variable) a.getTerm(1), a.getTerm(0))) {
                    return generateBottomResult();
                }
                toRemove.add(a);
            } else {
                return generateBottomResult();
            }
        }
    }
    return new ImmutablePair<ConjunctiveQuery, Substitution>(generateQuery(q, s, toRemove), 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) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList)

Example 22 with Variable

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

the class AbstractDlgpListener method endsConjunction.

@Override
public void endsConjunction(OBJECT_TYPE objectType) {
    switch(objectType) {
        case QUERY:
            Set<Variable> bodyVars = this.atomSet.getVariables();
            for (Term t : this.answerVars) {
                if (t.isVariable() && !bodyVars.contains(t)) {
                    throw new ParseError("The variable [" + t + "] of the answer list does not appear in the query body.");
                }
            }
            this.createQuery(DefaultConjunctiveQueryFactory.instance().create(this.label, this.atomSet, this.answerVars));
            break;
        case NEG_CONSTRAINT:
            this.createNegConstraint(new DefaultNegativeConstraint(this.label, this.atomSet));
            break;
        case RULE:
            if (this.atomSet2 == null) {
                this.atomSet2 = this.atomSet;
                this.atomSet = new LinkedListAtomSet();
            } else {
                this.createRule(DefaultRuleFactory.instance().create(this.label, this.atomSet, this.atomSet2));
            }
            break;
        case FACT:
            this.createAtomSet(this.atomSet);
            break;
        default:
            break;
    }
}
Also used : DefaultNegativeConstraint(fr.lirmm.graphik.graal.core.DefaultNegativeConstraint) Variable(fr.lirmm.graphik.graal.api.core.Variable) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) ParseError(fr.lirmm.graphik.graal.api.io.ParseError) Term(fr.lirmm.graphik.graal.api.core.Term)

Example 23 with Variable

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

the class AbstractNFC method init.

@Override
public void init(VarSharedData[] vars, Map<Variable, Integer> map) {
    this.data = new VarData[vars.length];
    for (int i = 0; i < vars.length; ++i) {
        this.data[vars[i].level] = new VarData();
        this.data[vars[i].level].candidats = new AcceptableCandidats[vars.length];
        this.data[vars[i].level].tmp = new HashSet<Term>();
        this.data[vars[i].level].toCheckAfterAssignment = new LinkedList<Atom>();
        for (Atom a : vars[i].preAtoms) {
            int cpt = 0;
            boolean toAdd = true;
            for (Variable t : a.getVariables()) {
                if (map.containsKey(t)) {
                    if (t.equals(vars[i].value))
                        ++cpt;
                    else
                        toAdd = false;
                }
            }
            if (toAdd || cpt > 1) {
                this.data[vars[i].level].toCheckAfterAssignment.add(a);
            }
        }
        AcceptableCandidats previous = new AcceptableCandidats();
        for (VarSharedData z : vars[i].preVars) {
            AcceptableCandidats ac = new AcceptableCandidats();
            ac.candidats = new TreeSet<Term>();
            ac.previous = previous;
            previous = ac;
            this.data[vars[i].level].candidats[z.level] = ac;
        }
        this.data[vars[i].level].last = previous;
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Example 24 with Variable

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

the class FixedOrderScheduler method execute.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public VarSharedData[] execute(InMemoryAtomSet h, List<Term> ans, AtomSet data, RulesCompilation rc) throws HomomorphismException {
    Set<Variable> terms = h.getVariables();
    VarSharedData[] vars = new VarSharedData[terms.size() + 2];
    int level = 0;
    vars[level] = new VarSharedData(level);
    Set<Term> alreadyAffected = new TreeSet<Term>();
    for (Variable v : this.order) {
        if (!terms.contains(v)) {
            throw new HomomorphismException("Try to schedule a variable which is not in the query :" + v);
        }
        if (alreadyAffected.contains(v)) {
            throw new HomomorphismException("There is two occurences of the same variable in the specified order.");
        }
        ++level;
        vars[level] = new VarSharedData(level);
        vars[level].value = v;
        alreadyAffected.add(v);
    }
    terms.removeAll(alreadyAffected);
    if (!terms.isEmpty()) {
        throw new HomomorphismException("Some variables of the query are not scheduled :" + terms);
    }
    ++level;
    vars[level] = new VarSharedData(level);
    vars[level].previousLevel = level - 1;
    return vars;
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) TreeSet(java.util.TreeSet) Term(fr.lirmm.graphik.graal.api.core.Term) VarSharedData(fr.lirmm.graphik.graal.homomorphism.VarSharedData)

Example 25 with Variable

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

the class BacktrackIteratorData method computeAtomOrder.

/**
 * The index 0 contains the fully instantiated atoms.
 *
 * @param atomset
 * @param vars
 */
private static void computeAtomOrder(CloseableIterableWithoutException<Atom> atomset, VarSharedData[] vars, Map<Variable, Integer> index) {
    int tmp, rank;
    // initialisation preAtoms and postAtoms Collections
    for (int i = 0; i < vars.length; ++i) {
        vars[i].preAtoms = new HashSet<Atom>();
        vars[i].postAtoms = new HashSet<Atom>();
        vars[i].postVars = new HashSet<VarSharedData>();
        vars[i].preVars = new TreeSet<VarSharedData>();
    }
    // 
    CloseableIteratorWithoutException<Atom> it = atomset.iterator();
    while (it.hasNext()) {
        Atom a = it.next();
        rank = 0;
        for (Variable t : a.getVariables()) {
            Integer idx = index.get(t);
            if (idx != null) {
                tmp = vars[idx].level;
                vars[tmp].postAtoms.add(a);
                if (rank < tmp)
                    rank = tmp;
            }
        }
        vars[rank].postAtoms.remove(a);
        vars[rank].preAtoms.add(a);
    }
    for (int i = 0; i < vars.length; ++i) {
        for (Atom a : vars[i].postAtoms) {
            for (Variable t : a.getVariables()) {
                Integer idx = index.get(t);
                if (idx != null) {
                    if (vars[idx].level > i) {
                        vars[i].postVars.add(vars[idx]);
                        vars[idx].preVars.add(vars[i]);
                    }
                }
            }
        }
    }
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Atom(fr.lirmm.graphik.graal.api.core.Atom)

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