Search in sources :

Example 36 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class Binary method getSchemaConcept.

@Nullable
@Override
public SchemaConcept getSchemaConcept() {
    if (type == null && getTypeId() != null) {
        SchemaConcept concept = tx().getConcept(getTypeId());
        if (concept == null)
            throw GraqlQueryException.idNotFound(getTypeId());
        type = concept;
    }
    return type;
}
Also used : SchemaConcept(ai.grakn.concept.SchemaConcept) Nullable(javax.annotation.Nullable)

Example 37 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class Atom method validateAsRuleHead.

@Override
public Set<String> validateAsRuleHead(Rule rule) {
    Set<String> errors = new HashSet<>();
    Set<Atomic> parentAtoms = getParentQuery().getAtoms(Atomic.class).filter(at -> !at.equals(this)).collect(Collectors.toSet());
    Set<Var> varNames = Sets.difference(getVarNames(), this.getInnerPredicates().map(Atomic::getVarName).collect(Collectors.toSet()));
    boolean unboundVariables = varNames.stream().anyMatch(var -> parentAtoms.stream().noneMatch(at -> at.getVarNames().contains(var)));
    if (unboundVariables) {
        errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_ATOM_WITH_UNBOUND_VARIABLE.getMessage(rule.getThen(), rule.getLabel()));
    }
    SchemaConcept schemaConcept = getSchemaConcept();
    if (schemaConcept == null) {
        errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_ATOM_WITH_AMBIGUOUS_SCHEMA_CONCEPT.getMessage(rule.getThen(), rule.getLabel()));
    } else if (schemaConcept.isImplicit()) {
        errors.add(ErrorMessage.VALIDATION_RULE_ILLEGAL_HEAD_ATOM_WITH_IMPLICIT_SCHEMA_CONCEPT.getMessage(rule.getThen(), rule.getLabel()));
    }
    return errors;
}
Also used : SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) Rule(ai.grakn.concept.Rule) ReasonerUtils.typesCompatible(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.typesCompatible) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) RuleUtils(ai.grakn.graql.internal.reasoner.rule.RuleUtils) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) RelationshipAtom(ai.grakn.graql.internal.reasoner.atom.binary.RelationshipAtom) Nullable(javax.annotation.Nullable) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) DirectIsaProperty(ai.grakn.graql.internal.pattern.property.DirectIsaProperty) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) VarProperty(ai.grakn.graql.admin.VarProperty) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) TypeAtom(ai.grakn.graql.internal.reasoner.atom.binary.TypeAtom) List(java.util.List) Stream(java.util.stream.Stream) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) InferenceRule(ai.grakn.graql.internal.reasoner.rule.InferenceRule) Var(ai.grakn.graql.Var) Unifier(ai.grakn.graql.admin.Unifier) Var(ai.grakn.graql.Var) Atomic(ai.grakn.graql.admin.Atomic) SchemaConcept(ai.grakn.concept.SchemaConcept) HashSet(java.util.HashSet)

Example 38 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class RelationshipAtom method validateOntologically.

