use of ai.grakn.concept.Label in project grakn by graknlabs.
the class ValidateGlobalRules method validateRelationTypesToRolesSchema.
/**
* @param relationshipType the {@link RelationshipType} to be validated
* @return Error messages if the role type sub structure does not match the {@link RelationshipType} sub structure
*/
static Set<String> validateRelationTypesToRolesSchema(RelationshipType relationshipType) {
RelationshipTypeImpl superRelationType = (RelationshipTypeImpl) relationshipType.sup();
if (Schema.MetaSchema.isMetaLabel(superRelationType.getLabel()) || superRelationType.isAbstract()) {
// If super type is a meta type no validation needed
return Collections.emptySet();
}
Set<String> errorMessages = new HashSet<>();
Collection<Role> superRelates = superRelationType.relates().collect(Collectors.toSet());
Collection<Role> relates = relationshipType.relates().collect(Collectors.toSet());
Set<Label> relatesLabels = relates.stream().map(SchemaConcept::getLabel).collect(Collectors.toSet());
// Check 1) Every role of relationTypes is the sub of a role which is in the relates of it's supers
if (!superRelationType.isAbstract()) {
Set<Label> allSuperRolesPlayed = new HashSet<>();
superRelationType.sups().forEach(rel -> rel.relates().forEach(roleType -> allSuperRolesPlayed.add(roleType.getLabel())));
for (Role relate : relates) {
boolean validRoleTypeFound = SchemaConceptImpl.from(relate).sups().anyMatch(superRole -> allSuperRolesPlayed.contains(superRole.getLabel()));
if (!validRoleTypeFound) {
errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(relate.getLabel(), relationshipType.getLabel(), "super", "super", superRelationType.getLabel()));
}
}
}
// Check 2) Every role of superRelationType has a sub role which is in the relates of relationTypes
for (Role superRelate : superRelates) {
boolean subRoleNotFoundInRelates = superRelate.subs().noneMatch(sub -> relatesLabels.contains(sub.getLabel()));
if (subRoleNotFoundInRelates) {
errorMessages.add(VALIDATION_RELATION_TYPES_ROLES_SCHEMA.getMessage(superRelate.getLabel(), superRelationType.getLabel(), "sub", "sub", relationshipType.getLabel()));
}
}
return errorMessages;
}
use of ai.grakn.concept.Label in project grakn by graknlabs.
the class TxCache method remove.
/**
* @param concept The concept to no longer track
*/
@SuppressWarnings("SuspiciousMethodCalls")
public void remove(Concept concept) {
modifiedThings.remove(concept);
modifiedRoles.remove(concept);
modifiedRelationshipTypes.remove(concept);
modifiedRules.remove(concept);
if (concept.isAttribute()) {
newAttributes.remove(AttributeImpl.from(concept.asAttribute()).getIndex());
}
if (concept.isRelationship()) {
newRelationships.remove(concept.asRelationship());
}
conceptCache.remove(concept.getId());
if (concept.isSchemaConcept()) {
Label label = concept.asSchemaConcept().getLabel();
schemaConceptCache.remove(label);
labelCache.remove(label);
}
}
use of ai.grakn.concept.Label 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.concept.Label in project grakn by graknlabs.
the class MatchTest method testGraqlPlaysSemanticsMatchGraphAPI.
@Test
public void testGraqlPlaysSemanticsMatchGraphAPI() {
GraknTx tx = emptyKB.tx();
QueryBuilder qb = tx.graql();
Label a = Label.of("a");
Label b = Label.of("b");
Label c = Label.of("c");
Label d = Label.of("d");
Label e = Label.of("e");
Label f = Label.of("f");
qb.define(Graql.label(c).sub(Graql.label(b).sub(Graql.label(a).sub("entity"))), Graql.label(f).sub(Graql.label(e).sub(Graql.label(d).sub("role"))), Graql.label(b).plays(Graql.label(e))).execute();
Stream.of(a, b, c, d, e, f).forEach(type -> {
Set<Concept> graqlPlays = qb.match(Graql.label(type).plays(x)).get(x).collect(Collectors.toSet());
Collection<Role> graphAPIPlays;
SchemaConcept schemaConcept = tx.getSchemaConcept(type);
if (schemaConcept.isType()) {
graphAPIPlays = schemaConcept.asType().plays().collect(toSet());
} else {
graphAPIPlays = Collections.EMPTY_SET;
}
assertEquals(graqlPlays, graphAPIPlays);
});
Stream.of(d, e, f).forEach(type -> {
Set<Concept> graqlPlayedBy = qb.match(x.plays(Graql.label(type))).get(x).collect(toSet());
Collection<Type> graphAPIPlayedBy = tx.<Role>getSchemaConcept(type).playedByTypes().collect(toSet());
assertEquals(graqlPlayedBy, graphAPIPlayedBy);
});
}
use of ai.grakn.concept.Label in project grakn by graknlabs.
the class MatchTest method whenQueryingForHas_AllowReferringToTheImplicitRelation.
@Test
public void whenQueryingForHas_AllowReferringToTheImplicitRelation() {
Label title = Label.of("title");
RelationshipType hasTitle = movieKB.tx().getType(HAS.getLabel(title));
Role titleOwner = movieKB.tx().getSchemaConcept(HAS_OWNER.getLabel(title));
Role titleValue = movieKB.tx().getSchemaConcept(HAS_VALUE.getLabel(title));
Relationship implicitRelation = hasTitle.instances().iterator().next();
ConceptId owner = implicitRelation.rolePlayers(titleOwner).iterator().next().getId();
ConceptId value = implicitRelation.rolePlayers(titleValue).iterator().next().getId();
Match query = qb.match(x.id(owner).has(title, y.id(value), r));
assertThat(query, variable(r, contains(MatchableConcept.of(implicitRelation))));
}
Aggregations