mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
track_relationship() has no dedicated subject/object fields, so the Step 2 example only stored relationship_id + type, leaving readers unable to reconstruct which two entities the relationship connects. Encode subject_entity_id/object_entity_id in metadata by convention, and note the lack of dedicated fields in the prose.
9.2 KiB
9.2 KiB
In [ ]:
!pip install -q semanticaIn [ ]:
import json
from semantica.provenance import (
ProvenanceManager,
compute_checksum,
verify_checksum,
)
# In-memory storage for this demo; pass storage_path="provenance.db"
# (or a config with provenance.storage_path) for a persistent SQLite backend.
prov = ProvenanceManager()
print("ProvenanceManager ready (in-memory storage)")In [ ]:
# Finding from paper #1
entry_biomass = prov.track_entity(
entity_id="claim_biomass_increase",
source="DOI:10.1371/journal.pone.0023601",
confidence=0.92,
source_location="Figure 2",
source_quote="Total fish biomass increased by 463% ...",
)
# Supporting entity from paper #2
entry_reserve = prov.track_entity(
entity_id="marine_reserve_1",
source="DOI:10.1126/science.1088121",
confidence=0.88,
source_location="Table 1",
source_quote="... no-take marine reserve at Cabo Pulmo ...",
)
print("Tracked:", entry_biomass.entity_id, "|", entry_reserve.entity_id)In [ ]:
rel = prov.track_relationship(
relationship_id="rel_biomass_about_reserve",
source="DOI:10.1371/journal.pone.0023601",
metadata={
"type": "measured_at",
# No dedicated endpoint fields on track_relationship() yet -- record
# which entities this relationship connects here by convention.
"subject_entity_id": "claim_biomass_increase",
"object_entity_id": "marine_reserve_1",
},
)
print("Relationship tracked:", rel.entity_id, "|", rel.metadata["subject_entity_id"], "->", rel.metadata["object_entity_id"])In [ ]:
lineage = prov.get_lineage("claim_biomass_increase")
print(json.dumps(lineage, indent=2, default=str)[:800])
print("\n--- ordered chain ---")
for e in prov.trace_lineage("claim_biomass_increase"):
print(f"{e.entity_id} | seq#{e.sequence_id} | {e.activity_id}")In [ ]:
revisions = prov.revision_history("claim_biomass_increase")
print(f"{len(revisions)} revision(s) on record")
for s in prov.get_all_sources("claim_biomass_increase"):
print("source:", s)In [ ]:
invalidated = prov.invalidate(
entity_id="claim_biomass_increase",
agent_id="reviewer_dr_chen",
reason="Partial retraction: Figure 2 statistics corrected by publisher (see erratum).",
)
print("Invalidated:", invalidated.entity_id, "| invalidated flag:", getattr(invalidated, "invalidated", True))
stats = prov.get_statistics()
print("\nStorage statistics:", json.dumps(stats, indent=2, default=str))In [ ]:
# entry_biomass was returned by track_entity in Step 1
ok = verify_checksum(entry_biomass)
print("Checksum verified:", ok)
print("Computed:", compute_checksum(entry_biomass)[:16], "...")
print("Stored: ", entry_biomass.checksum[:16] if getattr(entry_biomass, 'checksum', None) else "(see entry fields)")
chain = prov.verify_chain()
print("Chain verification:", json.dumps(chain, default=str)[:200])