Search in sources :

Example 1 with Partial

use of com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial in project grakn by graknlabs.

the class AnswerStateTest method test_root_partially_mapped_to_downstream_and_back.

@Test
public void test_root_partially_mapped_to_downstream_and_back() {
    Map<Identifier.Variable.Retrievable, Identifier.Variable.Retrievable> mapping = new HashMap<>();
    mapping.put(Identifier.Variable.name("a"), Identifier.Variable.name("x"));
    mapping.put(Identifier.Variable.name("b"), Identifier.Variable.name("y"));
    Map<Identifier.Variable.Retrievable, Concept> concepts = new HashMap<>();
    concepts.put(Identifier.Variable.name("a"), new MockConcept(0));
    Set<Identifier.Variable.Retrievable> filter = set(Identifier.Variable.name("a"), Identifier.Variable.name("b"));
    Concludable.Match<?> mapped = InitialImpl.create(filter, new ConceptMap(), null, false).toDownstream().with(new ConceptMap(concepts)).toDownstream(Mapping.of(mapping), null);
    Map<Identifier.Variable.Retrievable, Concept> expectedMapped = new HashMap<>();
    expectedMapped.put(Identifier.Variable.name("x"), new MockConcept(0));
    assertEquals(new ConceptMap(expectedMapped), mapped.conceptMap());
    Map<Identifier.Variable.Retrievable, Concept> downstreamConcepts = new HashMap<>();
    downstreamConcepts.put(Identifier.Variable.name("x"), new MockConcept(0));
    downstreamConcepts.put(Identifier.Variable.name("y"), new MockConcept(1));
    Partial.Compound<?, ?> partial = mapped.toUpstreamLookup(new ConceptMap(downstreamConcepts), false);
    Map<Identifier.Variable.Retrievable, Concept> expectedWithInitial = new HashMap<>();
    expectedWithInitial.put(Identifier.Variable.name("a"), new MockConcept(0));
    expectedWithInitial.put(Identifier.Variable.name("b"), new MockConcept(1));
    assertEquals(new ConceptMap(expectedWithInitial), partial.conceptMap());
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) HashMap(java.util.HashMap) Concludable(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Concludable) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Partial(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 2 with Partial

use of com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial in project grakn by graknlabs.

the class ConcludableResolver method nextAnswer.

private void nextAnswer(Request fromUpstream, CachingRequestState<?, ConceptMap> requestState, int iteration) {
    Optional<Partial.Compound<?, ?>> upstreamAnswer = requestState.nextAnswer().map(Partial::asCompound);
    if (upstreamAnswer.isPresent()) {
        answerFound(upstreamAnswer.get(), fromUpstream, iteration);
    } else {
        Exploration exploration;
        if (requestState.isExploration() && !requestState.answerCache().isComplete()) {
            if ((exploration = requestState.asExploration()).downstreamManager().hasDownstream()) {
                requestFromDownstream(exploration.downstreamManager().nextDownstream(), fromUpstream, iteration);
            } else {
                // TODO: The cache should not be set as complete during recursion
                requestState.answerCache().setComplete();
                failToUpstream(fromUpstream, iteration);
            }
        } else {
            failToUpstream(fromUpstream, iteration);
        }
    }
}
Also used : Partial(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial) Exploration(com.vaticle.typedb.core.reasoner.resolution.framework.RequestState.Exploration)

Example 3 with Partial

use of com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial in project grakn by graknlabs.

the class AnswerStateTest method test_root_with_unmapped_elements.

@Test
public void test_root_with_unmapped_elements() {
    Map<Identifier.Variable.Retrievable, Identifier.Variable.Retrievable> mapping = new HashMap<>();
    mapping.put(Identifier.Variable.name("a"), Identifier.Variable.name("x"));
    mapping.put(Identifier.Variable.name("b"), Identifier.Variable.name("y"));
    Map<Identifier.Variable.Retrievable, Concept> concepts = new HashMap<>();
    concepts.put(Identifier.Variable.name("a"), new MockConcept(0));
    concepts.put(Identifier.Variable.name("c"), new MockConcept(2));
    Set<Identifier.Variable.Retrievable> filter = set(Identifier.Variable.name("a"), Identifier.Variable.name("b"));
    Concludable.Match<?> mapped = InitialImpl.create(filter, new ConceptMap(), null, false).toDownstream().with(new ConceptMap(concepts)).toDownstream(Mapping.of(mapping), null);
    Map<Identifier.Variable.Retrievable, Concept> expectedMapped = new HashMap<>();
    expectedMapped.put(Identifier.Variable.name("x"), new MockConcept(0));
    assertEquals(new ConceptMap(expectedMapped), mapped.conceptMap());
    Map<Identifier.Variable.Retrievable, Concept> downstreamConcepts = new HashMap<>();
    downstreamConcepts.put(Identifier.Variable.name("x"), new MockConcept(0));
    downstreamConcepts.put(Identifier.Variable.name("y"), new MockConcept(1));
    Partial.Compound<?, ?> partial = mapped.toUpstreamLookup(new ConceptMap(downstreamConcepts), false);
    Map<Identifier.Variable.Retrievable, Concept> expectedWithInitial = new HashMap<>();
    expectedWithInitial.put(Identifier.Variable.name("a"), new MockConcept(0));
    expectedWithInitial.put(Identifier.Variable.name("b"), new MockConcept(1));
    expectedWithInitial.put(Identifier.Variable.name("c"), new MockConcept(2));
    assertEquals(new ConceptMap(expectedWithInitial), partial.conceptMap());
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) HashMap(java.util.HashMap) Concludable(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Concludable) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Partial(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Example 4 with Partial

use of com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial in project grakn by graknlabs.

the class AnswerStateTest method test_root_empty_mapped_to_downstream_and_back.

@Test
public void test_root_empty_mapped_to_downstream_and_back() {
    Map<Identifier.Variable.Retrievable, Identifier.Variable.Retrievable> mapping = new HashMap<>();
    mapping.put(Identifier.Variable.name("a"), Identifier.Variable.name("x"));
    mapping.put(Identifier.Variable.name("b"), Identifier.Variable.name("y"));
    Set<Identifier.Variable.Retrievable> filter = set(Identifier.Variable.name("a"), Identifier.Variable.name("b"));
    Concludable.Match<?> mapped = InitialImpl.create(filter, new ConceptMap(), null, false).toDownstream().toDownstream(Mapping.of(mapping), null);
    assertTrue(mapped.conceptMap().concepts().isEmpty());
    Map<Identifier.Variable.Retrievable, Concept> concepts = new HashMap<>();
    concepts.put(Identifier.Variable.name("x"), new MockConcept(0));
    concepts.put(Identifier.Variable.name("y"), new MockConcept(1));
    Partial.Compound<?, ?> partial = mapped.toUpstreamLookup(new ConceptMap(concepts), false);
    Map<Identifier.Variable.Retrievable, Concept> expected = new HashMap<>();
    expected.put(Identifier.Variable.name("a"), new MockConcept(0));
    expected.put(Identifier.Variable.name("b"), new MockConcept(1));
    assertEquals(new ConceptMap(expected), partial.conceptMap());
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) HashMap(java.util.HashMap) Concludable(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Concludable) Identifier(com.vaticle.typedb.core.traversal.common.Identifier) Partial(com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) Test(org.junit.Test)

Aggregations

Partial (com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial)4 Concept (com.vaticle.typedb.core.concept.Concept)3 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)3 Concludable (com.vaticle.typedb.core.reasoner.resolution.answer.AnswerState.Partial.Concludable)3 Identifier (com.vaticle.typedb.core.traversal.common.Identifier)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 Exploration (com.vaticle.typedb.core.reasoner.resolution.framework.RequestState.Exploration)1