Search in sources :

Example 11 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class QueryVisitor method visitGetQuery.

@Override
public GetQuery visitGetQuery(GraqlParser.GetQueryContext ctx) {
    Set<Var> vars = ctx.VARIABLE().stream().map(this::getVariable).collect(toSet());
    Match match = visitMatchPart(ctx.matchPart());
    if (vars.isEmpty()) {
        return match.get();
    } else {
        return match.get(vars);
    }
}
Also used : Var(ai.grakn.graql.Var) Match(ai.grakn.graql.Match)

Example 12 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class QueryVisitor method visitMatchOrderBy.

@Override
public Match visitMatchOrderBy(GraqlParser.MatchOrderByContext ctx) {
    Match match = visitMatchPart(ctx.matchPart());
    // decide which ordering method to use
    Var var = getVariable(ctx.VARIABLE());
    if (ctx.order() != null) {
        return match.orderBy(var, visitOrder(ctx.order()));
    } else {
        return match.orderBy(var);
    }
}
Also used : Var(ai.grakn.graql.Var) Match(ai.grakn.graql.Match)

Example 13 with Var

use of ai.grakn.graql.Var 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)

Example 14 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class ResourceAtom method materialise.

@Override
public Stream<Answer> materialise() {
    Answer substitution = getParentQuery().getSubstitution();
    AttributeType type = getSchemaConcept().asAttributeType();
    Concept owner = substitution.get(getVarName());
    Var resourceVariable = getPredicateVariable();
    // if the attribute already exists, only attach a new link to the owner, otherwise create a new attribute
    if (substitution.containsVar(resourceVariable)) {
        Attribute attribute = substitution.get(resourceVariable).asAttribute();
        attachAttribute(owner, attribute);
        return Stream.of(substitution);
    } else {
        Attribute attribute = AttributeTypeImpl.from(type).putAttributeInferred(Iterables.getOnlyElement(getMultiPredicate()).getPredicate().equalsValue().get());
        attachAttribute(owner, attribute);
        return Stream.of(substitution.merge(new QueryAnswer(ImmutableMap.of(resourceVariable, attribute))));
    }
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) Var(ai.grakn.graql.Var)

Example 15 with Var

use of ai.grakn.graql.Var in project grakn by graknlabs.

the class ValuePredicate method getVarNames.

@Override
public Set<Var> getVarNames() {
    Set<Var> vars = super.getVarNames();
    VarPatternAdmin innerVar = getPredicate().getInnerVar().orElse(null);
    if (innerVar != null && innerVar.var().isUserDefinedName())
        vars.add(innerVar.var());
    return vars;
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var)

Aggregations

Var (ai.grakn.graql.Var)100 Test (org.junit.Test)29 Answer (ai.grakn.graql.admin.Answer)28 ConceptId (ai.grakn.concept.ConceptId)26 Concept (ai.grakn.concept.Concept)25 Role (ai.grakn.concept.Role)25 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)24 HashSet (java.util.HashSet)24 Set (java.util.Set)24 VarPattern (ai.grakn.graql.VarPattern)21 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)21 Sets (com.google.common.collect.Sets)20 Map (java.util.Map)20 Stream (java.util.stream.Stream)20 GraqlQueryException (ai.grakn.exception.GraqlQueryException)19 ImmutableSet (com.google.common.collect.ImmutableSet)19 HashMap (java.util.HashMap)19 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)18 List (java.util.List)18 SchemaConcept (ai.grakn.concept.SchemaConcept)17