Search in sources :

Example 31 with SchemaConcept

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

the class GreedyTraversalPlan method getLabelVarTypeMap.

// find all vars representing types
private static Map<Var, Type> getLabelVarTypeMap(EmbeddedGraknTx<?> tx, Set<Fragment> allFragments) {
    Map<Var, Type> labelVarTypeMap = new HashMap<>();
    allFragments.stream().filter(LabelFragment.class::isInstance).forEach(fragment -> {
        // TODO: labels() should return ONE label instead of a set
        SchemaConcept schemaConcept = tx.getSchemaConcept(Iterators.getOnlyElement(((LabelFragment) fragment).labels().iterator()));
        if (schemaConcept != null && !schemaConcept.isRole() && !schemaConcept.isRule()) {
            labelVarTypeMap.put(fragment.start(), schemaConcept.asType());
        }
    });
    return labelVarTypeMap;
}
Also used : Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 32 with SchemaConcept

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

the class GraqlCompleter method create.

public static GraqlCompleter create(GraknSession session) {
    ImmutableSet<Label> labels;
    try (GraknAdmin tx = session.open(GraknTxType.READ).admin()) {
        Stream<SchemaConcept> metaConcepts = Stream.of(tx.getMetaConcept(), tx.getMetaRole(), tx.getMetaRule()).flatMap(SchemaConcept::subs);
        labels = metaConcepts.map(SchemaConcept::getLabel).collect(toImmutableSet());
    }
    return new GraqlCompleter(labels);
}
Also used : Label(ai.grakn.concept.Label) GraknAdmin(ai.grakn.kb.admin.GraknAdmin) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 33 with SchemaConcept

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

the class GraqlPrinter method build.

@Override
public Function<StringBuilder, StringBuilder> build(Concept concept) {
    return sb -> {
        // Display values for resources and ids for everything else
        if (concept.isAttribute()) {
            sb.append(colorKeyword("val ")).append(StringUtil.valueToString(concept.asAttribute().getValue()));
        } else if (concept.isSchemaConcept()) {
            SchemaConcept ontoConcept = concept.asSchemaConcept();
            sb.append(colorKeyword("label ")).append(colorType(ontoConcept));
            SchemaConcept superConcept = ontoConcept.sup();
            if (superConcept != null) {
                sb.append(colorKeyword(" sub ")).append(colorType(superConcept));
            }
        } else {
            sb.append(colorKeyword("id ")).append(idToString(concept.getId()));
        }
        if (concept.isRelationship()) {
            String relationString = concept.asRelationship().allRolePlayers().entrySet().stream().flatMap(entry -> {
                Role role = entry.getKey();
                Set<Thing> things = entry.getValue();
                return things.stream().map(instance -> Optional.of(colorType(role) + ": id " + idToString(instance.getId())));
            }).flatMap(CommonUtil::optionalToStream).collect(Collectors.joining(", "));
            sb.append(" (").append(relationString).append(")");
        }
        // Display type of each instance
        if (concept.isThing()) {
            Type type = concept.asThing().type();
            sb.append(colorKeyword(" isa ")).append(colorType(type));
        }
        // Display when and then for rules
        if (concept.isRule()) {
            sb.append(colorKeyword(" when ")).append("{ ").append(concept.asRule().getWhen()).append(" }");
            sb.append(colorKeyword(" then ")).append("{ ").append(concept.asRule().getThen()).append(" }");
        }
        // Display any requested resources
        if (concept.isThing() && attributeTypes.length > 0) {
            concept.asThing().attributes(attributeTypes).forEach(resource -> {
                String resourceType = colorType(resource.type());
                String value = StringUtil.valueToString(resource.getValue());
                sb.append(colorKeyword(" has ")).append(resourceType).append(" ").append(value);
            });
        }
        return sb;
    };
}
Also used : StringConverter.typeLabelToString(ai.grakn.graql.internal.util.StringConverter.typeLabelToString) ANSI(ai.grakn.graql.internal.util.ANSI) StringConverter.idToString(ai.grakn.graql.internal.util.StringConverter.idToString) Role(ai.grakn.concept.Role) Collection(java.util.Collection) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) StringUtil(ai.grakn.util.StringUtil) Set(java.util.Set) Type(ai.grakn.concept.Type) Printer(ai.grakn.graql.Printer) Answer(ai.grakn.graql.admin.Answer) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AttributeType(ai.grakn.concept.AttributeType) Thing(ai.grakn.concept.Thing) CommonUtil(ai.grakn.util.CommonUtil) Map(java.util.Map) Optional(java.util.Optional) Role(ai.grakn.concept.Role) CommonUtil(ai.grakn.util.CommonUtil) Type(ai.grakn.concept.Type) AttributeType(ai.grakn.concept.AttributeType) Set(java.util.Set) SchemaConcept(ai.grakn.concept.SchemaConcept) StringConverter.typeLabelToString(ai.grakn.graql.internal.util.StringConverter.typeLabelToString) StringConverter.idToString(ai.grakn.graql.internal.util.StringConverter.idToString)

