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 visit_class(self, cls: ClassDefinition) -> bool: self.shape = Shape() # # TODO: Add this when shex 2.1 is committed # if cls.abstract: # self.shapeExpr.abstract = True return True
def visit_class(self, cls: ClassDefinition) -> bool: self.shape = Shape() # if not cls.mixin and not cls.name in self.synopsis.mixinrefs and not cls.abstract: # self.shapeExpr.closed = jsg.Boolean(True) # # TODO: Add this when shex 2.1 is committed # if cls.abstract: # self.shapeExpr.abstract = True # TODO: Figure out the semantics of union_of # TODO: symmetric return True
def _shape_not(self, ctx: Union[ShExDocParser.ShapeNotContext, ShExDocParser.InlineShapeNotContext], is_inline: bool) -> None: if ctx.KW_NOT(): self.expr = ShapeNot(id=self.label) sn = ShexShapeExpressionParser(self.context) sn.visit( ctx.shapeAtom() if not is_inline else ctx.inlineShapeAtom()) self.expr.shapeExpr = sn.expr if sn.expr is not None else Shape() else: self.visitChildren(ctx)
def visit_class(self, cls: ClassDefinition) -> bool: self.shape = Shape() # Start with all the parent classes, mixins and appytos struct_ref_list = [cls.is_a] if cls.is_a else [] struct_ref_list += cls.mixins if cls.name in self.synopsis.applytorefs: for applier in self.synopsis.applytorefs[cls.name].classrefs: struct_ref_list.append(applier) for sr in struct_ref_list: self._add_constraint(self._class_or_type_uri(sr, '_tes')) self._add_constraint(self._type_arc(self.schema.classes[sr].class_uri, opt=True)) return True
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_schema(self, output: Optional[str] = None, **_) -> None: self.shex.shapes = self.shapes if self.shapes else [Shape()] shex = as_json(self.shex) if self.format == 'rdf': g = Graph() g.parse(data=shex, format="json-ld") g.bind('owl', OWL) shex = g.serialize(format='turtle').decode() elif self.format == 'shex': g = Graph() self.namespaces.load_graph(g) shex = str(ShExC(self.shex, base=sfx(self.namespaces._base), namespaces=g)) if output: with open(output, 'w') as outf: outf.write(shex) else: print(shex)
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[Union[IRIREF, BNODE]] = None): ShExDocVisitor.__init__(self) self.context = context self.shape = Shape(label)