use of ai.grakn.graql.Var in project grakn by graknlabs.
the class ReasoningTests method reasoningWithReifiedRelations.
// tests if partial substitutions are propagated correctly - atom disjointness may lead to variable loss (bug #15476)
// Expected result: 2 relations obtained by correctly finding reified relations
@Test
public void reasoningWithReifiedRelations() {
QueryBuilder qb = testSet26.tx().graql().infer(true);
String queryString = "match (role1: $x1, role2: $x2) isa relation2; get;";
List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
assertEquals(answers.size(), 2);
String queryString2 = "match " + "$b isa entity2;" + "$b has res1 'value';" + "$rel1 has res2 'value1';" + "$rel1 (role1: $p, role2: $b) isa relation1;" + "$rel2 has res2 'value2';" + "$rel2 (role1: $c, role2: $b) isa relation1; get;";
List<Answer> answers2 = qb.<GetQuery>parse(queryString2).execute();
assertEquals(answers2.size(), 2);
Set<Var> vars = Sets.newHashSet(var("b"), var("p"), var("c"), var("rel1"), var("rel2"));
answers2.forEach(ans -> assertTrue(ans.vars().containsAll(vars)));
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class TinkerQueryRunner method deleteResult.
private void deleteResult(Answer result, Collection<? extends Var> vars) {
Collection<? extends Var> toDelete = vars.isEmpty() ? result.vars() : vars;
for (Var var : toDelete) {
Concept concept = result.get(var);
if (concept.isSchemaConcept()) {
throw GraqlQueryException.deleteSchemaConcept(concept.asSchemaConcept());
}
concept.delete();
}
}
use of ai.grakn.graql.Var 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.Var in project grakn by graknlabs.
the class InsertQueryTest method whenExecutingAnInsertQuery_ResultContainsAllInsertedVars.
@Test
public void whenExecutingAnInsertQuery_ResultContainsAllInsertedVars() {
Var x = var("x");
Var type = var("type");
// Note that two variables refer to the same type. They should both be in the result
InsertQuery query = qb.insert(x.isa(type), type.label("movie"));
Answer result = Iterables.getOnlyElement(query);
assertThat(result.vars(), containsInAnyOrder(x, type));
assertEquals(result.get(type), result.get(x).asEntity().type());
assertEquals(result.get(type).asType().getLabel(), Label.of("movie"));
}
use of ai.grakn.graql.Var in project grakn by graknlabs.
the class MatchTest method testValueQuery.
@Test
public void testValueQuery() {
Var tgf = var("tgf");
Match query = qb.match(tgf.val("Godfather"));
assertThat(query, variable(tgf, contains(both(hasValue("Godfather")).and(hasType(title)))));
}
Aggregations