use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class ShortestPathTest method testMultipleIndependentShortestPaths.
@Test
public void testMultipleIndependentShortestPaths() throws InvalidKBException {
Set<List<ConceptId>> validPaths = new HashSet<>();
ConceptId startId;
ConceptId endId;
int numberOfPaths = 3;
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 relationshipType = graph.putRelationshipType(related).relates(role1).relates(role2);
Entity start = entityType.addEntity();
Entity end = entityType.addEntity();
startId = start.getId();
endId = end.getId();
// create N identical length paths
for (int i = 0; i < numberOfPaths; i++) {
List<ConceptId> validPath = new ArrayList<>();
validPath.add(startId);
Entity middle = entityType.addEntity();
ConceptId middleId = middle.getId();
ConceptId assertion1 = relationshipType.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
validPath.add(assertion1);
validPath.add(middleId);
ConceptId assertion2 = relationshipType.addRelationship().addRolePlayer(role1, middle).addRolePlayer(role2, end).getId();
validPath.add(assertion2);
validPath.add(endId);
validPaths.add(validPath);
}
graph.commit();
}
try (GraknTx graph = session.open(GraknTxType.READ)) {
List<List<Concept>> allPaths = graph.graql().compute().paths().from(startId).to(endId).execute();
assertEquals(numberOfPaths, allPaths.size());
Set<List<ConceptId>> computedPaths = allPaths.stream().map(path -> path.stream().map(Concept::getId).collect(Collectors.toList())).collect(Collectors.toSet());
assertEquals(validPaths, computedPaths);
}
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class ShortestPathTest method testMultiplePathsSharing3Instances.
@Test
public void testMultiplePathsSharing3Instances() 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");
Role role5 = graph.putRole("role5");
entityType.plays(role3).plays(role4).plays(role5);
RelationshipType relationshipType2 = graph.putRelationshipType(veryRelated).relates(role3).relates(role4).relates(role5);
Entity start = entityType.addEntity();
Entity end = entityType.addEntity();
Entity middle = entityType.addEntity();
Entity middleA = entityType.addEntity();
Entity middleB = entityType.addEntity();
startId = start.getId();
endId = end.getId();
ConceptId middleId = middle.getId();
ConceptId middleAId = middleA.getId();
ConceptId middleBId = middleB.getId();
ConceptId assertion1 = relationshipType1.addRelationship().addRolePlayer(role1, start).addRolePlayer(role2, middle).getId();
ConceptId assertion2 = relationshipType2.addRelationship().addRolePlayer(role3, middle).addRolePlayer(role4, middleA).addRolePlayer(role5, middleB).getId();
ConceptId assertion1A = relationshipType1.addRelationship().addRolePlayer(role1, middleA).addRolePlayer(role2, end).getId();
ConceptId assertion1B = relationshipType1.addRelationship().addRolePlayer(role1, middleB).addRolePlayer(role2, end).getId();
List<ConceptId> sharedPath = Lists.newArrayList(startId, assertion1, middleId, assertion2);
List<ConceptId> path1 = new ArrayList<>(sharedPath);
path1.addAll(Lists.newArrayList(middleAId, assertion1A, endId));
List<ConceptId> path2 = new ArrayList<>(sharedPath);
path2.addAll(Lists.newArrayList(middleBId, assertion1B, endId));
correctPaths.add(path1);
correctPaths.add(path2);
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 QueryParserTest method testCustomAggregate.
@Test
public void testCustomAggregate() {
QueryBuilder qb = withoutGraph();
QueryParser parser = qb.parser();
parser.registerAggregate("get-any", args -> new GetAny((Var) args.get(0)));
AggregateQuery<Concept> expected = qb.match(var("x").isa("movie")).aggregate(new GetAny(Graql.var("x")));
AggregateQuery<Concept> parsed = qb.parse("match $x isa movie; aggregate get-any $x;");
assertEquals(expected, parsed);
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class GeoInferenceTest method testTransitiveQuery_withSubstitution_variableRoles.
@Test
public void testTransitiveQuery_withSubstitution_variableRoles() {
GraknTx graph = geoKB.tx();
QueryBuilder iqb = geoKB.tx().graql().infer(true);
Concept masovia = getConcept(graph, "name", "Masovia");
String queryString = "match " + "($r1: $x, $r2: $y) isa is-located-in;" + "$y id '" + masovia.getId().getValue() + "'; get;";
List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
List<Answer> answers2 = iqb.materialise(true).<GetQuery>parse(queryString).execute();
answers.forEach(ans -> assertEquals(ans.size(), 4));
answers.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), masovia.getId().getValue()));
answers2.forEach(ans -> assertEquals(ans.size(), 4));
answers2.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), masovia.getId().getValue()));
assertEquals(answers.size(), 20);
assertCollectionsEqual(answers, answers2);
}
use of ai.grakn.concept.Concept in project grakn by graknlabs.
the class GeoInferenceTest method testTransitiveQuery_Closure_singleVariableRole_withSubstitution.
@Test
public void testTransitiveQuery_Closure_singleVariableRole_withSubstitution() {
GraknTx graph = geoKB.tx();
QueryBuilder iqb = geoKB.tx().graql().infer(true);
Concept masovia = getConcept(graph, "name", "Masovia");
String queryString = "match " + "($x, $r2: $y) isa is-located-in;" + "$y id '" + masovia.getId().getValue() + "'; get;";
List<Answer> answers = iqb.materialise(false).<GetQuery>parse(queryString).execute();
List<Answer> answers2 = iqb.materialise(true).<GetQuery>parse(queryString).execute();
answers.forEach(ans -> assertEquals(ans.size(), 3));
answers.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), masovia.getId().getValue()));
assertEquals(answers.size(), 10);
answers2.forEach(ans -> assertEquals(ans.size(), 3));
answers2.forEach(ans -> assertEquals(ans.get(var("y")).getId().getValue(), masovia.getId().getValue()));
assertCollectionsEqual(answers, answers2);
}
Aggregations