mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-23 04:01:02 +02:00
Fixed quotaed triples
This commit is contained in:
parent
86ca3edbfc
commit
fe36f7db81
5 changed files with 16 additions and 128 deletions
|
|
@ -22,22 +22,15 @@ def to_value(x):
|
||||||
elif x.get("t") == LITERAL:
|
elif x.get("t") == LITERAL:
|
||||||
return Literal(x.get("v", ""))
|
return Literal(x.get("v", ""))
|
||||||
elif x.get("t") == TRIPLE:
|
elif x.get("t") == TRIPLE:
|
||||||
# Parse the nested triple from JSON or structured data
|
|
||||||
# Wire format uses "tr" key for nested triple dict
|
# Wire format uses "tr" key for nested triple dict
|
||||||
triple_data = x.get("tr") or x.get("v", "")
|
triple_data = x.get("tr")
|
||||||
if isinstance(triple_data, str):
|
if triple_data:
|
||||||
import json
|
|
||||||
try:
|
|
||||||
triple_data = json.loads(triple_data)
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
return Literal(triple_data)
|
|
||||||
if isinstance(triple_data, dict):
|
|
||||||
return QuotedTriple(
|
return QuotedTriple(
|
||||||
s=to_value(triple_data.get("s", {})),
|
s=to_value(triple_data.get("s", {})),
|
||||||
p=to_value(triple_data.get("p", {})),
|
p=to_value(triple_data.get("p", {})),
|
||||||
o=to_value(triple_data.get("o", {})),
|
o=to_value(triple_data.get("o", {})),
|
||||||
)
|
)
|
||||||
return Literal(str(triple_data))
|
return Literal("")
|
||||||
# Fallback for any other type
|
# Fallback for any other type
|
||||||
return Literal(x.get("v", x.get("i", "")))
|
return Literal(x.get("v", x.get("i", "")))
|
||||||
|
|
||||||
|
|
@ -47,14 +40,13 @@ def from_value(v):
|
||||||
if isinstance(v, Uri):
|
if isinstance(v, Uri):
|
||||||
return {"t": IRI, "i": str(v)}
|
return {"t": IRI, "i": str(v)}
|
||||||
elif isinstance(v, QuotedTriple):
|
elif isinstance(v, QuotedTriple):
|
||||||
import json
|
|
||||||
return {
|
return {
|
||||||
"t": TRIPLE,
|
"t": TRIPLE,
|
||||||
"v": json.dumps({
|
"tr": {
|
||||||
"s": from_value(v.s),
|
"s": from_value(v.s),
|
||||||
"p": from_value(v.p),
|
"p": from_value(v.p),
|
||||||
"o": from_value(v.o),
|
"o": from_value(v.o),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return {"t": LITERAL, "v": str(v)}
|
return {"t": LITERAL, "v": str(v)}
|
||||||
|
|
|
||||||
|
|
@ -21,22 +21,15 @@ def to_value(x):
|
||||||
elif x.get("t") == LITERAL:
|
elif x.get("t") == LITERAL:
|
||||||
return Literal(x.get("v", ""))
|
return Literal(x.get("v", ""))
|
||||||
elif x.get("t") == TRIPLE:
|
elif x.get("t") == TRIPLE:
|
||||||
# Parse the nested triple from JSON or structured data
|
|
||||||
# Wire format uses "tr" key for nested triple dict
|
# Wire format uses "tr" key for nested triple dict
|
||||||
triple_data = x.get("tr") or x.get("v", "")
|
triple_data = x.get("tr")
|
||||||
if isinstance(triple_data, str):
|
if triple_data:
|
||||||
import json
|
|
||||||
try:
|
|
||||||
triple_data = json.loads(triple_data)
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
return Literal(triple_data)
|
|
||||||
if isinstance(triple_data, dict):
|
|
||||||
return QuotedTriple(
|
return QuotedTriple(
|
||||||
s=to_value(triple_data.get("s", {})),
|
s=to_value(triple_data.get("s", {})),
|
||||||
p=to_value(triple_data.get("p", {})),
|
p=to_value(triple_data.get("p", {})),
|
||||||
o=to_value(triple_data.get("o", {})),
|
o=to_value(triple_data.get("o", {})),
|
||||||
)
|
)
|
||||||
return Literal(str(triple_data))
|
return Literal("")
|
||||||
# Fallback for any other type
|
# Fallback for any other type
|
||||||
return Literal(x.get("v", x.get("i", "")))
|
return Literal(x.get("v", x.get("i", "")))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,22 +33,15 @@ def to_value(x):
|
||||||
elif x.get("t") == LITERAL:
|
elif x.get("t") == LITERAL:
|
||||||
return Literal(x.get("v", ""))
|
return Literal(x.get("v", ""))
|
||||||
elif x.get("t") == TRIPLE:
|
elif x.get("t") == TRIPLE:
|
||||||
# Parse the nested triple from JSON or structured data
|
|
||||||
# Wire format uses "tr" key for nested triple dict
|
# Wire format uses "tr" key for nested triple dict
|
||||||
triple_data = x.get("tr") or x.get("v", "")
|
triple_data = x.get("tr")
|
||||||
if isinstance(triple_data, str):
|
if triple_data:
|
||||||
import json
|
|
||||||
try:
|
|
||||||
triple_data = json.loads(triple_data)
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
return Literal(triple_data)
|
|
||||||
if isinstance(triple_data, dict):
|
|
||||||
return QuotedTriple(
|
return QuotedTriple(
|
||||||
s=to_value(triple_data.get("s", {})),
|
s=to_value(triple_data.get("s", {})),
|
||||||
p=to_value(triple_data.get("p", {})),
|
p=to_value(triple_data.get("p", {})),
|
||||||
o=to_value(triple_data.get("o", {})),
|
o=to_value(triple_data.get("o", {})),
|
||||||
)
|
)
|
||||||
return Literal(str(triple_data))
|
return Literal("")
|
||||||
# Fallback for any other type
|
# Fallback for any other type
|
||||||
return Literal(x.get("v", x.get("i", "")))
|
return Literal(x.get("v", x.get("i", "")))
|
||||||
|
|
||||||
|
|
@ -58,14 +51,13 @@ def from_value(v):
|
||||||
if isinstance(v, Uri):
|
if isinstance(v, Uri):
|
||||||
return {"t": IRI, "i": str(v)}
|
return {"t": IRI, "i": str(v)}
|
||||||
elif isinstance(v, QuotedTriple):
|
elif isinstance(v, QuotedTriple):
|
||||||
import json
|
|
||||||
return {
|
return {
|
||||||
"t": TRIPLE,
|
"t": TRIPLE,
|
||||||
"v": json.dumps({
|
"tr": {
|
||||||
"s": from_value(v.s),
|
"s": from_value(v.s),
|
||||||
"p": from_value(v.p),
|
"p": from_value(v.p),
|
||||||
"o": from_value(v.o),
|
"o": from_value(v.o),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return {"t": LITERAL, "v": str(v)}
|
return {"t": LITERAL, "v": str(v)}
|
||||||
|
|
|
||||||
|
|
@ -81,65 +81,11 @@ def _triple_translator_to_pulsar(data: Dict[str, Any]) -> Triple:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _term_dict_to_wire(term_dict: Dict[str, Any]) -> Dict[str, Any]:
|
def _triple_translator_from_pulsar(obj: Triple) -> Dict[str, Any]:
|
||||||
"""
|
"""Convert Triple object to wire format dict."""
|
||||||
Convert a Term dict (with 'type', 'iri', etc.) to wire format (with 't', 'i', etc.).
|
|
||||||
|
|
||||||
If already in wire format, returns as-is.
|
|
||||||
"""
|
|
||||||
# Check if already in wire format (has 't' key)
|
|
||||||
if "t" in term_dict:
|
|
||||||
return term_dict
|
|
||||||
|
|
||||||
# Convert from Term attribute format to wire format
|
|
||||||
result: Dict[str, Any] = {"t": term_dict.get("type", "")}
|
|
||||||
term_type = term_dict.get("type", "")
|
|
||||||
|
|
||||||
if term_type == IRI:
|
|
||||||
result["i"] = term_dict.get("iri", "")
|
|
||||||
elif term_type == BLANK:
|
|
||||||
result["d"] = term_dict.get("id", "")
|
|
||||||
elif term_type == LITERAL:
|
|
||||||
result["v"] = term_dict.get("value", "")
|
|
||||||
if term_dict.get("datatype"):
|
|
||||||
result["dt"] = term_dict.get("datatype")
|
|
||||||
if term_dict.get("language"):
|
|
||||||
result["ln"] = term_dict.get("language")
|
|
||||||
elif term_type == TRIPLE:
|
|
||||||
nested = term_dict.get("triple")
|
|
||||||
if nested:
|
|
||||||
result["tr"] = _triple_translator_from_pulsar(nested)
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def _triple_translator_from_pulsar(obj) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Convert Triple object or dict to wire format dict.
|
|
||||||
|
|
||||||
Handles both Triple objects (with .s, .p, .o attributes) and
|
|
||||||
dict representations (with "s", "p", "o" keys) since message
|
|
||||||
deserialization may not fully reconstruct nested objects.
|
|
||||||
"""
|
|
||||||
term_translator = TermTranslator()
|
term_translator = TermTranslator()
|
||||||
result: Dict[str, Any] = {}
|
result: Dict[str, Any] = {}
|
||||||
|
|
||||||
# Handle dict representation
|
|
||||||
if isinstance(obj, dict):
|
|
||||||
if obj.get("s"):
|
|
||||||
s = obj["s"]
|
|
||||||
result["s"] = _term_dict_to_wire(s) if isinstance(s, dict) else term_translator.from_pulsar(s)
|
|
||||||
if obj.get("p"):
|
|
||||||
p = obj["p"]
|
|
||||||
result["p"] = _term_dict_to_wire(p) if isinstance(p, dict) else term_translator.from_pulsar(p)
|
|
||||||
if obj.get("o"):
|
|
||||||
o = obj["o"]
|
|
||||||
result["o"] = _term_dict_to_wire(o) if isinstance(o, dict) else term_translator.from_pulsar(o)
|
|
||||||
if obj.get("g"):
|
|
||||||
result["g"] = obj["g"]
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Handle Triple object
|
|
||||||
if obj.s:
|
if obj.s:
|
||||||
result["s"] = term_translator.from_pulsar(obj.s)
|
result["s"] = term_translator.from_pulsar(obj.s)
|
||||||
if obj.p:
|
if obj.p:
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,7 @@ default_ident = "triples-write"
|
||||||
|
|
||||||
|
|
||||||
def serialize_triple(triple):
|
def serialize_triple(triple):
|
||||||
"""
|
"""Serialize a Triple object to JSON for storage."""
|
||||||
Serialize a Triple object or dict to JSON for storage.
|
|
||||||
|
|
||||||
Handles both Triple objects (with .s, .p, .o attributes) and
|
|
||||||
dict representations (with "s", "p", "o" keys) since message
|
|
||||||
deserialization may not fully reconstruct nested objects.
|
|
||||||
"""
|
|
||||||
if triple is None:
|
if triple is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
@ -41,25 +35,6 @@ def serialize_triple(triple):
|
||||||
if term is None:
|
if term is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Handle dict representation (from message deserialization)
|
|
||||||
if isinstance(term, dict):
|
|
||||||
term_type = term.get("type", "")
|
|
||||||
result = {"type": term_type}
|
|
||||||
if term_type == IRI:
|
|
||||||
result["iri"] = term.get("iri", "")
|
|
||||||
elif term_type == LITERAL:
|
|
||||||
result["value"] = term.get("value", "")
|
|
||||||
if term.get("datatype"):
|
|
||||||
result["datatype"] = term.get("datatype")
|
|
||||||
if term.get("language"):
|
|
||||||
result["language"] = term.get("language")
|
|
||||||
elif term_type == BLANK:
|
|
||||||
result["id"] = term.get("id", "")
|
|
||||||
elif term_type == TRIPLE:
|
|
||||||
result["triple"] = serialize_triple(term.get("triple"))
|
|
||||||
return result
|
|
||||||
|
|
||||||
# Handle Term object
|
|
||||||
result = {"type": term.type}
|
result = {"type": term.type}
|
||||||
if term.type == IRI:
|
if term.type == IRI:
|
||||||
result["iri"] = term.iri
|
result["iri"] = term.iri
|
||||||
|
|
@ -72,19 +47,9 @@ def serialize_triple(triple):
|
||||||
elif term.type == BLANK:
|
elif term.type == BLANK:
|
||||||
result["id"] = term.id
|
result["id"] = term.id
|
||||||
elif term.type == TRIPLE:
|
elif term.type == TRIPLE:
|
||||||
# Recursive for nested triples
|
|
||||||
result["triple"] = serialize_triple(term.triple)
|
result["triple"] = serialize_triple(term.triple)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# Handle dict representation
|
|
||||||
if isinstance(triple, dict):
|
|
||||||
return json.dumps({
|
|
||||||
"s": term_to_dict(triple.get("s")),
|
|
||||||
"p": term_to_dict(triple.get("p")),
|
|
||||||
"o": term_to_dict(triple.get("o")),
|
|
||||||
})
|
|
||||||
|
|
||||||
# Handle Triple object
|
|
||||||
return json.dumps({
|
return json.dumps({
|
||||||
"s": term_to_dict(triple.s),
|
"s": term_to_dict(triple.s),
|
||||||
"p": term_to_dict(triple.p),
|
"p": term_to_dict(triple.p),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue