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