@Override
public Set<String> validateOntologically() {
    Set<String> errors = new HashSet<>();
    SchemaConcept type = getSchemaConcept();
    if (type != null && !type.isRelationshipType()) {
        errors.add(ErrorMessage.VALIDATION_RULE_INVALID_RELATION_TYPE.getMessage(type.getLabel()));
        return errors;
    }
    // check role-type compatibility
    Map<Var, Type> varTypeMap = getParentQuery().getVarTypeMap();
    for (Map.Entry<Role, Collection<Var>> e : getRoleVarMap().asMap().entrySet()) {
        Role role = e.getKey();
        if (!Schema.MetaSchema.isMetaLabel(role.getLabel())) {
            // check whether this role can be played in this relation
            if (type != null && type.asRelationshipType().relates().noneMatch(r -> r.equals(role))) {
                errors.add(ErrorMessage.VALIDATION_RULE_ROLE_CANNOT_BE_PLAYED.getMessage(role.getLabel(), type.getLabel()));
            }
            // check whether the role player's type allows playing this role
            for (Var player : e.getValue()) {
                Type playerType = varTypeMap.get(player);
                if (playerType != null && playerType.plays().noneMatch(plays -> plays.equals(role))) {
                    errors.add(ErrorMessage.VALIDATION_RULE_TYPE_CANNOT_PLAY_ROLE.getMessage(playerType.getLabel(), role.getLabel(), type == null ? "" : type.getLabel()));
                }
            }
        }
    }
    return errors;
}
Also used : Role(ai.grakn.concept.Role) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Pair(ai.grakn.graql.internal.reasoner.utils.Pair) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) VarPattern(ai.grakn.graql.VarPattern) RelationshipProperty(ai.grakn.graql.internal.pattern.property.RelationshipProperty) RelationshipTypeImpl(ai.grakn.kb.internal.concept.RelationshipTypeImpl) Graql(ai.grakn.graql.Graql) ReasonerUtils.top(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.top) Type(ai.grakn.concept.Type) MultiUnifierImpl(ai.grakn.graql.internal.reasoner.MultiUnifierImpl) EntityType(ai.grakn.concept.EntityType) HashMultimap(com.google.common.collect.HashMultimap) Label(ai.grakn.concept.Label) RelationshipType(ai.grakn.concept.RelationshipType) GraknTx(ai.grakn.GraknTx) Map(java.util.Map) ReasonerUtils.supers(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.supers) RoleConverter(ai.grakn.graql.internal.reasoner.utils.conversion.RoleConverter) ConceptId(ai.grakn.concept.ConceptId) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) List(java.util.List) Stream(java.util.stream.Stream) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) ReasonerUtils.compatibleRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRoles) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Iterables(com.google.common.collect.Iterables) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) ReasonerUtils.compatibleRelationTypesWithRoles(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.compatibleRelationTypesWithRoles) HashMap(java.util.HashMap) Answer(ai.grakn.graql.admin.Answer) Multimap(com.google.common.collect.Multimap) Rule(ai.grakn.concept.Rule) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReasonerUtils.multimapIntersection(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.multimapIntersection) ImmutableList(com.google.common.collect.ImmutableList) CommonUtil(ai.grakn.util.CommonUtil) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) Relationship(ai.grakn.concept.Relationship) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TypeConverter(ai.grakn.graql.internal.reasoner.utils.conversion.TypeConverter) Nullable(javax.annotation.Nullable) ErrorMessage(ai.grakn.util.ErrorMessage) GraqlQueryException(ai.grakn.exception.GraqlQueryException) Iterator(java.util.Iterator) Memoized(com.google.auto.value.extension.memoized.Memoized) MultiUnifier(ai.grakn.graql.admin.MultiUnifier) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) ReasonerUtils(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils) VarProperty(ai.grakn.graql.admin.VarProperty) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) IsaProperty(ai.grakn.graql.internal.pattern.property.IsaProperty) UnifierComparison(ai.grakn.graql.admin.UnifierComparison) RelationPlayer(ai.grakn.graql.admin.RelationPlayer) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) Comparator(java.util.Comparator) Unifier(ai.grakn.graql.admin.Unifier) Type(ai.grakn.concept.Type) EntityType(ai.grakn.concept.EntityType) RelationshipType(ai.grakn.concept.RelationshipType) UnifierType(ai.grakn.graql.internal.reasoner.UnifierType) Var(ai.grakn.graql.Var) Collection(java.util.Collection) SchemaConcept(ai.grakn.concept.SchemaConcept) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 39 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class ConceptBuilder method tryGetConcept.

@Nullable
private Concept tryGetConcept() {
    Concept concept = null;
    if (has(ID)) {
        concept = executor.tx().getConcept(use(ID));
        if (has(LABEL)) {
            concept.asSchemaConcept().setLabel(use(LABEL));
        }
    } else if (has(LABEL)) {
        concept = executor.tx().getSchemaConcept(use(LABEL));
    }
    if (concept != null) {
        // The super can be changed on an existing concept
        if (has(SUPER_CONCEPT)) {
            SchemaConcept superConcept = use(SUPER_CONCEPT);
            setSuper(concept.asSchemaConcept(), superConcept);
        }
        validate(concept);
    }
    return concept;
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) SchemaConcept(ai.grakn.concept.SchemaConcept) Nullable(javax.annotation.Nullable)

Example 40 with SchemaConcept

use of ai.grakn.concept.SchemaConcept in project grakn by graknlabs.

the class ResourceAtom method validateOntologically.

