use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class RelationshipAtom method inferRoles.
/**
* attempt to infer role types of this relation and return a fresh relationship with inferred role types
* @return either this if nothing/no roles can be inferred or fresh relation with inferred role types
*/
private RelationshipAtom inferRoles(Answer sub) {
// return if all roles known and non-meta
List<Role> explicitRoles = getExplicitRoles().collect(Collectors.toList());
Map<Var, Type> varTypeMap = getParentQuery().getVarTypeMap(sub);
boolean allRolesMeta = explicitRoles.stream().allMatch(role -> Schema.MetaSchema.isMetaLabel(role.getLabel()));
boolean roleRecomputationViable = allRolesMeta && (!sub.isEmpty() || !Sets.intersection(varTypeMap.keySet(), getRolePlayers()).isEmpty());
if (explicitRoles.size() == getRelationPlayers().size() && !roleRecomputationViable)
return this;
GraknTx graph = getParentQuery().tx();
Role metaRole = graph.admin().getMetaRole();
List<RelationPlayer> allocatedRelationPlayers = new ArrayList<>();
RelationshipType relType = getSchemaConcept() != null ? getSchemaConcept().asRelationshipType() : null;
// explicit role types from castings
List<RelationPlayer> inferredRelationPlayers = new ArrayList<>();
getRelationPlayers().forEach(rp -> {
Var varName = rp.getRolePlayer().var();
VarPatternAdmin rolePattern = rp.getRole().orElse(null);
if (rolePattern != null) {
Label roleLabel = rolePattern.getTypeLabel().orElse(null);
// allocate if variable role or if label non meta
if (roleLabel == null || !Schema.MetaSchema.isMetaLabel(roleLabel)) {
inferredRelationPlayers.add(RelationPlayer.of(rolePattern, varName.admin()));
allocatedRelationPlayers.add(rp);
}
}
});
// remaining roles
// role types can repeat so no matter what has been allocated still the full spectrum of possibilities is present
// TODO make restrictions based on cardinality constraints
Set<Role> possibleRoles = relType != null ? relType.relates().collect(toSet()) : inferPossibleTypes(sub).stream().filter(Concept::isRelationshipType).map(Concept::asRelationshipType).flatMap(RelationshipType::relates).collect(toSet());
// possible role types for each casting based on its type
Map<RelationPlayer, Set<Role>> mappings = new HashMap<>();
getRelationPlayers().stream().filter(rp -> !allocatedRelationPlayers.contains(rp)).forEach(rp -> {
Var varName = rp.getRolePlayer().var();
Type type = varTypeMap.get(varName);
mappings.put(rp, top(type != null ? compatibleRoles(type, possibleRoles) : possibleRoles));
});
// allocate all unambiguous mappings
mappings.entrySet().stream().filter(entry -> entry.getValue().size() == 1).forEach(entry -> {
RelationPlayer rp = entry.getKey();
Var varName = rp.getRolePlayer().var();
Role role = Iterables.getOnlyElement(entry.getValue());
VarPatternAdmin rolePattern = Graql.var().label(role.getLabel()).admin();
inferredRelationPlayers.add(RelationPlayer.of(rolePattern, varName.admin()));
allocatedRelationPlayers.add(rp);
});
// fill in unallocated roles with metarole
getRelationPlayers().stream().filter(rp -> !allocatedRelationPlayers.contains(rp)).forEach(rp -> {
Var varName = rp.getRolePlayer().var();
VarPatternAdmin rolePattern = rp.getRole().orElse(null);
rolePattern = rolePattern != null ? rolePattern.var().label(metaRole.getLabel()).admin() : Graql.var().label(metaRole.getLabel()).admin();
inferredRelationPlayers.add(RelationPlayer.of(rolePattern, varName.admin()));
});
VarPattern relationPattern = relationPattern(getVarName(), inferredRelationPlayers);
VarPatternAdmin newPattern = (isDirect() ? relationPattern.directIsa(getPredicateVariable()) : relationPattern.isa(getPredicateVariable())).admin();
return create(newPattern, getPredicateVariable(), getTypeId(), getParentQuery());
}
use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class RelationshipAtom method relationPattern.
/**
* construct a $varName (rolemap) isa $typeVariable relation
* @param varName variable name
* @param relationPlayers list of rolePlayer-roleType mappings
* @return corresponding {@link VarPatternAdmin}
*/
private VarPattern relationPattern(Var varName, List<RelationPlayer> relationPlayers) {
VarPattern var = varName;
for (RelationPlayer rp : relationPlayers) {
VarPatternAdmin rolePattern = rp.getRole().orElse(null);
var = rolePattern != null ? var.rel(rolePattern, rp.getRolePlayer()) : var.rel(rp.getRolePlayer());
}
return var.admin();
}
use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsMetaRoles_ParentRepeatsRPs.
@Test
public void testExactUnification_TernaryRelation_ParentRepeatsMetaRoles_ParentRepeatsRPs() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String parentString = "{(role: $x, role: $x, role2: $y) isa ternary;}";
String childString = "{(role1: $u, role2: $v, role3: $q) isa ternary;}";
String childString2 = "{(role1: $u, role2: $v, role2: $q) isa ternary;}";
String childString3 = "{(role1: $u, role1: $v, role2: $q) isa ternary;}";
String childString4 = "{(role1: $u, role1: $u, role2: $q) isa ternary;}";
Conjunction<VarPatternAdmin> parentPattern = conjunction(parentString, graph);
Conjunction<VarPatternAdmin> childPattern = conjunction(childString, graph);
Conjunction<VarPatternAdmin> childPattern2 = conjunction(childString2, graph);
Conjunction<VarPatternAdmin> childPattern3 = conjunction(childString3, graph);
Conjunction<VarPatternAdmin> childPattern4 = conjunction(childString4, graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(parentPattern, graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(childPattern, graph);
ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(childPattern2, graph);
ReasonerAtomicQuery childQuery3 = ReasonerQueries.atomic(childPattern3, graph);
ReasonerAtomicQuery childQuery4 = ReasonerQueries.atomic(childPattern4, graph);
Unifier unifier = childQuery.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier = new UnifierImpl(ImmutableMultimap.of(var("q"), var("x"), var("u"), var("x"), var("v"), var("y")));
assertTrue(unifier.containsAll(correctUnifier));
MultiUnifier unifier2 = childQuery2.getMultiUnifier(parentQuery);
MultiUnifier correctUnifier2 = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("q"), var("x"), var("v"), var("y")), ImmutableMultimap.of(var("u"), var("x"), var("v"), var("x"), var("q"), var("y")));
assertTrue(unifier2.containsAll(correctUnifier2));
assertEquals(unifier2.size(), 2);
Unifier unifier3 = childQuery3.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier3 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("x"), var("q"), var("y")));
assertTrue(unifier3.containsAll(correctUnifier3));
Unifier unifier4 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier4 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("q"), var("y")));
assertTrue(unifier4.containsAll(correctUnifier4));
}
use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class AtomicQueryTest method testExactUnification_TernaryRelation_ParentRepeatsMetaRoles.
@Test
public void testExactUnification_TernaryRelation_ParentRepeatsMetaRoles() {
EmbeddedGraknTx<?> graph = unificationWithTypesSet.tx();
String parentString = "{(role: $x, role: $y, role2: $z) isa ternary;}";
String childString = "{(role1: $u, role2: $v, role3: $q) isa ternary;}";
String childString2 = "{(role1: $u, role2: $v, role2: $q) isa ternary;}";
String childString3 = "{(role1: $u, role1: $v, role2: $q) isa ternary;}";
String childString4 = "{(role1: $u, role1: $u, role2: $q) isa ternary;}";
Conjunction<VarPatternAdmin> parentPattern = conjunction(parentString, graph);
Conjunction<VarPatternAdmin> childPattern = conjunction(childString, graph);
Conjunction<VarPatternAdmin> childPattern2 = conjunction(childString2, graph);
Conjunction<VarPatternAdmin> childPattern3 = conjunction(childString3, graph);
Conjunction<VarPatternAdmin> childPattern4 = conjunction(childString4, graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(parentPattern, graph);
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(childPattern, graph);
ReasonerAtomicQuery childQuery2 = ReasonerQueries.atomic(childPattern2, graph);
ReasonerAtomicQuery childQuery3 = ReasonerQueries.atomic(childPattern3, graph);
ReasonerAtomicQuery childQuery4 = ReasonerQueries.atomic(childPattern4, graph);
MultiUnifier unifier = childQuery.getMultiUnifier(parentQuery);
MultiUnifier correctUnifier = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("z"), var("q"), var("y")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("z"), var("q"), var("x")));
assertTrue(unifier.containsAll(correctUnifier));
assertEquals(unifier.size(), 2);
MultiUnifier unifier2 = childQuery2.getMultiUnifier(parentQuery);
MultiUnifier correctUnifier2 = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("y"), var("q"), var("z")), ImmutableMultimap.of(var("u"), var("x"), var("v"), var("z"), var("q"), var("y")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("z"), var("q"), var("x")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("x"), var("q"), var("z")));
assertTrue(unifier2.containsAll(correctUnifier2));
assertEquals(unifier2.size(), 4);
MultiUnifier unifier3 = childQuery3.getMultiUnifier(parentQuery);
MultiUnifier correctUnifier3 = new MultiUnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("v"), var("y"), var("q"), var("z")), ImmutableMultimap.of(var("u"), var("y"), var("v"), var("x"), var("q"), var("z")));
assertTrue(unifier3.containsAll(correctUnifier3));
assertEquals(unifier3.size(), 2);
Unifier unifier4 = childQuery4.getMultiUnifier(parentQuery).getUnifier();
Unifier correctUnifier4 = new UnifierImpl(ImmutableMultimap.of(var("u"), var("x"), var("u"), var("y"), var("q"), var("z")));
assertTrue(unifier4.containsAll(correctUnifier4));
}
use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class AtomicQueryTest method testWhenMaterialising_MaterialisedInformationIsPresentInGraph.
@Test
public void testWhenMaterialising_MaterialisedInformationIsPresentInGraph() {
EmbeddedGraknTx<?> graph = geoKB.tx();
QueryBuilder qb = graph.graql().infer(false);
String explicitQuery = "match (geo-entity: $x, entity-location: $y) isa is-located-in;$x has name 'Warsaw';$y has name 'Poland'; get;";
assertTrue(!qb.<GetQuery>parse(explicitQuery).iterator().hasNext());
String patternString = "{(geo-entity: $x, entity-location: $y) isa is-located-in;}";
Conjunction<VarPatternAdmin> pattern = conjunction(patternString, graph);
List<Answer> answers = new ArrayList<>();
answers.add(new QueryAnswer(ImmutableMap.of(var("x"), getConceptByResourceValue(graph, "Warsaw"), var("y"), getConceptByResourceValue(graph, "Poland"))));
ReasonerAtomicQuery atomicQuery = ReasonerQueries.atomic(pattern, graph);
assertNotExists(qb.parse(explicitQuery));
answers.forEach(atomicQuery::materialise);
assertExists(qb.parse(explicitQuery));
}
Aggregations