Skip to content

Commit

Permalink
Update generated RDF writer to latest model spec
Browse files Browse the repository at this point in the history
Signed-off-by: Holger Frydrych <[email protected]>
  • Loading branch information
fholger committed Nov 17, 2023
1 parent 7fce562 commit 722e0a8
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 196 deletions.
35 changes: 28 additions & 7 deletions src/spdx_tools/spdx3/writer/rdf/converters/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rdflib import Graph, URIRef, RDF, Literal, BNode
from rdflib.term import Identifier
from spdx_tools.spdx.casing_tools import snake_case_to_camel_case
from . import expanded_license, core, dataset, licensing, ai, security, build, software
from . import expanded_licensing, core, dataset, ai, security, build, software, simple_licensing


def a_ipackage_to_rdf(obj, graph: Graph) -> Identifier:
Expand Down Expand Up @@ -48,9 +48,16 @@ def a_ipackage_properties_to_rdf(node: Identifier, obj, graph: Graph):
prop_node = URIRef("https://spdx.org/rdf/v3/AI/informationAboutApplication")
value = obj.information_about_application
graph.add((node, prop_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
for value in obj.hyperparameter:
for key, value in obj.hyperparameter.items():
dict_node = BNode()
graph.add((dict_node, RDF.type, URIRef("https://spdx.org/rdf/v3/Core/DictionaryEntry")))
key_node = URIRef("https://spdx.org/rdf/v3/Core/key")
graph.add((dict_node, key_node, Literal(key, datatype="http://www.w3.org/2001/XMLSchema#string")))
if value is not None:
value_node = URIRef("https://spdx.org/rdf/v3/Core/value")
graph.add((dict_node, value_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
prop_node = URIRef("https://spdx.org/rdf/v3/AI/hyperparameter")
graph.add((node, prop_node, model_to_rdf(value, graph)))
graph.add((node, prop_node, dict_node))
for value in obj.model_data_preprocessing:
prop_node = URIRef("https://spdx.org/rdf/v3/AI/modelDataPreprocessing")
graph.add((node, prop_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
Expand All @@ -61,12 +68,26 @@ def a_ipackage_properties_to_rdf(node: Identifier, obj, graph: Graph):
prop_node = URIRef("https://spdx.org/rdf/v3/AI/sensitivePersonalInformation")
value = obj.sensitive_personal_information
graph.add((node, prop_node, model_to_rdf(value, graph)))
for value in obj.metric_decision_threshold:
for key, value in obj.metric_decision_threshold.items():
dict_node = BNode()
graph.add((dict_node, RDF.type, URIRef("https://spdx.org/rdf/v3/Core/DictionaryEntry")))
key_node = URIRef("https://spdx.org/rdf/v3/Core/key")
graph.add((dict_node, key_node, Literal(key, datatype="http://www.w3.org/2001/XMLSchema#string")))
if value is not None:
value_node = URIRef("https://spdx.org/rdf/v3/Core/value")
graph.add((dict_node, value_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
prop_node = URIRef("https://spdx.org/rdf/v3/AI/metricDecisionThreshold")
graph.add((node, prop_node, model_to_rdf(value, graph)))
for value in obj.metric:
graph.add((node, prop_node, dict_node))
for key, value in obj.metric.items():
dict_node = BNode()
graph.add((dict_node, RDF.type, URIRef("https://spdx.org/rdf/v3/Core/DictionaryEntry")))
key_node = URIRef("https://spdx.org/rdf/v3/Core/key")
graph.add((dict_node, key_node, Literal(key, datatype="http://www.w3.org/2001/XMLSchema#string")))
if value is not None:
value_node = URIRef("https://spdx.org/rdf/v3/Core/value")
graph.add((dict_node, value_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
prop_node = URIRef("https://spdx.org/rdf/v3/AI/metric")
graph.add((node, prop_node, model_to_rdf(value, graph)))
graph.add((node, prop_node, dict_node))
for value in obj.domain:
prop_node = URIRef("https://spdx.org/rdf/v3/AI/domain")
graph.add((node, prop_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
Expand Down
24 changes: 19 additions & 5 deletions src/spdx_tools/spdx3/writer/rdf/converters/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rdflib import Graph, URIRef, RDF, Literal, BNode
from rdflib.term import Identifier
from spdx_tools.spdx.casing_tools import snake_case_to_camel_case
from . import expanded_license, core, dataset, licensing, ai, security, build, software
from . import expanded_licensing, core, dataset, ai, security, build, software, simple_licensing


def build_to_rdf(obj, graph: Graph) -> Identifier:
Expand Down Expand Up @@ -43,9 +43,16 @@ def build_properties_to_rdf(node: Identifier, obj, graph: Graph):
for value in obj.config_source_digest:
prop_node = URIRef("https://spdx.org/rdf/v3/Build/configSourceDigest")
graph.add((node, prop_node, model_to_rdf(value, graph)))
for value in obj.parameters:
for key, value in obj.parameters.items():
dict_node = BNode()
graph.add((dict_node, RDF.type, URIRef("https://spdx.org/rdf/v3/Core/DictionaryEntry")))
key_node = URIRef("https://spdx.org/rdf/v3/Core/key")
graph.add((dict_node, key_node, Literal(key, datatype="http://www.w3.org/2001/XMLSchema#string")))
if value is not None:
value_node = URIRef("https://spdx.org/rdf/v3/Core/value")
graph.add((dict_node, value_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
prop_node = URIRef("https://spdx.org/rdf/v3/Build/parameters")
graph.add((node, prop_node, model_to_rdf(value, graph)))
graph.add((node, prop_node, dict_node))
if obj.build_start_time is not None:
prop_node = URIRef("https://spdx.org/rdf/v3/Build/buildStartTime")
value = obj.build_start_time
Expand All @@ -54,9 +61,16 @@ def build_properties_to_rdf(node: Identifier, obj, graph: Graph):
prop_node = URIRef("https://spdx.org/rdf/v3/Build/buildEndTime")
value = obj.build_end_time
graph.add((node, prop_node, Literal(value, datatype="https://spdx.org/rdf/v3/Core/DateTime")))
for value in obj.environment:
for key, value in obj.environment.items():
dict_node = BNode()
graph.add((dict_node, RDF.type, URIRef("https://spdx.org/rdf/v3/Core/DictionaryEntry")))
key_node = URIRef("https://spdx.org/rdf/v3/Core/key")
graph.add((dict_node, key_node, Literal(key, datatype="http://www.w3.org/2001/XMLSchema#string")))
if value is not None:
value_node = URIRef("https://spdx.org/rdf/v3/Core/value")
graph.add((dict_node, value_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
prop_node = URIRef("https://spdx.org/rdf/v3/Build/environment")
graph.add((node, prop_node, model_to_rdf(value, graph)))
graph.add((node, prop_node, dict_node))
core.element_properties_to_rdf(node, obj, graph)


Expand Down
40 changes: 20 additions & 20 deletions src/spdx_tools/spdx3/writer/rdf/converters/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@
from rdflib.term import Identifier
from spdx_tools.spdx.casing_tools import snake_case_to_camel_case
from spdx_tools.spdx3.model import HashAlgorithm
from . import expanded_license, core, dataset, licensing, ai, security, build, software
from . import expanded_licensing, core, dataset, ai, security, build, software, simple_licensing


_CONVERTER_FUNCTIONS: Dict[str, Callable[[any, Graph], Identifier]] = {
"ExpandedLicense/ConjunctiveLicenseSet": expanded_license.conjunctive_license_set_to_rdf,
"ExpandedLicense/DisjunctiveLicenseSet": expanded_license.disjunctive_license_set_to_rdf,
"ExpandedLicense/ExtendableLicense": expanded_license.extendable_license_to_rdf,
"ExpandedLicensing/ListedLicense": expanded_licensing.listed_license_to_rdf,
"ExpandedLicensing/WithAdditionOperator": expanded_licensing.with_addition_operator_to_rdf,
"ExpandedLicensing/License": expanded_licensing.license_to_rdf,
"ExpandedLicensing/CustomLicenseAddition": expanded_licensing.custom_license_addition_to_rdf,
"ExpandedLicensing/ConjunctiveLicenseSet": expanded_licensing.conjunctive_license_set_to_rdf,
"ExpandedLicensing/LicenseAddition": expanded_licensing.license_addition_to_rdf,
"ExpandedLicensing/OrLaterOperator": expanded_licensing.or_later_operator_to_rdf,
"ExpandedLicensing/DisjunctiveLicenseSet": expanded_licensing.disjunctive_license_set_to_rdf,
"ExpandedLicensing/CustomLicense": expanded_licensing.custom_license_to_rdf,
"ExpandedLicensing/ListedLicenseException": expanded_licensing.listed_license_exception_to_rdf,
"ExpandedLicensing/ExtendableLicense": expanded_licensing.extendable_license_to_rdf,
"Core/PositiveIntegerRange": core.positive_integer_range_to_rdf,
"Core/ElementCollection": core.element_collection_to_rdf,
"Core/ExternalReference": core.external_reference_to_rdf,
"Core/ExternalIdentifier": core.external_identifier_to_rdf,
"Core/Bom": core.bom_to_rdf,
"Core/SpdxDocument": core.spdx_document_to_rdf,
"Core/Tool": core.tool_to_rdf,
"Core/NamespaceMap": core.namespace_map_to_rdf,
"Core/CreationInfo": core.creation_info_to_rdf,
"Core/Organization": core.organization_to_rdf,
"Core/LifecycleScopedRelationship": core.lifecycle_scoped_relationship_to_rdf,
Expand Down Expand Up @@ -53,16 +60,6 @@
"Dataset/DatasetType": dataset.dataset_type_to_rdf,
"Dataset/DatasetAvailabilityType": dataset.dataset_availability_type_to_rdf,
"Dataset/ConfidentialityLevelType": dataset.confidentiality_level_type_to_rdf,
"Licensing/ListedLicense": licensing.listed_license_to_rdf,
"Licensing/WithAdditionOperator": licensing.with_addition_operator_to_rdf,
"Licensing/License": licensing.license_to_rdf,
"Licensing/CustomLicenseAddition": licensing.custom_license_addition_to_rdf,
"Licensing/LicenseAddition": licensing.license_addition_to_rdf,
"Licensing/OrLaterOperator": licensing.or_later_operator_to_rdf,
"Licensing/CustomLicense": licensing.custom_license_to_rdf,
"Licensing/LicenseExpression": licensing.license_expression_to_rdf,
"Licensing/ListedLicenseException": licensing.listed_license_exception_to_rdf,
"Licensing/AnyLicenseInfo": licensing.any_license_info_to_rdf,
"AI/AIPackage": ai.a_ipackage_to_rdf,
"AI/SafetyRiskAssessmentType": ai.safety_risk_assessment_type_to_rdf,
"AI/PresenceType": ai.presence_type_to_rdf,
Expand Down Expand Up @@ -91,21 +88,22 @@
"Software/DependencyConditionalityType": software.dependency_conditionality_type_to_rdf,
"Software/SoftwarePurpose": software.software_purpose_to_rdf,
"Software/SoftwareDependencyLinkType": software.software_dependency_link_type_to_rdf,
"Software/SbomType": software.sbom_type_to_rdf
"Software/SbomType": software.sbom_type_to_rdf,
"SimpleLicensing/SimpleLicensingText": simple_licensing.simple_licensing_text_to_rdf,
"SimpleLicensing/LicenseExpression": simple_licensing.license_expression_to_rdf,
"SimpleLicensing/AnyLicenseInfo": simple_licensing.any_license_info_to_rdf
}


def module_to_namespace(module: str) -> Optional[str]:
if not module.startswith("spdx_tools.spdx3.model"):
return None
if module.startswith("spdx_tools.spdx3.model.expanded_license"):
return "ExpandedLicense"
if module.startswith("spdx_tools.spdx3.model.expanded_licensing"):
return "ExpandedLicensing"
if module.startswith("spdx_tools.spdx3.model.core"):
return "Core"
if module.startswith("spdx_tools.spdx3.model.dataset"):
return "Dataset"
if module.startswith("spdx_tools.spdx3.model.licensing"):
return "Licensing"
if module.startswith("spdx_tools.spdx3.model.ai"):
return "AI"
if module.startswith("spdx_tools.spdx3.model.security"):
Expand All @@ -114,6 +112,8 @@ def module_to_namespace(module: str) -> Optional[str]:
return "Build"
if module.startswith("spdx_tools.spdx3.model.software"):
return "Software"
if module.startswith("spdx_tools.spdx3.model.simple_licensing"):
return "SimpleLicensing"
return "Core"


Expand Down
36 changes: 1 addition & 35 deletions src/spdx_tools/spdx3/writer/rdf/converters/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rdflib import Graph, URIRef, RDF, Literal, BNode
from rdflib.term import Identifier
from spdx_tools.spdx.casing_tools import snake_case_to_camel_case
from . import expanded_license, core, dataset, licensing, ai, security, build, software
from . import expanded_licensing, core, dataset, ai, security, build, software, simple_licensing


def positive_integer_range_to_rdf(obj, graph: Graph) -> Identifier:
Expand Down Expand Up @@ -55,9 +55,6 @@ def element_collection_properties_to_rdf(node: Identifier, obj, graph: Graph):
for value in obj.root_element:
prop_node = URIRef("https://spdx.org/rdf/v3/Core/rootElement")
graph.add((node, prop_node, model_to_rdf(value, graph)))
for value in obj.namespaces:
prop_node = URIRef("https://spdx.org/rdf/v3/Core/namespaces")
graph.add((node, prop_node, model_to_rdf(value, graph)))
for value in obj.imports:
prop_node = URIRef("https://spdx.org/rdf/v3/Core/imports")
graph.add((node, prop_node, model_to_rdf(value, graph)))
Expand Down Expand Up @@ -157,10 +154,6 @@ def spdx_document_to_rdf(obj, graph: Graph) -> Identifier:

def spdx_document_properties_to_rdf(node: Identifier, obj, graph: Graph):
from .converter import model_to_rdf
if obj.name is not None:
prop_node = URIRef("https://spdx.org/rdf/v3/Core/name")
value = obj.name
graph.add((node, prop_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
core.bundle_properties_to_rdf(node, obj, graph)


Expand All @@ -180,29 +173,6 @@ def tool_properties_to_rdf(node: Identifier, obj, graph: Graph):
core.element_properties_to_rdf(node, obj, graph)


def namespace_map_to_rdf(obj, graph: Graph) -> Identifier:
if '_spdx_id' in obj.__dict__:
element_node = URIRef(obj.spdx_id)
else:
element_node = BNode()
type_node = URIRef("https://spdx.org/rdf/v3/Core/NamespaceMap")
graph.add((element_node, RDF.type, type_node))
namespace_map_properties_to_rdf(element_node, obj, graph)
return element_node


def namespace_map_properties_to_rdf(node: Identifier, obj, graph: Graph):
from .converter import model_to_rdf
if obj.prefix is not None:
prop_node = URIRef("https://spdx.org/rdf/v3/Core/prefix")
value = obj.prefix
graph.add((node, prop_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
if obj.namespace is not None:
prop_node = URIRef("https://spdx.org/rdf/v3/Core/namespace")
value = obj.namespace
graph.add((node, prop_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#anyURI")))


def creation_info_to_rdf(obj, graph: Graph) -> Identifier:
if '_spdx_id' in obj.__dict__:
element_node = URIRef(obj.spdx_id)
Expand Down Expand Up @@ -395,10 +365,6 @@ def element_properties_to_rdf(node: Identifier, obj, graph: Graph):
for value in obj.external_identifier:
prop_node = URIRef("https://spdx.org/rdf/v3/Core/externalIdentifier")
graph.add((node, prop_node, model_to_rdf(value, graph)))
if obj.extension is not None:
prop_node = URIRef("https://spdx.org/rdf/v3/Core/extension")
value = obj.extension
graph.add((node, prop_node, Literal(value, datatype="https://spdx.org/rdf/v3/Core/Extension")))


def agent_to_rdf(obj, graph: Graph) -> Identifier:
Expand Down
13 changes: 10 additions & 3 deletions src/spdx_tools/spdx3/writer/rdf/converters/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rdflib import Graph, URIRef, RDF, Literal, BNode
from rdflib.term import Identifier
from spdx_tools.spdx.casing_tools import snake_case_to_camel_case
from . import expanded_license, core, dataset, licensing, ai, security, build, software
from . import expanded_licensing, core, dataset, ai, security, build, software, simple_licensing


def dataset_to_rdf(obj, graph: Graph) -> Identifier:
Expand Down Expand Up @@ -48,9 +48,16 @@ def dataset_properties_to_rdf(node: Identifier, obj, graph: Graph):
for value in obj.data_preprocessing:
prop_node = URIRef("https://spdx.org/rdf/v3/Dataset/dataPreprocessing")
graph.add((node, prop_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
for value in obj.sensor:
for key, value in obj.sensor.items():
dict_node = BNode()
graph.add((dict_node, RDF.type, URIRef("https://spdx.org/rdf/v3/Core/DictionaryEntry")))
key_node = URIRef("https://spdx.org/rdf/v3/Core/key")
graph.add((dict_node, key_node, Literal(key, datatype="http://www.w3.org/2001/XMLSchema#string")))
if value is not None:
value_node = URIRef("https://spdx.org/rdf/v3/Core/value")
graph.add((dict_node, value_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
prop_node = URIRef("https://spdx.org/rdf/v3/Dataset/sensor")
graph.add((node, prop_node, model_to_rdf(value, graph)))
graph.add((node, prop_node, dict_node))
for value in obj.known_bias:
prop_node = URIRef("https://spdx.org/rdf/v3/Dataset/knownBias")
graph.add((node, prop_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
Expand Down
70 changes: 0 additions & 70 deletions src/spdx_tools/spdx3/writer/rdf/converters/expanded_license.py

This file was deleted.

Loading

0 comments on commit 722e0a8

Please sign in to comment.