Example 34 with SchemaConcept

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

the class HasAttributeTypeProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // NB: HasResourceType is a special case and it doesn't allow variables as resource types
    Var varName = var.var().asUserDefined();
    Label label = this.resourceType().getTypeLabel().orElse(null);
    Var predicateVar = var().asUserDefined();
    SchemaConcept schemaConcept = parent.tx().getSchemaConcept(label);
    ConceptId predicateId = schemaConcept != null ? schemaConcept.getId() : null;
    // isa part
    VarPatternAdmin resVar = varName.has(Graql.label(label)).admin();
    return HasAtom.create(resVar, predicateVar, predicateId, parent);
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) ConceptId(ai.grakn.concept.ConceptId)

Example 35 with SchemaConcept

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

the class TinkerComputeQueryRunner method run.

public ComputeJob<Map<Long, Set<String>>> run(CorenessQuery query) {
    return runCompute(query, tinkerComputeQuery -> {
        long k = query.minK();
        if (k < 2L)
            throw GraqlQueryException.kValueSmallerThanTwo();
        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);
                if (type.isRelationshipType())
                    throw GraqlQueryException.kCoreOnRelationshipType(typeLabel);
                return type.subs();
            }).map(SchemaConcept::getLabel).collect(Collectors.toSet());
        }
        Set<Label> subLabels = Sets.union(tinkerComputeQuery.subLabels(), ofLabels);
        if (!tinkerComputeQuery.selectedTypesHaveInstance()) {
            return Collections.emptyMap();
        }
        ComputerResult result;
        Set<LabelId> subLabelIds = convertLabelsToIds(subLabels);
        Set<LabelId> ofLabelIds = convertLabelsToIds(ofLabels);
        try {
            result = tinkerComputeQuery.compute(new CorenessVertexProgram(k), new DegreeDistributionMapReduce(ofLabelIds, CorenessVertexProgram.CORENESS), subLabelIds);
        } catch (NoResultException e) {
            return Collections.emptyMap();
        }
        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) CorenessVertexProgram(ai.grakn.graql.internal.analytics.CorenessVertexProgram) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) Label(ai.grakn.concept.Label) SchemaConcept(ai.grakn.concept.SchemaConcept) LabelId(ai.grakn.concept.LabelId) NoResultException(ai.grakn.graql.internal.analytics.NoResultException)

Aggregations

SchemaConcept (ai.grakn.concept.SchemaConcept)51 Label (ai.grakn.concept.Label)19 Set (java.util.Set)15 Type (ai.grakn.concept.Type)14 GraknTx (ai.grakn.GraknTx)12 HashSet (java.util.HashSet)12 ConceptId (ai.grakn.concept.ConceptId)11 Stream (java.util.stream.Stream)11 Test (org.junit.Test)11 AttributeType (ai.grakn.concept.AttributeType)10 Property (com.pholser.junit.quickcheck.Property)10 Role (ai.grakn.concept.Role)9 Concept (ai.grakn.concept.Concept)8 Sets (com.google.common.collect.Sets)8 Optional (java.util.Optional)8 RelationshipType (ai.grakn.concept.RelationshipType)7 Rule (ai.grakn.concept.Rule)7 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)7 ErrorMessage (ai.grakn.util.ErrorMessage)7 Var (ai.grakn.graql.Var)6