Search in sources :

Example 26 with Label

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

the class TinkerComputeQueryRunner method run.

public ComputeJob<Map<Long, Set<String>>> run(DegreeQuery query) {
    return runCompute(query, tinkerComputeQuery -> {
        Set<Label> ofLabels;
        // Check if ofType is valid before returning emptyMap
        if (query.targetLabels().isEmpty()) {
            ofLabels = tinkerComputeQuery.subLabels();
        } else {
            ofLabels = query.targetLabels().stream().flatMap(typeLabel -> {
                Type type = tx.getSchemaConcept(typeLabel);
                if (type == null)
                    throw GraqlQueryException.labelNotFound(typeLabel);
                return type.subs();
            }).map(SchemaConcept::getLabel).collect(Collectors.toSet());
        }
        Set<Label> subLabels = Sets.union(tinkerComputeQuery.subLabels(), ofLabels);
        if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
            return Collections.emptyMap();
        }
        Set<LabelId> subLabelIds = convertLabelsToIds(subLabels);
        Set<LabelId> ofLabelIds = convertLabelsToIds(ofLabels);
        ComputerResult result = tinkerComputeQuery.compute(new DegreeVertexProgram(ofLabelIds), new DegreeDistributionMapReduce(ofLabelIds, DegreeVertexProgram.DEGREE), subLabelIds);
        return result.memory().get(DegreeDistributionMapReduce.class.getName());
    });
}
Also used : MaxMapReduce(ai.grakn.graql.internal.analytics.MaxMapReduce) LoggerFactory(org.slf4j.LoggerFactory) ConnectedComponentQuery(ai.grakn.graql.analytics.ConnectedComponentQuery) Type(ai.grakn.concept.Type) KCoreVertexProgram(ai.grakn.graql.internal.analytics.KCoreVertexProgram) CountMapReduceWithAttribute(ai.grakn.graql.internal.analytics.CountMapReduceWithAttribute) Label(ai.grakn.concept.Label) LabelId(ai.grakn.concept.LabelId) Map(java.util.Map) CorenessVertexProgram(ai.grakn.graql.internal.analytics.CorenessVertexProgram) ComputeQuery(ai.grakn.graql.ComputeQuery) ConceptId(ai.grakn.concept.ConceptId) MinQuery(ai.grakn.graql.analytics.MinQuery) CorenessQuery(ai.grakn.graql.analytics.CorenessQuery) MaxQuery(ai.grakn.graql.analytics.MaxQuery) ConnectedComponentVertexProgram(ai.grakn.graql.internal.analytics.ConnectedComponentVertexProgram) Set(java.util.Set) DegreeStatisticsVertexProgram(ai.grakn.graql.internal.analytics.DegreeStatisticsVertexProgram) GraknMapReduce(ai.grakn.graql.internal.analytics.GraknMapReduce) MeanMapReduce(ai.grakn.graql.internal.analytics.MeanMapReduce) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SumMapReduce(ai.grakn.graql.internal.analytics.SumMapReduce) Serializable(java.io.Serializable) List(java.util.List) Memory(org.apache.tinkerpop.gremlin.process.computer.Memory) MedianVertexProgram(ai.grakn.graql.internal.analytics.MedianVertexProgram) Optional(java.util.Optional) DegreeDistributionMapReduce(ai.grakn.graql.internal.analytics.DegreeDistributionMapReduce) DegreeQuery(ai.grakn.graql.analytics.DegreeQuery) ComputeJob(ai.grakn.ComputeJob) StdQuery(ai.grakn.graql.analytics.StdQuery) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Multimap(com.google.common.collect.Multimap) Function(java.util.function.Function) StatisticsQuery(ai.grakn.graql.StatisticsQuery) ConnectedComponentsVertexProgram(ai.grakn.graql.internal.analytics.ConnectedComponentsVertexProgram) ClusterSizeMapReduce(ai.grakn.graql.internal.analytics.ClusterSizeMapReduce) PathQuery(ai.grakn.graql.analytics.PathQuery) AttributeType(ai.grakn.concept.AttributeType) NoResultException(ai.grakn.graql.internal.analytics.NoResultException) GraknComputer(ai.grakn.GraknComputer) MeanQuery(ai.grakn.graql.analytics.MeanQuery) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) GraknVertexProgram(ai.grakn.graql.internal.analytics.GraknVertexProgram) SumQuery(ai.grakn.graql.analytics.SumQuery) GraqlQueryException(ai.grakn.exception.GraqlQueryException) DegreeVertexProgram(ai.grakn.graql.internal.analytics.DegreeVertexProgram) Logger(org.slf4j.Logger) ShortestPathVertexProgram(ai.grakn.graql.internal.analytics.ShortestPathVertexProgram) MedianQuery(ai.grakn.graql.analytics.MedianQuery) MinMapReduce(ai.grakn.graql.internal.analytics.MinMapReduce) CountVertexProgram(ai.grakn.graql.internal.analytics.CountVertexProgram) ClusterMemberMapReduce(ai.grakn.graql.internal.analytics.ClusterMemberMapReduce) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) CountQuery(ai.grakn.graql.analytics.CountQuery) PathsQuery(ai.grakn.graql.analytics.PathsQuery) KCoreQuery(ai.grakn.graql.analytics.KCoreQuery) MapReduce(org.apache.tinkerpop.gremlin.process.computer.MapReduce) StdMapReduce(ai.grakn.graql.internal.analytics.StdMapReduce) Collections(java.util.Collections) Type(ai.grakn.concept.Type) AttributeType(ai.grakn.concept.AttributeType) DegreeDistributionMapReduce(ai.grakn.graql.internal.analytics.DegreeDistributionMapReduce) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) Label(ai.grakn.concept.Label) DegreeVertexProgram(ai.grakn.graql.internal.analytics.DegreeVertexProgram) SchemaConcept(ai.grakn.concept.SchemaConcept) LabelId(ai.grakn.concept.LabelId)

