use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.
the class GraqlTraversalPlanner method planFromTraversal.
/**
* @param atoms list of current atoms of interest
* @param queryPattern corresponding pattern
* @return an optimally ordered list of provided atoms
*/
static ImmutableList<Atom> planFromTraversal(List<Atom> atoms, PatternAdmin queryPattern, EmbeddedGraknTx<?> tx) {
Multimap<VarProperty, Atom> propertyMap = HashMultimap.create();
atoms.stream().filter(at -> !(at instanceof OntologicalAtom)).forEach(at -> at.getVarProperties().forEach(p -> propertyMap.put(p, at)));
Set<VarProperty> properties = propertyMap.keySet();
GraqlTraversal graqlTraversal = GreedyTraversalPlan.createTraversal(queryPattern, tx);
ImmutableList<Fragment> fragments = graqlTraversal.fragments().iterator().next();
ImmutableList.Builder<Atom> builder = ImmutableList.builder();
builder.addAll(atoms.stream().filter(at -> at instanceof OntologicalAtom).iterator());
builder.addAll(fragments.stream().map(Fragment::varProperty).filter(Objects::nonNull).filter(properties::contains).distinct().flatMap(p -> propertyMap.get(p).stream()).distinct().iterator());
return builder.build();
}
use of ai.grakn.kb.internal.EmbeddedGraknTx 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.kb.internal.EmbeddedGraknTx 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.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.
the class GraqlTraversal method getGraphTraversal.
/**
* Get the {@code GraphTraversal} that this {@code GraqlTraversal} represents.
*/
// Because 'union' accepts an array, we can't use generics
@SuppressWarnings("unchecked")
public GraphTraversal<Vertex, Map<String, Element>> getGraphTraversal(EmbeddedGraknTx<?> tx, Set<Var> vars) {
if (fragments().size() == 1) {
// If there are no disjunctions, we don't need to union them and get a performance boost
ImmutableList<Fragment> list = Iterables.getOnlyElement(fragments());
return getConjunctionTraversal(tx, tx.getTinkerTraversal().V(), vars, list);
} else {
Traversal[] traversals = fragments().stream().map(list -> getConjunctionTraversal(tx, __.V(), vars, list)).toArray(Traversal[]::new);
// This is a sneaky trick - we want to do a union but tinkerpop requires all traversals to start from
// somewhere, so we start from a single arbitrary vertex.
GraphTraversal traversal = tx.getTinkerTraversal().V().limit(1).union(traversals);
return selectVars(traversal, vars);
}
}
use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.
the class GraknTest method testSingletonBetweenBatchAndNormalInMemory.
@Test
public void testSingletonBetweenBatchAndNormalInMemory() {
String keyspace = "test1";
EmbeddedGraknTx<?> graph = (EmbeddedGraknTx<?>) Grakn.session(Grakn.IN_MEMORY, keyspace).open(GraknTxType.WRITE);
Graph tinkerGraph = graph.getTinkerPopGraph();
graph.close();
EmbeddedGraknTx<?> batchGraph = (EmbeddedGraknTx<?>) Grakn.session(Grakn.IN_MEMORY, keyspace).open(GraknTxType.BATCH);
assertNotEquals(graph, batchGraph);
assertEquals(tinkerGraph, batchGraph.getTinkerPopGraph());
graph.close();
batchGraph.close();
}
Aggregations