-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2r_parser.py
More file actions
388 lines (325 loc) · 17.5 KB
/
s2r_parser.py
File metadata and controls
388 lines (325 loc) · 17.5 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import re
import uuid
import glob
import os
class DRLParser:
def __init__(self):
# Mapping DRL Objects to DMN Input Variables
self.obj_mappings = {
"p": "person",
"Person": "person",
"h": "household",
"Household": "household",
"RequestExpense": "expense",
"IncomeHeadAndSpouseEarnedYearly": "incomeEarnedYearly",
"IncomeHouseholdTotalYearly": "incomeHouseholdYearly",
"IncomeHouseholdMonthlyCAMinusWorkExpense": "incomeCA",
"IncomeHouseholdTotalMonthly": "incomeMonthly",
"IncomeHouseholdHasCashAssistance": "hasCashAssistance",
"IncomeHouseholdHasSSI": "hasSSI",
"MembersPlusPregnant": "membersPlusPregnant",
"HeadOfHouseholdMarried": "isMarried",
"ChildrenStudentBlindDisabledEITC": "childEITC",
"IncomeOwnersTotalYearly": "incomeOwnersYearly",
"IncomeAdultsTotalMonthly": "incomeAdultsMonthly",
"IncomeMedicaidTotalYearly": "incomeMedicaidYearly",
"MembersMedicaid": "membersMedicaid",
"IncomeChildCareVoucherTotalMonthly": "incomeVoucherMonthly",
"IncomeHouseholdHasBenefit": "hasBenefit"
# Add more as discovered
}
def parse_file(self, filepath):
with open(filepath, 'r') as f:
content = f.read()
rule_name = os.path.basename(filepath).replace(".drl", "")
# logical_groups is a list of lists of constraints
all_logical_groups = []
# Extract 'when' block (FIND ALL RULES)
matches = re.finditer(r'when\s+(.*?)\s+then', content, re.DOTALL)
for match in matches:
condition_block = match.group(1).strip()
# Strip comments (// and /* */)
def replacer(match):
s = match.group(0)
if s.startswith('/'): return " " # note: a space
else: return s
pattern = re.compile(r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', re.DOTALL | re.MULTILINE)
condition_block = re.sub(pattern, replacer, condition_block)
# Normalize whitespace
condition_block = re.sub(r'\s+', ' ', condition_block)
# Normalize legacy operators (Regex to handle spacing like a||b)
condition_block = re.sub(r'\|\|', ' or ', condition_block)
condition_block = re.sub(r'&&', ', ', condition_block)
# Logic parsing for this rule...
current_rule_groups = []
cursor = 0
while cursor < len(condition_block):
# Check for block start "("
if condition_block[cursor] == '(':
# Find matching paren
end = self._find_matching_paren(condition_block, cursor)
if end == -1: break
block = condition_block[cursor+1:end].strip()
# Check if this block is an OR group
if " or " in block:
sub_variants = self._split_logic(block, " or ")
# Expand atoms within variants?
expanded_variants = []
for v in sub_variants:
# v might be "Household(a) and Person(b)"
# We need to preserve the AND structure of the variant
# But if "Household(a or b)" is inside, we need to explode it?
# This gets complicated.
# Simplification: Assume variants at this level are mostly clear.
# But let's run _expand_atom on them just in case they are single atoms?
if v.startswith("(") or " and " in v:
# It's a composite. Leave it for now or recurse?
# Recursive splitting handled by main loop structure usually.
expanded_variants.append(v)
else:
expanded_variants.extend(self._expand_atom(v))
current_rule_groups.append(expanded_variants)
else:
if block.startswith("Household") or block.startswith("Person"):
current_rule_groups.append(self._expand_atom(block))
else:
current_rule_groups.append(self._expand_atom(block))
cursor = end + 1
else:
# Look for Object(...) start
match_obj = re.match(r'([A-Z][a-zA-Z0-9]+)\s*\(', condition_block[cursor:])
if match_obj:
# Found Object(
start_paren_idx = cursor + condition_block[cursor:].find('(')
end = self._find_matching_paren(condition_block, start_paren_idx)
whole_str = condition_block[cursor:end+1]
current_rule_groups.append(self._expand_atom(whole_str)) # Explode if internal logic
cursor = end + 1
else:
cursor += 1
# Add this rule's logic as a separate set of rows?
# Actually, DIFFERENT rules in a file usually mean "OR" (Rule 1 OR Rule 2 OR Rule 3).
# So we should process `current_rule_groups` (which are ANDed) into rows, and add them to the master list.
# Cartesian product for THIS rule
import itertools
rule_rows_combinations = list(itertools.product(*current_rule_groups))
all_logical_groups.extend(rule_rows_combinations)
return self._generate_dmn_model_from_rows(rule_name, all_logical_groups)
def _find_matching_paren(self, text, start_index):
count = 0
for i in range(start_index, len(text)):
if text[i] == '(': count += 1
elif text[i] == ')': count -= 1
if count == 0: return i
return -1
def _split_logic(self, text, separator):
# Split by separator but respect parentheses
parts = []
last_split = 0
depth = 0
for i in range(len(text)):
if text[i] == '(': depth += 1
elif text[i] == ')': depth -= 1
if depth == 0 and text[i:i+len(separator)] == separator:
parts.append(text[last_split:i].strip())
last_split = i + len(separator)
parts.append(text[last_split:].strip())
return parts
def _expand_atom(self, atom):
# atom: "Income(type in X, (A or B))" -> ["Income(type in X, A)", "Income(type in X, B)"]
match = re.match(r'([A-Z][a-zA-Z0-9]+)\s*\((.*)\)', atom)
if not match:
return [atom]
obj_name = match.group(1)
content = match.group(2)
# Recursively strip outer parens from content if it wraps the whole thing
# e.g. "( (A, B) )" -> "A, B"
# Be careful not to strip "(A) and (B)" if parens match but logic is inside?
# Only strip if the outer parens correspond to each other and cover length
while content.startswith('(') and content.endswith(')') and self._find_matching_paren(content, 0) == len(content)-1:
content = content[1:-1].strip()
# Split content by comma (AND inside object)
parts = self._split_logic(content, ",")
# Expand each part if it has OR
expanded_parts = [] # List of lists of variants for each part
for p in parts:
p = p.strip()
if not p: continue
# Check for " or " inside
# Handle recursive parens: "(A or B)" -> "A or B"
while p.startswith("(") and p.endswith(")") and self._find_matching_paren(p, 0) == len(p)-1:
p = p[1:-1].strip()
if " or " in p:
# Check if the 'or' is real branching logic or part of string?
# Assuming logic for now
variants = self._split_logic(p, " or ")
expanded_parts.append(variants)
else:
expanded_parts.append([p])
# Cartesian product of parts
import itertools
product = list(itertools.product(*expanded_parts))
results = []
for combo in product:
# Reassemble: "type in X, A"
new_content = ", ".join(combo)
results.append(f"{obj_name}({new_content})")
return results
def _generate_dmn_model_from_rows(self, rule_name, rows_combinations):
# Now we parse the specific constraints in each row to build the table columns
# We need a unified set of inputs (columns)
all_inputs = set()
parsed_rows = []
for combination in rows_combinations:
# combination is tuple of strings, e.g. ("Person(age < 5)", "Household(...)")
row_constraints = {}
for segment in combination:
# Segment might be "(Household(x=1) and Income(y=2))"
# Recursively strip outer parens from segment if fully enclosed
while segment.startswith('(') and segment.endswith(')') and self._find_matching_paren(segment, 0) == len(segment)-1:
segment = segment[1:-1].strip()
# Split by " and "
atoms = self._split_logic(segment, " and ")
for atom in atoms:
atom = atom.strip()
while atom.startswith('(') and atom.endswith(')') and self._find_matching_paren(atom, 0) == len(atom)-1:
atom = atom[1:-1].strip()
if not atom: continue
# atom: "Person( age < 13 )"
# Extract Object and content
m = re.match(r'([A-Z][a-zA-Z0-9]+)\s*\((.*)\)', atom)
if m:
obj_type = m.group(1)
content = m.group(2)
# Parse content: "age < 13, disabled == true"
# Handle && as comma
content = content.replace(" && ", ", ")
# Split by comma
field_pairs = self._split_logic(content, ",")
for pair in field_pairs:
pair = pair.strip()
while pair.startswith('(') and pair.endswith(')') and self._find_matching_paren(pair, 0) == len(pair)-1:
pair = pair[1:-1].strip()
if not pair: continue
# Parse "field op value"
# Regex for "field op val"
# Op can be: <, >, <=, >=, ==, in, matches, && (handled by split?), not in
# Check for "not in" first
op_match = re.search(r'([a-zA-Z0-9_\.]+)\s*(not in|==|!=|>=|<=|>|<|in)\s*(.*)', pair)
if op_match:
field = op_match.group(1)
op = op_match.group(2)
val = op_match.group(3)
# Clean value
val = val.strip().strip("'").strip('"')
while val.startswith("(") and val.endswith(")"):
# Remove parens for DMN list
val = val[1:-1].strip()
# Final safety net: handle failed expansion artifacts
if " or " in val:
val = val.replace(" or ", ", ")
# Remove redundant "field ==" from list
val = re.sub(r'[a-zA-Z0-9_\.]+\s*==\s*', '', val)
# Build column key
if obj_type in self.obj_mappings:
base = self.obj_mappings[obj_type]
else:
base = obj_type.lower()
col_key = f"{base}.{field}"
all_inputs.add(col_key)
# Format for DMN
dmn_val = self._format_dmn_value(op, val)
row_constraints[col_key] = dmn_val
parsed_rows.append(row_constraints)
# Generate XML
return self._build_xml(rule_name, sorted(list(all_inputs)), parsed_rows)
def _format_dmn_value(self, op, val):
if op == "==": return f"{val}" # Exact match, but handle boolean "true"
if op == "in": return f"{val}" # DMN can handle comma lists naturally? "A, B" is implicit OR.
if op == "not in": return f"not({val})"
if op == "<": return f"< {val}"
if op == ">": return f"> {val}"
if op == "<=": return f"<= {val}"
if op == ">=": return f">= {val}"
return "-"
def _build_xml(self, rule_name, inputs, rows):
# inputs: list of "person.age"
# rows: list of dicts {"person.age": "< 18"}
# Build Input Columns
input_xml_parts = []
for i, inp in enumerate(inputs):
# Simplistic typing
type_ref = "string" # Default safe
low = inp.lower()
if "age" in low or "members" in low: type_ref = "integer"
elif "amount" in low or "income" in low or "expense" in low: type_ref = "double"
elif "disabled" in low or "blind" in low or "pregnant" in low or "married" in low or "has" in low or "headofhousehold" == low.split(".")[-1]: type_ref = "boolean"
# Input Expression: we use the dot notation "person.age" which maps to the JSON variable structure
input_xml_parts.append(f"""
<input id="Input_{i}" label="{inp}">
<inputExpression id="InputExpression_{i}" typeRef="{type_ref}">
<text>{inp}</text>
</inputExpression>
</input>""")
# Build Rules
rule_xml_parts = []
for row in rows:
entries = []
for inp in inputs:
val = row.get(inp, "-")
if val == "true" or val == "false":
# Ensure boolean is unquoted? DMN standard usually likes just true/false
pass
# Wrap in <text>
# Use standard XML quote for safety if needed, but we did basic escaping
entries.append(f'<inputEntry id="Unary_{uuid.uuid4().hex}"><text>{val}</text></inputEntry>')
entries.append(f'<outputEntry id="Lit_{uuid.uuid4().hex}"><text>true</text></outputEntry>')
rule_xml_parts.append(f"""
<rule id="Rule_{uuid.uuid4().hex}">
{ "".join(entries) }
</rule>""")
# Fallback Rule (Implicit Ineligible)
fallback_entries = []
for _ in inputs:
fallback_entries.append(f'<inputEntry id="Unary_{uuid.uuid4().hex}"><text>-</text></inputEntry>')
fallback_entries.append(f'<outputEntry id="Lit_{uuid.uuid4().hex}"><text>false</text></outputEntry>')
rule_xml_parts.append(f"""
<rule id="Rule_Fallback">
{ "".join(fallback_entries) }
</rule>""")
def_id = f"Definitions_{uuid.uuid4().hex}"
dec_id = f"Decision_{rule_name}"
return f"""<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/" xmlns:dmndi="https://www.omg.org/spec/DMN/20191111/DMNDI/" xmlns:dc="http://www.omg.org/spec/DMN/20180521/DC/" id="{def_id}" name="{rule_name}" namespace="http://camunda.org/schema/1.0/dmn" exporter="Camunda Modeler" exporterVersion="4.6.0">
<decision id="{dec_id}" name="{rule_name}">
<decisionTable id="Table_{uuid.uuid4().hex}" hitPolicy="FIRST">
{"".join(input_xml_parts)}
<output id="Output_1" label="Is Eligible" typeRef="boolean" />
{"".join(rule_xml_parts)}
</decisionTable>
</decision>
<dmndi:DMNDI>
<dmndi:DMNDiagram>
<dmndi:DMNShape dmnElementRef="{dec_id}">
<dc:Bounds height="80" width="180" x="160" y="80" />
</dmndi:DMNShape>
</dmndi:DMNDiagram>
</dmndi:DMNDI>
</definitions>"""
if __name__ == "__main__":
parser = DRLParser()
files = glob.glob("ACCESS-NYC-Rules/accessnyc/rules/S2R*.drl")
# Test with S2R008
# files = ["ACCESS-NYC-Rules/accessnyc/rules/S2R008.drl"]
if not os.path.exists("dmn_output"):
os.makedirs("dmn_output")
for f in files:
print(f"Converting {f}...")
try:
xml = parser.parse_file(f)
if xml:
name = os.path.basename(f).replace(".drl", ".dmn")
with open(f"dmn_output/{name}", "w") as out:
out.write(xml)
except Exception as e:
print(f"Error converting {f}: {e}")