use of ai.grakn.graql.admin.VarPatternAdmin in project grakn by graknlabs.
the class RelationshipProperty method addRoleplayer.
/**
* Add a roleplayer to the given {@link Relationship}
* @param relationship the concept representing the {@link Relationship}
* @param relationPlayer a casting between a role type and role player
*/
private void addRoleplayer(QueryOperationExecutor executor, Relationship relationship, RelationPlayer relationPlayer) {
VarPatternAdmin roleVar = getRole(relationPlayer);
Role role = executor.get(roleVar.var()).asRole();
Thing roleplayer = executor.get(relationPlayer.getRolePlayer().var()).asThing();
relationship.addRolePlayer(role, roleplayer);
}
use of ai.grakn.graql.admin.VarPatternAdmin 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.admin.VarPatternAdmin in project grakn by graknlabs.
the class AbstractVarPattern method implicitInnerVarPatterns.
@Override
public final Collection<VarPatternAdmin> implicitInnerVarPatterns() {
Stack<VarPatternAdmin> newVars = new Stack<>();
List<VarPatternAdmin> vars = new ArrayList<>();
newVars.add(this);
while (!newVars.isEmpty()) {
VarPatternAdmin var = newVars.pop();
vars.add(var);
var.getProperties().flatMap(VarProperty::implicitInnerVarPatterns).forEach(newVars::add);
}
return vars;
}
use of ai.grakn.graql.admin.VarPatternAdmin 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.admin.VarPatternAdmin in project grakn by graknlabs.
the class ValidateGlobalRules method validateRuleHead.
/**
* @param graph graph used to ensure the rule head is valid
* @param rule the rule to be validated
* @return Error messages if the rule head is invalid - is not a single-atom conjunction, doesn't contain illegal atomics and is ontologically valid
*/
private static Set<String> validateRuleHead(GraknTx graph, Rule rule) {
Set<String> errors = new HashSet<>();
Set<Conjunction<VarPatternAdmin>> headPatterns = rule.getThen().admin().getDisjunctiveNormalForm().getPatterns();
if (headPatterns.size() != 1) {
errors.add(ErrorMessage.VALIDATION_RULE_DISJUNCTION_IN_HEAD.getMessage(rule.getLabel()));
} else {
ReasonerQuery bodyQuery = Iterables.getOnlyElement(rule.getWhen().admin().getDisjunctiveNormalForm().getPatterns()).toReasonerQuery(graph);
ReasonerQuery headQuery = Iterables.getOnlyElement(headPatterns).toReasonerQuery(graph);
ReasonerQuery combinedQuery = headQuery.conjunction(bodyQuery);
Set<Atomic> headAtoms = headQuery.getAtoms();
combinedQuery.getAtoms().stream().filter(headAtoms::contains).map(at -> at.validateAsRuleHead(rule)).forEach(errors::addAll);
Set<Atomic> selectableHeadAtoms = headAtoms.stream().filter(Atomic::isAtom).filter(Atomic::isSelectable).collect(Collectors.toSet());
if (selectableHeadAtoms.size() > 1) {
errors.add(ErrorMessage.VALIDATION_RULE_HEAD_NON_ATOMIC.getMessage(rule.getLabel()));
}
}
return errors;
}
Aggregations