use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RelationshipAtom method inferRoles.
/**
* attempt to infer role types of this relation and return a fresh relationship with inferred role types
* @return either this if nothing/no roles can be inferred or fresh relation with inferred role types
*/
private RelationshipAtom inferRoles(Answer sub) {
// return if all roles known and non-meta
List<Role> explicitRoles = getExplicitRoles().collect(Collectors.toList());
Map<Var, Type> varTypeMap = getParentQuery().getVarTypeMap(sub);
boolean allRolesMeta = explicitRoles.stream().allMatch(role -> Schema.MetaSchema.isMetaLabel(role.getLabel()));
boolean roleRecomputationViable = allRolesMeta && (!sub.isEmpty() || !Sets.intersection(varTypeMap.keySet(), getRolePlayers()).isEmpty());
if (explicitRoles.size() == getRelationPlayers().size() && !roleRecomputationViable)
return this;
GraknTx graph = getParentQuery().tx();
Role metaRole = graph.admin().getMetaRole();
List<RelationPlayer> allocatedRelationPlayers = new ArrayList<>();
RelationshipType relType = getSchemaConcept() != null ? getSchemaConcept().asRelationshipType() : null;
// explicit role types from castings
List<RelationPlayer> inferredRelationPlayers = new ArrayList<>();
getRelationPlayers().forEach(rp -> {
Var varName = rp.getRolePlayer().var();
VarPatternAdmin rolePattern = rp.getRole().orElse(null);
if (rolePattern != null) {
Label roleLabel = rolePattern.getTypeLabel().orElse(null);
// allocate if variable role or if label non meta
if (roleLabel == null || !Schema.MetaSchema.isMetaLabel(roleLabel)) {
inferredRelationPlayers.add(RelationPlayer.of(rolePattern, varName.admin()));
allocatedRelationPlayers.add(rp);
}
}
});
// remaining roles
// role types can repeat so no matter what has been allocated still the full spectrum of possibilities is present
// TODO make restrictions based on cardinality constraints
Set<Role> possibleRoles = relType != null ? relType.relates().collect(toSet()) : inferPossibleTypes(sub).stream().filter(Concept::isRelationshipType).map(Concept::asRelationshipType).flatMap(RelationshipType::relates).collect(toSet());
// possible role types for each casting based on its type
Map<RelationPlayer, Set<Role>> mappings = new HashMap<>();
getRelationPlayers().stream().filter(rp -> !allocatedRelationPlayers.contains(rp)).forEach(rp -> {
Var varName = rp.getRolePlayer().var();
Type type = varTypeMap.get(varName);
mappings.put(rp, top(type != null ? compatibleRoles(type, possibleRoles) : possibleRoles));
});
// allocate all unambiguous mappings
mappings.entrySet().stream().filter(entry -> entry.getValue().size() == 1).forEach(entry -> {
RelationPlayer rp = entry.getKey();
Var varName = rp.getRolePlayer().var();
Role role = Iterables.getOnlyElement(entry.getValue());
VarPatternAdmin rolePattern = Graql.var().label(role.getLabel()).admin();
inferredRelationPlayers.add(RelationPlayer.of(rolePattern, varName.admin()));
allocatedRelationPlayers.add(rp);
});
// fill in unallocated roles with metarole
getRelationPlayers().stream().filter(rp -> !allocatedRelationPlayers.contains(rp)).forEach(rp -> {
Var varName = rp.getRolePlayer().var();
VarPatternAdmin rolePattern = rp.getRole().orElse(null);
rolePattern = rolePattern != null ? rolePattern.var().label(metaRole.getLabel()).admin() : Graql.var().label(metaRole.getLabel()).admin();
inferredRelationPlayers.add(RelationPlayer.of(rolePattern, varName.admin()));
});
VarPattern relationPattern = relationPattern(getVarName(), inferredRelationPlayers);
VarPatternAdmin newPattern = (isDirect() ? relationPattern.directIsa(getPredicateVariable()) : relationPattern.isa(getPredicateVariable())).admin();
return create(newPattern, getPredicateVariable(), getTypeId(), getParentQuery());
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class AttributeTest method whenGettingTheRelationsOfResources_EnsureIncomingResourceEdgesAreTakingIntoAccount.
@Test
public void whenGettingTheRelationsOfResources_EnsureIncomingResourceEdgesAreTakingIntoAccount() {
AttributeType<String> attributeType = tx.putAttributeType("Attribute Type Thingy", AttributeType.DataType.STRING);
Attribute<String> attribute = attributeType.putAttribute("Thingy");
EntityType entityType = tx.putEntityType("Entity Type Thingy").key(attributeType);
Entity e1 = entityType.addEntity();
Entity e2 = entityType.addEntity();
assertThat(attribute.relationships().collect(toSet()), empty());
e1.attribute(attribute);
e2.attribute(attribute);
Relationship rel1 = Iterables.getOnlyElement(e1.relationships().collect(toSet()));
Relationship rel2 = Iterables.getOnlyElement(e2.relationships().collect(toSet()));
assertThat(attribute.relationships().collect(toSet()), containsInAnyOrder(rel1, rel2));
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class GraknTxJanusTest method whenLookingUpRelationEdgeViaConceptId_EnsureTheRelationEdgeIsReturned.
@Test
public void whenLookingUpRelationEdgeViaConceptId_EnsureTheRelationEdgeIsReturned() {
AttributeType<String> attributeType = graknTx.putAttributeType("Looky a attribute type", AttributeType.DataType.STRING);
Attribute<String> attribute = attributeType.putAttribute("A Attribute Thing");
EntityType entityType = graknTx.putEntityType("My entity").attribute(attributeType);
Relationship relationship = Iterators.getOnlyElement(entityType.addEntity().attribute(attribute).relationships().iterator());
// Closing so the cache is not accessed when doing the lookup
graknTx.commit();
graknTx = janusGraphFactory.open(GraknTxType.WRITE);
assertEquals(relationship, graknTx.getConcept(relationship.getId()));
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RemoteConceptsTest method whenCallingAddRelationship_ExecuteAConceptMethod.
@Test
public void whenCallingAddRelationship_ExecuteAConceptMethod() {
Relationship relationship = RemoteConcepts.createRelationship(tx, A);
mockConceptMethod(ConceptMethods.ADD_RELATIONSHIP, relationship);
assertEquals(relationship, relationshipType.addRelationship());
}
use of ai.grakn.concept.Relationship in project grakn by graknlabs.
the class RemoteConceptsTest method whenCallingAddAttributeOnThing_ExecuteAConceptMethod.
@Test
public void whenCallingAddAttributeOnThing_ExecuteAConceptMethod() {
Attribute<Long> attribute = RemoteConcepts.createAttribute(tx, A);
Relationship relationship = RemoteConcepts.createRelationship(tx, C);
mockConceptMethod(ConceptMethods.setAttribute(attribute), relationship);
assertEquals(thing, thing.attribute(attribute));
verifyConceptMethodCalled(ConceptMethods.setAttribute(attribute));
}
Aggregations