use of ai.grakn.graql.admin.RelationPlayer in project grakn by graknlabs.
the class RelationshipAtom method getExplicitRoles.
private Stream<Role> getExplicitRoles() {
ReasonerQueryImpl parent = (ReasonerQueryImpl) getParentQuery();
GraknTx graph = parent.tx();
return getRelationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream).map(graph::<Role>getSchemaConcept);
}
use of ai.grakn.graql.admin.RelationPlayer in project grakn by graknlabs.
the class RelationshipProperty method checkValidProperty.
@Override
public void checkValidProperty(GraknTx graph, VarPatternAdmin var) throws GraqlQueryException {
Set<Label> roleTypes = relationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream).collect(toSet());
Optional<Label> maybeLabel = var.getProperty(IsaProperty.class).map(IsaProperty::type).flatMap(VarPatternAdmin::getTypeLabel);
maybeLabel.ifPresent(label -> {
SchemaConcept schemaConcept = graph.getSchemaConcept(label);
if (schemaConcept == null || !schemaConcept.isRelationshipType()) {
throw GraqlQueryException.notARelationType(label);
}
});
// Check all role types exist
roleTypes.forEach(roleId -> {
SchemaConcept schemaConcept = graph.getSchemaConcept(roleId);
if (schemaConcept == null || !schemaConcept.isRole()) {
throw GraqlQueryException.notARoleType(roleId);
}
});
}
use of ai.grakn.graql.admin.RelationPlayer in project grakn by graknlabs.
the class RelationshipAtom method rewriteWithRelationVariable.
@Override
public RelationshipAtom rewriteWithRelationVariable() {
VarPattern newVar = Graql.var().asUserDefined();
VarPattern relVar = getPattern().admin().getProperty(IsaProperty.class).map(prop -> newVar.isa(prop.type())).orElse(newVar);
for (RelationPlayer c : getRelationPlayers()) {
VarPatternAdmin roleType = c.getRole().orElse(null);
if (roleType != null) {
relVar = relVar.rel(roleType, c.getRolePlayer());
} else {
relVar = relVar.rel(c.getRolePlayer());
}
}
return create(relVar.admin(), getPredicateVariable(), getTypeId(), getParentQuery());
}
use of ai.grakn.graql.admin.RelationPlayer 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.admin.RelationPlayer in project grakn by graknlabs.
the class RelationshipAtom method getRoleRelationPlayerMap.
private Multimap<Role, RelationPlayer> getRoleRelationPlayerMap() {
Multimap<Role, RelationPlayer> roleRelationPlayerMap = ArrayListMultimap.create();
Multimap<Role, Var> roleVarMap = getRoleVarMap();
List<RelationPlayer> relationPlayers = getRelationPlayers();
roleVarMap.asMap().forEach((role, value) -> {
Label roleLabel = role.getLabel();
relationPlayers.stream().filter(rp -> rp.getRole().isPresent()).forEach(rp -> {
VarPatternAdmin roleTypeVar = rp.getRole().orElse(null);
Label rl = roleTypeVar != null ? roleTypeVar.getTypeLabel().orElse(null) : null;
if (roleLabel != null && roleLabel.equals(rl)) {
roleRelationPlayerMap.put(role, rp);
}
});
});
return roleRelationPlayerMap;
}
Aggregations