Search in sources :

Example 11 with VarPatternAdmin

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

the class RelationshipAtom method getExplicitRoles.

private Stream<Role> getExplicitRoles() {
    ReasonerQueryImpl parent = (ReasonerQueryImpl) getParentQuery();
    GraknTx graph = parent.tx();
    return getRelationPlayers().stream().map(RelationPlayer::getRole).flatMap(CommonUtil::optionalToStream).map(VarPatternAdmin::getTypeLabel).flatMap(CommonUtil::optionalToStream).map(graph::<Role>getSchemaConcept);
}
Also used : GraknTx(ai.grakn.GraknTx) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl)

Example 12 with VarPatternAdmin

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

the class RelationshipAtom method validateRelationPlayers.

private Set<String> validateRelationPlayers(Rule rule) {
    Set<String> errors = new HashSet<>();
    getRelationPlayers().forEach(rp -> {
        VarPatternAdmin role = rp.getRole().orElse(null);
        if (role == null) {
            errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.getThen(), rule.getLabel()));
        } else {
            Label roleLabel = role.getTypeLabel().orElse(null);
            if (roleLabel == null) {
                errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.getThen(), rule.getLabel()));
            } else {
                if (Schema.MetaSchema.isMetaLabel(roleLabel)) {
                    errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_AMBIGUOUS_ROLE.getMessage(rule.getThen(), rule.getLabel()));
                }
                Role roleType = tx().getRole(roleLabel.getValue());
                if (roleType != null && roleType.isImplicit()) {
                    errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_RELATION_WITH_IMPLICIT_ROLE.getMessage(rule.getThen(), rule.getLabel()));
                }
            }
        }
    });
    return errors;
}
Also used : Role(ai.grakn.concept.Role) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Label(ai.grakn.concept.Label) HashSet(java.util.HashSet)

Example 13 with VarPatternAdmin

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

the class ConceptBuilder method set.

private <T> ConceptBuilder set(BuilderParam<T> param, T value) {
    if (preProvidedParams.containsKey(param) && !preProvidedParams.get(param).equals(value)) {
        VarPatternAdmin varPattern = executor.printableRepresentation(var);
        Object otherValue = preProvidedParams.get(param);
        throw GraqlQueryException.insertMultipleProperties(varPattern, param.name(), value, otherValue);
    }
    preProvidedParams.put(param, checkNotNull(value));
    return this;
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin)

Example 14 with VarPatternAdmin

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

the class QueryOperationExecutor method create.

