Skip to content

Commit 8b9edbd

Browse files
exported files now have all referenced entities (#4622)
lint, change comments into debug log statements.
1 parent 59f9e4d commit 8b9edbd

File tree

3 files changed

+198
-19
lines changed

3 files changed

+198
-19
lines changed

software/SchemaTerms/sdotermsource.py

Lines changed: 150 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,152 @@
3838
LOADEDDEFAULT = False
3939

4040

41+
def bindNameSpaces(graph):
42+
# --- Standard / W3C / Dublin Core ---
43+
graph.bind("dc", "http://purl.org/dc/elements/1.1/")
44+
graph.bind("dcat", "http://www.w3.org/ns/dcat#")
45+
graph.bind("dct", "http://purl.org/dc/terms/")
46+
graph.bind("dctype", "http://purl.org/dc/dcmitype/")
47+
graph.bind("foaf", "http://xmlns.com/foaf/0.1/")
48+
graph.bind("owl", "http://www.w3.org/2002/07/owl#")
49+
graph.bind("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
50+
graph.bind("rdfs", "http://www.w3.org/2000/01/rdf-schema#")
51+
graph.bind("skos", "http://www.w3.org/2004/02/skos/core#")
52+
graph.bind("void", "http://rdfs.org/ns/void#")
53+
54+
# --- OMG Commons ---
55+
graph.bind("cmns-cls", "https://www.omg.org/spec/Commons/Classifiers/")
56+
graph.bind("cmns-col", "https://www.omg.org/spec/Commons/Collections/")
57+
graph.bind("cmns-dt", "https://www.omg.org/spec/Commons/DatesAndTimes/")
58+
graph.bind("cmns-ge", "https://www.omg.org/spec/Commons/GeopoliticalEntities/")
59+
graph.bind("cmns-id", "https://www.omg.org/spec/Commons/Identifiers/")
60+
graph.bind("cmns-loc", "https://www.omg.org/spec/Commons/Locations/")
61+
graph.bind("cmns-q", "https://www.omg.org/spec/Commons/Quantities/")
62+
graph.bind("cmns-txt", "https://www.omg.org/spec/Commons/Text/")
63+
64+
# --- OMG LCC (ISO Codes) ---
65+
graph.bind(
66+
"lcc-3166-1", "https://www.omg.org/spec/LCC/Countries/ISO3166-1-CountryCodes/"
67+
)
68+
graph.bind(
69+
"lcc-4217", "https://www.omg.org/spec/LCC/Countries/ISO4217-CurrencyCodes/"
70+
)
71+
graph.bind(
72+
"lcc-lr", "https://www.omg.org/spec/LCC/Languages/LanguageRepresentation/"
73+
)
74+
75+
# --- FIBO (Financial Industry Business Ontology) ---
76+
graph.bind(
77+
"fibo-be-corp-corp",
78+
"https://spec.edmcouncil.org/fibo/ontology/BE/Corporations/Corporations/",
79+
)
80+
graph.bind(
81+
"fibo-be-ge-ge",
82+
"https://spec.edmcouncil.org/fibo/ontology/BE/GovernmentEntities/GovernmentEntities/",
83+
)
84+
graph.bind(
85+
"fibo-be-le-cb",
86+
"https://spec.edmcouncil.org/fibo/ontology/BE/LegalEntities/CorporateBodies/",
87+
)
88+
graph.bind(
89+
"fibo-be-le-lp",
90+
"https://spec.edmcouncil.org/fibo/ontology/BE/LegalEntities/LegalPersons/",
91+
)
92+
graph.bind(
93+
"fibo-be-nfp-nfp",
94+
"https://spec.edmcouncil.org/fibo/ontology/BE/NotForProfitOrganizations/NotForProfitOrganizations/",
95+
)
96+
graph.bind(
97+
"fibo-be-oac-cctl",
98+
"https://spec.edmcouncil.org/fibo/ontology/BE/OwnershipAndControl/CorporateControl/",
99+
)
100+
graph.bind(
101+
"fibo-fbc-dae-dbt",
102+
"https://spec.edmcouncil.org/fibo/ontology/FBC/DebtAndEquities/Debt/",
103+
)
104+
graph.bind(
105+
"fibo-fbc-pas-fpas",
106+
"https://spec.edmcouncil.org/fibo/ontology/FBC/ProductsAndServices/FinancialProductsAndServices/",
107+
)
108+
graph.bind(
109+
"fibo-fnd-acc-cur",
110+
"https://spec.edmcouncil.org/fibo/ontology/FND/Accounting/CurrencyAmount/",
111+
)
112+
graph.bind(
113+
"fibo-fnd-agr-ctr",
114+
"https://spec.edmcouncil.org/fibo/ontology/FND/Agreements/Contracts/",
115+
)
116+
graph.bind(
117+
"fibo-fnd-arr-doc",
118+
"https://spec.edmcouncil.org/fibo/ontology/FND/Arrangements/Documents/",
119+
)
120+
graph.bind(
121+
"fibo-fnd-arr-lif",
122+
"https://spec.edmcouncil.org/fibo/ontology/FND/Arrangements/Lifecycles/",
123+
)
124+
graph.bind(
125+
"fibo-fnd-dt-oc",
126+
"https://spec.edmcouncil.org/fibo/ontology/FND/DatesAndTimes/Occurrences/",
127+
)
128+
graph.bind(
129+
"fibo-fnd-org-org",
130+
"https://spec.edmcouncil.org/fibo/ontology/FND/Organizations/Organizations/",
131+
)
132+
graph.bind(
133+
"fibo-fnd-pas-pas",
134+
"https://spec.edmcouncil.org/fibo/ontology/FND/ProductsAndServices/ProductsAndServices/",
135+
)
136+
graph.bind(
137+
"fibo-fnd-plc-adr",
138+
"https://spec.edmcouncil.org/fibo/ontology/FND/Places/Addresses/",
139+
)
140+
graph.bind(
141+
"fibo-fnd-plc-fac",
142+
"https://spec.edmcouncil.org/fibo/ontology/FND/Places/Facilities/",
143+
)
144+
graph.bind(
145+
"fibo-fnd-plc-loc",
146+
"https://spec.edmcouncil.org/fibo/ontology/FND/Places/Locations/",
147+
)
148+
graph.bind(
149+
"fibo-fnd-pty-pty",
150+
"https://spec.edmcouncil.org/fibo/ontology/FND/Parties/Parties/",
151+
)
152+
graph.bind(
153+
"fibo-fnd-rel-rel",
154+
"https://spec.edmcouncil.org/fibo/ontology/FND/Relations/Relations/",
155+
)
156+
graph.bind(
157+
"fibo-pay-ps-ps",
158+
"https://spec.edmcouncil.org/fibo/ontology/PAY/PaymentServices/PaymentServices/",
159+
)
160+
161+
# --- Other External Vocabularies ---
162+
graph.bind("gleif-L1", "https://www.gleif.org/ontology/L1/")
163+
graph.bind("gs1", "https://ref.gs1.org/voc/")
164+
graph.bind(
165+
"lcc-cr", "https://www.omg.org/spec/LCC/Countries/CountryRepresentation/"
166+
)
167+
graph.bind("unece", "http://unece.org/vocab#")
168+
graph.bind("vcard", "http://www.w3.org/2006/vcard/ns#")
169+
graph.bind("bibo", "http://purl.org/ontology/bibo/")
170+
graph.bind("sarif", "http://sarif.info/")
171+
172+
# --- Libraries and Health ---
173+
graph.bind("lrmoo", "http://iflastandards.info/ns/lrm/lrmoo/")
174+
graph.bind("snomed", "http://purl.bioontology.org/ontology/SNOMEDCT/")
175+
176+
# --- European Legislation Identifier (ELI) ---
177+
graph.bind("eli", "http://data.europa.eu/eli/ontology#")
178+
179+
# --- W3C Standard extensions ---
180+
graph.bind("prov", "http://www.w3.org/ns/prov#")
181+
graph.bind("hydra", "http://www.w3.org/ns/hydra/core#")
182+
183+
# --- Music Ontology ---
184+
graph.bind("mo", "http://purl.org/ontology/mo/")
185+
186+
41187
class _TermAccumulator:
42188
"""Temporary holder to accumulate term information."""
43189

@@ -313,9 +459,9 @@ def getAcknowledgements(self) -> typing.Sequence[str]:
313459
) # To accept later ttl versions.
314460
for obj in objs:
315461
if obj:
316-
cont = sdocollaborators.collaborator.getContributor(obj)
317-
if cont:
318-
acks.append(cont)
462+
cont = sdocollaborators.collaborator.getContributor(obj)
463+
if cont:
464+
acks.append(cont)
319465
self.acks = sorted(acks, key=lambda t: t.title)
320466
return self.acks
321467

@@ -954,12 +1100,7 @@ def setSourceGraph(cls, g: rdflib.graph.Graph):
9541100
global VOCABURI
9551101
cls.SOURCEGRAPH = g
9561102
g.bind("schema", VOCABURI)
957-
g.bind("owl", "http://www.w3.org/2002/07/owl#")
958-
g.bind("dc", "http://purl.org/dc/elements/1.1/")
959-
g.bind("dct", "http://purl.org/dc/terms/")
960-
g.bind("dctype", "http://purl.org/dc/dcmitype/")
961-
g.bind("void", "http://rdfs.org/ns/void#")
962-
g.bind("dcat", "http://www.w3.org/ns/dcat#")
1103+
bindNameSpaces(g)
9631104

9641105
cls.TERMS = {} # Clear cache
9651106
cls.EXPANDEDTERMS = {}

software/tests/test_sdotermsource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def testUriForPrefix(self):
8888
sdotermsource.uriForPrefix("dc"),
8989
rdflib.term.URIRef("http://purl.org/dc/elements/1.1/"),
9090
)
91-
self.assertIsNone(sdotermsource.uriForPrefix("gs1"))
91+
self.assertIsNone(sdotermsource.uriForPrefix("feedface"))
9292

