summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xmonkey/monkey.py30
-rwxr-xr-xmonkey/test.py2
2 files changed, 17 insertions, 15 deletions
diff --git a/monkey/monkey.py b/monkey/monkey.py
index 3491186..b67f7fe 100755
--- a/monkey/monkey.py
+++ b/monkey/monkey.py
@@ -31,20 +31,22 @@ def fix(name, code, edits, program_lines, aux_code='', timeout=30, debug=False):
rule_lines = lines[start:end]
rule_tokens = [t for line in rule_lines for t in line]
- # Add a new rule (fact) before this rule.
- for after, cost in inserts.items():
- new_lines = lines[:start] + (after,) + lines[start:]
- new_rules = []
- for old_start, old_end in rules:
- if old_start == start:
- new_rules.append((start, start+1))
- new_rules.append((old_start + (0 if old_start < start else 1),
- old_end + (0 if old_end < start else 1)))
- new_step = ('add_rule', start, (tuple(), after))
- # Decrease probability as we add more rules.
- new_cost = cost * math.pow(0.3, len(rules))
-
- yield (new_lines, new_rules, new_step, new_cost)
+ # Prepend a new rule (fact) before this rule (only if no line in
+ # the current rule has been modified yet).
+ if start_line == 0 or start > start_line:
+ for after, cost in inserts.items():
+ new_lines = lines[:start] + (after,) + lines[start:]
+ new_rules = []
+ for old_start, old_end in rules:
+ if old_start == start:
+ new_rules.append((start, start+1))
+ new_rules.append((old_start + (0 if old_start < start else 1),
+ old_end + (0 if old_end < start else 1)))
+ new_step = ('add_rule', start, (tuple(), after))
+ # Decrease probability as we add more rules.
+ new_cost = cost * math.pow(0.3, len(rules))
+
+ yield (new_lines, new_rules, new_step, new_cost)
# Apply some edits for each line in this rule.
for line_idx in range(start, end):
diff --git a/monkey/test.py b/monkey/test.py
index b75858a..2b01b95 100755
--- a/monkey/test.py
+++ b/monkey/test.py
@@ -170,5 +170,5 @@ else:
# Try finding a fix.
print(colored('Analyzing program…', 'yellow'))
- solution, steps, fix_time, n_tested = fix(problem.name, code, edits, aux_code=aux_code, debug=True)
+ solution, steps, fix_time, n_tested = fix(problem.name, code, edits, lines, aux_code=aux_code, debug=True)
print_hint(solution, steps, fix_time, n_tested)