/** * Helper method to get the labels of the type in the query scope * * @return a set of Concept Type Labels */ private ImmutableSet<Label> scopeTypeLabels() { return scopeTypes().map(SchemaConcept::label).collect(CommonUtil.toImmutableSet()); }
@Override public final ComputeQuery<T> of(String type, String... types) { ArrayList<String> typeList = new ArrayList<>(types.length + 1); typeList.add(type); typeList.addAll(Arrays.asList(types)); return of(typeList.stream().map(Label::of).collect(toImmutableSet())); }
@Override public final ComputeQuery<T> in(String type, String... types) { ArrayList<String> typeList = new ArrayList<>(types.length + 1); typeList.add(type); typeList.addAll(Arrays.asList(types)); return in(typeList.stream().map(Label::of).collect(toImmutableSet())); }
static GraqlTraversal create(Set<? extends List<Fragment>> fragments) { ImmutableSet<ImmutableList<Fragment>> copy = fragments.stream().map(ImmutableList::copyOf).collect(toImmutableSet()); return new AutoValue_GraqlTraversal(copy); }
/** * Helper method to get the labels of the type in the query target * * @return a set of type Labels */ private Set<Label> targetTypeLabels() { return targetTypes().stream() .map(SchemaConcept::label) .collect(CommonUtil.toImmutableSet()); }
@Override public <V> Collection<Attribute<V>> getAttributesByValue(V value) { return client().getAttributesByValue(value).map(Concept::<V>asAttribute).collect(toImmutableSet()); }
private Stream<ConceptMap> runMatchInsert(Match match, Collection<VarPatternAdmin> varPatterns) { Set<Var> varsInMatch = match.admin().getSelectedNames(); Set<Var> varsInInsert = varPatterns.stream().map(VarPatternAdmin::var).collect(toImmutableSet()); Set<Var> projectedVars = Sets.intersection(varsInMatch, varsInInsert); Stream<ConceptMap> answers = match.get(projectedVars).stream(); return answers.map(answer -> QueryOperationExecutor.insertAll(varPatterns, tx, answer)).collect(toList()).stream(); }
private Set<Var> requiredVars(Var var) { Stream<Var> relationPlayers = this.relationPlayers().stream() .flatMap(relationPlayer -> Stream.of(relationPlayer.getRolePlayer(), getRole(relationPlayer))) .map(VarPatternAdmin::var); return Stream.concat(relationPlayers, Stream.of(var)).collect(toImmutableSet()); }
@Override public Collection<EquivalentFragmentSet> match(Var start) { Collection<Var> castingNames = new HashSet<>(); ImmutableSet<EquivalentFragmentSet> traversals = relationPlayers().stream().flatMap(relationPlayer -> { Var castingName = Graql.var(); castingNames.add(castingName); return equivalentFragmentSetFromCasting(start, castingName, relationPlayer); }).collect(toImmutableSet()); ImmutableSet<EquivalentFragmentSet> distinctCastingTraversals = castingNames.stream().flatMap( castingName -> castingNames.stream() .filter(otherName -> !otherName.equals(castingName)) .map(otherName -> EquivalentFragmentSets.neq(this, castingName, otherName)) ).collect(toImmutableSet()); return Sets.union(traversals, distinctCastingTraversals); }
private RolePlayerFragmentSet substituteLabels(Set<Role> roles, Set<RelationshipType> relTypes){ ImmutableSet<Label> newRoleTypeLabels = relTypes != null? relTypes.stream().flatMap(RelationshipType::subs).map(SchemaConcept::label).collect(toImmutableSet()) : null; ImmutableSet<Label> newRoleLabels = roles != null? roles.stream().flatMap(Role::subs).map(SchemaConcept::label).collect(toImmutableSet()) : null; return new AutoValue_RolePlayerFragmentSet( varProperty(), relation(), edge(), rolePlayer(), null, newRoleLabels!= null? newRoleLabels : roleLabels(), newRoleTypeLabels != null? newRoleTypeLabels : relationshipTypeLabels() ); }
/** * @param types the graph to find types in * @param query a graql query * @param token the token the cursor is on in the query * @return a set of potential autocomplete words */ private static ImmutableSet<String> findCandidates(Set<String> types, String query, Token token) { ImmutableSet<String> allCandidates = Stream.of(GRAQL_KEYWORDS.stream(), types.stream(), getVariables(query)) .flatMap(Function.identity()).collect(toImmutableSet()); if (token != null) { ImmutableSet<String> candidates = allCandidates.stream() .filter(candidate -> candidate.startsWith(token.getText())) .collect(toImmutableSet()); if (candidates.size() == 1 && candidates.iterator().next().equals(token.getText())) { return ImmutableSet.of(" "); } else { return candidates; } } else { return allCandidates; } }
/** * Create a traversal plan. * * @param pattern a pattern to find a query plan for * @return a semi-optimal traversal plan */ public static GraqlTraversal createTraversal(PatternAdmin pattern, EmbeddedGraknTx<?> tx) { Collection<Conjunction<VarPatternAdmin>> patterns = pattern.getDisjunctiveNormalForm().getPatterns(); Set<? extends List<Fragment>> fragments = patterns.stream() .map(conjunction -> new ConjunctionQuery(conjunction, tx)) .map((ConjunctionQuery query) -> planForConjunction(query, tx)) .collect(toImmutableSet()); return GraqlTraversal.create(fragments); }
vars.stream().flatMap(ConjunctionQuery::equivalentFragmentSetsRecursive).collect(toImmutableSet()); .filter(fragment -> !fragment.isStartingFragment()) .flatMap(fragment -> fragment.vars().stream()) .collect(toImmutableSet()); .flatMap(EquivalentFragmentSet::stream) .flatMap(fragment -> fragment.dependencies().stream()) .collect(toImmutableSet());
/** * Helper method to get the types to be included in the query target * * @return a set of Types */ private ImmutableSet<Type> targetTypes() { if (!query.of().isPresent() || query.of().get().isEmpty()) { throw GraqlQueryException.statisticsAttributeTypesNotSpecified(); } return query.of().get().stream() .map((label) -> { Type type = tx.getSchemaConcept(label); if (type == null) throw GraqlQueryException.labelNotFound(label); if (!type.isAttributeType()) throw GraqlQueryException.mustBeAttributeType(type.label()); return type; }) .flatMap(Type::subs) .collect(CommonUtil.toImmutableSet()); }
/** * NB: doesn't allow overwrites * Apply an optimisation where we check the {@link Role} property instead of navigating to the {@link Role} directly. * @param roles the role-player must link to any of these (or their sub-types) * @return a new {@link RolePlayerFragmentSet} with the same properties excepting role-types */ private RolePlayerFragmentSet substituteRoleLabel(Stream<Role> roles) { Preconditions.checkNotNull(this.role()); Preconditions.checkState(roleLabels() == null); ImmutableSet<Label> newRoleLabels = roles.flatMap(Role::subs).map(SchemaConcept::label).collect(toImmutableSet()); return new AutoValue_RolePlayerFragmentSet( varProperty(), relation(), edge(), rolePlayer(), null, newRoleLabels, relationshipTypeLabels() ); }
@Override public <V> Collection<Attribute<V>> getAttributesByValue(V value) { transceiver.send(RequestBuilder.Transaction.getAttributes(value)); int iteratorId = responseOrThrow().getGetAttributesIter().getId(); Iterable<Concept> iterable = () -> new Iterator<>( this, iteratorId, response -> RemoteConcept.of(response.getGetAttributesIterRes().getAttribute(), this) ); return StreamSupport.stream(iterable.spliterator(), false).map(Concept::<V>asAttribute).collect(toImmutableSet()); }
ImmutableSet<VarAndProperty> properties = patterns.stream() .flatMap(pattern -> VarAndProperty.fromPattern(pattern, executionType)) .collect(toImmutableSet());