Search in sources :

Example 81 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class ThingImpl method deleteAttribute.

@Override
public T deleteAttribute(Attribute attribute) {
    Role roleHasOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(attribute.type().getLabel()));
    Role roleKeyOwner = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_OWNER.getLabel(attribute.type().getLabel()));
    Role roleHasValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(attribute.type().getLabel()));
    Role roleKeyValue = vertex().tx().getSchemaConcept(Schema.ImplicitType.KEY_VALUE.getLabel(attribute.type().getLabel()));
    Stream<Relationship> relationships = relationships(filterNulls(roleHasOwner, roleKeyOwner));
    relationships.filter(relationship -> {
        Stream<Thing> rolePlayers = relationship.rolePlayers(filterNulls(roleHasValue, roleKeyValue));
        return rolePlayers.anyMatch(rolePlayer -> rolePlayer.equals(attribute));
    }).forEach(Concept::delete);
    return getThis();
}
Also used : Role(ai.grakn.concept.Role) Arrays(java.util.Arrays) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Type(ai.grakn.concept.Type) Cacheable(ai.grakn.kb.internal.cache.Cacheable) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) LabelId(ai.grakn.concept.LabelId) RelationshipType(ai.grakn.concept.RelationshipType) Cache(ai.grakn.kb.internal.cache.Cache) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EdgeElement(ai.grakn.kb.internal.structure.EdgeElement) P(org.apache.tinkerpop.gremlin.process.traversal.P) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) VertexElement(ai.grakn.kb.internal.structure.VertexElement) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Casting(ai.grakn.kb.internal.structure.Casting) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Concept(ai.grakn.concept.Concept) Relationship(ai.grakn.concept.Relationship) Stream(java.util.stream.Stream)

Example 82 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class GetQueryPropertyTest method joinAnswer.

private Optional<Answer> joinAnswer(Answer answerA, Answer answerB) {
    Map<Var, Concept> answer = Maps.newHashMap(answerA.map());
    answer.putAll(answerB.map());
    for (Var var : Sets.intersection(answerA.vars(), answerB.vars())) {
        if (!answerA.get(var).equals(answerB.get(var))) {
            return Optional.empty();
        }
    }
    return Optional.of(new QueryAnswer(answer));
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var)

Example 83 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class RemoteGraknTxTest method whenGettingConceptViaID_EnsureCorrectRequestIsSent.

@Test
public void whenGettingConceptViaID_EnsureCorrectRequestIsSent() {
    ConceptId id = ConceptId.of(V123.getValue());
    try (RemoteGraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.READ))) {
        // The open request
        verify(server.requests()).onNext(any());
        Concept concept = RemoteConcepts.createEntity(tx, id);
        server.setResponse(GrpcUtil.getConceptRequest(id), GrpcUtil.optionalConceptResponse(Optional.of(concept)));
        assertEquals(concept, tx.getConcept(id));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 84 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class RemoteGraknTxTest method whenPuttingEntityType_EnsureCorrectRequestIsSent.

@Test
public void whenPuttingEntityType_EnsureCorrectRequestIsSent() {
    ConceptId id = ConceptId.of(V123.getValue());
    Label label = Label.of("foo");
    try (RemoteGraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.READ))) {
        // The open request
        verify(server.requests()).onNext(any());
        Concept concept = RemoteConcepts.createEntityType(tx, id);
        server.setResponse(GrpcUtil.putEntityTypeRequest(label), GrpcUtil.conceptResponse(concept));
        assertEquals(concept, tx.putEntityType(label));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 85 with Concept

use of ai.grakn.concept.Concept in project grakn by graknlabs.

the class QueryAnswer method merge.

@Override
public Answer merge(Answer a2, boolean mergeExplanation) {
    if (a2.isEmpty())
        return this;
    if (this.isEmpty())
        return a2;
    Sets.SetView<Var> varUnion = Sets.union(this.vars(), a2.vars());
    Set<Var> varIntersection = Sets.intersection(this.vars(), a2.vars());
    Map<Var, Concept> entryMap = Sets.union(this.entrySet(), a2.entrySet()).stream().filter(e -> !varIntersection.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    varIntersection.forEach(var -> {
        Concept concept = this.get(var);
        Concept otherConcept = a2.get(var);
        if (concept.equals(otherConcept))
            entryMap.put(var, concept);
        else {
            if (concept.isSchemaConcept() && otherConcept.isSchemaConcept() && !ReasonerUtils.areDisjointTypes(concept.asSchemaConcept(), otherConcept.asSchemaConcept())) {
                entryMap.put(var, Iterables.getOnlyElement(ReasonerUtils.topOrMeta(Sets.newHashSet(concept.asSchemaConcept(), otherConcept.asSchemaConcept()))));
            }
        }
    });
    if (!entryMap.keySet().equals(varUnion))
        return new QueryAnswer();
    return new QueryAnswer(entryMap, mergeExplanation ? this.mergeExplanation(a2) : this.getExplanation());
}
Also used : Concept(ai.grakn.concept.Concept) Iterables(com.google.common.collect.Iterables) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Concept(ai.grakn.concept.Concept) Graql(ai.grakn.graql.Graql) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) JoinExplanation(ai.grakn.graql.internal.reasoner.explanation.JoinExplanation) HashSet(java.util.HashSet) AnswerExplanation(ai.grakn.graql.admin.AnswerExplanation) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) Explanation(ai.grakn.graql.internal.reasoner.explanation.Explanation) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Collections(java.util.Collections) Sets(com.google.common.collect.Sets) Var(ai.grakn.graql.Var) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Concept (ai.grakn.concept.Concept)91 Test (org.junit.Test)56 ConceptId (ai.grakn.concept.ConceptId)26 GraknTx (ai.grakn.GraknTx)25 Answer (ai.grakn.graql.admin.Answer)25 SchemaConcept (ai.grakn.concept.SchemaConcept)19 Label (ai.grakn.concept.Label)18 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)18 QueryBuilder (ai.grakn.graql.QueryBuilder)17 Var (ai.grakn.graql.Var)15 Set (java.util.Set)15 Role (ai.grakn.concept.Role)14 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)14 HashSet (java.util.HashSet)13 List (java.util.List)12 AttributeType (ai.grakn.concept.AttributeType)11 EntityType (ai.grakn.concept.EntityType)11 Schema (ai.grakn.util.Schema)10 Collectors (java.util.stream.Collectors)10 Stream (java.util.stream.Stream)10