Search in sources :

Example 1 with EmbeddedGraknTx

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();
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) GreedyTraversalPlan(ai.grakn.graql.internal.gremlin.GreedyTraversalPlan) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom) HashMultimap(com.google.common.collect.HashMultimap) ImmutableList(com.google.common.collect.ImmutableList) ConceptId(ai.grakn.concept.ConceptId) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Nullable(javax.annotation.Nullable) Patterns(ai.grakn.graql.internal.pattern.Patterns) Conjunction(ai.grakn.graql.admin.Conjunction) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) VarProperty(ai.grakn.graql.admin.VarProperty) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Comparator(java.util.Comparator) VarProperty(ai.grakn.graql.admin.VarProperty) ImmutableList(com.google.common.collect.ImmutableList) Objects(java.util.Objects) GraqlTraversal(ai.grakn.graql.internal.gremlin.GraqlTraversal) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom) OntologicalAtom(ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom)

Example 2 with EmbeddedGraknTx

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);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Iterables(com.google.common.collect.Iterables) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) ReasonerQueryEquivalence(ai.grakn.graql.internal.reasoner.query.ReasonerQueryEquivalence) ArrayList(java.util.ArrayList) GraqlTestUtil.assertExists(ai.grakn.util.GraqlTestUtil.assertExists) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ClassRule(org.junit.ClassRule) ExpectedException(org.junit.rules.ExpectedException) Collectors.toSet(java.util.stream.Collectors.toSet) QueryBuilder(ai.grakn.graql.QueryBuilder) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Equivalence(com.google.common.base.Equivalence) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) List(java.util.List) Rule(org.junit.Rule) GeoKB(ai.grakn.test.kbs.GeoKB) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) GraqlTestUtil.assertNotExists(ai.grakn.util.GraqlTestUtil.assertNotExists) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Schema(ai.grakn.util.Schema) GetQuery(ai.grakn.graql.GetQuery) QueryBuilder(ai.grakn.graql.QueryBuilder) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) Test(org.junit.Test)

Example 3 with EmbeddedGraknTx

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()));
}
Also used : Iterables(com.google.common.collect.Iterables) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) GraqlTestUtil.assertCollectionsEqual(ai.grakn.util.GraqlTestUtil.assertCollectionsEqual) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Assert.assertThat(org.junit.Assert.assertThat) HashMultimap(com.google.common.collect.HashMultimap) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) ClassRule(org.junit.ClassRule) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Patterns(ai.grakn.graql.internal.pattern.Patterns) Graql.var(ai.grakn.graql.Graql.var) Matchers.empty(org.hamcrest.Matchers.empty) Conjunction(ai.grakn.graql.admin.Conjunction) ImmutableMap(com.google.common.collect.ImmutableMap) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) GetQuery(ai.grakn.graql.GetQuery) Sets(com.google.common.collect.Sets) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) ReasonerQueries(ai.grakn.graql.internal.reasoner.query.ReasonerQueries) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) SampleKBContext(ai.grakn.test.rule.SampleKBContext) Assume.assumeTrue(org.junit.Assume.assumeTrue) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Schema(ai.grakn.util.Schema) Unifier(ai.grakn.graql.admin.Unifier) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) Assert.assertEquals(org.junit.Assert.assertEquals) Answer(ai.grakn.graql.admin.Answer) ReasonerAtomicQuery(ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) ResourceAtom(ai.grakn.graql.internal.reasoner.atom.binary.ResourceAtom) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Unifier(ai.grakn.graql.admin.Unifier) Test(org.junit.Test)

Example 4 with EmbeddedGraknTx

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);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) SelectStep(org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectStep) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) ConceptId(ai.grakn.concept.ConceptId) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Collectors.joining(java.util.stream.Collectors.joining) Match(ai.grakn.graql.Match) List(java.util.List) Element(org.apache.tinkerpop.gremlin.structure.Element) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) Schema(ai.grakn.util.Schema) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment)

Example 5 with EmbeddedGraknTx

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();
}
Also used : Graph(org.apache.tinkerpop.gremlin.structure.Graph) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Test(org.junit.Test)

Aggregations

EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)12 Set (java.util.Set)7 Answer (ai.grakn.graql.admin.Answer)6 Conjunction (ai.grakn.graql.admin.Conjunction)6 Schema (ai.grakn.util.Schema)6 Sets (com.google.common.collect.Sets)6 Test (org.junit.Test)6 Concept (ai.grakn.concept.Concept)5 ConceptId (ai.grakn.concept.ConceptId)5 Var (ai.grakn.graql.Var)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 Collectors.toSet (java.util.stream.Collectors.toSet)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Unifier (ai.grakn.graql.admin.Unifier)4 Patterns (ai.grakn.graql.internal.pattern.Patterns)4 GraknTestUtil (ai.grakn.util.GraknTestUtil)4 Iterables (com.google.common.collect.Iterables)4