|
1 | 1 | """
|
2 |
| -Copyright 2023, Leibniz-Institut für Werkstofforientierte Technologien - IWT. |
| 2 | +Copyright 2025, Leibniz-Institut für Werkstofforientierte Technologien - IWT. |
3 | 3 | All rights reserved.
|
4 | 4 |
|
5 | 5 | 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
|
117 | 117 |
|
118 | 118 | #Parse uploaded ontologies into a rdflib Graph
|
119 | 119 | self.g = rdflib.Graph()
|
| 120 | + default_namespaces = [str(namespace) for _, namespace in self.g.namespaces()] |
120 | 121 | [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) |
123 | 126 | self.entityContextTuple = entityContextTuple
|
124 | 127 | if len(entityContextTuple) != 2:
|
125 | 128 | 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:
|
469 | 472 | labelAppendix = value
|
470 | 473 | if "http://www.w3.org/2000/01/rdf-schema#label" in iterable:
|
471 | 474 | 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}" |
474 | 477 | elif isinstance(value, list) or isinstance(value, dict):
|
475 | 478 | __set_uuid(value, key)
|
476 | 479 | elif isinstance(iterable, list):
|
@@ -498,34 +501,43 @@ def __serialize_graph(self) -> str:
|
498 | 501 |
|
499 | 502 | # save onto as ttl (as as output)
|
500 | 503 | return gData.serialize(format='turtle')
|
501 |
| - |
| 504 | + |
| 505 | + |
| 506 | + |
502 | 507 | def example():
|
| 508 | + from agnosticmapper import Mapper |
| 509 | + import json |
| 510 | + import os |
| 511 | + |
503 | 512 | 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 | + |
508 | 519 | context = {
|
509 | 520 | "foaf": "http://xmlns.com/foaf/0.1",
|
510 | 521 | "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
|
511 | 522 | "dcterms": "http://purl.org/dc/terms/"
|
512 | 523 | }
|
513 |
| - |
| 524 | + |
514 | 525 | entityContextTuple = ("entity", "http://example.org/entity/")
|
515 |
| - |
| 526 | + |
516 | 527 | ignoreEntityInstantiationList = ["interest"]
|
517 | 528 |
|
518 | 529 | 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) |
526 | 534 |
|
| 535 | + print(result) |
| 536 | + exit() |
527 | 537 |
|
528 | 538 | if __name__=="__main__":
|
| 539 | + #example() |
| 540 | + |
529 | 541 | mapper = Mapper()
|
530 | 542 |
|
531 | 543 | parser=argparse.ArgumentParser(description='Converts canonical json json to Turtle', prog='agnosticmapper')
|
|
0 commit comments