Skip to content
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

Single SpdxDocument per serialization #924

Open
ilans opened this issue Nov 14, 2024 · 1 comment
Open

Single SpdxDocument per serialization #924

ilans opened this issue Nov 14, 2024 · 1 comment
Labels
Profile:Core Core Profile and related matters RDF/OWL/SHACL RDF graph, schema, ontology, constraint
Milestone

Comments

@ilans
Copy link
Collaborator

ilans commented Nov 14, 2024

From SpdxDocument:

Any instance of serialization of SPDX data MUST NOT contain more than one SpdxDocument element definition.

Suggested rule:

[
  a sh:NodeShape ;
  sh:targetNode <https://spdx.org/rdf/3.0.1/terms/Core/SpdxDocument> ;
  sh:property [
    sh:path [ sh:inversePath rdf:type ] ;
    sh:maxCount 1 ;
    sh:message "Any instance of serialization of SPDX data MUST NOT contain more than one SpdxDocument element definition."
  ]
] .

Validation script:

import rdflib
from rdflib import SH
from pyshacl import validate


shapes_graph = rdflib.Graph()
shapes_graph.parse("https://spdx.github.io/spdx-spec/v3.0.1/rdf/spdx-model.ttl", format="turtle") 

new_shape = '''
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

[
  a sh:NodeShape ;
  sh:targetNode <https://spdx.org/rdf/3.0.1/terms/Core/SpdxDocument> ;
  sh:property [
    sh:path [ sh:inversePath rdf:type ] ;
    sh:maxCount 1 ;
    sh:message "Any instance of serialization of SPDX data MUST NOT contain more than one SpdxDocument element definition."
  ]
] .
'''

shapes_graph.parse(data=new_shape, format="turtle")

s = """
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://example.org/ns#> .
@prefix spdxcore: <https://spdx.org/rdf/3.0.1/terms/Core/> .

ex:MyAgent
    a spdxcore:Agent ;
    spdxcore:creationInfo _:MyCreationInfo .

_:MyCreationInfo
    a spdxcore:CreationInfo ;
    spdxcore:createdBy ex:MyAgent ;
    spdxcore:created "2024-09-04T20:25:34Z"^^xsd:dateTimeStamp ;
    spdxcore:specVersion "3.0.1" .

ex:SpdxDocument1
    a spdxcore:SpdxDocument ;
    spdxcore:creationInfo _:MyCreationInfo .

ex:SpdxDocument2
    a spdxcore:SpdxDocument ;
    spdxcore:creationInfo _:MyCreationInfo .
"""
data_graph = rdflib.Graph()
data_graph.parse(data=s, format="turtle")

conforms, report, message = validate(data_graph, shacl_graph=shapes_graph, allow_warnings=True, advanced=True, debug=False)
for violation in report.subjects(rdflib.RDF.type, SH["ValidationResult"]):
    message = report.value(subject=violation, predicate=SH["resultMessage"], )
    print(message)

Result:
Any instance of serialization of SPDX data MUST NOT contain more than one SpdxDocument element definition.

Notes:
Running this rule on a graph database with multiple SPDX documents would fail. Not sure if this is an interesting use case, just pointing it out...

@ilans ilans added Profile:Core Core Profile and related matters RDF/OWL/SHACL RDF graph, schema, ontology, constraint labels Nov 14, 2024
@goneall
Copy link
Member

goneall commented Nov 14, 2024

Has this already been decided that we would have only one SPDX document? See issue #860

@goneall goneall added this to the 3.1 milestone Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Profile:Core Core Profile and related matters RDF/OWL/SHACL RDF graph, schema, ontology, constraint
Projects
None yet
Development

No branches or pull requests

2 participants