Search in sources :

Example 11 with Constant

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

the class AbstractAtomSet method getConstants.

@Override
public Set<Constant> getConstants() throws AtomSetException {
    Set<Constant> terms = new HashSet<Constant>();
    CloseableIterator<Atom> atomIt = this.iterator();
    try {
        while (atomIt.hasNext()) {
            Iterator<Term> termIt = atomIt.next().iterator();
            while (termIt.hasNext()) {
                Term t = termIt.next();
                if (t.isConstant()) {
                    terms.add((Constant) t);
                }
            }
        }
    } catch (Exception e) {
        throw new AtomSetException(e);
    }
    return terms;
}
Also used : Constant(fr.lirmm.graphik.graal.api.core.Constant) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) Atom(fr.lirmm.graphik.graal.api.core.Atom) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) HashSet(java.util.HashSet)

Example 12 with Constant

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

the class FrontierRestrictedChaseHaltingConditionTest method test2.

@Test
public void test2() throws IteratorException, HomomorphismFactoryException, HomomorphismException {
    InMemoryAtomSet atomset = DefaultAtomSetFactory.instance().create(DlgpParser.parseAtomSet("p(a,b), p(b,a)."));
    Rule rule = DlgpParser.parseRule("p(X,Z):-p(X,Y).");
    Variable x = DefaultTermFactory.instance().createVariable("X");
    Variable y = DefaultTermFactory.instance().createVariable("Y");
    Constant a = DefaultTermFactory.instance().createConstant("a");
    Constant b = DefaultTermFactory.instance().createConstant("b");
    Substitution s = DefaultSubstitutionFactory.instance().createSubstitution();
    s.put(x, a);
    s.put(y, b);
    FrontierRestrictedChaseHaltingCondition condition = new FrontierRestrictedChaseHaltingCondition();
    CloseableIterator<Atom> toAdd = condition.apply(rule, s, atomset);
    Assert.assertTrue(toAdd.hasNext());
    Atom atom1 = toAdd.next();
    atomset.add(atom1);
    Assert.assertFalse(toAdd.hasNext());
    toAdd.close();
    s = DefaultSubstitutionFactory.instance().createSubstitution();
    s.put(x, b);
    s.put(y, a);
    toAdd = condition.apply(rule, s, atomset);
    Assert.assertTrue(toAdd.hasNext());
    Atom atom2 = toAdd.next();
    atomset.add(atom2);
    Assert.assertFalse(toAdd.hasNext());
    toAdd.close();
    Assert.assertNotEquals(atom1.getTerm(1), atom2.getTerm(1));
}
Also used : Variable(fr.lirmm.graphik.graal.api.core.Variable) Substitution(fr.lirmm.graphik.graal.api.core.Substitution) Constant(fr.lirmm.graphik.graal.api.core.Constant) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) Rule(fr.lirmm.graphik.graal.api.core.Rule) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Aggregations

Constant (fr.lirmm.graphik.graal.api.core.Constant)12 Atom (fr.lirmm.graphik.graal.api.core.Atom)11 Substitution (fr.lirmm.graphik.graal.api.core.Substitution)9 Term (fr.lirmm.graphik.graal.api.core.Term)9 Variable (fr.lirmm.graphik.graal.api.core.Variable)7 Test (org.junit.Test)7 Predicate (fr.lirmm.graphik.graal.api.core.Predicate)5 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)5 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)5 LinkedList (java.util.LinkedList)5 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)3 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)2 Rule (fr.lirmm.graphik.graal.api.core.Rule)2 BacktrackException (fr.lirmm.graphik.graal.homomorphism.BacktrackException)2 CloseableIteratorAdapter (fr.lirmm.graphik.util.stream.CloseableIteratorAdapter)2 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)2 Store (fr.lirmm.graphik.graal.api.store.Store)1 HashSet (java.util.HashSet)1 Theory (org.junit.experimental.theories.Theory)1