9393
def testPrefixedIdFromUri(self):
9494
self.assertEqual(

software/util/buildfiles.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,33 +188,73 @@ def exportrdf(exportType, subdirectory_path: str | None = None):
188188
global allGraph, currentGraph
189189

190190
if not allGraph:
191+
# The bindings need to be done for each graph
192+
# as they are not copied over.
191193
allGraph = rdflib.Graph()
192194
allGraph.bind("schema", VOCABURI)
195+
sdotermsource.bindNameSpaces(allGraph)
193196
currentGraph = rdflib.Graph()
197+
sdotermsource.bindNameSpaces(currentGraph)
194198
currentGraph.bind("schema", VOCABURI)
195199

200+
# Loads triples AND the bindings you added to sourceGraph()
196201
allGraph += sdotermsource.SdoTermSource.sourceGraph()
197202

198203
protocol, altprotocol = protocols()
199204

205+
log.debug("Cleanup non schema org things.")
206+
# We delete everything where Subject is not schema.org.
207+
# Since we haven't created the new types yet, they are safe.
200208
deloddtriples = """DELETE {?s ?p ?o}
201-
WHERE {
202-
?s ?p ?o.
203-
FILTER (! strstarts(str(?s), "%s://schema.org") ).
204-
}""" % (protocol)
209+
WHERE {
210+
?s ?p ?o.
211+
FILTER (! strstarts(str(?s), "%s://schema.org") ).
212+
}""" % (protocol)
205213
allGraph.update(deloddtriples)
214+
215+
log.debug("Generate Foreign types – SPARQL INSERT")
216+
# Insert all the types and properties that are equivalent or inherited from.
217+
insert_foreign_types = """
218+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
219+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
220+
PREFIX owl: <http://www.w3.org/2002/07/owl#>
221+
222+
INSERT {
223+
?classNode a rdfs:Class .
224+
?propNode a rdf:Property .
225+
}
226+
WHERE {
227+
# Find Foreign Classes (used in subClassOf or equivalentClass)
228+
{
229+
{ ?s rdfs:subClassOf ?classNode } UNION { ?s owl:equivalentClass ?classNode }
230+
FILTER (isURI(?classNode) && !strstarts(str(?classNode), "%s://schema.org"))
231+
}
232+
UNION
233+
# Find Foreign Properties (used in subPropertyOf or equivalentProperty)
234+
{
235+
{ ?s rdfs:subPropertyOf ?propNode } UNION { ?s owl:equivalentProperty ?propNode }
236+
FILTER (isURI(?propNode) && !strstarts(str(?propNode), "%s://schema.org"))
237+
}
238+
}
239+
""" % (protocol, protocol)
240+
241+
allGraph.update(insert_foreign_types)
242+
243+
log.debug("Merge")
206244
currentGraph += allGraph
207245

246+
log.debug("Delete items from the attic")
208247
delattic = """PREFIX schema: <%s://schema.org/>
209248
DELETE {?s ?p ?o}
210249
WHERE{
211-
?s ?p ?o;
212-
schema:isPartOf <%s://attic.schema.org>.
250+
?s ?p ?o;
251+
schema:isPartOf <%s://attic.schema.org>.
213252
}""" % (protocol, protocol)
253+
214254
currentGraph.update(delattic)
215255

216256
formats = ["json-ld", "turtle", "nt", "nquads", "rdf"]
217-
extype = exportType[len("RDFExport."):]
257+
extype = exportType[len("RDFExport.") :]
218258
if exportType == "RDFExports":
219259
for output_format in sorted(formats):
220260
_exportrdf(output_format, allGraph, currentGraph, subdirectory_path)
@@ -229,7 +269,6 @@ def exportrdf(exportType, subdirectory_path: str | None = None):
229269

230270

231271
def _exportrdf(output_format, all, current, subdirectory_path: str | None = None):
232-
233272
protocol, altprotocol = protocols()
234273

235274
if output_format in completed_rdf_exports:
@@ -250,7 +289,6 @@ def _exportrdf(output_format, all, current, subdirectory_path: str | None = None
250289
qg += g
251290
g = gr
252291

253-
254292
for p in fileutils.FILESET_PROTOCOLS:
255293
fn = fileutils.releaseFilePath(
256294
output_dir=schemaglobals.getOutputDir(),

0 commit comments

Comments
 (0)