Skip to content

Commit cefad65

Browse files
committed
FIX: rdf:ID formatting for CYME compatible export
1 parent b4f5bd7 commit cefad65

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

ravens/xml/graph.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pathlib
2+
import re
23

34
from uuid import uuid4
45

@@ -32,6 +33,23 @@ def __init__(self, profile_path: str | pathlib.Path | None = None, cim_namespace
3233
nm.bind("cim", self.cim, override=True)
3334
self.graph.namespace_manager = nm
3435

36+
@staticmethod
37+
def _transform_to_cyme_uri(match):
38+
"""
39+
Transform URIs to CYME format: #uuid -> #_UUID
40+
Match patterns like rdf:about="#uuid" or rdf:resource="#uuid"
41+
"""
42+
prefix = match.group(1) # rdf:about=" or rdf:resource="
43+
hash_sign = match.group(2) # # or empty
44+
uuid = match.group(3) # the UUID
45+
46+
# Transform: remove leading underscore if present, uppercase, add underscore
47+
uuid_clean = uuid.lstrip('_')
48+
uuid_transformed = f"_{uuid_clean.upper()}"
49+
50+
return f'{prefix}{hash_sign}{uuid_transformed}"'
51+
52+
3553
def mRID(self) -> str:
3654
return self.uuid_format(str(uuid4()))
3755

@@ -82,8 +100,19 @@ def get(self, subject, predicate, default=None):
82100
def get_name(self, subject):
83101
return self.get(subject, self.cim["IdentifiedObject.name"], str(subject))
84102

85-
def save(self, path: pathlib.Path | str) -> None:
103+
def save(self, path: pathlib.Path | str, make_cyme_compatible: bool = False) -> None:
86104
rdfxml = self.graph.serialize(max_depth=1, format="pretty-xml")
87-
rdfxml = rdfxml.replace("rdf:about", "rdf:ID")
105+
106+
if make_cyme_compatible:
107+
# Transform both rdf:about and rdf:resource attributes
108+
rdfxml = re.sub(
109+
r'(rdf:(?:about|resource)=")(#?)([a-fA-F0-9_-]+)"',
110+
self._transform_to_cyme_uri,
111+
rdfxml
112+
)
113+
114+
# Change rdf:about to rdf:ID
115+
rdfxml = rdfxml.replace("rdf:about", "rdf:ID")
116+
88117
with open(path, "w") as f:
89118
f.write(rdfxml)

0 commit comments

Comments
 (0)