use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class NaturalRDBMSStore method add.
// /////////////////////////////////////////////////////////////////////////
// PROTECTED AND PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
protected Statement add(Statement statement, Atom atom) throws AtomSetException {
if (!this.check(atom)) {
// FIXME say why
throw new UnsupportedAtomTypeException("");
}
try {
DBTable table = this.createPredicateTableIfNotExist(atom.getPredicate());
Iterator<DBColumn> cols = table.getColumns().iterator();
Map<String, String> data = new TreeMap<String, String>();
for (Term t : atom.getTerms()) {
DBColumn col = cols.next();
data.put(col.getName(), this.getConjunctiveQueryTranslator().formatFromColumnType(col, t));
}
String query = this.getDriver().getInsertOrIgnoreQuery(table, data);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(atom.toString() + " : " + query);
}
statement.addBatch(query);
} catch (SQLException e) {
throw new AtomSetException(e);
}
return statement;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class RDF4jStore method getTerms.
@Override
public Set<Term> getTerms() throws AtomSetException {
TreeSet<Term> set = new TreeSet<Term>();
CloseableIterator<Term> it = this.termsIterator();
try {
while (it.hasNext()) {
set.add(it.next());
}
} catch (IteratorException e) {
throw new AtomSetException("An error occurs during iteration over terms", e);
}
return set;
}
use of fr.lirmm.graphik.graal.api.core.Term 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.Term in project graal by graphik-team.
the class Utils method imply.
/**
* This methods test if the first rule logically imply the second. This
* methods works with linear rules (atomic head and body).
*
* @param r1
* @param r2
* @return true, if r1 logically imply r2.
*/
public static boolean imply(Rule r1, Rule r2) {
Atom b1 = r1.getBody().iterator().next();
Atom b2 = r2.getBody().iterator().next();
Atom h1 = r1.getHead().iterator().next();
Atom h2 = r2.getHead().iterator().next();
if (b1.getPredicate().equals(b2.getPredicate()) && h1.getPredicate().equals(h2.getPredicate())) {
Map<Term, Term> s = new TreeMap<Term, Term>();
Term term;
for (int i = 0; i < b1.getPredicate().getArity(); i++) {
term = s.get(b1.getTerm(i));
if (term == null)
s.put(b1.getTerm(i), b2.getTerm(i));
else if (!term.equals(b2.getTerm(i)))
return false;
}
for (int i = 0; i < h1.getPredicate().getArity(); i++) {
term = s.get(h1.getTerm(i));
if (term == null)
s.put(h1.getTerm(i), h2.getTerm(i));
else if (!term.equals(h2.getTerm(i)))
return false;
}
return true;
}
return false;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class Utils method rewrite.
/**
* Rewrite the fact q according to the unifier u.
*
* @param q
* the fact to rewrite
* @param u
* the unifier between q and r
* @return the rewrite of q according to the unifier u.
*/
public static ConjunctiveQuery rewrite(ConjunctiveQuery q, QueryUnifier u) {
InMemoryAtomSet ajout = u.getImageOf(u.getRule().getBody());
InMemoryAtomSet restant = u.getImageOf(AtomSetUtils.minus(q.getAtomSet(), u.getPiece()));
ConjunctiveQuery rew = null;
if (ajout != null && restant != null) {
// FIXME
InMemoryAtomSet res = AtomSetUtils.union(ajout, restant);
List<Term> ansVar = new LinkedList<Term>();
ansVar.addAll(q.getAnswerVariables());
rew = DefaultConjunctiveQueryFactory.instance().create(res, ansVar);
}
return rew;
}
Aggregations