Search in sources :

Example 16 with Concept

use of com.vaticle.typedb.core.concept.Concept in project typedb by vaticle.

the class SoundnessVerifier method verifyExplanation.

private void verifyExplanation(Explanation explanation) {
    ConceptMap recordedWhen = mapInferredConcepts(explanation.conditionAnswer());
    Optional<ConceptMap> recordedThen = materialiser.conditionMaterialisations(explanation.rule(), recordedWhen).map(materialisation -> materialisation.boundConclusion().pattern().bounds());
    if (recordedThen.isPresent()) {
        // Update the inferred variables mapping between the two reasoners
        assert recordedThen.get().concepts().keySet().equals(explanation.conclusionAnswer().concepts().keySet());
        recordedThen.get().concepts().forEach((var, recordedConcept) -> {
            Concept inferredConcept = explanation.conclusionAnswer().concepts().get(var);
            if (inferredConceptMapping.containsKey(inferredConcept)) {
                // Check that the mapping stored is one-to-one
                assert inferredConceptMapping.get(inferredConcept).equals(recordedConcept);
            } else {
                inferredConceptMapping.put(inferredConcept, recordedConcept);
            }
        });
    } else {
        throw new SoundnessException(String.format("Soundness testing found an answer within an explanation that " + "should not be present for rule \"%s\"" + ".\nAnswer:\n%s\nIncorrectly derived from " + "condition:\n%s", explanation.rule().getLabel(), explanation.conclusionAnswer(), explanation.conditionAnswer()));
    }
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) SoundnessException(com.vaticle.typedb.core.test.behaviour.reasoner.verification.CorrectnessVerifier.SoundnessException)

Example 17 with Concept

use of com.vaticle.typedb.core.concept.Concept in project typedb by vaticle.

the class TypeQLSteps method applyQueryTemplate.

private String applyQueryTemplate(String template, ConceptMap templateFiller) {
    // find shortest matching strings between <>
    Pattern pattern = Pattern.compile("<.+?>");
    Matcher matcher = pattern.matcher(template);
    StringBuilder builder = new StringBuilder();
    int i = 0;
    while (matcher.find()) {
        String matched = matcher.group(0);
        String requiredVariable = variableFromTemplatePlaceholder(matched.substring(1, matched.length() - 1));
        builder.append(template, i, matcher.start());
        if (templateFiller.contains(Reference.name(requiredVariable))) {
            Concept concept = templateFiller.get(requiredVariable);
            if (!concept.isThing())
                throw new ScenarioDefinitionException("Cannot apply IID templating to Type concepts");
            String conceptId = concept.asThing().getIID().toHexString();
            builder.append(conceptId);
        } else {
            throw new ScenarioDefinitionException(String.format("No IID available for template placeholder: %s.", matched));
        }
        i = matcher.end();
    }
    builder.append(template.substring(i));
    return builder.toString();
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ScenarioDefinitionException(com.vaticle.typedb.core.test.behaviour.exception.ScenarioDefinitionException)

Example 18 with Concept

use of com.vaticle.typedb.core.concept.Concept in project typedb by vaticle.

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)

Example 19 with Concept

use of com.vaticle.typedb.core.concept.Concept in project typedb by vaticle.

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 20 with Concept

use of com.vaticle.typedb.core.concept.Concept in project typedb by vaticle.

the class Resolver method compatibleBounds.

private Optional<ConceptMap> compatibleBounds(Conjunction conjunction, ConceptMap bounds) {
    Map<Retrievable, Concept> newBounds = new HashMap<>();
    for (Map.Entry<Retrievable, ? extends Concept> entry : bounds.concepts().entrySet()) {
        Retrievable id = entry.getKey();
        Concept bound = entry.getValue();
        Variable conjVariable = conjunction.variable(id);
        assert conjVariable != null;
        if (conjVariable.isThing()) {
            if (!conjVariable.asThing().iid().isPresent())
                newBounds.put(id, bound);
            else if (!conjVariable.asThing().iid().get().iid().equals(bound.asThing().getIID())) {
                return Optional.empty();
            }
        } else if (conjVariable.isType()) {
            if (!conjVariable.asType().label().isPresent())
                newBounds.put(id, bound);
            else if (!conjVariable.asType().label().get().properLabel().equals(bound.asType().getLabel())) {
                return Optional.empty();
            }
        } else {
            throw TypeDBException.of(ILLEGAL_STATE);
        }
    }
    return Optional.of(new ConceptMap(newBounds));
}
Also used : Concept(com.vaticle.typedb.core.concept.Concept) Variable(com.vaticle.typedb.core.pattern.variable.Variable) Retrievable(com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable) HashMap(java.util.HashMap) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap) HashMap(java.util.HashMap) Map(java.util.Map) ConceptMap(com.vaticle.typedb.core.concept.answer.ConceptMap)

Aggregations

Concept (com.vaticle.typedb.core.concept.Concept)64 ConceptMap (com.vaticle.typedb.core.concept.answer.ConceptMap)62 HashMap (java.util.HashMap)58 Test (org.junit.Test)46 Set (java.util.Set)40 Rule (com.vaticle.typedb.core.logic.Rule)34 Util.createRule (com.vaticle.typedb.core.logic.resolvable.Util.createRule)34 Variable (com.vaticle.typedb.core.traversal.common.Identifier.Variable)22 HashSet (java.util.HashSet)22 Map (java.util.Map)22 Relation (com.vaticle.typedb.core.concept.thing.Relation)16 Thing (com.vaticle.typedb.core.concept.thing.Thing)16 Retrievable (com.vaticle.typedb.core.traversal.common.Identifier.Variable.Retrievable)10 Collections.set (com.vaticle.typedb.common.collection.Collections.set)8 TypeDBException (com.vaticle.typedb.core.common.exception.TypeDBException)8 FunctionalIterator (com.vaticle.typedb.core.common.iterator.FunctionalIterator)8 Label (com.vaticle.typedb.core.common.parameters.Label)8 ThingType (com.vaticle.typedb.core.concept.type.ThingType)8 Type (com.vaticle.typedb.core.concept.type.Type)8 Lists (com.google.common.collect.Lists)6