Search in sources :

Example 46 with Var

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

the class QueryOperationExecutor method insertAll.

private Answer insertAll(Answer results) {
    concepts.putAll(results.map());
    sortProperties().forEach(property -> property.executor().execute(this));
    conceptBuilders.forEach(this::buildConcept);
    ImmutableMap.Builder<Var, Concept> allConcepts = ImmutableMap.<Var, Concept>builder().putAll(concepts);
    // Make sure to include all equivalent vars in the result
    for (Var var : equivalentVars.getNodes()) {
        allConcepts.put(var, concepts.get(equivalentVars.componentOf(var)));
    }
    Map<Var, Concept> namedConcepts = Maps.filterKeys(allConcepts.build(), Var::isUserDefinedName);
    return new QueryAnswer(namedConcepts);
}
Also used : Concept(ai.grakn.concept.Concept) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Var(ai.grakn.graql.Var) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 47 with Var

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

the class MatchBase method makeResults.

/**
 * @param vars set of variables of interest
 * @param tx the graph to get results from
 * @param elements a map of vertices and edges where the key is the variable name
 * @return a map of concepts where the key is the variable name
 */
private static Optional<Map<Var, Concept>> makeResults(Set<Var> vars, EmbeddedGraknTx<?> tx, Map<String, Element> elements) {
    Map<Var, Concept> map = new HashMap<>();
    for (Var var : vars) {
        Element element = elements.get(var.name());
        if (element == null) {
            throw GraqlQueryException.unexpectedResult(var);
        } else {
            Concept concept = buildConcept(tx, element);
            map.put(var, concept);
        }
    }
    return Optional.of(map);
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) Element(org.apache.tinkerpop.gremlin.structure.Element)

Example 48 with Var

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

the class ComparatorPredicate method applyPredicate.

@Override
public final <S, E> GraphTraversal<S, E> applyPredicate(GraphTraversal<S, E> traversal) {
    var.ifPresent(theVar -> {
        // Compare to another variable
        String thisVar = UUID.randomUUID().toString();
        Var otherVar = theVar.var();
        String otherValue = UUID.randomUUID().toString();
        Traversal[] traversals = Stream.of(VALUE_PROPERTIES).map(prop -> __.values(prop).as(otherValue).select(thisVar).values(prop).where(gremlinPredicate(otherValue))).toArray(Traversal[]::new);
        traversal.as(thisVar).select(otherVar.name()).or(traversals).select(thisVar);
    });
    persistedValue().ifPresent(theValue -> {
        // Compare to a given value
        DataType<?> dataType = DataType.SUPPORTED_TYPES.get(value().get().getClass().getTypeName());
        Schema.VertexProperty property = dataType.getVertexProperty();
        traversal.has(property.name(), gremlinPredicate(theValue));
    });
    return traversal;
}
Also used : GraqlQueryException(ai.grakn.exception.GraqlQueryException) VarPattern(ai.grakn.graql.VarPattern) StringUtil(ai.grakn.util.StringUtil) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) UUID(java.util.UUID) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ValuePredicate(ai.grakn.graql.ValuePredicate) Stream(java.util.stream.Stream) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) Var(ai.grakn.graql.Var) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) DataType(ai.grakn.concept.AttributeType.DataType) Schema(ai.grakn.util.Schema) P(org.apache.tinkerpop.gremlin.process.traversal.P) Var(ai.grakn.graql.Var) Schema(ai.grakn.util.Schema) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal)

Example 49 with Var

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

the class GreedyTraversalPlan method inferRelationshipTypes.

private static void inferRelationshipTypes(EmbeddedGraknTx<?> tx, Set<Fragment> allFragments) {
    Map<Var, Type> labelVarTypeMap = getLabelVarTypeMap(tx, allFragments);
    if (labelVarTypeMap.isEmpty())
        return;
    Multimap<Var, Type> instanceVarTypeMap = getInstanceVarTypeMap(allFragments, labelVarTypeMap);
    Multimap<Var, Var> relationshipRolePlayerMap = getRelationshipRolePlayerMap(allFragments, instanceVarTypeMap);
    if (relationshipRolePlayerMap.isEmpty())
        return;
    // for each type, get all possible relationship type it could be in
    Multimap<Type, RelationshipType> relationshipMap = HashMultimap.create();
    labelVarTypeMap.values().stream().distinct().forEach(type -> addAllPossibleRelationships(relationshipMap, type));
    // inferred labels should be kept separately, even if they are already in allFragments set
    Map<Label, Var> inferredLabels = new HashMap<>();
    relationshipRolePlayerMap.asMap().forEach((relationshipVar, rolePlayerVars) -> {
        Set<Type> possibleRelationshipTypes = rolePlayerVars.stream().filter(instanceVarTypeMap::containsKey).map(rolePlayer -> getAllPossibleRelationshipTypes(instanceVarTypeMap.get(rolePlayer), relationshipMap)).reduce(Sets::intersection).orElse(Collections.emptySet());
        // TODO: if possibleRelationshipTypes here is empty, the query will not match any data
        if (possibleRelationshipTypes.size() == 1) {
            Type relationshipType = possibleRelationshipTypes.iterator().next();
            Label label = relationshipType.getLabel();
            // add label fragment if this label has not been inferred
            if (!inferredLabels.containsKey(label)) {
                Var labelVar = var();
                inferredLabels.put(label, labelVar);
                Fragment labelFragment = Fragments.label(LabelProperty.of(label), labelVar, ImmutableSet.of(label));
                allFragments.add(labelFragment);
            }
            // finally, add inferred isa fragments
            Var labelVar = inferredLabels.get(label);
            IsaProperty isaProperty = IsaProperty.of(labelVar.admin());
            EquivalentFragmentSet isaEquivalentFragmentSet = EquivalentFragmentSets.isa(isaProperty, relationshipVar, labelVar, relationshipType.isImplicit());
            allFragments.addAll(isaEquivalentFragmentSet.fragments());
        }
    });
}
Also used : HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) RelationshipType(ai.grakn.concept.RelationshipType) Label(ai.grakn.concept.Label) InSubFragment(ai.grakn.graql.internal.gremlin.fragment.InSubFragment) InIsaFragment(ai.grakn.graql.internal.gremlin.fragment.InIsaFragment) OutRolePlayerFragment(ai.grakn.graql.internal.gremlin.fragment.OutRolePlayerFragment) Fragment(ai.grakn.graql.internal.gremlin.fragment.Fragment) LabelFragment(ai.grakn.graql.internal.gremlin.fragment.LabelFragment) Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType)

Example 50 with Var

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

the class GreedyTraversalPlan method getLabelVarTypeMap.

// find all vars representing types
private static Map<Var, Type> getLabelVarTypeMap(EmbeddedGraknTx<?> tx, Set<Fragment> allFragments) {
    Map<Var, Type> labelVarTypeMap = new HashMap<>();
    allFragments.stream().filter(LabelFragment.class::isInstance).forEach(fragment -> {
        // TODO: labels() should return ONE label instead of a set
        SchemaConcept schemaConcept = tx.getSchemaConcept(Iterators.getOnlyElement(((LabelFragment) fragment).labels().iterator()));
        if (schemaConcept != null && !schemaConcept.isRole() && !schemaConcept.isRule()) {
            labelVarTypeMap.put(fragment.start(), schemaConcept.asType());
        }
    });
    return labelVarTypeMap;
}
Also used : Type(ai.grakn.concept.Type) RelationshipType(ai.grakn.concept.RelationshipType) HashMap(java.util.HashMap) Var(ai.grakn.graql.Var) SchemaConcept(ai.grakn.concept.SchemaConcept)

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