Skip to content

Commit 6b5bc9d

Browse files
- context is optional (if prefixes are provides by tbox)
- short ids are now suffixes in labels - License updated
1 parent fc3c92a commit 6b5bc9d

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2023, Leibniz-Institut für Werkstofforientierte Technologien - IWT.
1+
Copyright 2025, Leibniz-Institut für Werkstofforientierte Technologien - IWT.
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

README.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ mapper = Mapper()
7171
The mapper only provides one method, called `map(...)`.
7272
The method creates the Turtle file out of the given canonical json. Based on provided ontology terminologies. It instantiates classes by its labels with a uuid and using the given entity context as the namespace.
7373

74+
It is advised to include a `@prefix` line inside each provided ontology terminologies that defines its own namespace, e.g. `@prefix foaf: <http://xmlns.com/foaf/0.1/> .
75+
` in the `./examples/foaf.ttl` or it must be included in the parameter `context`.
76+
7477
```
7578
mapper.map(canon, ontos, context, entityContextTuple, ignoreEntityInstantiationList)
7679
```
@@ -172,7 +175,7 @@ If the label is marked in "listHandler", the array will be handled as a ordered
172175

173176
"hasIdentifier" is used to cross reference class instances within the canon json. If you reference a class instance at another point, you must use it or else it will create two different class instances with different uuids.
174177

175-
"additoinalTypes" will add more subclasses to the class instance apart from the label that is used as the key.
178+
"additionalTypes" will add more subclasses to the class instance apart from the label that is used as the key.
176179

177180
The prefix and namespace that is prefixed to the uuids of the class instance IRIs ist provided via the "entityContextTuple" parameter.
178181