private static QueryOperationExecutor create(Collection<VarPatternAdmin> patterns, GraknTx graph, ExecutionType executionType) {
    ImmutableSet<VarAndProperty> properties = patterns.stream().flatMap(pattern -> VarAndProperty.fromPattern(pattern, executionType)).collect(toImmutableSet());
    /*
            We build several many-to-many relations, indicated by a `Multimap<X, Y>`. These are used to represent
            the dependencies between properties and variables.

            `propDependencies.containsEntry(prop, var)` indicates that the property `prop` cannot be inserted until
            the concept represented by the variable `var` is created.

            For example, the property `$x isa $y` depends on the existence of the concept represented by `$y`.
         */
    Multimap<VarAndProperty, Var> propDependencies = HashMultimap.create();
    for (VarAndProperty property : properties) {
        for (Var requiredVar : property.executor().requiredVars()) {
            propDependencies.put(property, requiredVar);
        }
    }
    /*
            `varDependencies.containsEntry(var, prop)` indicates that the concept represented by the variable `var`
            cannot be created until the property `prop` is inserted.

            For example, the concept represented by `$x` will not exist before the property `$x isa $y` is inserted.
         */
    Multimap<Var, VarAndProperty> varDependencies = HashMultimap.create();
    for (VarAndProperty property : properties) {
        for (Var producedVar : property.executor().producedVars()) {
            varDependencies.put(producedVar, property);
        }
    }
    /*
            Equivalent vars are variables that must represent the same concept as another var.

                 $X label movie, sub entity;
                 $Y label movie;
                 $z isa $Y;

            In this example, `$z isa $Y` must not be inserted before `$Y` is. However, `$Y` does not have enough
            information to insert on its own. It also needs a super type!

            We know `$Y` must represent the same concept as `$X`, because they both share the same label property.
            Therefore, we can share their dependencies, such that:

                varDependencies.containsEntry($X, prop) <=> varDependencies.containsEntry($Y, prop)

            Therefore:

                varDependencies.containsEntry($X, `$X sub entity`) => varDependencies.containsEntry($Y, `$X sub entity`)

            Now we know that `$Y` depends on `$X sub entity` as well as `$X label movie`, which is enough information to
            insert the type!
         */
    Partition<Var> equivalentVars = Partition.singletons(Collections.emptyList());
    equivalentProperties(properties).asMap().values().forEach(vars -> {
        // These vars must refer to the same concept, so share their dependencies
        Collection<VarAndProperty> producers = vars.stream().flatMap(var -> varDependencies.get(var).stream()).collect(toList());
        Var first = vars.iterator().next();
        vars.forEach(var -> {
            varDependencies.replaceValues(var, producers);
            equivalentVars.merge(first, var);
        });
    });
    /*
            Together, `propDependencies` and `varDependencies` can be composed into a single many-to-many relation:

                dependencies = propDependencies ∘ varDependencies

            By doing so, we map _directly_ between properties, skipping the vars. For example, if we previously had:

                propDependencies.containsEntry(`$x isa $y`, `$y`);         // `$x isa $y` depends on `$y`
                varDependencies.containsEntry(`$y`, `$y label movie`);     // `$y` depends on `$y label movie`

            Then it follows that:

                dependencies.containsEntry(`$x isa $y`, `$y label movie`); // `$x isa $y` depends on `$y label movie`

            The `dependencies` relation contains all the information to decide what order to execute the properties.
         */
    Multimap<VarAndProperty, VarAndProperty> dependencies = composeMultimaps(propDependencies, varDependencies);
    return new QueryOperationExecutor(graph, properties, equivalentVars, ImmutableMultimap.copyOf(dependencies));
}
Also used : PropertyExecutor(ai.grakn.graql.internal.pattern.property.PropertyExecutor) Concept(ai.grakn.concept.Concept) InsertQuery(ai.grakn.graql.InsertQuery) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Partition(ai.grakn.graql.internal.util.Partition) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Nullable(javax.annotation.Nullable) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) Patterns(ai.grakn.graql.internal.pattern.Patterns) VarPropertyInternal(ai.grakn.graql.internal.pattern.property.VarPropertyInternal) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) Maps(com.google.common.collect.Maps) Query(ai.grakn.graql.Query) Sets(com.google.common.collect.Sets) VarProperty(ai.grakn.graql.admin.VarProperty) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) DefineQuery(ai.grakn.graql.DefineQuery) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) Var(ai.grakn.graql.Var)

Example 15 with VarPatternAdmin

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

the class VarPatternImpl method toString.

@Override
public final String toString() {
    Collection<VarPatternAdmin> innerVars = innerVarPatterns();
    innerVars.remove(this);
    getProperties(HasAttributeProperty.class).map(HasAttributeProperty::attribute).flatMap(r -> r.innerVarPatterns().stream()).forEach(innerVars::remove);
    if (innerVars.stream().anyMatch(VarPatternImpl::invalidInnerVariable)) {
        LOG.warn("printing a query with inner variables, which is not supported in native Graql");
    }
    StringBuilder builder = new StringBuilder();
    String name = var().isUserDefinedName() ? var().toString() : "";
    builder.append(name);
    if (var().isUserDefinedName() && !properties().isEmpty()) {
        // Add a space after the var name
        builder.append(" ");
    }
    boolean first = true;
    for (VarProperty property : properties()) {
        if (!first) {
            builder.append(" ");
        }
        first = false;
        property.buildString(builder);
    }
    return builder.toString();
}
Also used : VarProperty(ai.grakn.graql.admin.VarProperty) Logger(org.slf4j.Logger) VarPattern(ai.grakn.graql.VarPattern) LabelProperty(ai.grakn.graql.internal.pattern.property.LabelProperty) HasAttributeProperty(ai.grakn.graql.internal.pattern.property.HasAttributeProperty) Collection(java.util.Collection) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) LoggerFactory(org.slf4j.LoggerFactory) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Set(java.util.Set) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) VarProperty(ai.grakn.graql.admin.VarProperty) HasAttributeProperty(ai.grakn.graql.internal.pattern.property.HasAttributeProperty)

Aggregations

VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)55 Test (org.junit.Test)20 Var (ai.grakn.graql.Var)17 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)16 Unifier (ai.grakn.graql.admin.Unifier)15 ConceptId (ai.grakn.concept.ConceptId)13 Label (ai.grakn.concept.Label)13 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)13 Role (ai.grakn.concept.Role)11 GraknTx (ai.grakn.GraknTx)10 VarProperty (ai.grakn.graql.admin.VarProperty)10 Collection (java.util.Collection)10 Set (java.util.Set)10 Stream (java.util.stream.Stream)10 SchemaConcept (ai.grakn.concept.SchemaConcept)9 GraqlQueryException (ai.grakn.exception.GraqlQueryException)9 VarPattern (ai.grakn.graql.VarPattern)9 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)9 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)9