Ejemplo n.º 1
0
def run_from_terminal():
    """Run ontology2dot from the terminal."""
    # Parse the user arguments
    parser = argparse.ArgumentParser(
        description="Convert an ontology in OWL format to "
                    "an ontology in YAML format."
    )
    parser.add_argument("to_plot", metavar="to_plot",
                        type=str, nargs="+",
                        help="Either installed namespaces or paths "
                        "to yaml ontology files")
    parser.add_argument("--output-filename", "-o",
                        type=os.path.abspath, default=None,
                        help="The name of the output file")
    parser.add_argument("--group", "-g",
                        action="store_true",
                        help="Whether to organize each namespace in a "
                        "separate cluster")
    args = parser.parse_args()

    namespaces = list()
    parser = Parser(_namespace_registry._graph)
    for x in args.to_plot:
        if x in _namespace_registry:
            namespaces.append(x)
            continue
        for n in Parser.get_namespace_names(x):
            if n in _namespace_registry:
                logger.warning("Using installed version of namespace %s" % n)
                namespaces.append(_namespace_registry[n])
            else:
                parser.parse(x)
                _namespace_registry.update_namespaces()
                namespaces.append(_namespace_registry[n])

    # Convert the ontology to dot
    converter = Ontology2Dot(
        namespaces=namespaces,
        output_filename=args.output_filename,
        group=args.group
    )
    converter.render()
