/** * When a relation is deleted this cleans up any solitary casting and resources. */ void cleanUp() { Stream<Thing> rolePlayers = rolePlayers(); boolean performDeletion = rolePlayers.noneMatch(thing -> thing != null && thing.id() != null); if(performDeletion) delete(); }
@Override public String innerToString(){ StringBuilder description = new StringBuilder(); description.append("ID [").append(id()).append("] Type [").append(type().label()).append("] Roles and Role Players: \n"); for (Map.Entry<Role, Set<Thing>> entry : allRolePlayers().entrySet()) { if(entry.getValue().isEmpty()){ description.append(" Role [").append(entry.getKey().label()).append("] not played by any instance \n"); } else { StringBuilder instancesString = new StringBuilder(); for (Thing thing : entry.getValue()) { instancesString.append(thing.id()).append(","); } description.append(" Role [").append(entry.getKey().label()).append("] played by ["). append(instancesString.toString()).append("] \n"); } } return description.toString(); }
/** * Thrown when an thing does not have a type */ public static GraknTxOperationException noType(Thing thing){ return create(NO_TYPE.getMessage(thing.id())); }
/** * Add the roleplayers of a {@link Relationship} to the relationship var * @param var var representing the relationship * @param relationship {@link Relationship} that contains roleplayer data * @return var pattern with roleplayers */ private static VarPattern roleplayers(VarPattern var, Relationship relationship){ for(Map.Entry<Role, Set<Thing>> entry: relationship.rolePlayersMap().entrySet()){ Role role = entry.getKey(); for (Thing thing : entry.getValue()) { var = var.rel(Graql.label(role.label()), thing.id().getValue()); } } return var; }
private String summaryFormat(Object object) { if (object instanceof SchemaConcept) { return ((SchemaConcept) object).label().getValue().replaceAll("-", "_"); } else if (object instanceof Thing) { Thing thing = (Thing) object; return summaryFormat(thing.type()) + thing.id().getValue(); } else if (object instanceof Label) { return valueToString(((Label) object).getValue()); } else { return object.toString(); } }
/** * * @param thing The thing to be validated * @return An error message if the thing does not have all the required resources */ static Optional<String> validateInstancePlaysAllRequiredRoles(Thing thing) { TypeImpl<?, ?> currentConcept = (TypeImpl) thing.type(); while(currentConcept != null){ Map<Role, Boolean> plays = currentConcept.directPlays(); for (Map.Entry<Role, Boolean> playsEntry : plays.entrySet()) { if(playsEntry.getValue()){ Role role = playsEntry.getKey(); // Assert there is a relationship for this type Stream<Relationship> relationships = thing.relationships(role); if(!CommonUtil.containsOnly(relationships, 1)){ Label resourceTypeLabel = Schema.ImplicitType.explicitLabel(role.label()); return Optional.of(VALIDATION_NOT_EXACTLY_ONE_KEY.getMessage(thing.id(), resourceTypeLabel)); } } } currentConcept = (TypeImpl) currentConcept.sup(); } return Optional.empty(); }
public Pattern pattern(Relationship concept) { VarPattern relationPattern = Graql.var(); Set<Pattern> idPatterns = new HashSet<>(); for (Map.Entry<Role, Set<Thing>> entry : concept.rolePlayersMap().entrySet()) { for (Thing var : entry.getValue()) { Var rolePlayer = Graql.var(); relationPattern = relationPattern.rel(Graql.label(entry.getKey().label()), rolePlayer); idPatterns.add(rolePlayer.asUserDefined().id(var.id())); } } relationPattern = relationPattern.isa(Graql.label(concept.type().label())); Pattern pattern = relationPattern; for (Pattern idPattern : idPatterns) { pattern = pattern.and(idPattern); } return pattern; } }
rolePlayerList.add(colorType(role) + ": id " + idToString(thing.id()));
/** * Given an thing, return a var with the type. * @param thing thing to map * @return var patterns representing given thing */ private static VarPattern base(Thing thing){ VarPattern var = var(thing.id().getValue()).isa(Graql.label(thing.type().label())); return hasResources(var, thing); }
public Pattern pattern(Attribute concept) { Var owner = Graql.var().asUserDefined(); VarPattern resourceVar = Graql.var().asUserDefined().val(concept.value()); return owner .has(concept.type().label(),resourceVar) .id(concept.owner().id()); } }
for(int i = 0 ; i < n ;i++) { for (int j = 0; j < m; j++) { instanceIds[i][j] = putEntityWithResource(tx, "a" + i + "," + j, entity1, key).id(); inserts++; if (inserts % 100 == 0) System.out.println("inst inserts: " + inserts);
aInstancesIds[0] = putEntityWithResource(graph, "a0", graph.getEntityType("start"), key).id(); aInstancesIds[m] = putEntityWithResource(graph, "a" + m, graph.getEntityType("end"), key).id(); for(int i = 1 ; i < m ;i++) { aInstancesIds[i] = putEntityWithResource(graph, "a" + i, aEntity, key).id(); bInstancesIds[i][j] = putEntityWithResource(graph, "b" + i + j, bEntity, key).id();
RelationshipType q = graph.getRelationshipType("Q"); ConceptId cId = putEntityWithResource(graph, "c", entity, key).id(); ConceptId dId = putEntityWithResource(graph, "d", entity, key).id(); ConceptId eId = putEntityWithResource(graph, "e", entity, key).id(); aInstancesIds[n+1] = putEntityWithResource(graph, "a" + (n+1), aEntity, key).id(); for(int i = 0 ; i <= n ;i++) { aInstancesIds[i] = putEntityWithResource(graph, "a" + i, aEntity, key).id(); bInstancesIds[i] = putEntityWithResource(graph, "b" + i, bEntity, key).id();
private void buildExtensionalDB(GraknTx graph, int n) { Role qfrom = graph.getRole("Q-from"); Role qto = graph.getRole("Q-to"); EntityType aEntity = graph.getEntityType("a-entity"); RelationshipType q = graph.getRelationshipType("Q"); Thing aInst = putEntityWithResource(graph, "a", graph.getEntityType("entity2"), key); ConceptId[] aInstanceIds = new ConceptId[n]; for(int i = 0 ; i < n ;i++) { aInstanceIds[i] = putEntityWithResource(graph, "a" + i, aEntity, key).id(); } q.create() .assign(qfrom, aInst) .assign(qto, graph.getConcept(aInstanceIds[0])); for(int i = 0 ; i < n - 1 ; i++) { q.create() .assign(qfrom, graph.getConcept(aInstanceIds[i])) .assign(qto, graph.getConcept(aInstanceIds[i+1])); } } }
for(int i = 1 ; i <= n ;i++) { for (int j = 1; j <= m; j++) { aInstancesIds[i][j] = putEntityWithResource(graph, "a" + i + "," + j, aEntity, key).id();
for(int i = 0 ; i < n ;i++) { for (int j = 0; j < m; j++) { aInstanceIds[i][j] = putEntityWithResource(graph, "a" + i + "," + j, aEntity, key).id();
/** * If the edge does not exist then it adds a {@link Schema.EdgeLabel#ROLE_PLAYER} edge from * this {@link Relationship} to a target {@link Thing} which is playing some {@link Role}. * * If the edge does exist nothing is done. * * @param role The {@link Role} being played by the {@link Thing} in this {@link Relationship} * @param toThing The {@link Thing} playing a {@link Role} in this {@link Relationship} */ public void putRolePlayerEdge(Role role, Thing toThing) { //Checking if the edge exists GraphTraversal<Vertex, Edge> traversal = vertex().tx().getTinkerTraversal().V(). has(Schema.VertexProperty.ID.name(), this.id().getValue()). outE(Schema.EdgeLabel.ROLE_PLAYER.getLabel()). has(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID.name(), this.type().labelId().getValue()). has(Schema.EdgeProperty.ROLE_LABEL_ID.name(), role.labelId().getValue()). as("edge"). inV(). has(Schema.VertexProperty.ID.name(), toThing.id()). select("edge"); if(traversal.hasNext()){ return; } //Role player edge does not exist create a new one EdgeElement edge = this.addEdge(ConceptVertex.from(toThing), Schema.EdgeLabel.ROLE_PLAYER); edge.property(Schema.EdgeProperty.RELATIONSHIP_TYPE_LABEL_ID, this.type().labelId().getValue()); edge.property(Schema.EdgeProperty.ROLE_LABEL_ID, role.labelId().getValue()); Casting casting = Casting.create(edge, owner, role, toThing); vertex().tx().txCache().trackForValidation(casting); }