-
Notifications
You must be signed in to change notification settings - Fork 32
Rdf serializer #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
s-heppner
merged 8 commits into
eclipse-basyx:Experimental/Adapter/RDF
from
JaFeKl:rdf_mapper
Nov 6, 2024
Merged
Rdf serializer #308
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cfdfd9a
Created RDF Serializer
JaFeKl cdcde2e
changed __init__.py to match current implementation
JaFeKl dd58cb7
added dependencies to requirements and empty line to __init__
JaFeKl a548c6d
Changed some typing to be compatible by mypy
JaFeKl 40d38f5
changed typing | to Union
JaFeKl 44c5624
Merge remote-tracking branch 'upstream/main' into rdf_mapper
JaFeKl e43d78d
added dependencies to pyproject and curlsto get schemas in ci pipeline
JaFeKl ac24a9f
Update basyx/aas/adapter/rdf/rdf_serialization.py
JaFeKl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
.. _adapter.rdf.__init__: | ||
|
||
This package contains functionality for serialization and deserialization of BaSyx Python SDK objects into RDF. | ||
|
||
:ref:`rdf_serialization <adapter.xml.rdf_serialization>`: The module offers a function to write an | ||
:class:`ObjectStore <basyx.aas.model.provider.AbstractObjectStore>` to a given file. | ||
""" | ||
|
||
from .rdf_serialization import AASToRDFEncoder, object_store_to_rdf, write_aas_rdf_file |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Copyright (c) 2023 the Eclipse BaSyx Authors | ||
# | ||
# This program and the accompanying materials are made available under the terms of the MIT License, available in | ||
# the LICENSE file of this project. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import io | ||
import os | ||
import unittest | ||
|
||
from rdflib import Graph, Namespace | ||
from pyshacl import validate | ||
|
||
from basyx.aas import model | ||
from basyx.aas.adapter.rdf import write_aas_rdf_file | ||
|
||
from basyx.aas.examples.data import example_submodel_template, example_aas_mandatory_attributes, example_aas_missing_attributes, example_aas | ||
|
||
RDF_ONTOLOGY_FILE = os.path.join(os.path.dirname(__file__), '../schemas/aasRDFOntology.ttl') | ||
RDF_SHACL_SCHEMA_FILE = os.path.join(os.path.dirname(__file__), '../schemas/aasRDFShaclSchema.ttl') | ||
|
||
|
||
class RDFSerializationTest(unittest.TestCase): | ||
def test_serialize_object(self) -> None: | ||
test_object = model.Property("test_id_short", model.datatypes.String, category="PARAMETER", | ||
description=model.MultiLanguageTextType({"en-US": "Germany", "de": "Deutschland"})) | ||
# TODO: The serialization of a single object to rdf is currently not supported. | ||
|
||
def test_random_object_serialization(self) -> None: | ||
aas_identifier = "AAS1" | ||
submodel_key = (model.Key(model.KeyTypes.SUBMODEL, "SM1"),) | ||
submodel_identifier = submodel_key[0].get_identifier() | ||
assert (submodel_identifier is not None) | ||
submodel_reference = model.ModelReference(submodel_key, model.Submodel) | ||
submodel = model.Submodel(submodel_identifier) | ||
test_aas = model.AssetAdministrationShell(model.AssetInformation(global_asset_id="Test"), | ||
aas_identifier, submodel={submodel_reference}) | ||
|
||
# TODO: The serialization of a single object to rdf is currently not supported. | ||
|
||
|
||
def validate_graph(data_graph: io.BytesIO): | ||
# load schema | ||
data_graph.seek(0) | ||
shacl_graph = Graph() | ||
shacl_graph.parse(RDF_SHACL_SCHEMA_FILE, format="turtle") | ||
|
||
# TODO: We need to remove the Sparql constraints on Abstract classes because | ||
# it somehow fails when using pychacl as validator | ||
SH = Namespace("http://www.w3.org/ns/shacl#") | ||
shacl_graph.remove((None, SH.sparql, None)) | ||
|
||
# load aas ontology | ||
aas_graph = Graph() | ||
aas_graph.parse(RDF_ONTOLOGY_FILE, format="turtle") | ||
|
||
# validate serialization against schema | ||
conforms, results_graph, results_text = validate( | ||
data_graph=data_graph, # Passing the BytesIO object here | ||
shacl_graph=shacl_graph, # The SHACL graph | ||
ont_graph=aas_graph, | ||
data_graph_format="turtle", # Specify the format for the data graph (since it's serialized) | ||
inference='both', # Optional: perform RDFS inference | ||
abort_on_first=True, # Don't continue validation after finding an error | ||
allow_infos=True, # Allow informational messages | ||
allow_warnings=True, # Allow warnings | ||
advanced=True) | ||
# print("Conforms:", conforms) | ||
# print("Validation Results:\n", results_text) | ||
assert conforms is True | ||
|
||
|
||
class RDFSerializationSchemaTest(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
if not os.path.exists(RDF_SHACL_SCHEMA_FILE): | ||
raise unittest.SkipTest(f"Shacl Schema does not exist at {RDF_SHACL_SCHEMA_FILE}, skipping test") | ||
|
||
def test_random_object_serialization(self) -> None: | ||
aas_identifier = "AAS1" | ||
submodel_key = (model.Key(model.KeyTypes.SUBMODEL, "SM1"),) | ||
submodel_identifier = submodel_key[0].get_identifier() | ||
assert submodel_identifier is not None | ||
submodel_reference = model.ModelReference(submodel_key, model.Submodel) | ||
submodel = model.Submodel(submodel_identifier, | ||
semantic_id=model.ExternalReference((model.Key(model.KeyTypes.GLOBAL_REFERENCE, | ||
"http://acplt.org/TestSemanticId"),))) | ||
test_aas = model.AssetAdministrationShell(model.AssetInformation(global_asset_id="test"), | ||
aas_identifier, submodel={submodel_reference}) | ||
|
||
# serialize object to rdf | ||
test_data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() | ||
test_data.add(test_aas) | ||
test_data.add(submodel) | ||
|
||
test_file = io.BytesIO() | ||
write_aas_rdf_file(file=test_file, data=test_data) | ||
validate_graph(test_file) | ||
|
||
def test_full_example_serialization(self) -> None: | ||
data = example_aas.create_full_example() | ||
file = io.BytesIO() | ||
write_aas_rdf_file(file=file, data=data) | ||
validate_graph(file) | ||
|
||
def test_submodel_template_serialization(self) -> None: | ||
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() | ||
data.add(example_submodel_template.create_example_submodel_template()) | ||
file = io.BytesIO() | ||
write_aas_rdf_file(file=file, data=data) | ||
validate_graph(file) | ||
|
||
def test_full_empty_example_serialization(self) -> None: | ||
data = example_aas_mandatory_attributes.create_full_example() | ||
file = io.BytesIO() | ||
write_aas_rdf_file(file=file, data=data) | ||
validate_graph(file) | ||
|
||
def test_missing_serialization(self) -> None: | ||
data = example_aas_missing_attributes.create_full_example() | ||
file = io.BytesIO() | ||
write_aas_rdf_file(file=file, data=data) | ||
validate_graph(file) | ||
|
||
def test_concept_description(self) -> None: | ||
data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() | ||
data.add(example_aas.create_example_concept_description()) | ||
file = io.BytesIO() | ||
write_aas_rdf_file(file=file, data=data) | ||
validate_graph(file) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.