Example 27 with Label

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

the class GrpcServerTest method whenGettingALabel_TheLabelIsReturned.

@Test
public void whenGettingALabel_TheLabelIsReturned() throws InterruptedException {
    ConceptId id = ConceptId.of("V123456");
    Label label = Label.of("Dunstan");
    Concept concept = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(id)).thenReturn(concept);
    when(concept.isSchemaConcept()).thenReturn(true);
    when(concept.asSchemaConcept().getLabel()).thenReturn(label);
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.READ));
        tx.receive().ok();
        tx.send(GrpcUtil.runConceptMethodRequest(id, ConceptMethods.GET_LABEL));
        assertEquals(label, ConceptMethods.GET_LABEL.get(conceptConverter, client, tx.receive().ok()));
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 28 with Label

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

the class LabelFragmentSet method tryExpandSubs.

/**
 * Expand a {@link LabelFragmentSet} to match all sub-concepts of the single existing {@link Label}.
 *
 * Returns null if there is not exactly one label any of the {@link Label}s mentioned are not in the knowledge base.
 */
@Nullable
LabelFragmentSet tryExpandSubs(Var typeVar, GraknTx tx) {
    if (labels().size() != 1)
        return null;
    Label oldLabel = Iterables.getOnlyElement(labels());
    SchemaConcept concept = tx.getSchemaConcept(oldLabel);
    if (concept == null)
        return null;
    Set<Label> newLabels = concept.subs().map(SchemaConcept::getLabel).collect(toSet());
    return new AutoValue_LabelFragmentSet(varProperty(), typeVar, ImmutableSet.copyOf(newLabels));
}
Also used : Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) Nullable(javax.annotation.Nullable)

Example 29 with Label

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

the class TypeImpl method deleteAttribute.

/**
 * Helper method to delete a {@link AttributeType} which is possible linked to this {@link Type}.
 * The link to {@link AttributeType} is removed if <code>attributeToRemove</code> is in the candidate list
 * <code>attributeTypes</code>
 *
 * @param implicitType the {@link Schema.ImplicitType} which specifies which implicit {@link Role} should be removed
 * @param attributeTypes The list of candidate which potentially contains the {@link AttributeType} to remove
 * @param attributeToRemove the {@link AttributeType} to remove
 * @return the {@link Type} itself
 */
