-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.py
More file actions
194 lines (151 loc) · 5.96 KB
/
sim.py
File metadata and controls
194 lines (151 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python3
import json
import glob
import sys
import argparse
from hyperon import MeTTa
# ------------------------------------------------------------
# CLI Setup
# ------------------------------------------------------------
parser = argparse.ArgumentParser(description="MeTTa consent simulation")
parser.add_argument("folder", help="Folder containing proposal JSON files")
parser.add_argument("-v", "--verbose", action="store_true",
help="Show debugging/logging output")
args = parser.parse_args()
# ------------------------------------------------------------
# Logging helpers
# ------------------------------------------------------------
def log(*msg):
if args.verbose:
print("[LOG]", *msg)
# ------------------------------------------------------------
# Canonical Python store of facts for deterministic checks
# { pid: { "raised": set((oid, reviewer)), "resolved": set(oid) } }
# ------------------------------------------------------------
proposal_facts = {}
# ------------------------------------------------------------
# Canonical MeTTa rules
# ------------------------------------------------------------
CANONICAL_RULES = r'''
;; ---------- canonical rules ----------
(: BudgetProposal P)
(: Proposal P)
(: Objection O)
(: Reviewer R)
(SubmittedBy P R)
(ReviewedBy P R)
;; canonical objection facts:
(ObjectionRaised P O R)
(ObjectionResolved P O)
;; derived predicate (kept for informational queries)
(HasUnresolvedObjection P) :-
(ObjectionRaised P O R)
(not (ObjectionResolved P O))
(HasConsent P) :-
(BudgetProposal P)
(not (HasUnresolvedObjection P))
;; ------------------------------------
'''
# ------------------------------------------------------------
# Initialize MeTTa
# ------------------------------------------------------------
metta = MeTTa()
# ------------------------------------------------------------
# Load MeTTa rules
# ------------------------------------------------------------
def load_rules():
log("Loading rules...")
metta.run(CANONICAL_RULES)
log("Rules loaded.")
# ------------------------------------------------------------
# Python-side deterministic consent check functions
# ------------------------------------------------------------
def python_has_unresolved(pid):
"""Deterministic check for unresolved objections using Python facts."""
facts = proposal_facts.get(pid, {"raised": set(), "resolved": set()})
raised_oids = {oid for (oid, _) in facts["raised"]}
unresolved = raised_oids - facts["resolved"]
return len(unresolved) > 0
def python_has_consent(pid):
"""Deterministic check for consent: consent when there are NO unresolved objections."""
return not python_has_unresolved(pid)
# ------------------------------------------------------------
# Convert a JSON proposal into MeTTa statements and record facts
# ------------------------------------------------------------
def assert_proposal_and_record(path):
with open(path) as f:
data = json.load(f)
log("\nParsing JSON:", path)
if args.verbose:
log(json.dumps(data, indent=2))
pid = data.get("id", "UnknownProposal")
submitter = data.get("submitter", "UnknownSubmitter")
reviewers = data.get("reviewers", [])
objections = data.get("objections", [])
stmts = []
stmts.append(f"(BudgetProposal {pid})")
stmts.append(f"(SubmittedBy {pid} {submitter})")
for r in reviewers:
stmts.append(f"(Reviewer {r})")
stmts.append(f"(ReviewedBy {pid} {r})")
# Prepare Python canonical store
if pid not in proposal_facts:
proposal_facts[pid] = {"raised": set(), "resolved": set()}
for obj in objections:
oid = obj.get("id")
reviewer = obj.get("reviewer", "UnknownReviewer")
# Assert canonical MeTTa facts: (ObjectionRaised P O R)
stmts.append(f"(ObjectionRaised {pid} {oid} {reviewer})")
if obj.get("resolved", False):
stmts.append(f"(ObjectionResolved {pid} {oid})")
proposal_facts[pid]["resolved"].add(oid)
else:
proposal_facts[pid]["raised"].add((oid, reviewer))
log("Generated MeTTa statements:")
for s in stmts:
log(" ", s)
# Assert statements into MeTTa
metta.run("\n".join(stmts))
return pid
# ------------------------------------------------------------
# Evaluate consent (Python-grounded check as canonical)
# ------------------------------------------------------------
def check_consent(pid):
log(f"\nChecking consent for {pid} ...")
# --- Python-grounded check (deterministic) ---
consent = python_has_consent(pid)
# MeTTa's view for debugging (advisory)
me_unresolved = metta.run(f"(HasUnresolvedObjection {pid})")
me_consent = metta.run(f"(HasConsent {pid})")
if args.verbose:
log(f"[DEBUG] pid={pid} python_unresolved={python_has_unresolved(pid)} python_consent={consent}")
log(f"[DEBUG] metta_unresolved={me_unresolved} metta_consent={me_consent}")
# Use Python consent as the canonical decision
return consent
# ------------------------------------------------------------
# Simulation Driver
# ------------------------------------------------------------
def simulate(folder):
# Reset MeTTa interpreter and clear facts for clean state
global metta, proposal_facts
metta = MeTTa()
proposal_facts.clear()
load_rules()
files = glob.glob(folder + "/*.json")
if not files:
print("No JSON files found in folder:", folder)
return {}
results = {}
for f in files:
pid = assert_proposal_and_record(f)
consent = check_consent(pid)
results[pid] = consent
return results
# ------------------------------------------------------------
# Main
# ------------------------------------------------------------
if __name__ == "__main__":
results = simulate(args.folder)
print("\n===== CONSENT EVALUATION RESULTS =====")
for pid, res in results.items():
print(f"{pid:25} => {res}")