@Override
public Set<String> validateOntologically() {
    SchemaConcept type = getSchemaConcept();
    Set<String> errors = new HashSet<>();
    if (type == null)
        return errors;
    if (!type.isAttributeType()) {
        errors.add(ErrorMessage.VALIDATION_RULE_INVALID_RESOURCE_TYPE.getMessage(type.getLabel()));
        return errors;
    }
    Type ownerType = getParentQuery().getVarTypeMap().get(getVarName());
    if (ownerType != null && ownerType.attributes().noneMatch(rt -> rt.equals(type.asAttributeType()))) {
        errors.add(ErrorMessage.VALIDATION_RULE_RESOURCE_OWNER_CANNOT_HAVE_RESOURCE.getMessage(type.getLabel(), ownerType.getLabel()));
    }
    return errors;
}
Also used : Iterables(com.google.common.collect.Iterables) Atom(ai.grakn.graql.internal.reasoner.atom.Atom) SortedSet(java.util.SortedSet) VarPattern(ai.grakn.graql.VarPattern) ReasonerQueryImpl(ai.grakn.graql.internal.reasoner.query.ReasonerQueryImpl) Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Graql(ai.grakn.graql.Graql) Type(ai.grakn.concept.Type) Answer(ai.grakn.graql.admin.Answer) Rule(ai.grakn.concept.Rule) TreeSet(java.util.TreeSet) ReasonerUtils.areDisjointTypes(ai.grakn.graql.internal.reasoner.utils.ReasonerUtils.areDisjointTypes) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) GraknTx(ai.grakn.GraknTx) Predicate(ai.grakn.graql.internal.reasoner.atom.predicate.Predicate) ConceptId(ai.grakn.concept.ConceptId) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) RelationshipImpl(ai.grakn.kb.internal.concept.RelationshipImpl) ErrorMessage(ai.grakn.util.ErrorMessage) Patterns(ai.grakn.graql.internal.pattern.Patterns) AttributeImpl(ai.grakn.kb.internal.concept.AttributeImpl) GraqlQueryException(ai.grakn.exception.GraqlQueryException) ImmutableSet(com.google.common.collect.ImmutableSet) ValuePredicate(ai.grakn.graql.internal.reasoner.atom.predicate.ValuePredicate) Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) IdPredicate(ai.grakn.graql.internal.reasoner.atom.predicate.IdPredicate) UnifierImpl(ai.grakn.graql.internal.reasoner.UnifierImpl) Collectors(java.util.stream.Collectors) VarProperty(ai.grakn.graql.admin.VarProperty) Objects(java.util.Objects) Atomic(ai.grakn.graql.admin.Atomic) ReasonerQuery(ai.grakn.graql.admin.ReasonerQuery) Stream(java.util.stream.Stream) HasAttributeProperty(ai.grakn.graql.internal.pattern.property.HasAttributeProperty) Var(ai.grakn.graql.Var) AutoValue(com.google.auto.value.AutoValue) VarPatternAdmin(ai.grakn.graql.admin.VarPatternAdmin) EntityImpl(ai.grakn.kb.internal.concept.EntityImpl) Schema(ai.grakn.util.Schema) Pattern(ai.grakn.graql.Pattern) AttributeTypeImpl(ai.grakn.kb.internal.concept.AttributeTypeImpl) Unifier(ai.grakn.graql.admin.Unifier) Type(ai.grakn.concept.Type) AttributeType(ai.grakn.concept.AttributeType) SchemaConcept(ai.grakn.concept.SchemaConcept) HashSet(java.util.HashSet)

Aggregations

SchemaConcept (ai.grakn.concept.SchemaConcept)51 Label (ai.grakn.concept.Label)19 Set (java.util.Set)15 Type (ai.grakn.concept.Type)14 GraknTx (ai.grakn.GraknTx)12 HashSet (java.util.HashSet)12 ConceptId (ai.grakn.concept.ConceptId)11 Stream (java.util.stream.Stream)11 Test (org.junit.Test)11 AttributeType (ai.grakn.concept.AttributeType)10 Property (com.pholser.junit.quickcheck.Property)10 Role (ai.grakn.concept.Role)9 Concept (ai.grakn.concept.Concept)8 Sets (com.google.common.collect.Sets)8 Optional (java.util.Optional)8 RelationshipType (ai.grakn.concept.RelationshipType)7 Rule (ai.grakn.concept.Rule)7 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)7 ErrorMessage (ai.grakn.util.ErrorMessage)7 Var (ai.grakn.graql.Var)6