Search in sources :

Example 66 with Predicate

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

the class AtomTest method testDefaultAtomPredicateListOfTerm.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.core.DefaultAtom#DefaultAtom(fr.lirmm.graphik.graal.api.core.Predicate, java.util.List)}.
 */
@Theory
public void testDefaultAtomPredicateListOfTerm(AtomFactory factory) {
    // given
    Predicate p = TestUtils.p;
    List<Term> terms = new ArrayList<>();
    terms.add(TestUtils.A);
    terms.add(TestUtils.X);
    // when
    Atom a = factory.create(p, terms);
    // then
    Assert.assertEquals(p, a.getPredicate());
    Assert.assertEquals(TestUtils.A, a.getTerm(0));
    Assert.assertEquals(TestUtils.X, a.getTerm(1));
}
Also used : ArrayList(java.util.ArrayList) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 67 with Predicate

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

the class AtomTest method testDefaultAtomPredicate.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.core.DefaultAtom#DefaultAtom(fr.lirmm.graphik.graal.api.core.Predicate)}.
 */
@Theory
public void testDefaultAtomPredicate(AtomFactory factory) {
    // given
    Predicate p = TestUtils.p;
    // when
    Atom a = factory.create(p);
    // then
    Assert.assertEquals(p, a.getPredicate());
    Assert.assertNull(a.getTerm(0));
    Assert.assertNull(a.getTerm(1));
}
Also used : Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 68 with Predicate

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

the class AtomTest method testDefaultAtomPredicateTermArray.

/**
 * Test method for
 * {@link fr.lirmm.graphik.graal.core.DefaultAtom#DefaultAtom(fr.lirmm.graphik.graal.api.core.Predicate, fr.lirmm.graphik.graal.api.core.Term[])}.
 */
@Theory
public void testDefaultAtomPredicateTermArray(AtomFactory factory) {
    // given
    Predicate p = TestUtils.p;
    // when
    Atom a = factory.create(p, TestUtils.A, TestUtils.X);
    // then
    Assert.assertEquals(p, a.getPredicate());
    Assert.assertEquals(TestUtils.A, a.getTerm(0));
    Assert.assertEquals(TestUtils.X, a.getTerm(1));
}
Also used : Atom(fr.lirmm.graphik.graal.api.core.Atom) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Theory(org.junit.experimental.theories.Theory)

Example 69 with Predicate

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

the class IDCompilation method getRewritingOf.

/**
 * Return all possible rewritings of this Atom by this compilation.
 */
@Override
public Collection<Pair<Atom, Substitution>> getRewritingOf(Atom atom) {
    TreeSet<Pair<Atom, Substitution>> res = new TreeSet<Pair<Atom, Substitution>>();
    res.add(new ImmutablePair<Atom, Substitution>(atom, DefaultSubstitutionFactory.instance().createSubstitution()));
    Predicate predH = atom.getPredicate();
    Map<Predicate, LinkedList<IDCondition>> condH = this.conditions.get(predH);
    if (condH != null) {
        LinkedList<IDCondition> conds;
        Predicate predB;
        for (Map.Entry<Predicate, LinkedList<IDCondition>> entry : condH.entrySet()) {
            predB = entry.getKey();
            conds = entry.getValue();
            for (IDCondition cond : conds) {
                Pair<List<Term>, Substitution> ret = cond.generateBody(atom.getTerms());
                if (ret != null) {
                    List<Term> generatedBody = ret.getLeft();
                    res.add(new ImmutablePair<Atom, Substitution>(new DefaultAtom(predB, generatedBody), ret.getRight()));
                }
            }
        }
    }
    return res;
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) TreeMapSubstitution(fr.lirmm.graphik.graal.core.TreeMapSubstitution) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 70 with Predicate

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

the class IDCompilation method getSaturation.

// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public Iterable<Rule> getSaturation() {
    LinkedListRuleSet saturation = new LinkedListRuleSet();
    Map<Predicate, TreeMap<List<Integer>, InMemoryAtomSet>> newMap = new TreeMap<Predicate, TreeMap<List<Integer>, InMemoryAtomSet>>();
    // p -> q
    Predicate p, q;
    for (Map.Entry<Predicate, TreeMap<Predicate, LinkedList<IDCondition>>> e : this.conditions.entrySet()) {
        q = e.getKey();
        for (Map.Entry<Predicate, LinkedList<IDCondition>> map : e.getValue().entrySet()) {
            p = map.getKey();
            TreeMap<List<Integer>, InMemoryAtomSet> head = newMap.get(p);
            if (head == null) {
                head = new TreeMap<List<Integer>, InMemoryAtomSet>(new ListComparator<Integer>());
                newMap.put(p, head);
            }
            for (IDCondition conditionPQ : map.getValue()) {
                InMemoryAtomSet atomSet = head.get(conditionPQ.getBody());
                if (atomSet == null) {
                    atomSet = new LinkedListAtomSet();
                    head.put(conditionPQ.getBody(), atomSet);
                }
                atomSet.add(new DefaultAtom(q, conditionPQ.generateHead()));
            }
        }
    }
    for (Map.Entry<Predicate, TreeMap<List<Integer>, InMemoryAtomSet>> e1 : newMap.entrySet()) {
        p = e1.getKey();
        for (Map.Entry<List<Integer>, InMemoryAtomSet> e2 : e1.getValue().entrySet()) {
            List<Term> terms = new LinkedList<Term>();
            for (Integer i : e2.getKey()) {
                terms.add(DefaultTermFactory.instance().createVariable("X" + i));
            }
            InMemoryAtomSet body = new LinkedListAtomSet();
            body.add(new DefaultAtom(p, terms));
            saturation.add(DefaultRuleFactory.instance().create(body, e2.getValue()));
        }
    }
    return saturation;
}
Also used : DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) ListComparator(fr.lirmm.graphik.util.collections.ListComparator) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) LinkedListRuleSet(fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) Predicate(fr.lirmm.graphik.graal.api.core.Predicate) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

Predicate (fr.lirmm.graphik.graal.api.core.Predicate)77 Atom (fr.lirmm.graphik.graal.api.core.Atom)35 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)28 Term (fr.lirmm.graphik.graal.api.core.Term)27 Test (org.junit.Test)25 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)16 LinkedList (java.util.LinkedList)16 Theory (org.junit.experimental.theories.Theory)14 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)9 Rule (fr.lirmm.graphik.graal.api.core.Rule)9 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)9 LinkedListRuleSet (fr.lirmm.graphik.graal.core.ruleset.LinkedListRuleSet)7 DefaultURI (fr.lirmm.graphik.util.DefaultURI)7 TreeSet (java.util.TreeSet)7 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)6 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)6 RuleSet (fr.lirmm.graphik.graal.api.core.RuleSet)6 Variable (fr.lirmm.graphik.graal.api.core.Variable)6 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)6 Pair (org.apache.commons.lang3.tuple.Pair)6