/** * Caches a concept so it does not have to be rebuilt later. * * @param concept The concept to be cached. */ public void cacheConcept(Concept concept){ conceptCache.put(concept.id(), concept); if(concept.isSchemaConcept()){ SchemaConcept schemaConcept = concept.asSchemaConcept(); schemaConceptCache.put(schemaConcept.label(), schemaConcept); labelCache.put(schemaConcept.label(), schemaConcept.labelId()); } }
@Override public Stream<ConceptMap> expandHierarchies(Set<Var> toExpand) { if (toExpand.isEmpty()) return Stream.of(this); List<Set<Pair<Var, Concept>>> entryOptions = map.entrySet().stream() .map(e -> { Var var = e.getKey(); if (toExpand.contains(var)) { Concept c = get(var); if (c.isSchemaConcept()) { return ReasonerUtils.upstreamHierarchy(c.asSchemaConcept()).stream() .map(r -> new Pair<Var, Concept>(var, r)) .collect(Collectors.toSet()); } } return Collections.singleton(new Pair<>(var, get(var))); }).collect(Collectors.toList()); return Sets.cartesianProduct(entryOptions).stream() .map(mappingList -> new ConceptMapImpl(mappingList.stream().collect(Collectors.toMap(Pair::getKey, Pair::getValue)), this.explanation())) .map(ans -> ans.explain(explanation())); }
@Override public Stream<ConceptSet> run(DeleteQuery query) { Stream<ConceptMap> answers = query.admin().match().stream().map(result -> result.project(query.admin().vars())).distinct(); // TODO: We should not need to collect toSet, once we fix ConceptId.id() to not use cache. // Stream.distinct() will then work properly when it calls ConceptImpl.equals() Set<Concept> conceptsToDelete = answers.flatMap(answer -> answer.concepts().stream()).collect(toSet()); conceptsToDelete.forEach(concept -> { if (concept.isSchemaConcept()) { throw GraqlQueryException.deleteSchemaConcept(concept.asSchemaConcept()); } concept.delete(); }); // TODO: return deleted Concepts instead of ConceptIds return Stream.of(new ConceptSet(conceptsToDelete.stream().map(Concept::id).collect(toSet()))); }
/** * * @param concept The concept to no longer track */ @SuppressWarnings("SuspiciousMethodCalls") public void remove(Concept concept){ modifiedThings.remove(concept); modifiedRoles.remove(concept); modifiedRelationshipTypes.remove(concept); modifiedRules.remove(concept); if(concept.isAttribute()) { newAttributes.removeAll(AttributeImpl.from(concept.asAttribute()).getIndex()); } if(concept.isRelationship()){ newRelationships.remove(concept.asRelationship()); } conceptCache.remove(concept.id()); if (concept.isSchemaConcept()) { Label label = concept.asSchemaConcept().label(); schemaConceptCache.remove(label); labelCache.remove(label); } }
if (concept.isSchemaConcept() && otherConcept.isSchemaConcept() && !ReasonerUtils.areDisjointTypes(concept.asSchemaConcept(), otherConcept.asSchemaConcept())) { entryMap.put( var, Iterables.getOnlyElement(ReasonerUtils.topOrMeta( Sets.newHashSet( concept.asSchemaConcept(), otherConcept.asSchemaConcept())
@Override public Collection<PropertyExecutor> define(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { SchemaConcept superConcept = executor.get(superType().var()).asSchemaConcept(); Optional<ConceptBuilder> builder = executor.tryBuilder(var); if (builder.isPresent()) { builder.get().sub(superConcept); } else { ConceptBuilder.setSuper(executor.get(var).asSchemaConcept(), superConcept); } }; return ImmutableSet.of(PropertyExecutor.builder(method).requires(superType().var()).produces(var).build()); }
@Override public Collection<PropertyExecutor> undefine(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { SchemaConcept concept = executor.get(var).asSchemaConcept(); SchemaConcept expectedSuperConcept = executor.get(superType().var()).asSchemaConcept(); SchemaConcept actualSuperConcept = concept.sup(); if (!concept.isDeleted() && expectedSuperConcept.equals(actualSuperConcept)) { concept.delete(); } }; return ImmutableSet.of(PropertyExecutor.builder(method).requires(var, superType().var()).build()); }
@Override public Collection<PropertyExecutor> define(Var var) throws GraqlQueryException { PropertyExecutor.Method method = executor -> { Concept concept = executor.get(var); if (concept.isType()) { concept.asType().isAbstract(true); } else { throw GraqlQueryException.insertAbstractOnNonType(concept.asSchemaConcept()); } }; return ImmutableSet.of(PropertyExecutor.builder(method).requires(var).build()); }
@Nullable @Override public <T extends SchemaConcept> T getSchemaConcept(Label label) { transceiver.send(RequestBuilder.Transaction.getSchemaConcept(label)); SessionProto.Transaction.Res response = responseOrThrow(); switch (response.getGetSchemaConceptRes().getResCase()) { case NULL: return null; default: return (T) RemoteConcept.of(response.getGetSchemaConceptRes().getSchemaConcept(), this).asSchemaConcept(); } }
output.append(colorKeyword("val ")).append(StringUtil.valueToString(concept.asAttribute().value())); } else if (concept.isSchemaConcept()) { SchemaConcept ontoConcept = concept.asSchemaConcept(); output.append(colorKeyword("label ")).append(colorType(ontoConcept));
public static ConceptMethod<Void> setDirectSuperConcept(SchemaConcept schemaConcept) { return ConceptMethod.builder(ConceptResponseType.UNIT) .requestSetter(builder -> builder.setSetDirectSuperConcept(convert(schemaConcept))) .functionVoid(concept -> setSuper(concept.asSchemaConcept(), schemaConcept)) .build(); }
public static ConceptMethod<Void> setLabel(Label label) { return ConceptMethod.builder(ConceptResponseType.UNIT) .requestSetter(builder -> builder.setSetLabel(convert(label))) .functionVoid(concept -> concept.asSchemaConcept().setLabel(label)) .build(); }
@Nullable private Concept tryGetConcept() { Concept concept = null; if (has(ID)) { concept = executor.tx().getConcept(use(ID)); if (has(LABEL)) { concept.asSchemaConcept().label(use(LABEL)); } } else if (has(LABEL)) { concept = executor.tx().getSchemaConcept(use(LABEL)); } if (concept != null) { // The super can be changed on an existing concept if (has(SUPER_CONCEPT)) { SchemaConcept superConcept = use(SUPER_CONCEPT); setSuper(concept.asSchemaConcept(), superConcept); } validate(concept); } return concept; }
@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; }
if(concept != null) { if (concept.isRole()) { Label roleLabel = concept.asSchemaConcept().label(); rolePattern = roleVar.label(roleLabel); } else {
case SETDIRECTSUPERCONCEPT: GrpcConcept.Concept setDirectSuperConcept = conceptMethod.getSetDirectSuperConcept(); SchemaConcept schemaConcept = converter.convert(setDirectSuperConcept).asSchemaConcept(); return setDirectSuperConcept(schemaConcept); case UNSETROLEPLAYER: