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));
}
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);
}
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);
}
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);
}
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())));
}
Aggregations