private T deleteAttribute(Schema.ImplicitType implicitType, Stream<AttributeType> attributeTypes, AttributeType attributeToRemove) {
    if (attributeTypes.anyMatch(a -> a.equals(attributeToRemove))) {
        Label label = implicitType.getLabel(attributeToRemove.getLabel());
        Role role = vertex().tx().getSchemaConcept(label);
        if (role != null)
            deletePlays(role);
    }
    return getThis();
}
Also used : Role(ai.grakn.concept.Role) Label(ai.grakn.concept.Label)

Example 30 with Label

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

the class TypeImpl method has.

/**
 * Creates a relation type which allows this type and a {@link ai.grakn.concept.Attribute} type to be linked.
 * @param attributeType The {@link AttributeType} which instances of this type should be allowed to play.
 * @param has the implicit relation type to build
 * @param hasValue the implicit role type to build for the {@link AttributeType}
 * @param hasOwner the implicit role type to build for the type
 * @param required Indicates if the {@link ai.grakn.concept.Attribute} is required on the entity
 * @return The {@link Type} itself
 */
private T has(AttributeType attributeType, Schema.ImplicitType has, Schema.ImplicitType hasValue, Schema.ImplicitType hasOwner, boolean required) {
    // Check if this is a met type
    checkSchemaMutationAllowed();
    // Check if attribute type is the meta
    if (Schema.MetaSchema.ATTRIBUTE.getLabel().equals(attributeType.getLabel())) {
        throw GraknTxOperationException.metaTypeImmutable(attributeType.getLabel());
    }
    Label attributeLabel = attributeType.getLabel();
    Role ownerRole = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(attributeLabel));
    Role valueRole = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(attributeLabel));
    RelationshipType relationshipType = vertex().tx().putRelationTypeImplicit(has.getLabel(attributeLabel)).relates(ownerRole).relates(valueRole);
    // Linking with ako structure if present
    AttributeType attributeTypeSuper = attributeType.sup();
    Label superLabel = attributeTypeSuper.getLabel();
    if (!Schema.MetaSchema.ATTRIBUTE.getLabel().equals(superLabel)) {
        // Check to make sure we dont add plays edges to meta types accidentally
        Role ownerRoleSuper = vertex().tx().putRoleTypeImplicit(hasOwner.getLabel(superLabel));
        Role valueRoleSuper = vertex().tx().putRoleTypeImplicit(hasValue.getLabel(superLabel));
        RelationshipType relationshipTypeSuper = vertex().tx().putRelationTypeImplicit(has.getLabel(superLabel)).relates(ownerRoleSuper).relates(valueRoleSuper);
        // Create the super type edges from sub role/relations to super roles/relation
        ownerRole.sup(ownerRoleSuper);
        valueRole.sup(valueRoleSuper);
        relationshipType.sup(relationshipTypeSuper);
        // Make sure the supertype attribute is linked with the role as well
        ((AttributeTypeImpl) attributeTypeSuper).plays(valueRoleSuper);
    }
    this.plays(ownerRole, required);
    // TODO: Use explicit cardinality of 0-1 rather than just false
    ((AttributeTypeImpl) attributeType).plays(valueRole, false);
    return getThis();
}
Also used : Role(ai.grakn.concept.Role) AttributeType(ai.grakn.concept.AttributeType) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType)

Aggregations

Label (ai.grakn.concept.Label)87 Test (org.junit.Test)41 Role (ai.grakn.concept.Role)25 GraknTx (ai.grakn.GraknTx)22 ConceptId (ai.grakn.concept.ConceptId)21 Concept (ai.grakn.concept.Concept)20 Set (java.util.Set)20 RelationshipType (ai.grakn.concept.RelationshipType)19 SchemaConcept (ai.grakn.concept.SchemaConcept)19 AttributeType (ai.grakn.concept.AttributeType)17 EntityType (ai.grakn.concept.EntityType)17 Relationship (ai.grakn.concept.Relationship)14 HashSet (java.util.HashSet)14 Collectors (java.util.stream.Collectors)13 Type (ai.grakn.concept.Type)12 Stream (java.util.stream.Stream)12 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)11 Schema (ai.grakn.util.Schema)11 Collection (java.util.Collection)11 Pattern (ai.grakn.graql.Pattern)10