use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class ConcludableResolver method createRequestState.
protected CachingRequestState<?, ConceptMap> createRequestState(Request fromUpstream, int iteration) {
LOG.debug("{}: Creating new Responses for iteration{}, request: {}", name(), iteration, fromUpstream);
Driver<? extends Resolver<?>> root = fromUpstream.partialAnswer().root();
cacheRegistersByRoot.putIfAbsent(root, new HashMap<>());
Map<ConceptMap, AnswerCache<?, ConceptMap>> cacheRegister = cacheRegistersByRoot.get(root);
ConceptMap answerFromUpstream = fromUpstream.partialAnswer().conceptMap();
CachingRequestState<?, ConceptMap> requestState;
assert fromUpstream.partialAnswer().isConcludable();
if (fromUpstream.partialAnswer().asConcludable().isExplain()) {
if (cacheRegister.containsKey(answerFromUpstream)) {
if (cacheRegister.get(answerFromUpstream).isConceptMapCache()) {
// We have a cache already which we must evict to use a cache suitable for explaining
ConcludableAnswerCache answerCache = new ConcludableAnswerCache(cacheRegister, answerFromUpstream);
cacheRegister.put(answerFromUpstream, answerCache);
requestState = new Explain(fromUpstream, answerCache, iteration);
registerRules(fromUpstream, requestState.asExploration());
} else if (cacheRegister.get(answerFromUpstream).isConcludableAnswerCache()) {
ConcludableAnswerCache answerCache = cacheRegister.get(answerFromUpstream).asConcludableAnswerCache();
requestState = new FollowingExplain(fromUpstream, answerCache, iteration);
} else {
throw TypeDBException.of(ILLEGAL_STATE);
}
} else {
ConcludableAnswerCache answerCache = new ConcludableAnswerCache(cacheRegister, answerFromUpstream);
cacheRegister.put(answerFromUpstream, answerCache);
requestState = new Explain(fromUpstream, answerCache, iteration);
registerRules(fromUpstream, requestState.asExploration());
}
} else if (fromUpstream.partialAnswer().asConcludable().isMatch()) {
if (cacheRegister.containsKey(answerFromUpstream)) {
if (cacheRegister.get(answerFromUpstream).isConceptMapCache()) {
ConceptMapCache answerCache = cacheRegister.get(answerFromUpstream).asConceptMapCache();
requestState = new FollowingMatch(fromUpstream, answerCache, iteration);
} else if (cacheRegister.get(answerFromUpstream).isConcludableAnswerCache()) {
ConcludableAnswerCache answerCache = cacheRegister.get(answerFromUpstream).asConcludableAnswerCache();
requestState = new FollowingExplain(fromUpstream, answerCache, iteration);
} else {
throw TypeDBException.of(ILLEGAL_STATE);
}
} else {
ConceptMapCache answerCache = new ConceptMapCache(cacheRegister, answerFromUpstream);
cacheRegister.put(answerFromUpstream, answerCache);
if (!answerCache.completeIfSubsumerComplete()) {
answerCache.addSource(traversalIterator(concludable.pattern(), answerFromUpstream));
}
boolean singleAnswerRequired = answerFromUpstream.concepts().keySet().containsAll(unboundVars);
requestState = new Match(fromUpstream, answerCache, iteration, singleAnswerRequired);
registerRules(fromUpstream, requestState.asExploration());
}
} else {
throw TypeDBException.of(ILLEGAL_STATE);
}
return requestState;
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyRelationConcludableTest method unUnify_produces_cartesian_named_types_only_for_unbound_vars.
@Test
public void unUnify_produces_cartesian_named_types_only_for_unbound_vars() {
String conjunction = "{$r ($role: $x) isa $rel;}";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Relation queryConcludable = concludables.iterator().next().asRelation();
Rule rule = createRule("people-are-self-friends", "{ $x isa person; }", " (friend: $x) isa friendship ", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
// test filter allows a valid answer
Relation friendship = instanceOf("friendship").asRelation();
Thing person = instanceOf("person");
addRolePlayer(friendship, "friend", person);
Map<Variable, Concept> concepts = map(pair(Variable.anon(0), friendship), pair(Variable.name("x"), person), pair(Variable.label("friendship"), friendship.getType()), pair(Variable.label("friendship:friend"), friendship.getType().getRelates("friend")));
List<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map(pair(Variable.name("rel"), friendship.getType())))).toList();
assertEquals(2, unified.size());
Set<Map<String, String>> expected = set(new HashMap<String, String>() {
{
put("$rel", "friendship");
put("$role", "friendship:friend");
}
}, new HashMap<String, String>() {
{
put("$rel", "friendship");
put("$role", "relation:role");
}
});
Set<Map<String, String>> actual = new HashSet<>();
iterate(unified).forEachRemaining(answer -> {
actual.add(new HashMap<String, String>() {
{
put("$rel", answer.get("rel").asType().getLabel().name());
put("$role", answer.get("role").asType().getLabel().scopedName());
}
});
});
assertEquals(expected, actual);
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyRelationConcludableTest method verifyUnificationSucceeds.
private void verifyUnificationSucceeds(String parent, Rule rule) {
Unifier unifier = uniqueUnifier(parent, rule);
List<ConceptMap> childAnswers = transaction.query().match(TypeQL.match(rule.getThenPreNormalised())).toList();
List<ConceptMap> parentAnswers = transaction.query().match(TypeQL.match(TypeQL.parsePattern(parent))).toList();
assertFalse(childAnswers.isEmpty());
assertFalse(parentAnswers.isEmpty());
List<ConceptMap> unifiedAnswers = iterate(childAnswers).flatMap(ans -> {
Map<Variable, Concept> labelledTypes = addRequiredLabeledTypes(ans, unifier);
Map<Variable, Concept> requiredRetrievableConcepts = addRequiredRetrievableConcepts(ans, unifier);
labelledTypes.putAll(requiredRetrievableConcepts);
// TODO if want to use with iids add instance requirements
FunctionalIterator<ConceptMap> unified = unifier.unUnify(labelledTypes, new Unifier.Requirements.Instance(map())).map(u -> {
Map<Variable.Retrievable, Concept> concepts = u.concepts().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
requiredRetrievableConcepts.forEach(concepts::remove);
return new ConceptMap(concepts);
});
return unified;
}).toList();
assertFalse(unifiedAnswers.isEmpty());
assertTrue(parentAnswers.containsAll(unifiedAnswers));
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyRelationConcludableTest method relation_duplicate_players_unifies_rule_relation_distinct_players.
@Test
public void relation_duplicate_players_unifies_rule_relation_distinct_players() {
List<Unifier> unifiers = unifiers("{ (employee: $p, employee: $p) isa employment; }", rule("($employee: $x, $employee: $y) isa $employment", "{ $x isa person; $y isa person; $employment type employment;$employee type employment:employee; }")).toList();
Set<Map<String, Set<String>>> result = iterate(unifiers).map(u -> getStringMapping(u.mapping())).toSet();
Set<Map<String, Set<String>>> expected = set(new HashMap<String, Set<String>>() {
{
put("$p", set("$x", "$y"));
put("$_0", set("$_0"));
}
});
assertEquals(expected, result);
Unifier unifier = unifiers.get(0);
// test requirements
assertEquals(typeHierarchy("employment"), unifier.requirements().isaExplicit().get(Variable.anon(0)));
assertEquals(2, unifier.requirements().isaExplicit().size());
assertEquals(2, unifier.requirements().types().size());
assertEquals(roleHierarchy("employee", "employment"), unifier.requirements().types().get(Variable.label("employment:employee")));
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);
addRolePlayer(employment, "employee", person);
Map<Variable, Concept> identifiedConcepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.name("y"), person), pair(Variable.name("employment"), employment.getType()), pair(Variable.name("employee"), employment.getType().getRelates("employee")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(identifiedConcepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
ConceptMap unifiedAnswer = unified.first().get();
assertEquals(2, unifiedAnswer.concepts().size());
assertEquals(person, unifiedAnswer.get("p"));
// filter out answers with differing role players that must be the same
employment = instanceOf("employment").asRelation();
person = instanceOf("person");
Thing differentPerson = instanceOf("person");
addRolePlayer(employment, "employee", person);
addRolePlayer(employment, "employee", differentPerson);
identifiedConcepts = map(pair(Variable.anon(0), employment), pair(Variable.name("x"), person), pair(Variable.name("y"), differentPerson), pair(Variable.name("employment"), employment.getType()), pair(Variable.name("employee"), employment.getType().getRelates("employee")));
unified = unifier.unUnify(identifiedConcepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
use of com.vaticle.typedb.core.concept.answer.ConceptMap in project grakn by graknlabs.
the class UnifyHasConcludableTest method has_attribute_typed_variable_unifies_rule_has_exact.
@Test
public void has_attribute_typed_variable_unifies_rule_has_exact() {
String conjunction = "{ $y has name $b; }";
Set<Concludable> concludables = Concludable.create(resolvedConjunction(conjunction, logicMgr));
Concludable.Has queryConcludable = concludables.iterator().next().asHas();
Rule rule = createRule("has-rule", "{ $x isa person; }", "$x has first-name \"john\"", logicMgr);
List<Unifier> unifiers = queryConcludable.unify(rule.conclusion(), conceptMgr).toList();
assertEquals(1, unifiers.size());
Unifier unifier = unifiers.get(0);
Map<String, Set<String>> result = getStringMapping(unifier.mapping());
Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {
{
put("$y", set("$x"));
put("$b", set("$_0"));
}
};
assertEquals(expected, result);
// test requirements
assertEquals(0, unifier.requirements().types().size());
assertEquals(2, unifier.requirements().isaExplicit().size());
assertEquals(set(Label.of("first-name"), Label.of("last-name")), unifier.requirements().isaExplicit().get(Identifier.Variable.name("b")));
assertEquals(0, unifier.requirements().predicates().size());
// test forward unification can reject an invalid partial answer
ConceptMap partialAnswer = new ConceptMap(map(pair(Identifier.Variable.name("b"), instanceOf("age"))));
assertFalse(unifier.unify(partialAnswer).isPresent());
// test filter allows a valid answer
Map<Identifier.Variable, Concept> concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.anon(0), instanceOf("first-name", "john")));
FunctionalIterator<ConceptMap> unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertTrue(unified.hasNext());
assertEquals(2, unified.next().concepts().size());
// filter out invalid type
concepts = map(pair(Identifier.Variable.name("x"), instanceOf("person")), pair(Identifier.Variable.anon(0), instanceOf("age")));
unified = unifier.unUnify(concepts, new Unifier.Requirements.Instance(map()));
assertFalse(unified.hasNext());
}
Aggregations