use of org.jpl7.Query in project packages-jpl by SWI-Prolog.
the class TestOLD method test10m.
private static void test10m() {
String text = "append(Xs,Ys,[_,_,_,_,_])";
Query q = new Query(text);
Map<String, Term>[] ss = q.allSolutions();
System.err.println("test10m:");
System.err.println(" all solutions of " + text);
for (int i = 0; i < ss.length; i++) {
System.err.println(" " + Util.toString(ss[i]));
}
System.err.println();
}
use of org.jpl7.Query in project packages-jpl by SWI-Prolog.
the class TestJUnit method testOpenGetClose1.
public void testOpenGetClose1() {
StringBuffer sb = new StringBuffer();
Query q = new Query("atom_chars(prolog, Cs), member(C, Cs)");
Map<String, Term> soln;
q.open();
while ((soln = q.getSolution()) != null) {
sb.append(((Atom) soln.get("C")).name());
}
q.close();
assertEquals("prolog", sb.toString());
}
use of org.jpl7.Query in project packages-jpl by SWI-Prolog.
the class AddWithThreads method run.
public void run() {
for (int i = 0; i < REPS; i++) {
// System.out.println("Asserting test('" + i + "')");
Query queryA = new Query("assert(" + namespace + "(test('" + i + "')))");
Thread.yield();
// System.out.println("adding query: " + queryA);
// boolean retA = queryA.hasMoreElements();
queryA.close();
}
latch.countDown();
}
use of org.jpl7.Query in project packages-jpl by SWI-Prolog.
the class FetchBigTree method main.
public static void main(String[] args) {
// Prolog.set_default_init_args(new String[] { "libpl.dll", "-f",
// "D:/pcm/bin/pcm.ini", "-g", "pcm_2000" });
(new Query("consult('jpl/test/test.pl')")).oneSolution();
Term t = (Term) ((new Query("p(18,T)")).oneSolution().get("T"));
int i = 1;
while (t.hasFunctor("a", 2)) {
t = t.arg(2);
i = i + 1;
}
System.err.println("got a tree of " + i + " generations");
}
use of org.jpl7.Query in project syndesis by syndesisio.
the class SqlJsonDB method getAsStreamingOutput.
@Override
@SuppressWarnings({ "PMD.ExcessiveMethodLength", "PMD.NPathComplexity" })
public Consumer<OutputStream> getAsStreamingOutput(String path, GetOptions options) {
GetOptions o;
if (options != null) {
o = options;
} else {
o = new GetOptions();
}
// Lets normalize the path a bit
String baseDBPath = JsonRecordSupport.convertToDBPath(path);
String like = baseDBPath + "%";
GetOptions.Order order = o.order();
if (order == null) {
order = GetOptions.Order.ASC;
}
Consumer<OutputStream> result = null;
final Handle h = dbi.open();
try {
StringBuilder sql = new StringBuilder(250);
// Creating the iterator could fail with a runtime exception,
ArrayList<Consumer<Query<Map<String, Object>>>> binds = new ArrayList<>();
if (o.filter() == null) {
sql.append("select path,value,ovalue from jsondb where path LIKE :like");
} else {
sql.append("SELECT path,value,ovalue FROM jsondb A INNER JOIN (");
SqlExpressionBuilder.create(this, o.filter(), baseDBPath).build(sql, binds);
sql.append(") B ON A.path LIKE B.match_path||'%'");
}
if (o.startAfter() != null) {
String startAfter = validateKey(o.startAfter());
if (o.order() == GetOptions.Order.DESC) {
sql.append(" and path <= :startAfter");
binds.add(query -> {
String bindPath = baseDBPath + startAfter;
query.bind("startAfter", bindPath);
});
} else {
sql.append(" and path >= :startAfter");
binds.add(query -> {
String bindPath = baseDBPath + incrementKey(startAfter);
query.bind("startAfter", bindPath);
});
}
}
if (o.startAt() != null) {
String startAt = validateKey(o.startAt());
if (o.order() == GetOptions.Order.DESC) {
sql.append(" and path < :startAt");
binds.add(query -> {
String bindPath = baseDBPath + incrementKey(startAt);
query.bind("startAt", bindPath);
});
} else {
sql.append(" and path >= :startAt");
binds.add(query -> {
String bindPath = baseDBPath + startAt;
query.bind("startAt", bindPath);
});
}
}
if (o.endAt() != null) {
String endAt = validateKey(o.endAt());
if (o.order() == GetOptions.Order.DESC) {
sql.append(" and path > :endAt");
binds.add(query -> {
String value = baseDBPath + endAt;
query.bind("endAt", value);
});
} else {
sql.append(" and path < :endAt");
binds.add(query -> {
String bindPath = baseDBPath + incrementKey(endAt);
query.bind("endAt", bindPath);
});
}
}
if (o.endBefore() != null) {
String endBefore = validateKey(o.endBefore());
if (o.order() == GetOptions.Order.DESC) {
sql.append(" and path >= :endBefore");
binds.add(query -> {
String value = baseDBPath + incrementKey(endBefore);
query.bind("endBefore", value);
});
} else {
sql.append(" and path < :endBefore");
binds.add(query -> {
String value = baseDBPath + endBefore;
query.bind("endBefore", value);
});
}
}
sql.append(" order by path ").append(order);
Query<Map<String, Object>> query = h.createQuery(sql.toString()).bind("like", like);
for (Consumer<Query<Map<String, Object>>> bind : binds) {
bind.accept(query);
}
ResultIterator<JsonRecord> iterator = query.map(JsonRecordMapper.INSTANCE).iterator();
try {
// At this point we know if we can produce results..
if (iterator.hasNext()) {
result = output -> {
try (JsonRecordConsumer toJson = new JsonRecordConsumer(baseDBPath, output, o)) {
while (!toJson.isClosed() && iterator.hasNext()) {
toJson.accept(iterator.next());
}
} catch (IOException e) {
throw new JsonDBException(e);
} finally {
iterator.close();
h.close();
}
};
}
} finally {
// if we are producing results, then defer closing the iterator
if (result == null) {
iterator.close();
}
}
} finally {
// if we are producing results, then defer closing the handle
if (result == null) {
h.close();
}
}
return result;
}
Aggregations