use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class RelationshipAtom method getRelationPlayerMappings.
/**
* @param parentAtom reference atom defining the mapping
* @param matchType type of match to be performed
* @return set of possible COMPLETE mappings between this (child) and parent relation players
*/
private Set<List<Pair<RelationPlayer, RelationPlayer>>> getRelationPlayerMappings(RelationshipAtom parentAtom, UnifierComparison matchType) {
Multimap<Role, RelationPlayer> childRoleRPMap = this.getRoleRelationPlayerMap();
Map<Var, Type> childVarTypeMap = this.getParentQuery().getVarTypeMap();
Map<Var, Type> parentVarTypeMap = parentAtom.getParentQuery().getVarTypeMap();
// establish compatible castings for each parent casting
List<Set<Pair<RelationPlayer, RelationPlayer>>> compatibleMappingsPerParentRP = new ArrayList<>();
ReasonerQueryImpl childQuery = (ReasonerQueryImpl) getParentQuery();
Set<Role> childRoles = childRoleRPMap.keySet();
parentAtom.getRelationPlayers().stream().filter(prp -> prp.getRole().isPresent()).forEach(prp -> {
VarPatternAdmin parentRolePattern = prp.getRole().orElse(null);
if (parentRolePattern == null) {
throw GraqlQueryException.rolePatternAbsent(this);
}
Label parentRoleLabel = parentRolePattern.getTypeLabel().orElse(null);
if (parentRoleLabel != null) {
Var parentRolePlayer = prp.getRolePlayer().var();
Type parentType = parentVarTypeMap.get(parentRolePlayer);
Set<Role> compatibleRoles = compatibleRoles(tx().getSchemaConcept(parentRoleLabel), parentType, childRoles);
List<RelationPlayer> compatibleRelationPlayers = new ArrayList<>();
compatibleRoles.stream().filter(childRoleRPMap::containsKey).forEach(role -> childRoleRPMap.get(role).stream().filter(crp -> {
Var childVar = crp.getRolePlayer().var();
Type childType = childVarTypeMap.get(childVar);
return matchType.typePlayability(childQuery, childVar, parentType) && matchType.typeCompatibility(parentType, childType);
}).filter(crp -> {
IdPredicate parentId = parentAtom.getIdPredicate(prp.getRolePlayer().var());
IdPredicate childId = this.getIdPredicate(crp.getRolePlayer().var());
return matchType.atomicCompatibility(parentId, childId);
}).filter(crp -> {
ValuePredicate parentVP = parentAtom.getPredicate(prp.getRolePlayer().var(), ValuePredicate.class);
ValuePredicate childVP = this.getPredicate(crp.getRolePlayer().var(), ValuePredicate.class);
return matchType.atomicCompatibility(parentVP, childVP);
}).forEach(compatibleRelationPlayers::add));
if (!compatibleRelationPlayers.isEmpty()) {
compatibleMappingsPerParentRP.add(compatibleRelationPlayers.stream().map(crp -> new Pair<>(crp, prp)).collect(Collectors.toSet()));
}
} else {
compatibleMappingsPerParentRP.add(getRelationPlayers().stream().map(crp -> new Pair<>(crp, prp)).collect(Collectors.toSet()));
}
});
return Sets.cartesianProduct(compatibleMappingsPerParentRP).stream().filter(list -> !list.isEmpty()).filter(list -> {
List<RelationPlayer> listChildRps = list.stream().map(Pair::getKey).collect(Collectors.toList());
// NB: this preserves cardinality instead of removing all occuring instances which is what we want
return ReasonerUtils.subtract(listChildRps, this.getRelationPlayers()).isEmpty();
}).filter(list -> {
List<RelationPlayer> listParentRps = list.stream().map(Pair::getValue).collect(Collectors.toList());
return listParentRps.containsAll(parentAtom.getRelationPlayers());
}).collect(toSet());
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ResolutionPlanTest method prioritiseSubbedRelationsOverNonSubbedOnes.
@Test
public void prioritiseSubbedRelationsOverNonSubbedOnes() {
EmbeddedGraknTx<?> testTx = testContext.tx();
String queryString = "{" + "(role1:$x, role2: $y) isa relation;" + "(role1:$y, role2: $z) isa anotherRelation;" + "(role1:$z, role2: $w) isa yetAnotherRelation;" + "$w id 'sampleId';" + "}";
ReasonerQueryImpl query = ReasonerQueries.create(conjunction(queryString, testTx), testTx);
ImmutableList<Atom> correctPlan = ImmutableList.of(getAtom(query, "yetAnotherRelation", testTx), getAtom(query, "anotherRelation", testTx), getAtom(query, "relation", testTx));
ImmutableList<Atom> plan = new ResolutionPlan(query).plan();
assertEquals(plan, correctPlan);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ResolutionPlanTest method prioritiseSpecificResourcesOverNonSpecific.
@Test
public void prioritiseSpecificResourcesOverNonSpecific() {
EmbeddedGraknTx<?> testTx = testContext.tx();
String queryString = "{" + "(role1:$x, role2: $y) isa relation;" + "(role1:$y, role2: $z) isa anotherRelation;" + "(role1:$z, role2: $w) isa yetAnotherRelation;" + "$x has anotherResource $r;" + "$w has resource 'test';" + "}";
ReasonerQueryImpl query = ReasonerQueries.create(conjunction(queryString, testTx), testTx);
ImmutableList<Atom> correctPlan = ImmutableList.of(getAtom(query, "resource", testTx), getAtom(query, "yetAnotherRelation", testTx), getAtom(query, "anotherRelation", testTx), getAtom(query, "relation", testTx), getAtom(query, "anotherResource", testTx));
ImmutableList<Atom> plan = new ResolutionPlan(query).plan();
assertEquals(plan, correctPlan);
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ResolutionPlanTest method makeSureOptimalOrderPickedWhenResourcesWithSubstitutionsArePresent.
@Test
public void makeSureOptimalOrderPickedWhenResourcesWithSubstitutionsArePresent() {
EmbeddedGraknTx<?> testTx = testContext.tx();
Concept concept = testTx.graql().match(var("x").isa("baseEntity")).get("x").findAny().orElse(null);
String basePatternString = "(role1:$x, role2: $y) isa relation;" + "$x has resource 'this';" + "$y has anotherResource 'that';";
String xPatternString = "{" + "$x id '" + concept.getId() + "';" + basePatternString + "}";
String yPatternString = "{" + "$y id '" + concept.getId() + "';" + basePatternString + "}";
ReasonerQueryImpl queryX = ReasonerQueries.create(conjunction(xPatternString, testTx), testTx);
ReasonerQueryImpl queryY = ReasonerQueries.create(conjunction(yPatternString, testTx), testTx);
assertNotEquals(new ResolutionPlan(queryX).plan().get(0), getAtom(queryX, "anotherResource", testTx));
assertNotEquals(new ResolutionPlan(queryY).plan().get(0), getAtom(queryX, "resource", testTx));
}
use of ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl in project grakn by graknlabs.
the class ResolutionPlanTest method prioritiseMostSubbedRelations.
@Test
public void prioritiseMostSubbedRelations() {
EmbeddedGraknTx<?> testTx = testContext.tx();
String queryString = "{" + "(role1:$x, role2: $y) isa relation;" + "(role1:$y, role2: $z) isa anotherRelation;" + "(role1:$z, role2: $w) isa yetAnotherRelation;" + "$z id 'sampleId';" + "$w id 'sampleId2';" + "}";
ReasonerQueryImpl query = ReasonerQueries.create(conjunction(queryString, testTx), testTx);
ImmutableList<Atom> correctPlan = ImmutableList.of(getAtom(query, "yetAnotherRelation", testTx), getAtom(query, "anotherRelation", testTx), getAtom(query, "relation", testTx));
ImmutableList<Atom> plan = new ResolutionPlan(query).plan();
assertEquals(plan, correctPlan);
}
Aggregations