def test_namespace(self): schema, _ = self._schema() rdf = pyxb.namespace.NamespaceForURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#", create_if_missing=True) schema_dom = schema.toDOM() domutils.BindingDOMSupport().addXMLNSDeclaration(schema_dom.documentElement, rdf, "rdf") print() print(prettyxml(schema_dom))
def test_cardinality(self): schema, shape = self._schema() tc = TripleConstraint() tc.min = 2 tc.max = "unbounded" tc.predicate = "http://foo.bar" shape.tripleConstraint = tc print() print(prettyxml(schema))
def eval_input_file(opts: argparse.Namespace) -> None: """ Evaluate an input XML or ShExC file :param opts: function arguments """ with open(opts.infile) as inp_file: if opts.infile_format == "shex": parsed_shex = do_parse(InputStream(inp_file.read())) parsed_xml = prettyxml(parsed_shex.to_dom()) else: parsed_xml = inp_file.read() g = Graph() g.parse(opts.graph, format=opts.graph_format) print("*** " + ("Success" if eval_shexml(parsed_xml, opts.start_shape, opts.focus_node, g, opts) else "NOMATCH") + "***")
def eval_manifest_entry(name: str, entries: Iterable, opts: argparse.Namespace) -> None: """ Evaluate a manifest entry :param name: name of the manifest entry :param entries: actual manifest entries (there can be more than one per name...) :param opts: function options """ print("Evaluating: %s" % name) for entry in entries: print(" Entry: %s - " % entry.entryuri, end='') parsed_shex = do_parse(InputStream(entry.schema)) parsed_xml = prettyxml(parsed_shex.to_dom()) rslt = eval_shexml(parsed_xml, entry.start_shape, entry.subject_iri, entry.instance(fmt=opts.graph_format), opts) print("PASS" if rslt == entry.should_pass else "FAIL")
def eval_input_file(opts: argparse.Namespace) -> None: """ Evaluate an input XML or ShExC file :param opts: function arguments """ with open(opts.infile) as inp_file: if opts.infile_format == "shex": parsed_shex = do_parse(InputStream(inp_file.read())) parsed_xml = prettyxml(parsed_shex.to_dom()) else: parsed_xml = inp_file.read() g = Graph() g.parse(opts.graph, format=opts.graph_format) print("*** " + ("Success" if eval_shexml(parsed_xml, opts.start_shape, opts. focus_node, g, opts) else "NOMATCH") + "***")
def test_annotation(self): schema, shape = self._schema() tc = TripleConstraint() ann = Annotation() ann.iri = "http://ann.com" ann.iriref = IRIRef(ref="http://ann.com/2") tc.annotation.append(ann) ann = Annotation() ann.iri = "http://ann.com/2" lit = RDFLiteral("abc") ann.literal = lit tc.annotation.append(ann) tc.predicate = "http://some.org/predicate" shape.tripleConstraint = tc print() print(prettyxml(schema))
def proc_file(infile, outfile, _): if infile: print("Parsing " + infile, file=sys.stderr) else: print("Enter ShEx to parse:") print() result = do_parse(FileStream(infile, encoding='utf8') if infile else StdInputStream()) if result: pretty_result = prettyxml(result.to_dom()) if outfile: open(outfile, 'w').write(pretty_result) else: print(pretty_result) else: print("Parse failure", file=sys.stderr) return False
def test_simple_xml(self): schema = Schema() shape = Shape() shape.label = "http://a.example/IssueShape" irirange = IRIRange() irirange.base = "rdf:type" vs = ValueSet() vs.iriRange.append(irirange) oc = TripleConstraintValueClass() oc.valueSet = vs tc = TripleConstraint() tc.predicate = "rdf:type" tc.objectConstraint = oc shape.tripleConstraint = (tc) schema.shape.append(shape) print() print(prettyxml(schema))
def test_attributes(self): schema, shape = self._schema() tc = TripleConstraint() tc.min = 0 tc.max = 2 tc.predicate = "http://foo.bar" tc.object = "ex:object" tc.objectShape = "foo" tc.subject = "ex.subject" tc.subjectShape = "bar" tc.negated = True tc.datatype = "xs:string" tc.objectType = NodeType.LITERAL tc.subjectType = NodeType.IRI shape.tripleConstraint = tc print() print(prettyxml(schema))
def eval_shexml(shex_xml: str, start_shape: str, focus_node: str, g: Graph, opts: argparse.Namespace) -> bool: """ Evaluate the shex Schema instance against the start shape and focus node :param shex_xml: ShExML representation of ShEx node :param start_shape: name of starting shape in schema :param focus_node: uri of focus node in graph :param g: object graph :param opts: additional options :return: """ if opts.print: print(prettyxml(shex_xml, toxml=False)) schema_dom = StringToDOM(shex_xml) schema = CreateFromDOM(schema_dom) si = ShapeInterpreter(schema, schema_dom, g) # TODO: Kludge to fix a bug in the manifset where ? if str(start_shape) not in si._shapes: start_shape = str(start_shape).split('/')[-1] return si.i_shape(URIRef(focus_node) if focus_node else None, ShapeLabel(start_shape))
def proc_file(infile, outfile, _): if infile: print("Parsing " + infile, file=sys.stderr) else: print("Enter ShEx to parse:") print() result = do_parse( FileStream(infile, encoding='utf8') if infile else StdInputStream()) if result: pretty_result = prettyxml(result.to_dom()) if outfile: open(outfile, 'w').write(pretty_result) else: print(pretty_result) else: print("Parse failure", file=sys.stderr) return False
def eval_shexml(shex_xml: str, start_shape: str, focus_node: str, g: Graph, opts: argparse.Namespace) -> bool: """ Evaluate the shex Schema instance against the start shape and focus node :param shex_xml: ShExML representation of ShEx node :param start_shape: name of starting shape in schema :param focus_node: uri of focus node in graph :param g: object graph :param opts: additional options :return: """ if opts.print: print(prettyxml(shex_xml, toxml=False)) schema_dom = StringToDOM(shex_xml) schema = CreateFromDOM(schema_dom) si = ShapeInterpreter(schema, schema_dom, g) # TODO: Kludge to fix a bug in the manifset where ? if str(start_shape) not in si._shapes: start_shape = str(start_shape).split('/')[-1] return si.i_shape( URIRef(focus_node) if focus_node else None, ShapeLabel(start_shape))