def _type_constraint(self, rnge: Optional[str]) -> NodeConstraint: # TODO: missing type - string or '.'? if rnge in builtin_names: return NodeConstraint(datatype=IRIREF(XSD[rnge])) elif rnge in self.schema.types: return self._type_constraint(self.schema.types[rnge].typeof) else: return NodeConstraint()
def visit_schema(self, **_): # Adjust the schema context to include the base model URI context = self.shex['@context'] self.shex['@context'] = [context, {'@base': self.namespaces._base}] # Emit all of the type definitions for typ in self.schema.types.values(): if typ.uri: uri = self.namespaces.uri_for(typ.uri) if uri == XSD.anyURI: self.shapes.append(NodeConstraint(id=self._shape_iri(typ.name), nodeKind="nonliteral")) else: self.shapes.append(NodeConstraint(id=self._shape_iri(typ.name), datatype=self.namespaces.uri_for(typ.uri))) else: self.shapes.append(Shape(id=self._shape_iri(typ.name), expression=self._shape_iri(typ.typeof)))
def end_class(self, cls: ClassDefinition) -> None: if cls.is_a: self._add_constraint(self.namespaces.uri_for(camelcase(cls.is_a) + "_t")) for mixin in cls.mixins: if self._class_has_expressions(mixin): self._add_constraint(self.namespaces.uri_for(camelcase(mixin) + "_t")) if cls.name in self.synopsis.applytorefs: for applyto in self.synopsis.applytorefs[cls.name].classrefs: if self._class_has_expressions(applyto): self._add_constraint(self.namespaces.uri_for(camelcase(applyto) + '_t')) self.shape.closed = True self.shape.extra = [RDF.type] if self.shape.expression: # TODO: Figure out how to label a single triple expression if isinstance_(self.shape.expression, tripleExprLabel): self.shape.expression = EachOf(expressions=[self.shape.expression, wildcard(None)]) self.shape.expression.id = self.namespaces.uri_for(camelcase(cls.name) + "_t") else: self.shape.expression = wildcard(self.namespaces.uri_for(camelcase(cls.name) + "_t")) if self.class_identifier(cls): self.shape.extra = [RDF.type] type_constraint = TripleConstraint() type_constraint.predicate = RDF.type type_constraint.valueExpr = NodeConstraint(values=[IRIREF(self.namespaces.uri_for(cls.class_uri))]) if not self.shape.expression: self.shape.expression = type_constraint else: self.shape.expression = EachOf(expressions=[self.shape.expression, type_constraint]) shapeExpr = self.shape shapeExpr.id = self._shape_iri(cls.name) self.shapes.append(shapeExpr)
def gen_multivalued_slot(self, target_name_base: str, target_type: IRIREF) -> IRIREF: """ Generate a shape that represents an RDF list of target_type @param target_name_base: @param target_type: @return: """ list_shape_id = IRIREF(target_name_base + "__List") if list_shape_id not in self.list_shapes: list_shape = Shape(id=list_shape_id, closed=True) list_shape.expression = EachOf() expressions = [ TripleConstraint(predicate=RDF.first, valueExpr=target_type, min=0, max=1) ] targets = ShapeOr() targets.shapeExprs = [(NodeConstraint(values=[RDF.nil])), list_shape_id] expressions.append( TripleConstraint(predicate=RDF.rest, valueExpr=targets)) list_shape.expression.expressions = expressions self.shapes.append(list_shape) self.list_shapes.append(list_shape_id) return list_shape_id
def add_builtins(self): # TODO: At some point we should get rid of the hard-coded builtins and add a set of TypeDefinitions for builtin_valueset = NodeConstraint(id=META.Builtins, values=[IriStem(IRIREF(XSD))]) self.shex.shapes.append(builtin_valueset) range_type_choices = ShapeOr(id=META.SlotRangeTypes, shapeExprs=[BIOENTITY.TypeDefinition, BIOENTITY.ClassDefinition, META.Builtins]) self.shex.shapes.append(range_type_choices)
def _type_arc(self, target: URIorCURIE, opt: bool = False) -> TripleConstraint: return TripleConstraint( predicate=RDF.type, valueExpr=NodeConstraint( values=[IRIREF(self.namespaces.uri_for(target))]), min=0 if opt else 1)
def visit_schema(self, **_): # Adjust the schema context to include the base model URI context = self.shex['@context'] self.shex['@context'] = [context, {'@base': self.namespaces._base}] # Emit all of the type definitions for typ in self.schema.types.values(): model_uri = self._class_or_type_uri(typ) if typ.uri: typ_type_uri = self.namespaces.uri_for(typ.uri) if typ_type_uri in (XSD.anyURI, SHEX.iri): self.shapes.append(NodeConstraint(id=model_uri, nodeKind="iri")) elif typ_type_uri == SHEX.nonLiteral: self.shapes.append(NodeConstraint(id=model_uri, nodeKind="nonliteral")) else: self.shapes.append(NodeConstraint(id=model_uri, datatype=self.namespaces.uri_for(typ.uri))) else: typeof_uri = self._class_or_type_uri(typ.typeof) self.shapes.append(Shape(id=model_uri, expression=typeof_uri))
def __init__(self, context: ParserContext, label: Optional[str]=None): ShExDocVisitor.__init__(self) self.context = context self.nodeconstraint = NodeConstraint(id=label) self._nc_values = None