From fe36f7db810e1f772246054989a88305f8a39caf Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Thu, 5 Mar 2026 23:43:27 +0000 Subject: [PATCH] Fixed quotaed triples --- trustgraph-base/trustgraph/api/flow.py | 18 ++---- trustgraph-base/trustgraph/api/knowledge.py | 13 +---- trustgraph-base/trustgraph/api/library.py | 18 ++---- .../messaging/translators/primitives.py | 58 +------------------ .../storage/triples/cassandra/write.py | 37 +----------- 5 files changed, 16 insertions(+), 128 deletions(-) diff --git a/trustgraph-base/trustgraph/api/flow.py b/trustgraph-base/trustgraph/api/flow.py index 7bbd7f60..0d34104c 100644 --- a/trustgraph-base/trustgraph/api/flow.py +++ b/trustgraph-base/trustgraph/api/flow.py @@ -22,22 +22,15 @@ def to_value(x): elif x.get("t") == LITERAL: return Literal(x.get("v", "")) elif x.get("t") == TRIPLE: - # Parse the nested triple from JSON or structured data # Wire format uses "tr" key for nested triple dict - triple_data = x.get("tr") or x.get("v", "") - if isinstance(triple_data, str): - import json - try: - triple_data = json.loads(triple_data) - except json.JSONDecodeError: - return Literal(triple_data) - if isinstance(triple_data, dict): + triple_data = x.get("tr") + if triple_data: return QuotedTriple( s=to_value(triple_data.get("s", {})), p=to_value(triple_data.get("p", {})), o=to_value(triple_data.get("o", {})), ) - return Literal(str(triple_data)) + return Literal("") # Fallback for any other type return Literal(x.get("v", x.get("i", ""))) @@ -47,14 +40,13 @@ def from_value(v): if isinstance(v, Uri): return {"t": IRI, "i": str(v)} elif isinstance(v, QuotedTriple): - import json return { "t": TRIPLE, - "v": json.dumps({ + "tr": { "s": from_value(v.s), "p": from_value(v.p), "o": from_value(v.o), - }) + } } else: return {"t": LITERAL, "v": str(v)} diff --git a/trustgraph-base/trustgraph/api/knowledge.py b/trustgraph-base/trustgraph/api/knowledge.py index c5e1f424..84f98918 100644 --- a/trustgraph-base/trustgraph/api/knowledge.py +++ b/trustgraph-base/trustgraph/api/knowledge.py @@ -21,22 +21,15 @@ def to_value(x): elif x.get("t") == LITERAL: return Literal(x.get("v", "")) elif x.get("t") == TRIPLE: - # Parse the nested triple from JSON or structured data # Wire format uses "tr" key for nested triple dict - triple_data = x.get("tr") or x.get("v", "") - if isinstance(triple_data, str): - import json - try: - triple_data = json.loads(triple_data) - except json.JSONDecodeError: - return Literal(triple_data) - if isinstance(triple_data, dict): + triple_data = x.get("tr") + if triple_data: return QuotedTriple( s=to_value(triple_data.get("s", {})), p=to_value(triple_data.get("p", {})), o=to_value(triple_data.get("o", {})), ) - return Literal(str(triple_data)) + return Literal("") # Fallback for any other type return Literal(x.get("v", x.get("i", ""))) diff --git a/trustgraph-base/trustgraph/api/library.py b/trustgraph-base/trustgraph/api/library.py index 63ec32b0..396d64e0 100644 --- a/trustgraph-base/trustgraph/api/library.py +++ b/trustgraph-base/trustgraph/api/library.py @@ -33,22 +33,15 @@ def to_value(x): elif x.get("t") == LITERAL: return Literal(x.get("v", "")) elif x.get("t") == TRIPLE: - # Parse the nested triple from JSON or structured data # Wire format uses "tr" key for nested triple dict - triple_data = x.get("tr") or x.get("v", "") - if isinstance(triple_data, str): - import json - try: - triple_data = json.loads(triple_data) - except json.JSONDecodeError: - return Literal(triple_data) - if isinstance(triple_data, dict): + triple_data = x.get("tr") + if triple_data: return QuotedTriple( s=to_value(triple_data.get("s", {})), p=to_value(triple_data.get("p", {})), o=to_value(triple_data.get("o", {})), ) - return Literal(str(triple_data)) + return Literal("") # Fallback for any other type return Literal(x.get("v", x.get("i", ""))) @@ -58,14 +51,13 @@ def from_value(v): if isinstance(v, Uri): return {"t": IRI, "i": str(v)} elif isinstance(v, QuotedTriple): - import json return { "t": TRIPLE, - "v": json.dumps({ + "tr": { "s": from_value(v.s), "p": from_value(v.p), "o": from_value(v.o), - }) + } } else: return {"t": LITERAL, "v": str(v)} diff --git a/trustgraph-base/trustgraph/messaging/translators/primitives.py b/trustgraph-base/trustgraph/messaging/translators/primitives.py index 4949427c..d54efc49 100644 --- a/trustgraph-base/trustgraph/messaging/translators/primitives.py +++ b/trustgraph-base/trustgraph/messaging/translators/primitives.py @@ -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]: - """ - 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. - """ +def _triple_translator_from_pulsar(obj: Triple) -> Dict[str, Any]: + """Convert Triple object to wire format dict.""" term_translator = TermTranslator() 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: result["s"] = term_translator.from_pulsar(obj.s) if obj.p: diff --git a/trustgraph-flow/trustgraph/storage/triples/cassandra/write.py b/trustgraph-flow/trustgraph/storage/triples/cassandra/write.py index 97d25400..ab13ccbc 100755 --- a/trustgraph-flow/trustgraph/storage/triples/cassandra/write.py +++ b/trustgraph-flow/trustgraph/storage/triples/cassandra/write.py @@ -27,13 +27,7 @@ default_ident = "triples-write" def serialize_triple(triple): - """ - 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. - """ + """Serialize a Triple object to JSON for storage.""" if triple is None: return None @@ -41,25 +35,6 @@ def serialize_triple(triple): if term is 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} if term.type == IRI: result["iri"] = term.iri @@ -72,19 +47,9 @@ def serialize_triple(triple): elif term.type == BLANK: result["id"] = term.id elif term.type == TRIPLE: - # Recursive for nested triples result["triple"] = serialize_triple(term.triple) 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({ "s": term_to_dict(triple.s), "p": term_to_dict(triple.p),