@Override protected final Thing asInstance(Concept concept) { return concept.asThing(); } }
@Override protected final Thing asInstance(Concept concept) { return concept.asThing(); } }
@Override Iterable<? super MatchableConcept> transform(MatchableConcept item) { return getTypes(item.get().asThing()).stream().map(MatchableConcept::of).collect(toSet()); } };
@Override Iterable<? super MatchableConcept> transform(MatchableConcept item) { return item.get().asThing().attributes() .filter(resource -> MatchableConcept.NAME_TYPES.contains(resource.type().label())) .map(MatchableConcept::of) .collect(toSet()); }
/** * * @param concept The element to be later validated */ public void trackForValidation(Concept concept) { if (concept.isThing()) { modifiedThings.add(concept.asThing()); } else if (concept.isRole()) { modifiedRoles.add(concept.asRole()); } else if (concept.isRelationshipType()) { modifiedRelationshipTypes.add(concept.asRelationshipType()); } else if (concept.isRule()){ modifiedRules.add(concept.asRule()); } } public void trackForValidation(Casting casting) {
public static ConceptMethod<Stream<? extends Concept>> getKeysByTypes(AttributeType<?>... attributeTypes) { return ConceptMethod.builder(ConceptResponseType.CONCEPTS) .requestSetter(builder -> builder.setGetKeysByTypes(convert(Stream.of(attributeTypes)))) .function(concept -> concept.asThing().keys(attributeTypes)) .build(); }
public static ConceptMethod<Stream<? extends Concept>> getRelationshipsByRoles(Role... roles) { return ConceptMethod.builder(ConceptResponseType.CONCEPTS) .requestSetter(builder -> builder.setGetRelationshipsByRoles(convert(Stream.of(roles)))) .function(concept -> concept.asThing().relationships(roles)) .build(); }
public static ConceptMethod<Stream<? extends Concept>> getAttributesByTypes(AttributeType<?>... attributeTypes) { return ConceptMethod.builder(ConceptResponseType.CONCEPTS) .requestSetter(builder -> builder.setGetAttributesByTypes(convert(Stream.of(attributeTypes)))) .function(concept -> concept.asThing().attributes(attributeTypes)) .build(); }
default RolePlayer convert(GrpcConcept.RolePlayer rolePlayer) { return RolePlayer.create(convert(rolePlayer.getRole()).asRole(), convert(rolePlayer.getPlayer()).asThing()); } }
@Override // TODO: Weird. Why is this not a stream, while other collections are returned as stream public final Map<Role, Set<Thing>> rolePlayersMap() { ConceptProto.Method.Req method = ConceptProto.Method.Req.newBuilder() .setRelationRolePlayersMapReq(ConceptProto.Relation.RolePlayersMap.Req.getDefaultInstance()).build(); int iteratorId = runMethod(method).getRelationRolePlayersMapIter().getId(); Iterable<ConceptProto.Relation.RolePlayersMap.Iter.Res> rolePlayers = () -> new Grakn.Transaction.Iterator<>( tx(), iteratorId, res -> res.getConceptMethodIterRes().getRelationRolePlayersMapIterRes() ); Map<Role, Set<Thing>> rolePlayerMap = new HashMap<>(); for (ConceptProto.Relation.RolePlayersMap.Iter.Res rolePlayer : rolePlayers) { Role role = RemoteConcept.of(rolePlayer.getRole(), tx()).asRole(); Thing player = RemoteConcept.of(rolePlayer.getPlayer(), tx()).asThing(); if (rolePlayerMap.containsKey(role)) { rolePlayerMap.get(role).add(player); } else { rolePlayerMap.put(role, new HashSet<>(Collections.singletonList(player))); } } return rolePlayerMap; }
public static ConceptMethod<Void> unsetAttribute(Attribute<?> attribute) { return ConceptMethod.builder(ConceptResponseType.UNIT) .requestSetter(builder -> builder.setUnsetAttribute(convert(attribute))) .functionVoid(concept -> concept.asThing().deleteAttribute(attribute)) .build(); }
public static ConceptMethod<Concept> setAttribute(Attribute<?> attribute) { return ConceptMethod.builder(ConceptResponseType.CONCEPT) .requestSetter(builder -> builder.setSetAttribute(convert(attribute))) .function(concept -> concept.asThing().attributeRelationship(attribute)) .build(); }
@Override public String toString() { if (concept.isAttribute()) { return "hasValue(" + valueToString(concept.asAttribute().value()) + ")"; } else if (concept.isThing()) { Thing thing = concept.asThing(); Stream<Attribute<?>> resources = thing.attributes(); Optional<?> value = resources .filter(resource -> NAME_TYPES.contains(resource.type().label())) .map(Attribute::value).findFirst(); return "instance(" + value.map(StringUtil::valueToString).orElse("") + ") isa " + thing.type().label(); } else if (concept.isType()) { return "type(" + concept.asType().label() + ")"; } else if (concept.isRole()) { return "role(" + concept.asRole().label() + ")"; } else if (concept.isRule()) { return "rule(" + concept.asRule().label() + ")"; } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } }
/** * Add a roleplayer to the given {@link Relationship} * @param relationship the concept representing the {@link Relationship} * @param relationPlayer a casting between a role type and role player */ private void addRoleplayer(QueryOperationExecutor executor, Relationship relationship, RelationPlayer relationPlayer) { VarPatternAdmin roleVar = getRole(relationPlayer); Role role = executor.get(roleVar.var()).asRole(); Thing roleplayer = executor.get(relationPlayer.getRolePlayer().var()).asThing(); relationship.assign(role, roleplayer); }
@Override public Stream<ConceptMap> materialise(){ RelationshipType relationType = getSchemaConcept().asRelationshipType(); Multimap<Role, Var> roleVarMap = getRoleVarMap(); ConceptMap substitution = getParentQuery().getSubstitution(); //if the relation already exists, only assign roleplayers, otherwise create a new relation Relationship relationship = substitution.containsVar(getVarName())? substitution.get(getVarName()).asRelationship() : RelationshipTypeImpl.from(relationType).addRelationshipInferred(); roleVarMap.asMap().forEach((key, value) -> value.forEach(var -> relationship.assign(key, substitution.get(var).asThing()))); ConceptMap relationSub = getRoleSubstitution().merge( getVarName().isUserDefinedName()? new ConceptMapImpl(ImmutableMap.of(getVarName(), relationship)) : new ConceptMapImpl() ); return Stream.of(substitution.merge(relationSub)); }
public static Map<Role, Set<Thing>> convert(GrpcConceptConverter converter, RolePlayers allRolePlayers) { ImmutableSetMultimap.Builder<Role, Thing> map = ImmutableSetMultimap.builder(); for (GrpcConcept.RolePlayer rolePlayer : allRolePlayers.getRolePlayerList()) { Role role = converter.convert(rolePlayer.getRole()).asRole(); Thing player = converter.convert(rolePlayer.getPlayer()).asThing(); map.put(role, player); } return Multimaps.asMap(map.build()); }
@Override public Collection<PropertyExecutor> insert(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { Attribute attributeConcept = executor.get(attribute().var()).asAttribute(); Thing thing = executor.get(var).asThing(); ConceptId relationshipId = thing.relhas(attributeConcept).id(); executor.builder(relationship().var()).id(relationshipId); }; PropertyExecutor executor = PropertyExecutor.builder(method) .produces(relationship().var()) .requires(var, attribute().var()) .build(); return ImmutableSet.of(executor); }
@Override protected Json concept(Concept concept) { Json json = Json.object("id", concept.id().getValue()); if (concept.isSchemaConcept()) { json.set("name", concept.asSchemaConcept().label().getValue()); SchemaConcept superConcept = concept.asSchemaConcept().sup(); if (superConcept != null) json.set("sub", superConcept.label().getValue()); } else if (concept.isThing()) { json.set("isa", concept.asThing().type().label().getValue()); } else { throw CommonUtil.unreachableStatement("Unrecognised concept " + concept); } if (concept.isAttribute()) { json.set("value", concept.asAttribute().value()); } if (concept.isRule()) { Pattern when = concept.asRule().when(); if (when != null) { json.set("when", when.toString()); } Pattern then = concept.asRule().then(); if (then != null) { json.set("then", then.toString()); } } return json; }