use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class TypeQLSteps method uniquely_identify_answer_concepts.
@Then("uniquely identify answer concepts")
public void uniquely_identify_answer_concepts(List<Map<String, String>> answerConcepts) {
assertEquals(String.format("The number of identifier entries (rows) should match the number of answers, but found %d identifier entries and %d answers.", answerConcepts.size(), answers.size()), answerConcepts.size(), answers.size());
for (ConceptMap answer : answers) {
List<Map<String, String>> matchingIdentifiers = new ArrayList<>();
for (Map<String, String> answerIdentifier : answerConcepts) {
if (matchAnswerConcept(answerIdentifier, answer)) {
matchingIdentifiers.add(answerIdentifier);
}
}
assertEquals(String.format("An identifier entry (row) should match 1-to-1 to an answer, but there were %d matching identifier entries for answer with variables %s.", matchingIdentifiers.size(), answer.concepts().keySet().toString()), 1, matchingIdentifiers.size());
}
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class QueryService method update.
private void update(String queryStr, Options.Query options, UUID reqID) {
TypeQLUpdate query = TypeQL.parseQuery(queryStr).asUpdate();
Context.Query context = new Context.Query(transactionSvc.context(), options.query(query), query);
FunctionalIterator<ConceptMap> answers = queryMgr.update(query, context);
transactionSvc.stream(answers, reqID, context.options(), a -> updateResPart(reqID, a));
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class QueryService method match.
private void match(String queryStr, Options.Query options, UUID reqID) {
TypeQLMatch query = TypeQL.parseQuery(queryStr).asMatch();
Context.Query context = new Context.Query(transactionSvc.context(), options.query(query), query);
FunctionalIterator<ConceptMap> answers = queryMgr.match(query, context);
transactionSvc.stream(answers, reqID, context.options(), a -> matchResPart(reqID, a));
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
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.answer.ConceptMap in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_role_unifies_rule_relation_exact.
@Test
public void relation_role_unifies_rule_relation_exact() {
Unifier unifier = uniqueUnifier("{ ($role: $y) isa employment; }", rule(" (employee: $x) isa employment ", "{ $x isa person; }"));
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$y", set("$x"));
put("$role", set("$_employment:employee"));
put("$_0", set("$_0"));
}
};
assertEquals(expected, result);
// test requirement
assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.anon(0)));
assertEquals(2, unifier.requirements().isaExplicit().size());
assertEquals(0, unifier.requirements().predicates().size());
// test filter allows a valid answer
Relation employment = instanceOf("employment").asRelation();
Thing person = instanceOf("person");
addRolePlayer(employment, "employee", person);
Map<Variable, Concept> concepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.label("employment"), employment.getType()), pair(Variable.label("employment:employee"), employment.getType().getRelates("employee")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
ConceptMap unifiedAnswer = unified.first().get();
assertEquals(3, unified.next().concepts().size());
assertEquals(employment.getType().getRelates("employee"), unifiedAnswer.get("role"));
assertEquals(person, unifiedAnswer.get("y"));
assertEquals(employment, unifiedAnswer.get(Variable.anon(0)));
// filter out invalid types
Relation friendship = instanceOf("friendship").asRelation();
person = instanceOf("person");
addRolePlayer(friendship, "friend", person);
concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("employment"), friendship.getType()), pair(Variable.label("employment:employee"), friendship.getType().getRelates("friend")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
Aggregations