Search in sources :

Example 91 with Var

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

the class HasAttributeProperty method match.

@Override
public Collection<EquivalentFragmentSet> match(Var start) {
    Var edge1 = Graql.var();
    Var edge2 = Graql.var();
    return ImmutableSet.of(rolePlayer(this, relationship().var(), edge1, start, null), rolePlayer(this, relationship().var(), edge2, attribute().var(), null), neq(this, edge1, edge2));
}
Also used : Var(ai.grakn.graql.Var)

Example 92 with Var

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

the class RelationshipProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // set varName as user defined if reified
    // reified if contains more properties than the RelationshipProperty itself and potential IsaProperty
    boolean isReified = var.getProperties().filter(prop -> !RelationshipProperty.class.isInstance(prop)).anyMatch(prop -> !AbstractIsaProperty.class.isInstance(prop));
    VarPattern relVar = isReified ? var.var().asUserDefined() : var.var();
    for (RelationPlayer rp : relationPlayers()) {
        VarPattern rolePattern = rp.getRole().orElse(null);
        VarPattern rolePlayer = rp.getRolePlayer();
        if (rolePattern != null) {
            Var roleVar = rolePattern.admin().var();
            // look for indirect role definitions
            IdPredicate roleId = getUserDefinedIdPredicate(roleVar, vars, parent);
            if (roleId != null) {
                Concept concept = parent.tx().getConcept(roleId.getPredicate());
                if (concept != null) {
                    if (concept.isRole()) {
                        Label roleLabel = concept.asSchemaConcept().getLabel();
                        rolePattern = roleVar.label(roleLabel);
                    } else {
                        throw GraqlQueryException.nonRoleIdAssignedToRoleVariable(var);
                    }
                }
            }
            relVar = relVar.rel(rolePattern, rolePlayer);
        } else
            relVar = relVar.rel(rolePlayer);
    }
    // isa part
    AbstractIsaProperty isaProp = var.getProperty(AbstractIsaProperty.class).orElse(null);
    IdPredicate predicate = null;
    // if no isa property present generate type variable
    Var typeVariable = isaProp != null ? isaProp.type().var() : Graql.var();
    // Isa present
    if (isaProp != null) {
        VarPatternAdmin isaVar = isaProp.type();
        Label label = isaVar.getTypeLabel().orElse(null);
        if (label != null) {
            predicate = IdPredicate.create(typeVariable, label, parent);
        } else {
            typeVariable = isaVar.var();
            predicate = getUserDefinedIdPredicate(typeVariable, vars, parent);
        }
    }
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    relVar = isaProp instanceof DirectIsaProperty ? relVar.directIsa(typeVariable.asUserDefined()) : relVar.isa(typeVariable.asUserDefined());
    return RelationshipAtom.create(relVar.admin(), typeVariable, predicateId, parent);
}
Also used : VarPattern(ai.grakn.graql.VarPattern) Role(ai.grakn.concept.Role) EquivalentFragmentSets.rolePlayer(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets.rolePlayer) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Graql(ai.grakn.graql.Graql) HashSet(java.util.HashSet) UniqueVarProperty(ai.grakn.graql.admin.UniqueVarProperty) Label(ai.grakn.concept.Label) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) QueryOperationExecutor(ai.grakn.graql.internal.query.runner.QueryOperationExecutor) ConceptId(ai.grakn.concept.ConceptId) Relationship(ai.grakn.concept.Relationship) EquivalentFragmentSet(ai.grakn.graql.internal.gremlin.EquivalentFragmentSet) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Collectors.toSet(java.util.stream.Collectors.toSet) CommonUtil.toImmutableSet(ai.grakn.util.CommonUtil.toImmutableSet) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Sets(com.google.common.collect.Sets) Collectors.joining(java.util.stream.Collectors.joining) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) EquivalentFragmentSets(ai.grakn.graql.internal.gremlin.sets.EquivalentFragmentSets) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) ReasonerUtils.getUserDefinedIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getUserDefinedIdPredicate) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) ReasonerUtils.getUserDefinedIdPredicate(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.getUserDefinedIdPredicate) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Var(ai.grakn.graql.Var) Label(ai.grakn.concept.Label) ConceptId(ai.grakn.concept.ConceptId) VarPattern(ai.grakn.graql.VarPattern) RelationPlayer(ai.grakn.graql.admin.RelationPlayer)

Example 93 with Var

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

the class SubProperty method mapToAtom.

@Override
public Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    Var varName = var.var().asUserDefined();
    VarPatternAdmin typeVar = this.superType();
    Var typeVariable = typeVar.var().asUserDefined();
    IdPredicate predicate = getIdPredicate(typeVariable, typeVar, vars, parent);
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    return SubAtom.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 94 with Var

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

the class AbstractIsaProperty method mapToAtom.

@Nullable
@Override
public final Atomic mapToAtom(VarPatternAdmin var, Set<VarPatternAdmin> vars, ReasonerQuery parent) {
    // IsaProperty is unique within a var, so skip if this is a relation
    if (var.hasProperty(RelationshipProperty.class))
        return null;
    Var varName = var.var().asUserDefined();
    VarPatternAdmin typePattern = this.type();
    Var typeVariable = typePattern.var();
    IdPredicate predicate = getIdPredicate(typeVariable, typePattern, vars, parent);
    ConceptId predicateId = predicate != null ? predicate.getPredicate() : null;
    // isa part
    VarPatternAdmin isaVar = varPatternForAtom(varName, typeVariable).admin();
    return IsaAtom.create(isaVar, 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) Nullable(javax.annotation.Nullable)

Example 95 with Var

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

the class InIsaFragment method toEdgeInstances.

private GraphTraversal<Vertex, Edge> toEdgeInstances() {
    Var type = var();
    Var labelId = var();
    // There is no fast way to retrieve all edge instances, because edges cannot be globally indexed.
    // This is a best-effort, that uses the schema to limit the search space...
    // First retrieve the type ID
    GraphTraversal<Vertex, Vertex> traversal = __.<Vertex>as(type.name()).values(LABEL_ID.name()).as(labelId.name()).select(type.name());
    // Next, navigate the schema to all possible types whose instances can be in this relation
    traversal = Fragments.inSubs(traversal.out(RELATES.getLabel()).in(PLAYS.getLabel()));
    // Navigate to all (vertex) instances of those types
    // (we do not need to navigate to edge instances, because edge instances cannot be role-players)
    traversal = toVertexInstances(traversal);
    // Finally, navigate to all relation edges with the correct type attached to these instances
    return traversal.outE(ATTRIBUTE.getLabel()).has(RELATIONSHIP_TYPE_LABEL_ID.name(), __.where(P.eq(labelId.name())));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) 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