use of com.vaticle.typedb.core.traversal.common.Identifier in project grakn by graknlabs.
the class ResolutionTest method createRootAndAssertResponses.
private void createRootAndAssertResponses(CoreTransaction transaction, Conjunction conjunction, long answerCount, long explainableAnswers) throws InterruptedException {
ResolverRegistry registry = transaction.reasoner().resolverRegistry();
LinkedBlockingQueue<Match.Finished> responses = new LinkedBlockingQueue<>();
AtomicLong doneReceived = new AtomicLong(0L);
Set<Identifier.Variable.Retrievable> filter = new HashSet<>();
iterate(conjunction.variables()).map(Variable::id).filter(Identifier::isName).map(Identifier.Variable::asName).forEachRemaining(filter::add);
Actor.Driver<RootResolver.Conjunction> root;
try {
root = registry.root(conjunction, responses::add, iterDone -> doneReceived.incrementAndGet(), (throwable) -> fail());
} catch (TypeDBException e) {
fail();
return;
}
assertResponses(root, filter, responses, doneReceived, answerCount, explainableAnswers);
}
use of com.vaticle.typedb.core.traversal.common.Identifier in project grakn by graknlabs.
the class GraphIterator method computeFirstBranch.
private boolean computeFirstBranch(int pos) {
ProcedureEdge<?, ?> edge = procedure.edge(pos);
Identifier toID = edge.to().id();
Identifier fromId = edge.from().id();
Forwardable<? extends Vertex<?, ?>, Order.Asc> toIter = branch(answer.get(fromId), edge);
if (toIter.hasNext()) {
iterators.put(toID, toIter);
answer.put(toID, toIter.next());
if (pos == edgeCount)
return true;
while (!computeFirst(pos + 1)) {
if (pos == branchSeekStack.peekLastPos()) {
branchSeekStack.popLastPos();
if (toIter.hasNext())
answer.put(toID, toIter.next());
else {
popScope(pos);
answer.remove(toID);
branchFailure(edge);
return false;
}
} else {
popScope(pos);
answer.remove(toID);
toIter.recycle();
return false;
}
}
return true;
} else {
branchFailure(edge);
return false;
}
}
use of com.vaticle.typedb.core.traversal.common.Identifier in project grakn by graknlabs.
the class GraphIterator method computeNext.
private boolean computeNext(int pos) {
if (pos == 0)
return false;
ProcedureEdge<?, ?> edge = procedure.edge(pos);
Identifier toID = edge.to().id();
if (pos == computeNextSeekPos) {
computeNextSeekPos = edgeCount;
} else if (pos > computeNextSeekPos) {
if (!edge.isClosureEdge())
iterators.get(toID).recycle();
if (!backTrack(pos))
return false;
if (edge.isClosureEdge()) {
Vertex<?, ?> fromVertex = answer.get(edge.from().id());
Vertex<?, ?> toVertex = answer.get(edge.to().id());
if (isClosure(edge, fromVertex, toVertex))
return true;
else
return computeNextClosure(pos);
} else {
Forwardable<? extends Vertex<?, ?>, Order.Asc> toIter = branch(answer.get(edge.from().id()), edge);
iterators.put(toID, toIter);
}
}
if (edge.isClosureEdge()) {
return computeNextClosure(pos);
} else if (iterators.get(toID).hasNext()) {
answer.put(toID, iterators.get(toID).next());
return true;
} else {
return computeNextBranch(pos);
}
}
use of com.vaticle.typedb.core.traversal.common.Identifier in project grakn by graknlabs.
the class ReiterationTest method test_first_iteration_exhausts_and_second_iteration_recurses_infinitely.
@Test
public void test_first_iteration_exhausts_and_second_iteration_recurses_infinitely() throws InterruptedException {
try (CoreSession session = schemaSession()) {
try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
transaction.query().define(TypeQL.parseQuery("define " + "X sub relation, relates item, plays Y:item;" + "Y sub relation, relates item, plays X:item;" + "object sub entity, plays X:item, plays Y:item;" + "rule rule-b: when {" + "$r(item:$x) isa X;" + "} then {" + "(item:$r) isa Y;" + "};" + "rule rule-a: when {" + "$r(item:$x) isa Y;" + "} then {" + "(item:$r) isa X;" + "};" + "rule rule-c: when {" + "$o isa object;" + "} then {" + "(item:$o) isa X;" + "};"));
transaction.commit();
}
}
try (CoreSession session = dataSession()) {
try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
transaction.query().insert(TypeQL.parseQuery("insert $o isa object;"));
transaction.commit();
}
}
try (CoreSession session = dataSession()) {
try (CoreTransaction transaction = singleThreadElgTransaction(session)) {
Conjunction conjunction = resolvedConjunction("{ $y isa Y; }", transaction.logic());
Set<Identifier.Variable.Retrievable> filter = new HashSet<>();
iterate(conjunction.variables()).map(Variable::id).filter(Identifier::isName).map(Identifier.Variable::asName).forEachRemaining(filter::add);
ResolverRegistry registry = transaction.reasoner().resolverRegistry();
LinkedBlockingQueue<Match.Finished> responses = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Integer> failed = new LinkedBlockingQueue<>();
int[] iteration = { 0 };
int[] doneInIteration = { 0 };
boolean[] receivedInferredAnswer = { false };
ResolutionTracer.get().start();
Actor.Driver<RootResolver.Conjunction> root = registry.root(conjunction, answer -> {
if (iterate(answer.conceptMap().concepts().entrySet()).map(e -> e.getValue().asThing().isInferred()).first().isPresent()) {
receivedInferredAnswer[0] = true;
}
responses.add(answer);
}, iterDone -> {
assert iteration[0] == iterDone;
doneInIteration[0]++;
failed.add(iterDone);
}, throwable -> fail());
Set<Match.Finished> answers = new HashSet<>();
// iteration 0
sendRootRequest(root, filter, iteration[0]);
answers.add(responses.take());
ResolutionTracer.get().finish();
ResolutionTracer.get().start();
sendRootRequest(root, filter, iteration[0]);
// Block and wait for an failed message
failed.take();
ResolutionTracer.get().finish();
assertTrue(receivedInferredAnswer[0]);
assertEquals(1, doneInIteration[0]);
// iteration 1 onwards
for (int j = 0; j <= 100; j++) {
ResolutionTracer.get().start();
sendRootRequest(root, filter, iteration[0]);
Match.Finished re = responses.poll(100, MILLISECONDS);
if (re == null) {
Integer ex = failed.poll(100, MILLISECONDS);
if (ex == null) {
ResolutionTracer.get().finish();
fail();
}
// Reset the iteration
iteration[0]++;
receivedInferredAnswer[0] = false;
doneInIteration[0] = 0;
}
ResolutionTracer.get().finish();
}
} catch (TypeDBException e) {
e.printStackTrace();
fail();
}
}
}
use of com.vaticle.typedb.core.traversal.common.Identifier in project grakn by graknlabs.
the class ExplanationTest method assertSingleExplainableExplanations.
private List<Explanation> assertSingleExplainableExplanations(ConceptMap ans, int anonymousConcepts, int explainablesCount, int explanationsCount, CoreTransaction txn) {
List<ConceptMap.Explainable> explainables = ans.explainables().iterator().toList();
assertEquals(anonymousConcepts, iterate(ans.concepts().keySet()).filter(Identifier::isAnonymous).count());
assertEquals(explainablesCount, explainables.size());
ConceptMap.Explainable explainable = explainables.iterator().next();
assertNotEquals(NOT_IDENTIFIED, explainable.id());
List<Explanation> explanations = txn.query().explain(explainable.id()).toList();
assertEquals(explanationsCount, explanations.size());
explanations.forEach(explanation -> {
Map<Retrievable, Set<Retrievable>> mapping = explanation.variableMapping();
ConceptMap projected = applyMapping(mapping, ans);
projected.concepts().forEach((var, concept) -> {
assertTrue(explanation.conclusionAnswer().contains(var));
assertEquals(explanation.conclusionAnswer().get(var), concept);
});
});
return explanations;
}
Aggregations