use of ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate in project grakn by graknlabs.
the class ReasonerQueryImpl method getSubstitution.
/**
* @return substitution obtained from all id predicates (including internal) in the query
*/
public Answer getSubstitution() {
if (substitution == null) {
Set<Var> varNames = getVarNames();
Set<IdPredicate> predicates = getAtoms(IsaAtomBase.class).map(IsaAtomBase::getTypePredicate).filter(Objects::nonNull).filter(p -> varNames.contains(p.getVarName())).collect(Collectors.toSet());
getAtoms(IdPredicate.class).forEach(predicates::add);
HashMap<Var, Concept> answerMap = new HashMap<>();
predicates.forEach(p -> {
Concept concept = tx().getConcept(p.getPredicate());
if (concept == null)
throw GraqlQueryException.idNotFound(p.getPredicate());
answerMap.put(p.getVarName(), concept);
});
substitution = new QueryAnswer(answerMap);
}
return substitution;
}
use of ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate in project grakn by graknlabs.
the class ReasonerQueryImpl method idTransform.
/**
* returns id transform that would convert this query to a query alpha-equivalent to the query,
* provided they are structurally equivalent
* @param query for which the transform is to be constructed
* @param unifier between this query and provided query
* @return id transform
*/
public Map<Var, ConceptId> idTransform(ReasonerQueryImpl query, Unifier unifier) {
Map<Var, ConceptId> transform = new HashMap<>();
this.getAtoms(IdPredicate.class).forEach(thisP -> {
Collection<Var> vars = unifier.get(thisP.getVarName());
Var var = !vars.isEmpty() ? Iterators.getOnlyElement(vars.iterator()) : thisP.getVarName();
IdPredicate p2 = query.getIdPredicate(var);
if (p2 != null)
transform.put(thisP.getVarName(), p2.getPredicate());
});
return transform;
}
use of ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate in project grakn by graknlabs.
the class GraqlTraversalPlanner method refinedPlan.
/**
* @param query top level query for which the plan is constructed
* @param atoms list of current atoms of interest
* @param subs extra substitutions
* @return an optimally ordered list of provided atoms
*/
private static List<Atom> refinedPlan(ReasonerQueryImpl query, List<Atom> atoms, Set<IdPredicate> subs) {
List<Atom> candidates = subs.isEmpty() ? atoms : atoms.stream().filter(at -> at.getPredicates(IdPredicate.class).findFirst().isPresent()).collect(Collectors.toList());
ImmutableList<Atom> initialPlan = planFromTraversal(atoms, atomsToPattern(atoms, subs), query.tx());
if (candidates.contains(initialPlan.get(0)) || candidates.isEmpty()) {
return initialPlan;
} else {
Atom first = optimalCandidate(candidates);
List<Atom> atomsToPlan = new ArrayList<>(atoms);
if (first != null) {
atomsToPlan.remove(first);
Set<IdPredicate> extraSubs = first.getVarNames().stream().filter(v -> subs.stream().noneMatch(s -> s.getVarName().equals(v))).map(v -> IdPredicate.create(v, ConceptId.of("placeholderId"), query)).collect(Collectors.toSet());
return Stream.concat(Stream.of(first), refinedPlan(query, atomsToPlan, Sets.union(subs, extraSubs)).stream()).collect(Collectors.toList());
} else {
return refinedPlan(query, atomsToPlan, subs);
}
}
}
use of ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate 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.internal.reasoner.atom.predicate.IdPredicate 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);
}
Aggregations