コード例 #1
0
    def visit_class_slot(self, cls: ClassDefinition, aliased_slot_name: str,
                         slot: SlotDefinition) -> None:
        constraint = TripleConstraint()
        # Juggling to get the constraint to be either a single triple constraint or an eachof construct
        if not self.shape.expression:
            self.shape.expression = constraint
        elif isinstance(self.shape.expression, TripleConstraint):
            self.shape.expression = EachOf(
                expressions=[self.shape.expression, constraint])
        else:
            self.shape.expression.expressions.append(constraint)

        constraint.predicate = self._predicate(slot.name)
        # JSON-LD generates multi-valued entries as lists
        constraint.min = 1 if slot.primary_key or slot.required else 0
        constraint.max = 1 if not slot.multivalued or self.collections else -1
        # TODO: This should not be hard coded -- figure out where to go with it
        rng = IRIREF(META.SlotRangeTypes) if slot.range == 'anytype' else\
              self._type_constraint(slot.range) if slot.range and slot.range not in self.schema.classes else\
              self._shapeIRI(slot.range)
        name_base = ("XSD_" +
                     self.grounded_slot_range(slot.range)) if isinstance(
                         rng, NodeConstraint) else str(rng)
        constraint.valueExpr = self.gen_multivalued_slot(name_base, rng) \
            if slot.multivalued and self.collections else rng