use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.
the class AtomicQueryTest method testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly.
@Test
public void testWhenRoleTypesAreAmbiguous_answersArePermutedCorrectly() {
EmbeddedGraknTx<?> graph = geoKB.tx();
String childString = "match (geo-entity: $x, entity-location: $y) isa is-located-in; get;";
String parentString = "match ($x, $y) isa is-located-in; get;";
QueryBuilder qb = graph.graql().infer(false);
GetQuery childQuery = qb.parse(childString);
GetQuery parentQuery = qb.parse(parentString);
Set<Answer> answers = childQuery.stream().collect(toSet());
Set<Answer> fullAnswers = parentQuery.stream().collect(toSet());
Atom childAtom = ReasonerQueries.atomic(conjunction(childQuery.match().admin().getPattern()), graph).getAtom();
Atom parentAtom = ReasonerQueries.atomic(conjunction(parentQuery.match().admin().getPattern()), graph).getAtom();
MultiUnifier multiUnifier = childAtom.getMultiUnifier(childAtom, UnifierType.RULE);
Set<Answer> permutedAnswers = answers.stream().flatMap(a -> multiUnifier.stream().map(a::unify)).collect(Collectors.toSet());
MultiUnifier multiUnifier2 = childAtom.getMultiUnifier(parentAtom, UnifierType.RULE);
Set<Answer> permutedAnswers2 = answers.stream().flatMap(a -> multiUnifier2.stream().map(a::unify)).collect(Collectors.toSet());
assertEquals(fullAnswers, permutedAnswers2);
assertEquals(answers, permutedAnswers);
}
use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.
the class AtomicQueryTest method testWhenMaterialisingRelations_MaterialisedInformationIsCorrectlyFlaggedAsInferred.
@Test
public void testWhenMaterialisingRelations_MaterialisedInformationIsCorrectlyFlaggedAsInferred() {
EmbeddedGraknTx<?> graph = materialisationTestSet.tx();
QueryBuilder qb = graph.graql().infer(false);
Concept firstEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity1; get;").execute()).get("x");
Concept secondEntity = Iterables.getOnlyElement(qb.<GetQuery>parse("match $x isa entity2; get;").execute()).get("x");
ReasonerAtomicQuery relationQuery = ReasonerQueries.atomic(conjunction("{" + "$r (role1: $x, role2: $y);" + "$x id " + firstEntity.getId().getValue() + ";" + "$y id " + secondEntity.getId().getValue() + ";" + "}", graph), graph);
assertEquals(relationQuery.materialise(new QueryAnswer()).findFirst().orElse(null).get("r").asRelationship().isInferred(), true);
}
use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.
the class BenchmarkTests method testDiagonal.
/**
* single-rule mimicking transitivity test rule defined by two-hop relations
* Initial data arranged in N x N square grid.
*
* Rule:
* (rel-from:$x, rel-to:$y) isa horizontal;
* (rel-from:$y, rel-to:$z) isa horizontal;
* (rel-from:$z, rel-to:$u) isa vertical;
* (rel-from:$u, rel-to:$v) isa vertical;
* ->
* (rel-from:$x, rel-to:$v) isa diagonal;
*
* Initial data arranged as follows:
*
* a_{i , j} - horizontal - a_{i, j + 1}
* | |
* vertical vertical
* | |
* a_{i+1, j} - horizontal - a_{i+1, j+1}
*
* i e [0, N)
* j e [0, N)
*/
@Test
public void testDiagonal() {
// 9604
final int N = 10;
final int limit = 10;
LOG.debug(new Object() {
}.getClass().getEnclosingMethod().getName());
// results @N = 40 1444 3.5s
// results @N = 50 2304 8s / 1s
// results @N = 100 9604 loading takes ages
SampleKBContext kb = DiagonalKB.context(N, N);
QueryBuilder iqb = kb.tx().graql().infer(true).materialise(false);
String queryString = "match (rel-from: $x, rel-to: $y) isa diagonal; get;";
GetQuery query = iqb.parse(queryString);
executeQuery(query, "full");
executeQuery(query.match().limit(limit).get(), "limit " + limit);
}
use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.
the class OntologicalQueryTest method allRolesGivenRelationRelates.
@Test
public void allRolesGivenRelationRelates() {
GraknTx tx = testContext.tx();
QueryBuilder qb = tx.graql().infer(true);
String queryString = "match reifying-relation relates $x; get;";
List<Answer> answers = qb.<GetQuery>parse(queryString).execute();
assertEquals(answers.stream().map(ans -> ans.get("x")).collect(Collectors.toSet()), tx.getRelationshipType("reifying-relation").relates().collect(Collectors.toSet()));
}
use of ai.grakn.graql.QueryBuilder in project grakn by graknlabs.
the class ReasonerTest method testReasoningWithQueryContainingSub.
@Test
public void testReasoningWithQueryContainingSub() {
GraknTx graph = nonMaterialisedGeoKB.tx();
String queryString = "match " + "$x isa $type;$type sub geoObject;" + "(geo-entity: $x, entity-location: $y) isa is-located-in; $y isa country;" + "$y has name 'Poland';" + "$x has name $name; get;";
String queryString2 = "match " + "$x isa $type;{$type label 'region';} or {$type label 'city';} or {$type label 'geoObject';};" + "(geo-entity: $x, entity-location: $y) isa is-located-in;$y isa country;" + "$y has name 'Poland';" + "$x has name $name; get;";
QueryBuilder iqb = graph.graql().infer(true);
GetQuery query = iqb.parse(queryString);
GetQuery query2 = iqb.parse(queryString2);
assertQueriesEqual(query, query2);
}
Aggregations