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);
}
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;
}
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());
}
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);
}
}
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;
};
}
Aggregations