diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index 13aaeee..53d400c 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -41,6 +41,21 @@ jobs: docs-folder: "sphinx/" pre-build-command: "apt-get update -y; apt-get install -y pandoc" + - name: Create context file from ttl + run: python sphinx/ttl_to_context.py + + - name: Check if HTML context directory exists + run: | + if [ ! -d "sphinx/_build/html/context/" ]; then + echo "Creating HTML context directory" + sudo mkdir -p sphinx/_build/html/context/ + else + echo "HTML context directory already exists" + fi + + - name: Copy context file to HTML directory + run: sudo cp context/context.json sphinx/_build/html/context/ + - name: Upload artifacts uses: actions/upload-artifact@v4 with: diff --git a/catalog-v001.xml b/catalog-v001.xml index ef9d839..c996e29 100644 --- a/catalog-v001.xml +++ b/catalog-v001.xml @@ -9,7 +9,7 @@ - + @@ -58,4 +58,4 @@ - \ No newline at end of file + diff --git a/contexts/BatteryModelContext.jsonld b/context/BatteryModelContext.jsonld similarity index 100% rename from contexts/BatteryModelContext.jsonld rename to context/BatteryModelContext.jsonld diff --git a/docs/scripts/ttl_to_context.py b/docs/scripts/ttl_to_context.py index 5cebece..855e21d 100644 --- a/docs/scripts/ttl_to_context.py +++ b/docs/scripts/ttl_to_context.py @@ -21,6 +21,9 @@ def generate_jsonld_context(ttl_file, predicate_uri, label_uri='http://www.w3.or """ g = rdflib.Graph() g.parse(ttl_file, format='ttl') + + CHAMEO = rdflib.Namespace("https://w3id.org/emmo/domain/chameo#") + g.bind('chameo', CHAMEO) context = {} object_properties = {} @@ -44,13 +47,15 @@ def generate_jsonld_context(ttl_file, predicate_uri, label_uri='http://www.w3.or elif p == predicate: # Normal context entry # Use the label as key if it exists - label_value = g.value(s, label) if g.value(s, label) else str(s) + #label_value = g.value(s, label) if g.value(s, label) else str(s) + label_value = str(s) other_entries[str(o)] = str(label_value) # Add namespace prefixes to the context for prefix, uri in g.namespace_manager.namespaces(): - namespace_prefixes[prefix] = str(uri) + if len(prefix) >= 2: + namespace_prefixes[prefix] = str(uri) # Sort the entries alphabetically sorted_object_properties = dict(sorted(object_properties.items())) @@ -58,7 +63,18 @@ def generate_jsonld_context(ttl_file, predicate_uri, label_uri='http://www.w3.or sorted_namespace_prefixes = dict(sorted(namespace_prefixes.items())) # Merge the sorted entries - context = {**sorted_namespace_prefixes, **sorted_object_properties, **sorted_other_entries} + context = { + "@context": { + **sorted_namespace_prefixes, + **sorted_object_properties, + **sorted_other_entries + } + } + + print("Namespaces:") + for prefix, uri in g.namespace_manager.namespaces(): + print(f"{prefix}: {uri}") + return context diff --git a/sphinx/index.rst b/sphinx/index.rst index 7f6517b..47ae8e1 100644 --- a/sphinx/index.rst +++ b/sphinx/index.rst @@ -60,7 +60,7 @@ Check out these resources to get started! Let's go! Here is some information to help you get started .. grid-item-card:: - :link: electrochemistry.html + :link: battery.html :octicon:`book;1em;sd-text-info` Class Index ^^^^^^^^^^^ diff --git a/sphinx/ttl_to_context.py b/sphinx/ttl_to_context.py new file mode 100644 index 0000000..861b2c4 --- /dev/null +++ b/sphinx/ttl_to_context.py @@ -0,0 +1,98 @@ +import rdflib +from rdflib.namespace import RDF, OWL, SKOS +import json +import os +from urllib.parse import urljoin +from urllib.request import pathname2url +import warnings + + +def generate_jsonld_context(ttl_file, predicate_uri, label_uri='http://www.w3.org/2000/01/rdf-schema#label'): + """ + Generate a JSON-LD context file from a Turtle file. + + Args: + - ttl_file: Path to the Turtle (.ttl) file. + - predicate_uri: The URI of the predicate to map to JSON-LD. + - label_uri: The URI for the label (default is rdfs:label). + + Returns: + - A Python dictionary representing the JSON-LD context. + """ + g = rdflib.Graph() + g.parse(ttl_file, format='ttl') + + CHAMEO = rdflib.Namespace("https://w3id.org/emmo/domain/chameo#") + g.bind('chameo', CHAMEO) + + context = {} + object_properties = {} + other_entries = {} + namespace_prefixes= {} + predicate = rdflib.URIRef(predicate_uri) + label = rdflib.URIRef(label_uri) + existing_keys = set() + + for s, p, o in g: + if (s, RDF.type, OWL.ObjectProperty) in g: + # If the subject is an OWL.ObjectProperty + label_value = g.value(s, SKOS.prefLabel) + if label_value: + object_properties[str(label_value)] = { + "@id": str(s), + "@type": "@id" + } + + + elif p == predicate: + # Normal context entry + # Use the label as key if it exists + #label_value = g.value(s, label) if g.value(s, label) else str(s) + label_value = str(s) + other_entries[str(o)] = str(label_value) + + + # Add namespace prefixes to the context + for prefix, uri in g.namespace_manager.namespaces(): + if len(prefix) >= 2: + namespace_prefixes[prefix] = str(uri) + + # Sort the entries alphabetically + sorted_object_properties = dict(sorted(object_properties.items())) + sorted_other_entries = dict(sorted(other_entries.items())) + sorted_namespace_prefixes = dict(sorted(namespace_prefixes.items())) + + # Merge the sorted entries + context = { + "@context": { + **sorted_namespace_prefixes, + **sorted_object_properties, + **sorted_other_entries + } + } + + print("Namespaces:") + for prefix, uri in g.namespace_manager.namespaces(): + print(f"{prefix}: {uri}") + + + return context + + +# Example usage +filename = 'battery-inferred.ttl' +parent_dir = os.path.dirname(os.path.abspath(__file__)) +file_path = os.path.join(parent_dir, '..', filename) + +# Convert the file path to a file URI +file_uri = urljoin('file:', pathname2url(file_path)) + +predicate_uri = 'http://www.w3.org/2004/02/skos/core#prefLabel' +context = generate_jsonld_context(file_uri, predicate_uri) + +# Determine the path for saving the context file in the same directory as the HTML docs +context_file_path = os.path.join(os.path.dirname(parent_dir), 'context/context.json') + +# Save to JSON file +with open(context_file_path, 'w') as f: + json.dump(context, f, indent=4) diff --git a/sphinx/ttl_to_rst.py b/sphinx/ttl_to_rst.py index 8264238..b0b1bd8 100644 --- a/sphinx/ttl_to_rst.py +++ b/sphinx/ttl_to_rst.py @@ -21,7 +21,7 @@ def extract_terms_info_sparql(g: Graph)-> list: # SPARQL QUERY # PREFIXES = """ - PREFIX emmo: + PREFIX emmo: PREFIX skos: PREFIX rdfs: """