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()));
}
}
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();
}
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());
}
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());
}
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));
}
Aggregations