use of ai.grakn.graql.GetQuery 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.GetQuery 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.GetQuery in project grakn by graknlabs.
the class AtomicTest method testUnification_ParentHasFewerRelationPlayers.
@Test
public void testUnification_ParentHasFewerRelationPlayers() {
EmbeddedGraknTx<?> graph = unificationTestSet.tx();
String childString = "{(subRole1: $y, subRole2: $x) isa binary;}";
String parentString = "{(subRole1: $x) isa binary;}";
String parentString2 = "{(subRole2: $y) isa binary;}";
ReasonerAtomicQuery childQuery = ReasonerQueries.atomic(conjunction(childString, graph), graph);
ReasonerAtomicQuery parentQuery = ReasonerQueries.atomic(conjunction(parentString, graph), graph);
ReasonerAtomicQuery parentQuery2 = ReasonerQueries.atomic(conjunction(parentString2, graph), graph);
Atom childAtom = childQuery.getAtom();
Atom parentAtom = parentQuery.getAtom();
Atom parentAtom2 = parentQuery2.getAtom();
List<Answer> childAnswers = childQuery.getQuery().execute();
List<Answer> parentAnswers = parentQuery.getQuery().execute();
List<Answer> parentAnswers2 = parentQuery2.getQuery().execute();
Unifier unifier = childAtom.getUnifier(parentAtom);
Unifier unifier2 = childAtom.getUnifier(parentAtom2);
assertCollectionsEqual(parentAnswers, childAnswers.stream().map(a -> a.unify(unifier)).map(a -> a.project(parentQuery.getVarNames())).distinct().collect(Collectors.toList()));
assertCollectionsEqual(parentAnswers2, childAnswers.stream().map(a -> a.unify(unifier2)).map(a -> a.project(parentQuery2.getVarNames())).distinct().collect(Collectors.toList()));
}
use of ai.grakn.graql.GetQuery in project grakn by graknlabs.
the class ReasonerTest method testReasoningWithQueryContainingPlays.
@Test
public void testReasoningWithQueryContainingPlays() {
GraknTx graph = nonMaterialisedGeoKB.tx();
String queryString = "match " + "$x isa $type;$type plays geo-entity;" + "$y isa country;$y has name 'Poland';" + "($x, $y) isa is-located-in; get;";
String explicitQuery = "match $y has name 'Poland';$x isa $type;$x has " + Schema.MetaSchema.ATTRIBUTE.getLabel().getValue() + " $name;" + "{" + "{$name val 'Europe';};" + "{$type label 'continent' or $type label 'geoObject';};" + "} or {" + "{$name val 'Warsaw-Polytechnics' or $name val 'University-of-Warsaw';};" + "{$type label 'university';};" + "} or {" + "{$name val 'Warsaw' or $name val 'Wroclaw';};" + "{$type label 'city' or $type label 'geoObject';};" + "} or {" + "{$name val 'Masovia' or $name val 'Silesia';};" + "{$type label 'region' or $type label 'geoObject';};" + "}; get $x, $y, $type;";
GetQuery query = graph.graql().infer(true).parse(queryString);
GetQuery query2 = graph.graql().infer(false).parse(explicitQuery);
assertQueriesEqual(query, query2);
}
use of ai.grakn.graql.GetQuery in project grakn by graknlabs.
the class ReasonerTest method testReasoningWithQueryContainingSub2.
@Test
public void testReasoningWithQueryContainingSub2() {
String queryString = "match $x isa person;$y isa $type;$type sub recommendable;($x, $y) isa recommendation; get;";
String explicitQuery = "match $x isa person, has name $xName;$y isa $type;$y has name $yName;" + "{$type label 'recommendable' or $type label 'product' or $type label 'tag';};" + "{$xName val 'Alice';$yName val 'War of the Worlds';} or" + "{$xName val 'Bob';{$yName val 'Ducatti 1299';} or {$yName val 'The Good the Bad the Ugly';};} or" + "{$xName val 'Charlie';{$yName val 'Blizzard of Ozz';} or {$yName val 'Stratocaster';};} or " + "{$xName val 'Denis';{$yName val 'Colour of Magic';} or {$yName val 'Dorian Gray';};} or" + "{$xName val 'Frank';$yName val 'Nocturnes';} or" + "{$xName val 'Karl Fischer';{$yName val 'Faust';} or {$yName val 'Nocturnes';};} or " + "{$xName val 'Gary';$yName val 'The Wall';} or" + "{$xName val 'Charlie';" + "{$yName val 'Yngwie Malmsteen';} or {$yName val 'Cacophony';} or {$yName val 'Steve Vai';} or {$yName val 'Black Sabbath';};} or " + "{$xName val 'Gary';$yName val 'Pink Floyd';};get $x, $y, $type;";
GetQuery query = snbKB.tx().graql().infer(true).parse(queryString);
GetQuery query2 = snbKB.tx().graql().infer(false).parse(explicitQuery);
assertQueriesEqual(query, query2);
}
Aggregations