use of fr.lirmm.graphik.util.stream.IteratorException in project graal by graphik-team.
the class NaturalRDBMSStore method termsIterator.
@Override
public CloseableIterator<Term> termsIterator() throws AtomSetException {
Set<Term> terms = new TreeSet<Term>();
CloseableIterator<Predicate> predIt = this.predicatesIterator();
try {
while (predIt.hasNext()) {
Predicate p = predIt.next();
for (int i = 0; i < p.getArity(); ++i) {
CloseableIterator<Term> termIt = this.termsByPredicatePosition(p, i);
while (termIt.hasNext()) {
terms.add(termIt.next());
}
}
}
} catch (IteratorException e) {
throw new AtomSetException(e);
}
return new CloseableIteratorAdapter<Term>(terms.iterator());
}
use of fr.lirmm.graphik.util.stream.IteratorException in project graal by graphik-team.
the class NaturalConjunctiveQueryTranslator method translate.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public SQLQuery translate(ConjunctiveQuery cquery) throws AtomSetException {
if (cquery.getAtomSet().isEmpty()) {
return SQLQuery.emptyInstance();
}
AtomSet atomSet = cquery.getAtomSet();
StringBuilder fields = new StringBuilder();
StringBuilder tables = new StringBuilder();
StringBuilder where = new StringBuilder();
HashMap<Atom, String> tableAsNames = new HashMap<Atom, String>();
HashMap<Atom, String> tableNames = new HashMap<Atom, String>();
HashMap<Atom, List<DBColumn>> tableColumns = new HashMap<Atom, List<DBColumn>>();
HashMap<String, String> lastOccurrence = new HashMap<String, String>();
ArrayList<String> constants = new ArrayList<String>();
ArrayList<String> equivalences = new ArrayList<String>();
TreeMap<Term, String> columns = new TreeMap<Term, String>();
int count = -1;
CloseableIterator<Atom> it = atomSet.iterator();
try {
while (it.hasNext()) {
Atom atom = it.next();
DBTable table = this.store.getPredicateTableIfExist(atom.getPredicate());
if (table != null) {
String tableName = "atom" + ++count;
tableAsNames.put(atom, tableName);
tableNames.put(atom, table.getName());
tableColumns.put(atom, table.getColumns());
} else {
return SQLQuery.hasSchemaErrorInstance();
}
}
// Create WHERE clause
it = atomSet.iterator();
while (it.hasNext()) {
Atom atom = it.next();
String currentAtom = tableAsNames.get(atom) + ".";
List<DBColumn> columnsInfo = tableColumns.get(atom);
int position = 0;
for (Term term : atom.getTerms()) {
String thisTerm = currentAtom + columnsInfo.get(position).getName();
if (term.isConstant()) {
constants.add(thisTerm + " = '" + term.getIdentifier().toString() + "'");
} else {
if (lastOccurrence.containsKey(term.getIdentifier().toString())) {
equivalences.add(lastOccurrence.get(term.getIdentifier().toString()) + " = " + thisTerm);
}
lastOccurrence.put(term.getIdentifier().toString(), thisTerm);
if (cquery.getAnswerVariables().contains(term))
columns.put(term, thisTerm + " as " + term.getIdentifier().toString());
}
++position;
}
}
for (String equivalence : equivalences) {
if (where.length() != 0)
where.append(" AND ");
where.append(equivalence);
}
for (String constant : constants) {
if (where.length() != 0)
where.append(" AND ");
where.append(constant);
}
// Create FROM clause
for (Map.Entry<Atom, String> entries : tableAsNames.entrySet()) {
if (tables.length() != 0)
tables.append(", ");
tables.append(tableNames.get(entries.getKey()));
tables.append(" as ");
tables.append(entries.getValue());
}
// Create SELECT clause
for (Term t : cquery.getAnswerVariables()) {
if (fields.length() != 0)
fields.append(", ");
if (t.isConstant()) {
fields.append("'");
fields.append(t.getIdentifier());
fields.append("'");
} else {
fields.append(columns.get(t));
}
}
StringBuilder query = new StringBuilder("SELECT DISTINCT ");
if (fields.length() > 0)
query.append(fields);
else
query.append("1");
query.append(" FROM ");
if (tables.length() > 0)
query.append(tables);
if (where.length() > 0)
query.append(" WHERE ").append(where);
if (LOGGER.isDebugEnabled())
LOGGER.debug("Generated SQL query :" + cquery + " --> " + query.toString());
return new SQLQuery(query.toString());
} catch (IteratorException e) {
throw new AtomSetException(e);
}
}
use of fr.lirmm.graphik.util.stream.IteratorException in project graal by graphik-team.
the class Neo4jStore 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 errors occurs while iterating terms", e);
}
it.close();
return set;
}
use of fr.lirmm.graphik.util.stream.IteratorException in project graal by graphik-team.
the class StoreTest method bugConcurrentModificationException.
@Theory
public void bugConcurrentModificationException(InMemoryAtomSet store) throws IteratorException, RuleApplicationException, AtomSetException {
Assume.assumeTrue(store instanceof Store);
Rule r = DlgpParser.parseRule("<T>(Z,W), <P>(Y,W) :- <P>(X,Y).");
store.addAll(DlgpParser.parseAtomSet("<P>(a,a)."));
try {
DefaultRuleApplier<InMemoryAtomSet> applier = new DefaultRuleApplier<InMemoryAtomSet>();
applier.apply(r, store);
applier.apply(r, store);
} catch (Exception e) {
Assert.fail();
}
}
use of fr.lirmm.graphik.util.stream.IteratorException in project graal by graphik-team.
the class BackwardChainingTest method issue35.
@Theory
public void issue35(RulesCompilation compilation, RewritingOperator operator) throws IteratorException {
try {
RuleSet rules = new LinkedListRuleSet();
Predicate p = new Predicate("p", 2);
Predicate q = new Predicate("q", 3);
Predicate r = new Predicate("r", 2);
rules.add(DlgpParser.parseRule("q(X,Y,X) :- p(X,Y)."));
ConjunctiveQuery query = DlgpParser.parseQuery("? :- q(X,Y,Y), r(X,Y).");
compilation.compile(rules.iterator());
PureRewriter bc = new PureRewriter(operator, true);
CloseableIterator<? extends ConjunctiveQuery> it = bc.execute(query, rules, compilation);
int i = 0;
while (it.hasNext()) {
ConjunctiveQuery next = it.next();
InMemoryAtomSet atomSet = next.getAtomSet();
Set<Predicate> predicates = atomSet.getPredicates();
Assert.assertTrue(predicates.contains(r));
if (predicates.contains(p)) {
Assert.assertEquals(1, atomSet.getTerms().size());
} else if (predicates.contains(q)) {
Assert.assertEquals(query, next);
} else {
Assert.assertFalse(true);
}
++i;
}
Assert.assertEquals(2, i);
} catch (Exception e) {
Assert.assertFalse("There is an error.", true);
}
}
Aggregations