use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class DomainRestrictedProperty method check.
@Override
public int check(Rule rule) {
boolean none;
boolean all;
Set<Variable> bodyVars = rule.getBody().getVariables();
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom a = it.next();
all = none = true;
for (Term t : bodyVars) {
if (a.getTerms().contains(t)) {
none = false;
} else {
all = false;
}
if (!none && !all) {
return -1;
}
}
}
return 1;
}
use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class GuardedProperty method check.
@Override
public int check(Rule rule) {
Set<Variable> bodyVars = rule.getBody().getVariables();
boolean isGuarded = true;
CloseableIteratorWithoutException<Atom> it = rule.getBody().iterator();
while (it.hasNext()) {
Atom a = it.next();
isGuarded = true;
for (Term v : bodyVars) {
if (!a.getTerms().contains(v)) {
isGuarded = false;
break;
}
}
if (isGuarded) {
break;
}
}
if (isGuarded)
return 1;
return -1;
}
use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class IDCompilationTest method test3.
/**
* Given p(X,Y) -> q(X,Y,X,Y,X) <br>
* Then rew(q(U,V,A,B,V)) <br>
* Return q(U,V,A,B,V)-{}
*/
@Test
public void test3() {
Predicate predicateQ = new Predicate("q", 5);
Predicate predicateP = new Predicate("p", 2);
Atom body = new DefaultAtom(predicateP, X, Y);
Atom head = new DefaultAtom(predicateQ, X, Y, X, Y, X);
Atom query = new DefaultAtom(predicateQ, U, V, A, B, V);
RuleSet rules = new LinkedListRuleSet();
rules.add(DefaultRuleFactory.instance().create(body, head));
RulesCompilation comp = new IDCompilation();
comp.compile(rules.iterator());
Collection<Pair<Atom, Substitution>> rewritingOf = comp.getRewritingOf(query);
Assert.assertEquals(1, rewritingOf.size());
Pair<Atom, Substitution> p = rewritingOf.iterator().next();
Atom a = p.getLeft();
Substitution s = p.getRight();
Assert.assertEquals(predicateQ, a.getPredicate());
Assert.assertEquals(U, a.getTerm(0));
Assert.assertEquals(V, a.getTerm(1));
Assert.assertEquals(A, a.getTerm(2));
Assert.assertEquals(B, a.getTerm(3));
Assert.assertEquals(V, a.getTerm(4));
Assert.assertEquals(0, s.getTerms().size());
}
use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class IDCompilationTest method test2.
/**
* Given p(X,Y) -> q(X,Y,X) <br>
* Then rew(q(U,U,U)) <br>
* Return q(U,U,U)-{} AND p(U,U)-{})
*/
@Test
public void test2() {
Predicate predicateQ = new Predicate("q", 3);
Predicate predicateP = new Predicate("p", 2);
Atom body = new DefaultAtom(predicateP, X, Y);
Atom head = new DefaultAtom(predicateQ, X, Y, X);
Atom query = new DefaultAtom(predicateQ, U, U, U);
RuleSet rules = new LinkedListRuleSet();
rules.add(DefaultRuleFactory.instance().create(body, head));
RulesCompilation comp = new IDCompilation();
comp.compile(rules.iterator());
Collection<Pair<Atom, Substitution>> rewritingOf = comp.getRewritingOf(query);
boolean rew1 = false;
boolean rew2 = false;
for (Pair<Atom, Substitution> p : rewritingOf) {
Atom a = p.getLeft();
Substitution s = p.getRight();
if (a.getPredicate().equals(predicateQ)) {
rew1 = true;
Assert.assertEquals(U, a.getTerm(0));
Assert.assertEquals(U, a.getTerm(1));
Assert.assertEquals(U, a.getTerm(2));
Assert.assertEquals(0, s.getTerms().size());
} else {
rew2 = true;
Assert.assertEquals(predicateP, a.getPredicate());
Assert.assertEquals(U, a.getTerm(0));
Assert.assertEquals(U, a.getTerm(1));
Assert.assertEquals(0, s.getTerms().size());
}
}
Assert.assertTrue(rew1 && rew2);
Assert.assertEquals(2, rewritingOf.size());
}
use of fr.lirmm.graphik.graal.api.core.Atom in project graal by graphik-team.
the class AtomTest method constructorTest.
@Test
public void constructorTest() {
Predicate predicate = new Predicate("pred", 3);
Term[] terms = new Term[3];
terms[0] = new DefaultVariable("X");
terms[1] = new DefaultConstant("a");
terms[2] = new DefaultConstant("b");
Atom atom = new DefaultAtom(predicate, Arrays.asList(terms));
Assert.assertTrue(atom.getPredicate().equals(predicate));
Assert.assertTrue(atom.getTerm(0).equals(terms[0]));
Assert.assertTrue(atom.getTerm(1).equals(terms[1]));
Assert.assertTrue(atom.getTerm(2).equals(terms[2]));
Assert.assertTrue("The list eiuae ", atom.getTerms().equals(Arrays.asList(terms)));
}
Aggregations