Search in sources :

Example 21 with Var

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

the class JsonPrinter method build.

@Override
public final Json build(Map<?, ?> map) {
    Json json = Json.object();
    map.forEach((Object key, Object value) -> {
        if (key instanceof Var)
            key = ((Var) key).getValue();
        String keyString = key == null ? "" : key.toString();
        json.set(keyString, build(value));
    });
    return json;
}
Also used : Var(ai.grakn.graql.Var) Json(mjson.Json)

Example 22 with Var

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

the class QueryAnswer method unify.

@Override
public Answer unify(Unifier unifier) {
    if (unifier.isEmpty())
        return this;
    Map<Var, Concept> unified = new HashMap<>();
    for (Map.Entry<Var, Concept> e : this.entrySet()) {
        Var var = e.getKey();
        Concept con = e.getValue();
        Collection<Var> uvars = unifier.get(var);
        if (uvars.isEmpty() && !unifier.values().contains(var)) {
            Concept put = unified.put(var, con);
            if (put != null && !put.equals(con))
                return new QueryAnswer();
        } else {
            for (Var uv : uvars) {
                Concept put = unified.put(uv, con);
                if (put != null && !put.equals(con))
                    return new QueryAnswer();
            }
        }
    }
    return new QueryAnswer(unified, this.getExplanation());
}
Also used : Concept(ai.grakn.concept.Concept) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 23 with Var

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

the class HasAttributeProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // NB: HasAttributeProperty always has (type) label specified
    Var varName = var.var().asUserDefined();
    Var relationVariable = relationship().var();
    Var attributeVariable = attribute().var().asUserDefined();
    Set<ValuePredicate> predicates = getValuePredicates(attributeVariable, attribute(), vars, parent);
    IsaProperty isaProp = attribute().getProperties(IsaProperty.class).findFirst().orElse(null);
    VarPatternAdmin typeVar = isaProp != null ? isaProp.type() : null;
    IdPredicate predicate = typeVar != null ? getIdPredicate(attributeVariable, typeVar, vars, parent) : null;
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    // add resource atom
    VarPatternAdmin resVar = relationVariable.isUserDefinedName() ? varName.has(type(), attributeVariable, relationVariable).admin() : varName.has(type(), attributeVariable).admin();
    return ResourceAtom.create(resVar, attributeVariable, relationVariable, predicateId, predicates, parent);
}
Also used : ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ConceptId(ai.grakn.concept.ConceptId)

Example 24 with Var

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

the class PlaysProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    Var varName = var.var().asUserDefined();
    VarPatternAdmin typeVar = this.role();
    Var typeVariable = typeVar.var().asUserDefined();
    IdPredicate predicate = getIdPredicate(typeVariable, typeVar, vars, parent);
    ConceptId predicateId = predicate == null ? null : predicate.getPredicate();
    return PlaysAtom.create(varName, typeVariable, predicateId, parent);
}
Also used : IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ReasonerUtils.getIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getIdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) ConceptId(ai.grakn.concept.ConceptId)

Example 25 with Var

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

the class Fragment method vars.

/**
 * Get all variables in the fragment including the start and end (if present)
 */
public final Set<Var> vars() {
    if (vars == null) {
        ImmutableSet.Builder<Var> builder = ImmutableSet.<Var>builder().add(start());
        Var end = end();
        if (end != null)
            builder.add(end);
        builder.addAll(otherVars());
        vars = builder.build();
    }
    return vars;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) 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