Search in sources :

Example 86 with Concept

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

the class RelationshipProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // set varName as user defined if reified
    // reified if contains more properties than the RelationshipProperty itself and potential IsaProperty
    boolean isReified = var.getProperties().filter(prop -> !RelationshipProperty.class.isInstance(prop)).anyMatch(prop -> !AbstractIsaProperty.class.isInstance(prop));
    VarPattern relVar = isReified ? var.var().asUserDefined() : var.var();
    for (RelationPlayer rp : relationPlayers()) {
        VarPattern rolePattern = rp.getRole().orElse(null);
        VarPattern rolePlayer = rp.getRolePlayer();
        if (rolePattern != null) {
            Var roleVar = rolePattern.admin().var();
            // look for indirect role definitions
            IdPredicate roleId = getUserDefinedIdPredicate(roleVar, vars, parent);
            if (roleId != null) {
                Concept concept = parent.tx().getConcept(roleId.getPredicate());
                if (concept != null) {
                    if (concept.isRole()) {
                        Label roleLabel = concept.asSchemaConcept().getLabel();
                        rolePattern = roleVar.label(roleLabel);
                    } else {
                        throw GraqlQueryException.nonRoleIdAssignedToRoleVariable(var);
                    }
                }
            }
            relVar = relVar.rel(rolePattern, rolePlayer);
        } else
            relVar = relVar.rel(rolePlayer);
    }
    // isa part
    AbstractIsaProperty isaProp = var.getProperty(AbstractIsaProperty.class).orElse(null);
    IdPredicate predicate = null;
    // if no isa property present generate type variable
    Var typeVariable = isaProp != null ? isaProp.type().var() : Graql.var();
    // Isa present
    if (isaProp != null) {
        VarPatternAdmin isaVar = isaProp.type();
        Label label = isaVar.getTypeLabel().orElse(null);
        if (label != null) {
            predicate = IdPredicate.create(typeVariable, label, parent);
        } else {
            typeVariable = isaVar.var();
            predicate = getUserDefinedIdPredicate(typeVariable, vars, parent);
        }
    }
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    relVar = isaProp instanceof DirectIsaProperty ? relVar.directIsa(typeVariable.asUserDefined()) : relVar.isa(typeVariable.asUserDefined());
    return RelationshipAtom.create(relVar.admin(), typeVariable, predicateId, parent);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Role(ai.grakn.concept.Role) EquivalentFragmentSets.rolePlayer(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.rolePlayer) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Graql(ai.grakn.graql.Graql) HashSet(java.util.HashSet) UniqueVarProperty(ai.grakn.graql.admin.UniqueVarProperty) Label(ai.grakn.concept.Label) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) QueryOperationExecutor(ai.grakn.graql.internal.query.runner.QueryOperationExecutor) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Sets(com.google.common.collect.Sets) Collectors.joining(java.util.stream.Collectors.joining) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) EquivalentFragmentSets(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) ReasonerUtils.getUserDefinedIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getUserDefinedIdPredicate) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ReasonerUtils.getUserDefinedIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getUserDefinedIdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) VarPattern(ai.grakn.graql.VarPattern) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Example 87 with Concept

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

the class GraknKeyspaceStoreTest method cleanSystemKeySpaceGraph.

@After
public void cleanSystemKeySpaceGraph() {
    try (GraknTx tx = graknFactory.tx(SYSTEM_KB_KEYSPACE, GraknTxType.WRITE)) {
        tx.getEntityType("keyspace").instances().forEach(Concept::delete);
        tx.getAttributeType("keyspace-name").instances().forEach(Concept::delete);
        tx.commit();
    }
    transactions.forEach(GraknTx::close);
}
Also used : Concept(ai.grakn.concept.Concept) GraknTx(ai.grakn.GraknTx) After(org.junit.After)

Example 88 with Concept

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

the class ElementFactory method buildConcept.

public <X extends Concept> X buildConcept(VertexElement vertexElement) {
    Schema.BaseType type;
    try {
        type = getBaseType(vertexElement);
    } catch (IllegalStateException e) {
        throw TemporaryWriteException.indexOverlap(vertexElement.element(), e);
    }
    ConceptId conceptId = ConceptId.of(vertexElement.property(Schema.VertexProperty.ID));
    if (!tx.txCache().isConceptCached(conceptId)) {
        Concept concept;
        switch(type) {
            case RELATIONSHIP:
                concept = RelationshipImpl.create(RelationshipReified.get(vertexElement));
                break;
            case TYPE:
                concept = new TypeImpl(vertexElement);
                break;
            case ROLE:
                concept = RoleImpl.get(vertexElement);
                break;
            case RELATIONSHIP_TYPE:
                concept = RelationshipTypeImpl.get(vertexElement);
                break;
            case ENTITY:
                concept = EntityImpl.get(vertexElement);
                break;
            case ENTITY_TYPE:
                concept = EntityTypeImpl.get(vertexElement);
                break;
            case ATTRIBUTE_TYPE:
                concept = AttributeTypeImpl.get(vertexElement);
                break;
            case ATTRIBUTE:
                concept = AttributeImpl.get(vertexElement);
                break;
            case RULE:
                concept = RuleImpl.get(vertexElement);
                break;
            default:
                throw GraknTxOperationException.unknownConcept(type.name());
        }
        tx.txCache().cacheConcept(concept);
    }
    return tx.txCache().getCachedConcept(conceptId);
}
Also used : Concept(ai.grakn.concept.Concept) Schema(ai.grakn.util.Schema) ConceptId(ai.grakn.concept.ConceptId)