Ejemplo n.º 2
0
class TestParser(unittest.TestCase):
    """Test the default parser used for parsing OWL ontologies."""

    def setUp(self):
        """Set up Graph and Parser."""
        self.graph = rdflib.Graph()
        self.parser = Parser(self.graph)

    def test_add_cuba_triples(self):
        """Test adding the cuba triples, like active relationships."""
        self.graph.add((rdflib.URIRef("has_part"), rdflib.RDF.type,
                        rdflib.OWL.ObjectProperty))
        self.graph.add((rdflib.URIRef("encloses"), rdflib.RDF.type,
                        rdflib.OWL.ObjectProperty))
        pre = set(self.graph)
        self.parser._add_cuba_triples([
            rdflib.URIRef("has_part"), rdflib.URIRef("encloses")
        ])
        self.assertEqual(set(self.graph) - pre, {
            (rdflib.URIRef("encloses"), rdflib.RDFS.subPropertyOf,
             rdflib_cuba.activeRelationship),
            (rdflib.URIRef("has_part"), rdflib.RDFS.subPropertyOf,
             rdflib_cuba.activeRelationship)
        })

    def test_add_default_rel_triples(self):
        """Test adding the default rel triples."""
        self.parser._add_default_rel_triples({
            "ns1": "has_part",
            "ns2": "encloses"
        })
        self.assertEqual(set(self.graph), {
            (rdflib.URIRef("ns1"), rdflib_cuba._default_rel,
             rdflib.URIRef("has_part")),
            (rdflib.URIRef("ns2"), rdflib_cuba._default_rel,
             rdflib.URIRef("encloses"))
        })

    def test_parse_rdf(self):
        """Test parsing an rdf file."""
        self.graph.parse(CUBA_FILE, format="ttl")
        len_cuba = len(self.graph)
        self.assertRaises(TypeError, self.parser._parse_rdf,
                          ontology_file=RDF_FILE, namespaces={})
        self.assertRaises(TypeError, self.parser._parse_rdf,
                          namespaces={}, identifier="x")
        self.assertRaises(TypeError, self.parser._parse_rdf,
                          ontology_file=RDF_FILE, identifier="x")
        self.assertRaises(TypeError, self.parser._parse_rdf,
                          ontology_file=RDF_FILE, namespaces={},
                          identifier="x", invalid=True)
        with tempfile.NamedTemporaryFile() as f:
            self.parser._parse_rdf(
                identifier="parser_test",
                ontology_file=RDF_FILE,
                namespaces={
                    "parser_test": "http://www.osp-core.com/parser_test"
                },
                format="ttl",
                file=f
            )
        rdf = rdflib.Graph()
        rdf.parse(RDF_FILE, format="ttl")
        self.assertEqual(len(self.graph), len(rdf) + len_cuba)
        self.assertEqual(len(self.parser._graphs), 1)
        self.assertEqual(len(self.parser._graphs["parser_test"]), len(rdf))
        self.assertTrue(isomorphic(self.parser._graphs["parser_test"], rdf))
        self.assertIn(dict(self.graph.namespaces())["parser_test"],
                      rdflib.URIRef("http://www.osp-core.com/parser_test#"))

    def test_parse_yml(self):
        """Test parsing the yaml file."""
        invalid = dict(YML_DOC)
        invalid["identifier"] = "parser.test"
        self.assertRaises(ValueError, self.parser._parse_yml, invalid,
                          "/a/b/c/parser_test.yml", None)
        if os.name == "posix":
            result = self.parser._parse_yml(dict(YML_DOC),
                                            "/a/b/c/parser_test.yml", None)
            self.assertEqual(result["ontology_file"],
                             "/a/b/c/parser_test.ttl")
        else:
            result = self.parser._parse_yml(dict(YML_DOC),
                                            "C:\\a\\b\\parser_test.yml", None)
            self.assertEqual(result["ontology_file"],
                             "C:\\a\\b\\parser_test.ttl")

    @responses.activate
    def test_parse_yml_download(self):
        """Test downloading owl ontologies."""
        def request_callback(request):
            headers = {'request-id': '728d329e-0e86-11e4-a748-0c84dc037c13'}
            return (200, headers, "TEST FILE CONTENT")

        url = "http://my_ontology.com/ontology.owl"
        responses.add_callback(
            responses.GET, url,
            callback=request_callback,
            content_type='text/plain',
        )
        doc = dict(YML_DOC)
        doc["ontology_file"] = url
        with tempfile.NamedTemporaryFile(mode="wb+") as f:
            r = self.parser._parse_yml(doc, "/a/b/c/parser_test.yml", f)
            self.assertEqual(r["ontology_file"], f.name)
            f.seek(0)
            self.assertEqual(f.read(), b"TEST FILE CONTENT")

    def test_get_file_path(self):
        """Test the get_file_path method."""
        self.assertEqual(self.parser.get_file_path("test/my_file.yml"),
                         "test/my_file.yml")
        self.assertEqual(
            self.parser.get_file_path("my_file").lower(),
            os.path.abspath(os.path.relpath(os.path.join(
                os.path.dirname(__file__), "..", "osp", "core", "ontology",
                "docs", "my_file.yml"
            ))).lower()
        )
        self.assertEqual(
            self.parser.get_file_path("emmo").lower(),
            os.path.abspath(os.path.relpath(os.path.join(
                os.path.dirname(__file__), "..", "osp", "core", "ontology",
                "docs", "emmo.yml"
            ))).lower()
        )
        self.assertEqual(
            self.parser.get_file_path("city").lower(),
            os.path.abspath(os.path.relpath(os.path.join(
                os.path.dirname(__file__), "..", "osp", "core", "ontology",
                "docs", "city.ontology.yml"
            ))).lower()
        )

    def test_get_identifier(self):
        """Test the get_identifier method."""
        self.assertEqual(self.parser.get_identifier(YML_DOC), "parser_test")
        self.assertEqual(self.parser.get_identifier(YML_FILE), "parser_test")
        self.assertEqual(self.parser.get_identifier("parser_test"),
                         "parser_test")
        self.assertEqual(self.parser.get_identifier("emmo"), "emmo")

    def test_get_namespace_name(self):
        """Test the get_namespace_name method."""
        self.assertEqual(self.parser.get_namespace_names(YML_DOC),
                         ["parser_test"])
        self.assertEqual(self.parser.get_namespace_names(YML_FILE),
                         ["parser_test"])
        self.assertEqual(self.parser.get_namespace_names("parser_test"),
                         ["parser_test"])
        self.assertEqual(self.parser.get_namespace_names("emmo"),
                         ['mereotopology', 'physical', 'top', 'semiotics',
                          'perceptual', 'reductionistic', 'holistic',
                          'physicalistic', 'math', 'properties', 'materials',
                          'metrology', 'models', 'manufacturing', 'isq',
                          'siunits'])

    def test_get_requirements(self):
        """Test the get_requirements() method."""
        self.assertEqual(self.parser.get_requirements(YML_DOC), set())
        self.assertEqual(self.parser.get_requirements(YML_FILE), set())
        self.assertEqual(self.parser.get_requirements("parser_test"), {"city"})

    def test_store(self):
        """Test the store method."""
        self.parser.parse(YML_FILE)
        with tempfile.TemporaryDirectory() as destination:
            self.parser.store(destination)
            self.assertEqual(os.listdir(destination),
                             ["parser_test.xml", "parser_test.yml"])
            with open(os.path.join(destination, "parser_test.yml")) as f:
                yml_doc = yaml.safe_load(f)
                self.assertEqual(yml_doc["ontology_file"], "parser_test.xml")
                yml_doc["ontology_file"] = YML_DOC["ontology_file"]
                self.assertEqual(yml_doc, YML_DOC)
            g = rdflib.Graph()
            g.parse(os.path.join(destination, "parser_test.xml"),
                    format="xml")
            self.assertTrue(
                isomorphic(g, self.parser._graphs["parser_test"]))

    def test_parse(self):
        """Test the parse method."""
        self.parser.parse(YML_FILE)
        g1 = rdflib.Graph()
        g1.parse(CUBA_FILE, format="ttl")
        g1.parse(RDF_FILE, format="ttl")
        self.assertTrue(self.parser.graph, g1)
        g2 = rdflib.Graph()
        g2.parse(RDF_FILE, format="ttl")
        self.assertTrue(self.parser._graphs["parser_test"], [g1])
        self.assertEqual(len(self.parser._yaml_docs), 1)
        self.assertEqual(
            self.parser._yaml_docs[0]["ontology_file"].lower(),
            os.path.abspath(os.path.realpath(os.path.join(
                os.path.dirname(__file__), "parser_test.ttl"))).lower()
        )
        x = dict(self.parser._yaml_docs[0])
        x["ontology_file"] = YML_DOC["ontology_file"]
        self.assertEqual(x, YML_DOC)

    def test_add_reference_style_triples(self):
        """Test adding the reference style triples."""
        self.parser._add_reference_style_triples({
            "ns1": True,
            "ns2": False,
            "ns3": True
        })
        self.assertEqual(set(self.graph), {
            (rdflib.URIRef("ns1"), rdflib_cuba._reference_by_label,
             rdflib.Literal(True)),
            (rdflib.URIRef("ns3"), rdflib_cuba._reference_by_label,
             rdflib.Literal(True)),
        })