Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jsimonclark committed Apr 1, 2024
2 parents c7c1b3b + af09e8d commit b5eecd9
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions catalog-v001.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<uri name="https://w3id.org/emmo/domain/battery/0.1.0-beta/batteryproducts" uri="./batteryproducts.ttl"/>

<uri name="https://w3id.org/emmo/domain/chemicalsubstance/0.4.0-beta/chemicalsubstance" uri="https://raw.githubusercontent.com/emmo-repo/domain-chemicalsubstance/main/chemicalsubstance.ttl"/>
<uri name="https://w3id.org/emmo/domain/chameo/1.0.0-beta3/chameo" uri="https://raw.githubusercontent.com/emmo-repo/domain-characterisation-methodology/version-update-emmo-1.0.0-beta7/chameo.ttl"/>
<uri name="https://w3id.org/emmo/domain/chameo/1.0.0-beta3/chameo" uri="https://raw.githubusercontent.com/emmo-repo/domain-characterisation-methodology/main/chameo.ttl"/>

<uri name="https://w3id.org/emmo/1.0.0-beta7/mereocausality" uri="https://raw.githubusercontent.com/emmo-repo/EMMO/1.0.0-beta7/mereocausality/mereocausality.ttl"/>

Expand Down Expand Up @@ -58,4 +58,4 @@
<uri name="https://w3id.org/emmo/1.0.0-beta7/disciplines/prefixedunits" uri="https://raw.githubusercontent.com/emmo-repo/EMMO/1.0.0-beta7/disciplines/units/prefixedunits.ttl"/>
<uri name="https://w3id.org/emmo/1.0.0-beta7/disciplines/deprecated" uri="https://raw.githubusercontent.com/emmo-repo/EMMO/1.0.0-beta7/disciplines/units/deprecated.ttl"/>
</group>
</catalog>
</catalog>
File renamed without changes.
22 changes: 19 additions & 3 deletions docs/scripts/ttl_to_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -44,21 +47,34 @@ 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()))
sorted_other_entries = dict(sorted(other_entries.items()))
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

Expand Down
2 changes: 1 addition & 1 deletion sphinx/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^
Expand Down
98 changes: 98 additions & 0 deletions sphinx/ttl_to_context.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion sphinx/ttl_to_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def extract_terms_info_sparql(g: Graph)-> list:

# SPARQL QUERY #
PREFIXES = """
PREFIX emmo: <http://emmo.info/emmo#>
PREFIX emmo: <https://w3id.org/emmo#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
"""
Expand Down

0 comments on commit b5eecd9

Please sign in to comment.