Search in sources :

Example 51 with VarPatternAdmin

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);
}
Also used : Role(ai.grakn.concept.Role) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Thing(ai.grakn.concept.Thing)

Example 52 with VarPatternAdmin

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);
}
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 53 with VarPatternAdmin

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;
}
Also used : VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) ArrayList(java.util.ArrayList) Stack(java.util.Stack)

Example 54 with VarPatternAdmin

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);
}
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 55 with VarPatternAdmin

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;
}
Also used : RuleImpl(ai.grakn.kb.internal.concept.RuleImpl) Iterables(com.google.common.collect.Iterables) VALIDATION_CASTING(ai.grakn.util.ErrorMessage.VALIDATION_CASTING) VALIDATION_REQUIRED_RELATION(ai.grakn.util.ErrorMessage.VALIDATION_REQUIRED_RELATION) Role(ai.grakn.concept.Role) SchemaConcept(ai.grakn.concept.SchemaConcept) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Type(ai.grakn.concept.Type) Rule(ai.grakn.concept.Rule) Attribute(ai.grakn.concept.Attribute) TypeImpl(ai.grakn.kb.internal.concept.TypeImpl) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) CommonUtil(ai.grakn.util.CommonUtil) Map(java.util.Map) Relationship(ai.grakn.concept.Relationship) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) ErrorMessage(ai.grakn.util.ErrorMessage) VALIDATION_RELATION_TYPES_ROLES_SCHEMA(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPES_ROLES_SCHEMA) SchemaConceptImpl(ai.grakn.kb.internal.concept.SchemaConceptImpl) Conjunction(ai.grakn.graql.admin.Conjunction) VALIDATION_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_TYPE) Collection(java.util.Collection) Set(java.util.Set) VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE(ai.grakn.util.ErrorMessage.VALIDATION_ROLE_TYPE_MISSING_RELATION_TYPE) Collectors(java.util.stream.Collectors) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) VALIDATION_RELATION_CASTING_LOOP_FAIL(ai.grakn.util.ErrorMessage.VALIDATION_RELATION_CASTING_LOOP_FAIL) Casting(ai.grakn.kb.internal.structure.Casting) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Optional(java.util.Optional) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Collections(java.util.Collections) VALIDATION_NOT_EXACTLY_ONE_KEY(ai.grakn.util.ErrorMessage.VALIDATION_NOT_EXACTLY_ONE_KEY) Conjunction(ai.grakn.graql.admin.Conjunction) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) HashSet(java.util.HashSet)

Aggregations

VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)55 Test (org.junit.Test)20 Var (ai.grakn.graql.Var)17 ReasonerAtomicQuery (ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery)17 MultiUnifier (ai.grakn.graql.admin.MultiUnifier)16 Unifier (ai.grakn.graql.admin.Unifier)15 ConceptId (ai.grakn.concept.ConceptId)13 Label (ai.grakn.concept.Label)13 IdPredicate (ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate)13 Role (ai.grakn.concept.Role)11 GraknTx (ai.grakn.GraknTx)10 VarProperty (ai.grakn.graql.admin.VarProperty)10 Collection (java.util.Collection)10 Set (java.util.Set)10 Stream (java.util.stream.Stream)10 SchemaConcept (ai.grakn.concept.SchemaConcept)9 GraqlQueryException (ai.grakn.exception.GraqlQueryException)9 VarPattern (ai.grakn.graql.VarPattern)9 ReasonerQuery (ai.grakn.graql.admin.ReasonerQuery)9 RelationPlayer (ai.grakn.graql.admin.RelationPlayer)9