Search in sources :

Example 1 with PatternAdmin

use of ai.grakn.graql.admin.PatternAdmin 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 PatternAdmin

use of ai.grakn.graql.admin.PatternAdmin in project grakn by graknlabs.

the class QueryParserTest method whenParsingAQueryAndDefiningAllVars_AllVarsExceptLabelsAreDefined.

@Test
public void whenParsingAQueryAndDefiningAllVars_AllVarsExceptLabelsAreDefined() {
    QueryParser parser = Graql.parser();
    parser.defineAllVars(true);
    GetQuery query = parser.parseQuery("match ($x, $y) isa foo; get;");
    System.out.println(query);
    Conjunction<PatternAdmin> conjunction = query.match().admin().getPattern();
    Set<PatternAdmin> patterns = conjunction.getPatterns();
    VarPatternAdmin pattern = Iterables.getOnlyElement(patterns).asVarPattern();
    assertTrue(pattern.var().isUserDefinedName());
    IsaProperty property = pattern.getProperty(IsaProperty.class).get();
    assertFalse(property.type().var().isUserDefinedName());
}
Also used : QueryParser(ai.grakn.graql.QueryParser) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) GetQuery(ai.grakn.graql.GetQuery) PatternAdmin(ai.grakn.graql.admin.PatternAdmin) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) Test(org.junit.Test)

Example 3 with PatternAdmin

use of ai.grakn.graql.admin.PatternAdmin in project grakn by graknlabs.

the class QueryBuilderImpl method match.

/**
 * @param patterns a collection of patterns to match in the knowledge base
 * @return a {@link Match} that will find matches of the given patterns
 */
@Override
public Match match(Collection<? extends Pattern> patterns) {
    Conjunction<PatternAdmin> conjunction = Patterns.conjunction(Sets.newHashSet(AdminConverter.getPatternAdmins(patterns)));
    MatchBase base = new MatchBase(conjunction);
    Match match = infer ? base.infer(materialise).admin() : base;
    return tx.map(match::withTx).orElse(match);
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) MatchBase(ai.grakn.graql.internal.query.match.MatchBase) Match(ai.grakn.graql.Match)

Example 4 with PatternAdmin

use of ai.grakn.graql.admin.PatternAdmin in project grakn by graknlabs.

the class AdminTest method testMutateMatch.

@Test
public void testMutateMatch() {
    Match match = qb.match(var("x").isa("movie"));
    Conjunction<PatternAdmin> pattern = match.admin().getPattern();
    pattern.getPatterns().add(var("x").has("title", "Spy").admin());
    assertEquals(1, match.stream().count());
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Example 5 with PatternAdmin

use of ai.grakn.graql.admin.PatternAdmin in project grakn by graknlabs.

the class AdminTest method testGetPatternInQuery.

@Test
public void testGetPatternInQuery() {
    Match match = qb.match(var("x").isa("movie"), var("x").val("Bob"));
    Conjunction<PatternAdmin> conjunction = match.admin().getPattern();
    assertNotNull(conjunction);
    Set<PatternAdmin> patterns = conjunction.getPatterns();
    assertEquals(2, patterns.size());
}
Also used : PatternAdmin(ai.grakn.graql.admin.PatternAdmin) Match(ai.grakn.graql.Match) Test(org.junit.Test)

Aggregations

PatternAdmin (ai.grakn.graql.admin.PatternAdmin)5 Match (ai.grakn.graql.Match)3 Test (org.junit.Test)3 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)2 ConceptId (ai.grakn.concept.ConceptId)1 GetQuery (ai.grakn.graql.GetQuery)1 QueryParser (ai.grakn.graql.QueryParser)1 Atomic (ai.grakn.graql.admin.Atomic)1 Conjunction (ai.grakn.graql.admin.Conjunction)1 VarProperty (ai.grakn.graql.admin.VarProperty)1 GraqlTraversal (ai.grakn.graql.internal.gremlin.GraqlTraversal)1 GreedyTraversalPlan (ai.grakn.graql.internal.gremlin.GreedyTraversalPlan)1 Fragment (ai.grakn.graql.internal.gremlin.fragment.Fragment)1 Patterns (ai.grakn.graql.internal.pattern.Patterns)1 IsaProperty (ai.grakn.graql.internal.pattern.property.IsaProperty)1 MatchBase (ai.grakn.graql.internal.query.match.MatchBase)1 Atom (ai.grakn.graql.internal.reasoner.atom.Atom)1 OntologicalAtom (ai.grakn.graql.internal.reasoner.atom.binary.OntologicalAtom)1 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)1 ReasonerQueryImpl (ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)1