Search in sources :

Example 1 with CommonUtil

use of ai.grakn.util.CommonUtil in project grakn by graknlabs.

the class InsertQueryImpl method getSchemaConcepts.

@Override
public Set<SchemaConcept> getSchemaConcepts() {
    GraknTx theGraph = getTx().orElseThrow(GraqlQueryException::noTx);
    Set<SchemaConcept> types = allVarPatterns().map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream).map(theGraph::<Type>getSchemaConcept).collect(Collectors.toSet());
    match().ifPresent(mq -> types.addAll(mq.admin().getSchemaConcepts()));
    return types;
}
Also used : GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) Type(ai.grakn.concept.Type) GraqlQueryException(ai.grakn.exception.GraqlQueryException) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 2 with CommonUtil

use of ai.grakn.util.CommonUtil 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)

Aggregations

SchemaConcept (ai.grakn.concept.SchemaConcept)2 Type (ai.grakn.concept.Type)2 CommonUtil (ai.grakn.util.CommonUtil)2 GraknTx (ai.grakn.GraknTx)1 AttributeType (ai.grakn.concept.AttributeType)1 Concept (ai.grakn.concept.Concept)1 Role (ai.grakn.concept.Role)1 Thing (ai.grakn.concept.Thing)1 GraqlQueryException (ai.grakn.exception.GraqlQueryException)1 Printer (ai.grakn.graql.Printer)1 Answer (ai.grakn.graql.admin.Answer)1 ANSI (ai.grakn.graql.internal.util.ANSI)1 StringConverter.idToString (ai.grakn.graql.internal.util.StringConverter.idToString)1 StringConverter.typeLabelToString (ai.grakn.graql.internal.util.StringConverter.typeLabelToString)1 StringUtil (ai.grakn.util.StringUtil)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1