use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class ShortestPathTest method testMultiplePathsSharing1Instance.
@Test
public void testMultiplePathsSharing1Instance() throws InvalidKBException {
ConceptId startId;
ConceptId endId;
Set<List<ConceptId>> correctPaths = new HashSet<>();
try (GraknTx graph = session.open(GraknTxType.WRITE)) {
EntityType entityType = graph.putEntityType(thing);
Role role1 = graph.putRole("role1");
Role role2 = graph.putRole("role2");
entityType.plays(role1).plays(role2);
RelationshipType relationshipType1 = graph.putRelationshipType(related).relates(role1).relates(role2);
Role role3 = graph.putRole("role3");
Role role4 = graph.putRole("role4");
entityType.plays(role3).plays(role4);
RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4);
Entity start = entityType.addEntity();
Entity end = entityType.addEntity();
Entity middle = entityType.addEntity();
startId = start.getId();
endId = end.getId();
ConceptId middleId = middle.getId();
ConceptId assertion11 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
ConceptId assertion12 = relationshipType1.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
ConceptId assertion21 = relationshipType2.addRelationship().addRolePlayer(role3, start).addRolePlayer(role4, middle).getId();
ConceptId assertion22 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, end).getId();
correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion12, endId));
correctPaths.add(Lists.newArrayList(startId, assertion11, middleId, assertion22, endId));
correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion12, endId));
correctPaths.add(Lists.newArrayList(startId, assertion21, middleId, assertion22, endId));
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
assertEquals(correctPaths.size(), allPaths.size());
Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
assertEquals(correctPaths, computedPaths);
}
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class ShortestPathTest method testMultiplePathInSubGraph.
@Test
public void testMultiplePathInSubGraph() {
Set<List<ConceptId>> correctPaths = new HashSet<>();
List<List<Concept>> allPaths;
addSchemaAndEntities();
try (GraknTx graph = session.open(GraknTxType.READ)) {
if (GraknTestUtil.usingJanus()) {
// If no path is found in the vertex program, NoResultException is thrown to skip map reduce.
// Tinker doesn't handle this well, so the next graql query would find the graph is empty.
allPaths = graph.graql().compute().paths().in(thing, anotherThing).to(entityId1).from(entityId4).execute();
assertEquals(0, allPaths.size());
}
correctPaths.add(Lists.newArrayList(entityId1, relationId12, entityId2, relationId24, entityId4));
correctPaths.add(Lists.newArrayList(entityId1, relationId13, entityId3, relationId34, entityId4));
allPaths = graph.graql().compute().paths().from(entityId1).to(entityId4).execute();
assertEquals(correctPaths.size(), allPaths.size());
Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
assertEquals(correctPaths, computedPaths);
}
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class GrpcClient method getAttributesByValue.
public Stream<? extends Concept> getAttributesByValue(Object value) {
communicator.send(GrpcUtil.getAttributesByValueRequest(value));
IteratorId iteratorId = responseOrThrow().getIteratorId();
Iterable<Concept> iterable = () -> new GraknGrpcIterator<Concept>(this, iteratorId) {
@Override
protected Concept getNextFromResponse(TxResponse response) {
return conceptConverter.convert(response.getConcept());
}
};
return StreamSupport.stream(iterable.spliterator(), false);
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class GeoInferenceTest method testTransitiveQuery_withSpecificResource_noRoles.
@Test
public void testTransitiveQuery_withSpecificResource_noRoles() {
GraknTx graph = geoKB.tx();
QueryBuilder iqb = graph.graql().infer(true);
Concept masovia = getConcept(graph, "name", "Masovia");
String queryString = "match " + "($x, $y) isa is-located-in;" + "$y has name 'Masovia'; get;";
String queryString2 = "match " + "{(geo-entity: $x, entity-location: $y) isa is-located-in or " + "(geo-entity: $y, entity-location: $x) isa is-located-in;};" + "$y has name 'Masovia'; get;";
List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
answers.forEach(ans -> assertEquals(ans.size(), 2));
answers.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), masovia.getId().getValue()));
assertEquals(answers.size(), 5);
List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute();
assertCollectionsEqual(answers, answers2);
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class GeoInferenceTest method testTransitiveQuery_withSubstitution.
@Test
public void testTransitiveQuery_withSubstitution() {
GraknTx graph = geoKB.tx();
QueryBuilder iqb = graph.graql().infer(true);
Concept poland = getConcept(graph, "name", "Poland");
Concept europe = getConcept(graph, "name", "Europe");
String queryString = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y id '" + poland.getId().getValue() + "'; get;";
String queryString2 = "match " + "(geo-entity: $x, entity-location: $y) isa is-located-in;" + "$y id '" + europe.getId().getValue() + "'; get;";
List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
answers.forEach(ans -> assertEquals(ans.size(), 2));
answers.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), poland.getId().getValue()));
assertEquals(answers.size(), 6);
List<Answer> answers2 = iqb.materialise(false).<GetQuery>parse(queryString2).execute();
answers2.forEach(ans -> assertEquals(ans.size(), 2));
answers2.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), europe.getId().getValue()));
assertEquals(answers2.size(), 21);
}
Aggregations