use of ai.grakn.concept.Label in project grakn by graknlabs.
the class RelationshipAtom method rewriteWithVariableRoles.
/**
* if any {@link Role} variable of the parent is user defined rewrite ALL {@link Role} variables to user defined (otherwise unification is problematic)
* @param parentAtom parent atom that triggers rewrite
* @return new relation atom with user defined {@link Role} variables if necessary or this
*/
private RelationshipAtom rewriteWithVariableRoles(Atom parentAtom) {
if (!parentAtom.requiresRoleExpansion())
return this;
VarPattern relVar = getPattern().admin().getProperty(IsaProperty.class).map(prop -> getVarName().isa(prop.type())).orElse(getVarName());
for (RelationPlayer rp : getRelationPlayers()) {
VarPatternAdmin rolePattern = rp.getRole().orElse(null);
if (rolePattern != null) {
Var roleVar = rolePattern.var();
Label roleLabel = rolePattern.getTypeLabel().orElse(null);
relVar = relVar.rel(roleVar.asUserDefined().label(roleLabel), rp.getRolePlayer());
} else {
relVar = relVar.rel(rp.getRolePlayer());
}
}
return create(relVar.admin(), getPredicateVariable(), getTypeId(), getParentQuery());
}
use of ai.grakn.concept.Label 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.Label in project grakn by graknlabs.
the class ConceptBuilder method tryPutConcept.
private Concept tryPutConcept() {
usedParams.clear();
Concept concept;
if (has(IS_ROLE)) {
use(IS_ROLE);
Label label = use(LABEL);
Role role = executor.tx().putRole(label);
if (has(SUPER_CONCEPT)) {
setSuper(role, use(SUPER_CONCEPT));
}
concept = role;
} else if (has(IS_RULE)) {
use(IS_RULE);
Label label = use(LABEL);
Pattern when = use(WHEN);
Pattern then = use(THEN);
Rule rule = executor.tx().putRule(label, when, then);
if (has(SUPER_CONCEPT)) {
setSuper(rule, use(SUPER_CONCEPT));
}
concept = rule;
} else if (has(SUPER_CONCEPT)) {
concept = putSchemaConcept();
} else if (has(TYPE)) {
concept = putInstance();
} else {
throw GraqlQueryException.insertUndefinedVariable(executor.printableRepresentation(var));
}
// Check for any unexpected parameters
preProvidedParams.forEach((param, value) -> {
if (!usedParams.contains(param)) {
throw GraqlQueryException.insertUnexpectedProperty(param.name(), value, concept);
}
});
return concept;
}
use of ai.grakn.concept.Label in project grakn by graknlabs.
the class SchemaConceptTest method whenChangingSchemaConceptLabel_EnsureLabelIsChangedAndOldLabelIsDead.
@Test
public void whenChangingSchemaConceptLabel_EnsureLabelIsChangedAndOldLabelIsDead() {
Label originalLabel = Label.of("my original label");
Label newLabel = Label.of("my new label");
EntityType entityType = tx.putEntityType(originalLabel.getValue());
// Original label works
assertEquals(entityType, tx.getType(originalLabel));
// Change The Label
entityType.setLabel(newLabel);
// Check the label is changes
assertEquals(newLabel, entityType.getLabel());
assertEquals(entityType, tx.getType(newLabel));
// Check old label is dead
assertNull(tx.getType(originalLabel));
}
use of ai.grakn.concept.Label in project grakn by graknlabs.
the class SchemaConceptTest method whenSpecifyingTheResourceTypeOfAnEntityType_EnsureTheImplicitStructureIsCreated.
@Test
public void whenSpecifyingTheResourceTypeOfAnEntityType_EnsureTheImplicitStructureIsCreated() {
Label resourceLabel = Label.of("Attribute Type");
EntityType entityType = tx.putEntityType("Entity1");
AttributeType attributeType = tx.putAttributeType("Attribute Type", AttributeType.DataType.STRING);
// Implicit Names
Label hasResourceOwnerLabel = Schema.ImplicitType.HAS_OWNER.getLabel(resourceLabel);
Label hasResourceValueLabel = Schema.ImplicitType.HAS_VALUE.getLabel(resourceLabel);
Label hasResourceLabel = Schema.ImplicitType.HAS.getLabel(resourceLabel);
entityType.attribute(attributeType);
RelationshipType relationshipType = tx.getRelationshipType(hasResourceLabel.getValue());
Assert.assertEquals(hasResourceLabel, relationshipType.getLabel());
Set<Label> roleLabels = relationshipType.relates().map(SchemaConcept::getLabel).collect(toSet());
assertThat(roleLabels, containsInAnyOrder(hasResourceOwnerLabel, hasResourceValueLabel));
assertThat(entityType.plays().collect(toSet()), containsInAnyOrder(tx.getRole(hasResourceOwnerLabel.getValue())));
assertThat(attributeType.plays().collect(toSet()), containsInAnyOrder(tx.getRole(hasResourceValueLabel.getValue())));
// Check everything is implicit
assertTrue(relationshipType.isImplicit());
relationshipType.relates().forEach(role -> assertTrue(role.isImplicit()));
// Check that resource is not required
EdgeElement entityPlays = ((EntityTypeImpl) entityType).vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.PLAYS).iterator().next();
assertFalse(entityPlays.propertyBoolean(Schema.EdgeProperty.REQUIRED));
EdgeElement resourcePlays = ((AttributeTypeImpl<?>) attributeType).vertex().getEdgesOfType(Direction.OUT, Schema.EdgeLabel.PLAYS).iterator().next();
assertFalse(resourcePlays.propertyBoolean(Schema.EdgeProperty.REQUIRED));
}
Aggregations