Exemple #1
0
    def test_meta_output(self):
        """ Generate a context AND a jsonld for the metamodel and make sure it parses as RDF """
        jsonld_path = os.path.join(testscriptstempdir, 'metajson.jsonld')
        rdf_path = os.path.join(testscriptstempdir, 'metardf.ttl')
        meta_context_path = os.path.join(testscriptstempdir,
                                         'metacontext.jsonld')

        # Generate an image of the metamodel
        gen = ContextGenerator(source_yaml_path)
        base = gen.schema.id
        if base[-1] not in '/#':
            base += '/'
        base += gen.schema.name
        with open(meta_context_path, 'w') as tfile:
            tfile.write(gen.serialize())
        with open(jsonld_path, 'w') as tfile:
            tfile.write(JSONLDGenerator(source_yaml_path, fmt=JSONLDGenerator.valid_formats[0])\
                .serialize(context=meta_context_path))
        g = Graph()
        g.load(jsonld_path, format="json-ld")
        g.serialize(rdf_path, format="ttl")
        g.bind('meta', METAMODEL_NAMESPACE)
        new_ttl = g.serialize(format="turtle").decode()
        new_g = Graph()
        new_g.parse(data=new_ttl, format="turtle")
        self.check_size(g, new_g, URIRef(base), 11, 79, 11, "meta")
Exemple #2
0
    def end_schema(self, output: Optional[str] = None, context: str = METAMODEL_CONTEXT_URI, **_) -> None:
        gen = JSONLDGenerator(self, fmt=JSONLDGenerator.valid_formats[0], emit_metadata=self.emit_metadata,
                              importmap=self.importmap)
        # Iterate over permissible text strings making them URI compatible
        for e in gen.schema.enums.values():
            for pv in e.permissible_values.values():
                pv.text = urlparse.quote(pv.text)
        jsonld_str = gen.serialize(context=context)

        graph = Graph()
        graph.parse(data=jsonld_str, format="json-ld", base=self.namespaces._base, prefix=True)
        if output:
            with open(output, 'w') as outf:
                outf.write(self._data(graph))
        else:
            print(self._data(graph))
Exemple #3
0
    def test_mappings_rdf(self):
        """ Test the imported mappings in the biolink metamodel """
        test_dir = self.env.temp_file_path('mappings_rdf_test', is_dir=True)

        # Create the mappings json file
        json_file = os.path.join(test_dir, 'mappings.jsonld')
        json_str = JSONLDGenerator(env.meta_yaml, importmap=env.import_map).serialize()
        with open(json_file, 'w') as f:
            f.write(json_str)

        # Create the mappings context file
        context_file = os.path.join(test_dir, 'mappings.context.jsonld')
        ContextGenerator(env.meta_yaml, importmap=env.import_map).serialize(output=context_file)
        self.assertTrue(os.path.exists(context_file))

        # Generate context and use it to create the RDF
        self.single_file_generator('context.jsonld', ContextGenerator, filtr=ldcontext_metadata_filter, subdir='includes')

        # Generate a copy of the JSON representation of the model
        context_loc = json_file
        context_args = {"context": ['file://' + LOCAL_METAMODEL_LDCONTEXT_FILE, 'file://' + context_loc]}
        msg += self.single_file_generator('json', JSONLDGenerator,  serialize_args=context_args,
                                         filtr=json_metadata_context_filter, fail_if_expected_missing=False)

        # Make a fresh copy of the RDF and validate it as well
        msg += self.single_file_generator('ttl', RDFGenerator, serialize_args=context_args,
                                          comparator=GeneratorTestCase.rdf_comparator, fail_if_expected_missing=False)
        if msg:
            self.fail(msg)

        g = Graph()
        rdf_file = os.path.join(sourcedir, 'meta_mappings.ttl')
        g.load(rdf_file, format='turtle')
        ns = PrefixLibrary()
        ns.add_rdf(g)
        ns['FULL'] = "http://example.org/fulluri/"
        ns['EX'] = "http://example.org/mappings/"
        ns['META'] = "https://w3id.org/biolink/biolinkml/meta/"
        # Make sure that the expected triples got added

        self.assertEqual({ns.EX.slot1_close, ns.FULL.slot1_close}, set(g.objects(ns.EX.s1, ns.SKOS.closeMatch)))
        self.assertEqual({ns.EX.slot1, ns.FULL.slot1}, set(g.objects(ns.EX.s1, ns.SKOS.exactMatch)))
        self.assertEqual(ns.EX.s3, g.value(ns.EX.s1, ns.META.deprecated_element_has_exact_replacement, any=False))
        self.assertEqual(ns.EX.s4, g.value(ns.EX.s1, ns.META.deprecated_element_has_possible_replacement, any=False))

        self.assertEqual({ns.EX.class1_close, ns.FULL.class1_close}, set(g.objects(ns.EX.C1, ns.SKOS.closeMatch)))
        self.assertEqual({ns.EX.class1, ns.FULL.class1}, set(g.objects(ns.EX.C1, ns.SKOS.exactMatch)))
        self.assertEqual(ns.EX.c2, g.value(ns.EX.C1, ns.META.deprecated_element_has_exact_replacement, any=False))
        self.assertEqual(ns.EX.c3, g.value(ns.EX.C1, ns.META.deprecated_element_has_possible_replacement, any=False))
        if DO_SHEX_VALIDATION:
            EX = Namespace("http://example.org/mappings/")
            focus = EX.testMetamodelMappings
            start = METAMODEL_NAMESPACE.SchemaDefinition
            results = ShExEvaluator(g, LOCAL_SHEXJ_FILE_NAME, focus, start).evaluate(debug=False)
            self.assertTrue(self._evaluate_shex_results(results))
        else:
            print("*** RDF Model validation step was skipped. Set: tests.__init__.DO_SHEX_VALIDATION to run it")