@@ -184,29 +187,30 @@ The prefix and namespace that is prefixed to the uuids of the class instance IRI
184187
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
185188
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
186189
187-
entity:ef4cc7abeebf4ca2b0db3e9625bfaba9 a foaf:Group ;
188-
rdfs:label "ef4cc7 Group" ;
189-
rdfs:member ( entity:e78d4393bc3d4decb38e2c335294b480 entity:b2c30466cbf34bc18429d3aa9e3d7a8b ) .
190+
entity:b5df5937bbe24b70bd88258222027260 a foaf:Group ;
191+
rdfs:label "Group b5df" ;
192+
rdfs:member ( entity:0c2becddb1f446618c14ab37e336e364 entity:45164feebf7a435dbe08be031f233975 ) .
190193
191-
entity:e78d4393bc3d4decb38e2c335294b480 a dcterms:Agent,
194+
entity:0c2becddb1f446618c14ab37e336e364 a dcterms:Agent,
192195
foaf:Person ;
193-
rdfs:label "e78d43 Person" ;
196+
rdfs:label "Person 0c2b" ;
194197
foaf:interest dcterms:BibliographicResource ;
195198
foaf:name "Alice" .
196-
197-
entity:b2c30466cbf34bc18429d3aa9e3d7a8b a foaf:Person ;
198-
rdfs:label "b2c304 Person" ;
199-
foaf:knows entity:c466dac731024d0a88d493ceaab70ea1,
200-
entity:e054f53709a84c27aade3b2c097375e5 ;
199+
200+
entity:14ff7200e44a45108108bde369281349 a foaf:Person ;
201+
rdfs:label "Person 14ff" ;
202+
foaf:name "Dave" .
203+
204+
entity:45164feebf7a435dbe08be031f233975 a foaf:Person ;
205+
rdfs:label "Person 4516" ;
206+
foaf:knows entity:14ff7200e44a45108108bde369281349,
207+
entity:cb0393390d684cd398f63934d30f3351 ;
201208
foaf:name "Bob" .
202209
203-
entity:e054f53709a84c27aade3b2c097375e5 a foaf:Person ;
204-
rdfs:label "e054f5 Person" ;
210+
entity:cb0393390d684cd398f63934d30f3351 a foaf:Person ;
211+
rdfs:label "Person cb03" ;
205212
foaf:name "Charlie" .
206213
207-
entity:c466dac731024d0a88d493ceaab70ea1 a foaf:Person ;
208-
rdfs:label "c466da Person" ;
209-
foaf:name "Dave" .
210214
```
211215

212216
The order of searching for the label is:

agnosticmapper/agnosticmapper.py

+31-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright 2023, Leibniz-Institut für Werkstofforientierte Technologien - IWT.
2+
Copyright 2025, Leibniz-Institut für Werkstofforientierte Technologien - IWT.
33
All rights reserved.
44
55
Redistribution and use in source and binary forms, with or without
@@ -117,9 +117,12 @@ def map(self, canon: Union[list[dict], dict], ontos: list[str], context: dict, e
117117

118118
#Parse uploaded ontologies into a rdflib Graph
119119
self.g = rdflib.Graph()
120+
default_namespaces = [str(namespace) for _, namespace in self.g.namespaces()]
120121
[self.g.parse(data=data) for data in ontos]
121-
122-
self.context = context
122+
# get context from graph
123+
self.context = {prefix: namespace for prefix, namespace in self.g.namespaces() if str(namespace) not in default_namespaces and "default" not in str(prefix) and prefix}
124+
if context: # manual addition
125+
self.context.update(context)
123126
self.entityContextTuple = entityContextTuple
124127
if len(entityContextTuple) != 2:
125128
raise Exception("Entity Context must be a Tuple of 2 columns")
@@ -469,8 +472,8 @@ def __set_uuid(iterable: Union[dict, list], parentKey: str) -> None:
469472
labelAppendix = value
470473
if "http://www.w3.org/2000/01/rdf-schema#label" in iterable:
471474
labelAppendix = iterable["http://www.w3.org/2000/01/rdf-schema#label"]
472-
shortid = idMap[value].replace(self.entityContextTuple[1], "")[:6]
473-
iterable[self.__get_class_by_label("label", "http://www.w3.org/2000/01/rdf-schema#label")] = f"{shortid} {labelAppendix}"
475+
shortid = idMap[value].replace(self.entityContextTuple[1], "")[:4]
476+
iterable[self.__get_class_by_label("label", "http://www.w3.org/2000/01/rdf-schema#label")] = f"{labelAppendix} {shortid}"
474477
elif isinstance(value, list) or isinstance(value, dict):
475478
__set_uuid(value, key)
476479
elif isinstance(iterable, list):
@@ -498,34 +501,43 @@ def __serialize_graph(self) -> str:
498501

499502
# save onto as ttl (as as output)
500503
return gData.serialize(format='turtle')
501-
504+
505+
506+
502507
def example():
508+
from agnosticmapper import Mapper
509+
import json
510+
import os
511+
503512
mapper = Mapper()
504-
ontos = [open(file, "r").read() for file in [f"{os.path.dirname(__file__)}/example/foaf.ttl",
505-
f"{os.path.dirname(__file__)}/example/rdf-schema.ttl",
506-
f"{os.path.dirname(__file__)}/example/dublin_core_terms.ttl"]]
507-
canon_json = json.loads(open(f"{os.path.dirname(__file__)}/example/foaf_canon.json", "r").read())
513+
ontos = [open(file, "r").read() for file in
514+
[f"./example/foaf.ttl", f"./example/rdf-schema.ttl",
515+
f"./example/dublin_core_terms.ttl"]]
516+
517+
canon_json = json.loads(open(f"./example/foaf_canon.json", "r").read())
518+
508519
context = {
509520
"foaf": "http://xmlns.com/foaf/0.1",
510521
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
511522
"dcterms": "http://purl.org/dc/terms/"
512523
}
513-
524+
514525
entityContextTuple = ("entity", "http://example.org/entity/")
515-
526+
516527
ignoreEntityInstantiationList = ["interest"]
517528

518529
result = mapper.map(canon=canon_json,
519-
ontos=ontos,
520-
context=context,
521-
entityContextTuple=entityContextTuple,
522-
ignoreEntityInstantiationList=ignoreEntityInstantiationList)
523-
524-
print(result)
525-
530+
ontos=ontos,
531+
context=context,
532+
entityContextTuple=entityContextTuple,
533+
ignoreEntityInstantiationList=ignoreEntityInstantiationList)
526534

535+
print(result)
536+
exit()
527537

528538
if __name__=="__main__":
539+
#example()
540+
529541
mapper = Mapper()
530542

531543
parser=argparse.ArgumentParser(description='Converts canonical json json to Turtle', prog='agnosticmapper')

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
setup(
44
name='agnosticmapper',
55
version='1.0',
6-
install_requires=["rdflib"],
6+
install_requires=["rdflib>=7.0.0", "owlready2"],
77
packages=find_packages()
88
)

0 commit comments

Comments
 (0)