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();
}
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());
}
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);
}
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());
}
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());
}
Aggregations