Search in sources :

Example 1 with Identifier

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);
}
Also used : Util.resolvedConjunction(com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction) Collections.set(com.vaticle.typedb.common.collection.Collections.set) Variable(com.vaticle.typedb.core.pattern.variable.Variable) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) MB(com.vaticle.typedb.core.common.collection.Bytes.MB) Arguments(com.vaticle.typedb.core.common.parameters.Arguments) HashSet(java.util.HashSet) Root(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Compound.Root) Util.resolvedDisjunction(com.vaticle.typedb.core.reasoner.resolution.Util.resolvedDisjunction) After(org.junit.After) InitialImpl(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerStateImpl.TopImpl.MatchImpl.InitialImpl) Assert.fail(org.junit.Assert.fail) Path(java.nio.file.Path) Before(org.junit.Before) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) TypeQL(com.vaticle.typeql.lang.TypeQL) CoreDatabaseManager(com.vaticle.typedb.core.database.CoreDatabaseManager) Set(java.util.Set) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test) IOException(java.io.IOException) RootResolver(com.vaticle.typedb.core.reasoner.resolution.resolver.RootResolver) Actor(com.vaticle.typedb.core.concurrent.actor.Actor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Disjunction(com.vaticle.typedb.core.pattern.Disjunction) TimeUnit(java.util.concurrent.TimeUnit) CoreSession(com.vaticle.typedb.core.database.CoreSession) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) NamedThreadFactory(com.vaticle.typedb.common.concurrent.NamedThreadFactory) Paths(java.nio.file.Paths) TestCase.assertTrue(junit.framework.TestCase.assertTrue) ActorExecutorGroup(com.vaticle.typedb.core.concurrent.actor.ActorExecutorGroup) Database(com.vaticle.typedb.core.common.parameters.Options.Database) Iterators.iterate(com.vaticle.typedb.core.common.iterator.Iterators.iterate) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) Request(com.vaticle.typedb.core.reasoner.resolution.framework.Request) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Match(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Top.Match) Resolver(com.vaticle.typedb.core.reasoner.resolution.framework.Resolver) Util(com.vaticle.typedb.core.test.integration.util.Util) TestCase.assertEquals(junit.framework.TestCase.assertEquals) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Actor(com.vaticle.typedb.core.concurrent.actor.Actor) Util.resolvedConjunction(com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) HashSet(java.util.HashSet)

Example 2 with Identifier

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;
    }
}
Also used : Identifier(com.vaticle.typedb.core.traversal.common.Identifier)

Example 3 with Identifier

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);
    }
}
Also used : Order(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Order) Vertex(com.vaticle.typedb.core.graph.vertex.Vertex) ThingVertex(com.vaticle.typedb.core.graph.vertex.ThingVertex) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Forwardable(com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Forwardable)

Example 4 with Identifier

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();
        }
    }
}
Also used : LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CoreTransaction(com.vaticle.typedb.core.database.CoreTransaction) Match(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Top.Match) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Actor(com.vaticle.typedb.core.concurrent.actor.Actor) Util.resolvedConjunction(com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction) Conjunction(com.vaticle.typedb.core.pattern.Conjunction) CoreSession(com.vaticle.typedb.core.database.CoreSession) TypeDBException(com.vaticle.typedb.core.common.exception.TypeDBException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with Identifier

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;
}
Also used : Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Set(java.util.Set) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) Explanation(com.vaticle.typedb.core.reasoner.resolution.answer.Explanation) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap)

Aggregations

Identifier (com.vaticle.typedb.core.traversal.common.Identifier)5 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)2 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)2 Actor (com.vaticle.typedb.core.concurrent.actor.Actor)2 CoreSession (com.vaticle.typedb.core.database.CoreSession)2 CoreTransaction (com.vaticle.typedb.core.database.CoreTransaction)2 Conjunction (com.vaticle.typedb.core.pattern.Conjunction)2 Util.resolvedConjunction (com.vaticle.typedb.core.reasoner.resolution.Util.resolvedConjunction)2 Match (com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Top.Match)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Test (org.junit.Test)2 Collections.set (com.vaticle.typedb.common.collection.Collections.set)1 NamedThreadFactory (com.vaticle.typedb.common.concurrent.NamedThreadFactory)1 MB (com.vaticle.typedb.core.common.collection.Bytes.MB)1 Iterators.iterate (com.vaticle.typedb.core.common.iterator.Iterators.iterate)1 Forwardable (com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Forwardable)1 Order (com.vaticle.typedb.core.common.iterator.sorted.SortedIterator.Order)1 Arguments (com.vaticle.typedb.core.common.parameters.Arguments)1