use of ai.grakn.concept.Role in project grakn by graknlabs.
the class ConceptMethods method fromGrpc.
public static ConceptMethod<?> fromGrpc(GrpcConceptConverter converter, GrpcConcept.ConceptMethod conceptMethod) {
Role[] roles;
switch(conceptMethod.getConceptMethodCase()) {
case GETVALUE:
return GET_VALUE;
case GETDATATYPEOFTYPE:
return GET_DATA_TYPE_OF_TYPE;
case GETDATATYPEOFATTRIBUTE:
return GET_DATA_TYPE_OF_ATTRIBUTE;
case GETLABEL:
return GET_LABEL;
case SETLABEL:
return setLabel(convert(conceptMethod.getSetLabel()));
case ISIMPLICIT:
return IS_IMPLICIT;
case ISINFERRED:
return IS_INFERRED;
case ISABSTRACT:
return IS_ABSTRACT;
case GETWHEN:
return GET_WHEN;
case GETTHEN:
return GET_THEN;
case GETREGEX:
return GET_REGEX;
case GETROLEPLAYERS:
return GET_ROLE_PLAYERS;
case GETATTRIBUTETYPES:
return GET_ATTRIBUTE_TYPES;
case SETATTRIBUTETYPE:
return setAttributeType(converter.convert(conceptMethod.getSetAttributeType()).asAttributeType());
case UNSETATTRIBUTETYPE:
return unsetAttributeType(converter.convert(conceptMethod.getUnsetAttributeType()).asAttributeType());
case GETKEYTYPES:
return GET_KEY_TYPES;
case GETDIRECTTYPE:
return GET_DIRECT_TYPE;
case GETDIRECTSUPERCONCEPT:
return GET_DIRECT_SUPER;
case SETDIRECTSUPERCONCEPT:
GrpcConcept.Concept setDirectSuperConcept = conceptMethod.getSetDirectSuperConcept();
SchemaConcept schemaConcept = converter.convert(setDirectSuperConcept).asSchemaConcept();
return setDirectSuperConcept(schemaConcept);
case UNSETROLEPLAYER:
return removeRolePlayer(converter.convert(conceptMethod.getUnsetRolePlayer()));
case DELETE:
return DELETE;
case GETATTRIBUTE:
return getAttribute(convert(conceptMethod.getGetAttribute()));
case GETOWNERS:
return GET_OWNERS;
case GETTYPESTHATPLAYROLE:
return GET_TYPES_THAT_PLAY_ROLE;
case GETROLESPLAYEDBYTYPE:
return GET_ROLES_PLAYED_BY_TYPE;
case GETINSTANCES:
return GET_INSTANCES;
case GETRELATEDROLES:
return GET_RELATED_ROLES;
case GETATTRIBUTES:
return GET_ATTRIBUTES;
case GETSUPERCONCEPTS:
return GET_SUPER_CONCEPTS;
case GETRELATIONSHIPTYPESTHATRELATEROLE:
return GET_RELATIONSHIP_TYPES_THAT_RELATE_ROLE;
case GETATTRIBUTESBYTYPES:
GrpcConcept.Concepts getAttributeTypes = conceptMethod.getGetAttributesByTypes();
AttributeType<?>[] attributeTypes = convert(converter, getAttributeTypes).toArray(AttributeType[]::new);
return getAttributesByTypes(attributeTypes);
case GETRELATIONSHIPS:
return GET_RELATIONSHIPS;
case GETSUBCONCEPTS:
return GET_SUB_CONCEPTS;
case GETRELATIONSHIPSBYROLES:
roles = convert(converter, conceptMethod.getGetRelationshipsByRoles()).toArray(Role[]::new);
return getRelationshipsByRoles(roles);
case GETROLESPLAYEDBYTHING:
return GET_ROLES_PLAYED_BY_THING;
case GETKEYS:
return GET_KEYS;
case GETKEYSBYTYPES:
GrpcConcept.Concepts getKeyTypes = conceptMethod.getGetAttributesByTypes();
AttributeType<?>[] keyTypes = convert(converter, getKeyTypes).toArray(AttributeType[]::new);
return getKeysByTypes(keyTypes);
case GETROLEPLAYERSBYROLES:
roles = convert(converter, conceptMethod.getGetRolePlayersByRoles()).toArray(Role[]::new);
return getRolePlayersByRoles(roles);
case SETKEYTYPE:
return setKeyType(converter.convert(conceptMethod.getSetKeyType()).asAttributeType());
case UNSETKEYTYPE:
return unsetKeyType(converter.convert(conceptMethod.getUnsetKeyType()).asAttributeType());
case SETABSTRACT:
return setAbstract(conceptMethod.getSetAbstract());
case SETROLEPLAYEDBYTYPE:
return setRolePlayedByType(converter.convert(conceptMethod.getSetRolePlayedByType()).asRole());
case UNSETROLEPLAYEDBYTYPE:
return unsetRolePlayedByType(converter.convert(conceptMethod.getUnsetRolePlayedByType()).asRole());
case ADDENTITY:
return ADD_ENTITY;
case SETRELATEDROLE:
return setRelatedRole(converter.convert(conceptMethod.getSetRelatedRole()).asRole());
case UNSETRELATEDROLE:
return unsetRelatedRole(converter.convert(conceptMethod.getUnsetRelatedRole()).asRole());
case PUTATTRIBUTE:
return putAttribute(convert(conceptMethod.getPutAttribute()));
case SETREGEX:
return setRegex(convert(conceptMethod.getSetRegex()));
case SETATTRIBUTE:
return setAttribute(converter.convert(conceptMethod.getSetAttribute()).asAttribute());
case UNSETATTRIBUTE:
return unsetAttribute(converter.convert(conceptMethod.getUnsetAttribute()).asAttribute());
case ADDRELATIONSHIP:
return ADD_RELATIONSHIP;
case SETROLEPLAYER:
return setRolePlayer(converter.convert(conceptMethod.getSetRolePlayer()));
default:
case CONCEPTMETHOD_NOT_SET:
throw new IllegalArgumentException("Unrecognised " + conceptMethod);
}
}
use of ai.grakn.concept.Role 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.Role in project grakn by graknlabs.
the class InsertQueryTest method whenInsertingMultipleRolePlayers_BothRolePlayersAreAdded.
@Test
public void whenInsertingMultipleRolePlayers_BothRolePlayersAreAdded() {
List<Answer> results = qb.match(var("g").has("title", "Godfather"), var("m").has("title", "The Muppets")).insert(var("c").isa("cluster").has("name", "2"), var("r").rel("cluster-of-production", "c").rel("production-with-cluster", "g").rel("production-with-cluster", "m").isa("has-cluster")).execute();
Thing cluster = results.get(0).get("c").asThing();
Thing godfather = results.get(0).get("g").asThing();
Thing muppets = results.get(0).get("m").asThing();
Relationship relationship = results.get(0).get("r").asRelationship();
Role clusterOfProduction = movieKB.tx().getRole("cluster-of-production");
Role productionWithCluster = movieKB.tx().getRole("production-with-cluster");
assertEquals(relationship.rolePlayers().collect(toSet()), ImmutableSet.of(cluster, godfather, muppets));
assertEquals(relationship.rolePlayers(clusterOfProduction).collect(toSet()), ImmutableSet.of(cluster));
assertEquals(relationship.rolePlayers(productionWithCluster).collect(toSet()), ImmutableSet.of(godfather, muppets));
}
use of ai.grakn.concept.Role 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.Role 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