use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class DlgpWriterTest method writePredicate.
@Test
public void writePredicate() throws IOException {
Predicate p = new Predicate("P", 1);
ByteArrayOutputStream os = new ByteArrayOutputStream();
DlgpWriter writer = new DlgpWriter(os);
writer.write(new DefaultAtom(p, cst));
writer.flush();
String s = new String(os.toByteArray(), "UTF-8");
writer.close();
Character c = s.charAt(0);
Assert.assertTrue("Predicate label does not begin with lower case or double quote.", Character.isLowerCase(c) || c == '<');
}
use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class DlgpWriterTest method writeURIForbiddenChars.
@Test
public void writeURIForbiddenChars() throws IOException {
Predicate p = new Predicate(new DefaultURI("/< >\"{}|^`\\/"), 1);
ByteArrayOutputStream os = new ByteArrayOutputStream();
DlgpWriter writer = new DlgpWriter(os);
writer.write(new DefaultAtom(p, cst));
writer.flush();
String s = new String(os.toByteArray(), "UTF-8");
writer.close();
System.out.println(s);
Assert.assertEquals("</\\u003c\\u0020\\u003e\\u0022\\u007b\\u007d\\u007c\\u005e\\u0060\\u005c/>(<A>).\n", s);
}
use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class DlgpWriterTest method bug76.
@Test
public void bug76() throws IOException {
Prefix p1 = new Prefix("a", "http://p#");
Prefix p2 = new Prefix("b", "http://p#p/");
Predicate p = new Predicate(new DefaultURI("http://p#p/toto"), 1);
ByteArrayOutputStream os = new ByteArrayOutputStream();
DlgpWriter writer = new DlgpWriter(os);
writer.write(p1);
writer.write(p2);
writer.write(new DefaultAtom(p, cst));
writer.flush();
String s = new String(os.toByteArray(), "UTF-8");
writer.close();
Assert.assertTrue(s.contains("b:toto(<A>)."));
}
use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class BackwardChainingTest method getBody2.
/**
* c(X) :- b(X,X).
*
* getBody([X]) => b(X, X).
*
* @throws ParseException
*/
@Theory
public void getBody2(RulesCompilation compilation, RewritingOperator operator) throws ParseException {
RuleSet rules = new LinkedListRuleSet();
rules.add(DlgpParser.parseRule("p(X) :- q(X,X)."));
ConjunctiveQuery query = DlgpParser.parseQuery("?(X) :- p(X).");
compilation.compile(rules.iterator());
PureRewriter bc = new PureRewriter(operator, true);
CloseableIteratorWithoutException<? extends ConjunctiveQuery> rewIt = bc.execute(query, rules, compilation);
boolean isFound = false;
while (rewIt.hasNext()) {
ConjunctiveQuery rew = rewIt.next();
CloseableIteratorWithoutException<Atom> it = rew.getAtomSet().iterator();
if (it.hasNext()) {
Atom a = it.next();
if (a.getPredicate().equals(new Predicate("q", 2))) {
isFound = true;
List<Term> terms = a.getTerms();
Assert.assertEquals(terms.get(0), terms.get(1));
}
}
}
Assert.assertTrue("Rewrite not found", isFound);
}
use of fr.lirmm.graphik.graal.api.core.Predicate in project graal by graphik-team.
the class Rules method computeAtomicHead.
/**
* Generate a set of atomic head rules equivalent of the specified rule.
*
* @param rule
* @return a Collection of Rule which is a decomposition of the specified rule to atomic head rules.
*/
public static Collection<Rule> computeAtomicHead(Rule rule) {
String label = rule.getLabel();
Collection<Rule> atomicHead = new LinkedList<Rule>();
if (rule.getHead().isEmpty() || hasAtomicHead(rule)) {
return Collections.<Rule>singleton(rule);
} else {
Predicate predicate = new Predicate("aux_" + ++auxIndex, rule.getTerms().size());
Atom aux = DefaultAtomFactory.instance().create(predicate, rule.getTerms().toArray(new Term[rule.getTerms().size()]));
if (label.isEmpty()) {
atomicHead.add(DefaultRuleFactory.instance().create(rule.getBody(), new LinkedListAtomSet(aux)));
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom atom = it.next();
atomicHead.add(DefaultRuleFactory.instance().create(aux, atom));
}
} else {
int i = -1;
atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, rule.getBody(), new LinkedListAtomSet(aux)));
CloseableIteratorWithoutException<Atom> it = rule.getHead().iterator();
while (it.hasNext()) {
Atom atom = it.next();
atomicHead.add(DefaultRuleFactory.instance().create(label + "-a" + ++i, aux, atom));
}
}
}
return atomicHead;
}
Aggregations