Example 89 with Concept

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

the class ElementFactory method buildConcept.

public <X extends Concept> X buildConcept(EdgeElement edgeElement) {
    Schema.EdgeLabel label = Schema.EdgeLabel.valueOf(edgeElement.label().toUpperCase(Locale.getDefault()));
    ConceptId conceptId = ConceptId.of(edgeElement.id().getValue());
    if (!tx.txCache().isConceptCached(conceptId)) {
        Concept concept;
        switch(label) {
            case ATTRIBUTE:
                concept = RelationshipImpl.create(RelationshipEdge.get(edgeElement));
                break;
            default:
                throw GraknTxOperationException.unknownConcept(label.name());
        }
        tx.txCache().cacheConcept(concept);
    }
    return tx.txCache().getCachedConcept(conceptId);
}
Also used : Concept(ai.grakn.concept.Concept) Schema(ai.grakn.util.Schema) ConceptId(ai.grakn.concept.ConceptId)

Example 90 with Concept

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

the class TxObserver method runConceptMethod.

private void runConceptMethod(RunConceptMethod runConceptMethod) {
    Concept concept = nonNull(tx().getConcept(GrpcUtil.getConceptId(runConceptMethod)));
    GrpcConceptConverter converter = grpcConcept -> tx().getConcept(GrpcUtil.convert(grpcConcept.getId()));
    ConceptMethod<?> conceptMethod = ConceptMethods.fromGrpc(converter, runConceptMethod.getConceptMethod());
    TxResponse response = conceptMethod.run(grpcIterators, concept);
    responseObserver.onNext(response);
}
Also used : Concept(ai.grakn.concept.Concept) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) RunConceptMethod(ai.grakn.rpc.generated.GrpcGrakn.RunConceptMethod) ExecQuery(ai.grakn.rpc.generated.GrpcGrakn.ExecQuery) GrpcGraknService.nonNull(ai.grakn.engine.rpc.GrpcGraknService.nonNull) Role(ai.grakn.concept.Role) GrpcOpenRequestExecutor(ai.grakn.grpc.GrpcOpenRequestExecutor) Concept(ai.grakn.concept.Concept) AttributeValue(ai.grakn.rpc.generated.GrpcConcept.AttributeValue) GrpcIterators(ai.grakn.grpc.GrpcIterators) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GrpcUtil(ai.grakn.grpc.GrpcUtil) Open(ai.grakn.rpc.generated.GrpcGrakn.Open) Rule(ai.grakn.concept.Rule) EntityType(ai.grakn.concept.EntityType) ConceptMethod(ai.grakn.grpc.ConceptMethod) Attribute(ai.grakn.concept.Attribute) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) StreamObserver(io.grpc.stub.StreamObserver) Label(ai.grakn.concept.Label) ConceptMethods(ai.grakn.grpc.ConceptMethods) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) GrpcConceptConverter(ai.grakn.grpc.GrpcConceptConverter) Status(io.grpc.Status) Stop(ai.grakn.rpc.generated.GrpcIterator.Stop) ThreadFactory(java.util.concurrent.ThreadFactory) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) QueryBuilder(ai.grakn.graql.QueryBuilder) Iterator(java.util.Iterator) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) Collection(java.util.Collection) PutRule(ai.grakn.rpc.generated.GrpcGrakn.PutRule) Next(ai.grakn.rpc.generated.GrpcIterator.Next) TxRequest(ai.grakn.rpc.generated.GrpcGrakn.TxRequest) Executors(java.util.concurrent.Executors) PostProcessor(ai.grakn.engine.task.postprocessing.PostProcessor) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Stream(java.util.stream.Stream) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Optional(java.util.Optional) PutAttributeType(ai.grakn.rpc.generated.GrpcGrakn.PutAttributeType) Pattern(ai.grakn.graql.Pattern) GrpcConceptConverter(ai.grakn.grpc.GrpcConceptConverter) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse)

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