use of fr.lirmm.graphik.graal.api.core.Term 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;
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class DefaultInMemoryGraphStore method add.
@Override
public boolean add(Atom atom) {
List<TermVertex> atomTerms = new LinkedList<TermVertex>();
PredicateVertex atomPredicate;
for (Term t : atom.getTerms()) {
atomTerms.add(this.addTermVertex(TermVertexFactory.instance().createTerm(t)));
}
atomPredicate = this.addPredicateVertex(new PredicateVertex(atom.getPredicate()));
AtomEdge atomEdge = new AtomEdge(atomPredicate, atomTerms);
return this.addAtomEdge(atomEdge);
}
use of fr.lirmm.graphik.graal.api.core.Term in project graal by graphik-team.
the class TripleStoreTest method simpleTest.
@Theory
public void simpleTest(AtomSet store) throws AtomSetException, IteratorException {
Assume.assumeTrue(store instanceof TripleStore);
Term t1 = DefaultTermFactory.instance().createConstant("http://to.to/b");
Term t2 = DefaultTermFactory.instance().createConstant("http://to.to/a");
Predicate p = new Predicate("http://to.to/p", 2);
Atom atom1 = new DefaultAtom(p, t1, t2);
store.add(atom1);
int i = 0;
for (CloseableIterator<Atom> it = store.iterator(); it.hasNext(); it.next()) {
++i;
}
Assert.assertEquals(1, i);
}
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 RdbmsAtomIterator method hasNext.
// /////////////////////////////////////////////////////////////////////////
// METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public boolean hasNext() throws IteratorException {
if (!this.hasNextCallDone) {
this.hasNextCallDone = true;
while (this.predicateIt.hasNext() && (this.atomIt == null || !this.atomIt.hasNext())) {
Predicate p = predicateIt.next();
List<Term> terms = new LinkedList<Term>();
VariableGenerator gen = new DefaultVariableGenerator("X");
for (int i = 0; i < p.getArity(); ++i) {
terms.add(gen.getFreshSymbol());
}
InMemoryAtomSet atomSet = new LinkedListAtomSet();
Atom atom = new DefaultAtom(p, terms);
atomSet.add(atom);
ConjunctiveQuery query = DefaultConjunctiveQueryFactory.instance().create(atomSet);
SqlHomomorphism solver = SqlHomomorphism.instance();
try {
this.atomIt = new SubstitutionIterator2AtomIterator(atom, solver.execute(query, this.store));
} catch (HomomorphismException e) {
throw new IteratorException(e);
}
}
}
return this.atomIt != null && this.atomIt.hasNext();
}
Aggregations