ソースを参照

Move prefix import to configuration

Arkadiusz Ryś 2 年 前
コミット
a245bfca4f

+ 3 - 0
README.rst

@@ -5,6 +5,9 @@ Graph Exploring Tool
 .. image:: docs/ui.png
    :alt: Screenshot of the user interface
 
+Virtual Knowledge Graph exploration tool built for use with a graph based on the FTG+PM concept.
+
+
 Installation
 ------------
 

+ 67 - 0
data/configuration.toml

@@ -17,3 +17,70 @@ SELECT ?outlier ?outlier_relation ?outlier_value WHERE {
   }
 }
 '''
+
+# Functions and services
+[[prefixes]]
+name = "dtf"
+namespace = "https://ontology.rys.app/dt/function/"
+# Generic
+[[prefixes]]
+name = "owl"
+namespace = "http://www.w3.org/2002/07/owl#"
+[[prefixes]]
+name = "rdf"
+namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+[[prefixes]]
+name = "xsd"
+namespace = "http://www.w3.org/2001/XMLSchema#"
+[[prefixes]]
+name = "rdfs"
+namespace = "http://www.w3.org/2000/01/rdf-schema#"
+[[prefixes]]
+name = "dc"
+namespace = "http://purl.org/dc/elements/1.1/"
+[[prefixes]]
+name = "vim4"
+namespace = "http://bipm.org/jcgm/vim4#"
+# Digital Twin Design
+[[prefixes]]
+name = "art"
+namespace = "http://ua.be/drivetrain/description/artifacts/artifacts#"
+[[prefixes]]
+name = "tabular"
+namespace = "http://ua.be/sdo2l/vocabulary/base/tabular#"
+[[prefixes]]
+name = "ftg"
+namespace = "http://ua.be/sdo2l/vocabulary/ftg#"
+[[prefixes]]
+name = "base"
+namespace = "http://ua.be/sdo2l/vocabulary/base#"
+[[prefixes]]
+name = "comp"
+namespace = "http://ua.be/sdo2l/vocabulary/component#"
+[[prefixes]]
+name = "code"
+namespace = "http://ua.be/sdo2l/vocabulary/base/code#"
+[[prefixes]]
+name = "script"
+namespace = "http://ua.be/sdo2l/vocabulary/base/script#"
+[[prefixes]]
+name = "file"
+namespace = "http://ua.be/sdo2l/vocabulary/base/file#"
+[[prefixes]]
+name = "swrl"
+namespace = "http://www.w3.org/2003/11/swrl#"
+[[prefixes]]
+name = "traces"
+namespace = "http://ua.be/sdo2l/vocabulary/processtraces#"
+[[prefixes]]
+name = "wf"
+namespace = "http://ua.be/sdo2l/vocabulary/workflow#"
+[[prefixes]]
+name = "text"
+namespace = "http://ua.be/sdo2l/vocabulary/base/text#"
+[[prefixes]]
+name = "federation"
+namespace = "http://ua.be/sdo2l/vocabulary/federation#"
+[[prefixes]]
+name = "dftg"
+namespace = "http://ua.be/drivetrain/description/ftg#"

+ 0 - 66
data/prefixes.toml

@@ -1,66 +0,0 @@
-# Functions and services
-[[prefixes]]
-name = "dtf"
-namespace = "https://ontology.rys.app/dt/function/"
-# Generic
-[[prefixes]]
-name = "owl"
-namespace = "http://www.w3.org/2002/07/owl#"
-[[prefixes]]
-name = "rdf"
-namespace = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
-[[prefixes]]
-name = "xsd"
-namespace = "http://www.w3.org/2001/XMLSchema#"
-[[prefixes]]
-name = "rdfs"
-namespace = "http://www.w3.org/2000/01/rdf-schema#"
-[[prefixes]]
-name = "dc"
-namespace = "http://purl.org/dc/elements/1.1/"
-[[prefixes]]
-name = "vim4"
-namespace = "http://bipm.org/jcgm/vim4#"
-# Digital Twin Design
-[[prefixes]]
-name = "art"
-namespace = "http://ua.be/drivetrain/description/artifacts/artifacts#"
-[[prefixes]]
-name = "tabular"
-namespace = "http://ua.be/sdo2l/vocabulary/base/tabular#"
-[[prefixes]]
-name = "ftg"
-namespace = "http://ua.be/sdo2l/vocabulary/ftg#"
-[[prefixes]]
-name = "base"
-namespace = "http://ua.be/sdo2l/vocabulary/base#"
-[[prefixes]]
-name = "comp"
-namespace = "http://ua.be/sdo2l/vocabulary/component#"
-[[prefixes]]
-name = "code"
-namespace = "http://ua.be/sdo2l/vocabulary/base/code#"
-[[prefixes]]
-name = "script"
-namespace = "http://ua.be/sdo2l/vocabulary/base/script#"
-[[prefixes]]
-name = "file"
-namespace = "http://ua.be/sdo2l/vocabulary/base/file#"
-[[prefixes]]
-name = "swrl"
-namespace = "http://www.w3.org/2003/11/swrl#"
-[[prefixes]]
-name = "traces"
-namespace = "http://ua.be/sdo2l/vocabulary/processtraces#"
-[[prefixes]]
-name = "wf"
-namespace = "http://ua.be/sdo2l/vocabulary/workflow#"
-[[prefixes]]
-name = "text"
-namespace = "http://ua.be/sdo2l/vocabulary/base/text#"
-[[prefixes]]
-name = "federation"
-namespace = "http://ua.be/sdo2l/vocabulary/federation#"
-[[prefixes]]
-name = "dftg"
-namespace = "http://ua.be/drivetrain/description/ftg#"

+ 24 - 0
graph_exploring_tool/configuration.py

@@ -0,0 +1,24 @@
+from collections import Counter
+from dataclasses import dataclass, field
+from typing import Iterable, List
+
+from graph_exploring_tool.query import Prefix
+
+
+def get_duplicates(candidates: Iterable) -> List:
+    for key, count in Counter(candidates).items():
+        if count > 1:
+            yield key
+
+
+@dataclass(init=True, repr=True, order=False, frozen=True)
+class Configuration:
+    endpoint_sparql: str = ""
+    example_prefix: str = ""
+    example_query: str = ""
+    prefixes: List[Prefix] = field(default_factory=list)
+
+    def check_for_duplicate_prefixes(self):
+        """"""
+        yield from get_duplicates(prefix.name for prefix in self.prefixes)
+        yield from get_duplicates(prefix.namespace for prefix in self.prefixes)

+ 5 - 4
graph_exploring_tool/graphical/interface.py

@@ -11,6 +11,7 @@ import dearpygui.dearpygui as dpg
 import dearpygui.demo as demo
 
 from graph_exploring_tool import query
+from graph_exploring_tool.configuration import Configuration
 from graph_exploring_tool.query import QueryTemplate
 
 arklog.set_config_logging()
@@ -291,7 +292,7 @@ def _select_file(sender: int, app_data: str, user_data: Optional[dict]):
     selected_file = cwd + "/" + selected_file
     dpg.set_value("file_info_n", "file :" + selected_file)
 
-def interface(example_prefix: str, example_query: str, palette: List[QueryTemplate], endpoint: str, width=1200, height=900):
+def interface(configuration: Configuration, palette: List[QueryTemplate], width=1200, height=900):
     """Show the full user interface."""
     dpg.create_context()
     dpg.create_viewport(title="Graph Exploring Tool", width=width, height=height)
@@ -300,9 +301,9 @@ def interface(example_prefix: str, example_query: str, palette: List[QueryTempla
         with dpg.group(horizontal=True, label="Main"):
             create_query_palette(palette)
             with dpg.group(horizontal=False):
-                create_query_options(endpoint)
-                create_query_editor_visual(example_prefix, example_query, show=True)
-                create_query_editor_textual(example_prefix, example_query, show=False)
+                create_query_options(configuration.endpoint_sparql)
+                create_query_editor_visual(configuration.example_prefix, configuration.example_query, show=True)
+                create_query_editor_textual(configuration.example_prefix, configuration.example_query, show=False)
                 create_status_console()
                 create_query_results()
     # demo.show_demo()

+ 18 - 9
graph_exploring_tool/main.py

@@ -9,6 +9,7 @@ import dacite
 import toml
 
 from graph_exploring_tool import __version__
+from graph_exploring_tool.configuration import Configuration
 from graph_exploring_tool.graphical.interface import interface
 from graph_exploring_tool.query import QueryTemplate
 
@@ -18,6 +19,7 @@ def handler(signal_code, _) -> None:
     logging.debug(f"Shutting down because signal {signal_code} was received.")
     sys.exit(5)
 
+
 def load_palette(palette_directory: Path) -> List:
     """"""
     for query_path in palette_directory.rglob("*.toml"):
@@ -31,23 +33,30 @@ def launch():
     signal(SIGINT, handler)
     signal(SIGTERM, handler)
     arklog.set_config_logging()
-    logging.info(f"GET {__version__}.")
+    logging.info(f"Graph Exploring Tool {__version__}.")
     data_dir = Path(__file__).resolve().parent.parent / Path("data")
+
     logging.debug(f"Looking for configuration in '{data_dir}'.")
     try:
         configuration = toml.loads((data_dir / Path("configuration.toml")).read_text(encoding="utf-8"))
+        configuration = dacite.from_dict(data_class=Configuration, data=configuration, )
     except FileNotFoundError as e:
         logging.error(f"Configuration not found. {e}")
         return 8
-    endpoint_sparql = configuration.get("endpoint_sparql", "http://127.0.0.1:3030/Drivetrain/sparql")
-    example_prefix = configuration.get("example_prefix", "")
-    example_query = configuration.get("example_query", "")
-    logging.debug("Parameters:")
-    logging.debug(f"  -> {endpoint_sparql=}")
-    logging.debug(f"  -> {example_prefix=}")
-    logging.debug(f"  -> {example_query=}")
+
+    logging.debug("Configuration:")
+    logging.debug(f"  -> {configuration.example_prefix=}")
+    logging.debug(f"  -> {configuration.example_query=}")
+    logging.debug(f"  -> {configuration.endpoint_sparql=}")
+    logging.debug(f"  -> {configuration.prefixes=}")
+
+    prefix_duplicates = configuration.check_for_duplicate_prefixes()
+    for duplicate in prefix_duplicates:
+        logging.warning(f"Duplicate prefix '{duplicate}'.")
+
     palette = load_palette(data_dir / Path("palette"))
-    return interface(endpoint=endpoint_sparql, example_prefix=example_prefix, example_query=example_query, palette=palette)
+    return interface(configuration=configuration, palette=palette)
+
 
 if __name__ == "__main__":
     launch()

+ 10 - 0
graph_exploring_tool/query.py

@@ -8,6 +8,16 @@ from SPARQLWrapper.SPARQLExceptions import QueryBadFormed
 
 arklog.set_config_logging()
 
+@dataclass(init=True, repr=True, order=False, frozen=True)
+class Prefix:
+    name: str
+    namespace: str
+
+    def __str__(self) -> str:
+        """Return the prefix in the SPARQL format."""
+        return f"PREFIX {self.name}: <{self.namespace}>"
+
+
 @dataclass(init=True, repr=True, order=False, frozen=True)
 class Replacement:
     placeholder: str