Search in sources :

Example 56 with Concept

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

the class MatchBase method makeResults.

/**
 * @param vars set of variables of interest
 * @param tx the graph to get results from
 * @param elements a map of vertices and edges where the key is the variable name
 * @return a map of concepts where the key is the variable name
 */
private static Optional<Map<Var, Concept>> makeResults(Set<Var> vars, EmbeddedGraknTx<?> tx, Map<String, Element> elements) {
    Map<Var, Concept> map = new HashMap<>();
    for (Var var : vars) {
        Element element = elements.get(var.name());
        if (element == null) {
            throw GraqlQueryException.unexpectedResult(var);
        } else {
            Concept concept = buildConcept(tx, element);
            map.put(var, concept);
        }
    }
    return Optional.of(map);
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) Element(org.apache.tinkerpop.gremlin.structure.Element)

Example 57 with Concept

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

the class TinkerComputeQuery method getExtendedPaths.

final List<List<Concept>> getExtendedPaths(List<List<Concept>> allPaths) {
    List<List<Concept>> extendedPaths = new ArrayList<>();
    for (List<Concept> currentPath : allPaths) {
        boolean hasAttribute = currentPath.stream().anyMatch(Concept::isAttribute);
        if (!hasAttribute) {
            extendedPaths.add(currentPath);
        }
    }
    // If there exist a path without attributes, we don't need to expand any path
    // as paths contain attributes would be longer after implicit relations are added
    int numExtensionAllowed = extendedPaths.isEmpty() ? Integer.MAX_VALUE : 0;
    for (List<Concept> currentPath : allPaths) {
        List<Concept> extendedPath = new ArrayList<>();
        // record the number of extensions needed for the current path
        int numExtension = 0;
        for (int j = 0; j < currentPath.size() - 1; j++) {
            extendedPath.add(currentPath.get(j));
            ConceptId resourceRelationId = Utility.getResourceEdgeId(tx, currentPath.get(j).getId(), currentPath.get(j + 1).getId());
            if (resourceRelationId != null) {
                numExtension++;
                if (numExtension > numExtensionAllowed)
                    break;
                extendedPath.add(tx.getConcept(resourceRelationId));
            }
        }
        if (numExtension == numExtensionAllowed) {
            extendedPath.add(currentPath.get(currentPath.size() - 1));
            extendedPaths.add(extendedPath);
        } else if (numExtension < numExtensionAllowed) {
            extendedPath.add(currentPath.get(currentPath.size() - 1));
            // longer paths are discarded
            extendedPaths.clear();
            extendedPaths.add(extendedPath);
            // update the minimum number of extensions needed so all the paths have the same length
            numExtensionAllowed = numExtension;
        }
    }
    return extendedPaths;
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ConceptId(ai.grakn.concept.ConceptId)

Example 58 with Concept

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

the class Utility method mayHaveResourceEdge.

/**
 * Check whether it is possible that there is a resource edge between the two given concepts.
 */
private static boolean mayHaveResourceEdge(GraknTx graknGraph, ConceptId conceptId1, ConceptId conceptId2) {
    Concept concept1 = graknGraph.getConcept(conceptId1);
    Concept concept2 = graknGraph.getConcept(conceptId2);
    return concept1 != null && concept2 != null && (concept1.isAttribute() || concept2.isAttribute());
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept)

Example 59 with Concept

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

the class GrpcServerTest method whenExecutingAQueryRemotelyAndAskingForOneResult_OnlyOneResultIsReturned.

// This tests uses an endless stream, so a failure may cause it to never terminate
@Test(timeout = 1000)
public void whenExecutingAQueryRemotelyAndAskingForOneResult_OnlyOneResultIsReturned() throws InterruptedException {
    Concept conceptX = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptX.getId()).thenReturn(ConceptId.of("V123"));
    when(conceptX.isEntity()).thenReturn(true);
    when(conceptX.asEntity().type().getLabel()).thenReturn(Label.of("L123"));
    Concept conceptY = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptY.getId()).thenReturn(ConceptId.of("V456"));
    when(conceptY.isEntity()).thenReturn(true);
    when(conceptY.asEntity().type().getLabel()).thenReturn(Label.of("L456"));
    ImmutableList<Answer> answers = ImmutableList.of(new QueryAnswer(ImmutableMap.of(Graql.var("x"), conceptX)), new QueryAnswer(ImmutableMap.of(Graql.var("y"), conceptY)));
    // TODO: reduce wtf
    when(query.results(any())).thenAnswer(params -> query.stream().map(params.<GrpcConverter>getArgument(0)::convert));
    // Produce an endless stream of results - this means if the behaviour is not lazy this will never terminate
    when(query.stream()).thenAnswer(params -> Stream.generate(answers::stream).flatMap(Function.identity()));
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.receive();
        tx.send(execQueryRequest(QUERY, null));
        IteratorId iterator = tx.receive().ok().getIteratorId();
        tx.send(nextRequest(iterator));
        tx.receive().ok();
        tx.send(nextRequest(iterator));
        tx.receive().ok();
        tx.send(stopRequest(iterator));
        TxResponse response = tx.receive().ok();
        assertEquals(doneResponse(), response);
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 60 with Concept

use of ai.grakn.concept.Concept 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

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