Create selfbuild.py
Browse files- selfbuild.py +100 -0
selfbuild.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class CascadingBody:
|
| 2 |
+
def __init__(self):
|
| 3 |
+
self.actions = {} # Action registry
|
| 4 |
+
self.things = {} # Object/state registry
|
| 5 |
+
self.relationships = {} # Thing-Action mappings
|
| 6 |
+
self.execution_stack = [] # Cascade tracking
|
| 7 |
+
self.meta_actions = {} # Actions that create actions
|
| 8 |
+
|
| 9 |
+
# Bootstrap with fundamental actions
|
| 10 |
+
self._bootstrap_core_actions()
|
| 11 |
+
|
| 12 |
+
def _bootstrap_core_actions(self):
|
| 13 |
+
# Core actions that enable self-modification
|
| 14 |
+
self.define_action('define_action', self._define_action)
|
| 15 |
+
self.define_action('relate', self._relate_thing_action)
|
| 16 |
+
self.define_action('cascade', self._execute_cascade)
|
| 17 |
+
self.define_action('compose', self._compose_actions)
|
| 18 |
+
|
| 19 |
+
def define_action(self, name, function):
|
| 20 |
+
"""Define or redefine an action"""
|
| 21 |
+
self.actions[name] = function
|
| 22 |
+
return f"Action '{name}' defined"
|
| 23 |
+
|
| 24 |
+
def _define_action(self, action_name, *params):
|
| 25 |
+
"""Meta-action: Define new actions at runtime"""
|
| 26 |
+
if len(params) >= 1:
|
| 27 |
+
# Define from existing actions
|
| 28 |
+
source_action = params[0]
|
| 29 |
+
if source_action in self.actions:
|
| 30 |
+
self.actions[action_name] = self.actions[source_action]
|
| 31 |
+
return f"Action '{action_name}' created from '{source_action}'"
|
| 32 |
+
|
| 33 |
+
# Define new programmable action
|
| 34 |
+
def new_action(*args):
|
| 35 |
+
# This can be extended to interpret code
|
| 36 |
+
result = f"Executing programmable action: {action_name}"
|
| 37 |
+
# Could compile and execute new code here
|
| 38 |
+
return result
|
| 39 |
+
|
| 40 |
+
self.actions[action_name] = new_action
|
| 41 |
+
return f"Programmable action '{action_name}' defined"
|
| 42 |
+
|
| 43 |
+
def _relate_thing_action(self, thing, action):
|
| 44 |
+
"""Create thing-action relationships"""
|
| 45 |
+
if thing not in self.relationships:
|
| 46 |
+
self.relationships[thing] = []
|
| 47 |
+
self.relationships[thing].append(action)
|
| 48 |
+
return f"Thing '{thing}' now relates to action '{action}'"
|
| 49 |
+
|
| 50 |
+
def execute_on_thing(self, thing, *args):
|
| 51 |
+
"""Execute all actions related to a thing, creating cascades"""
|
| 52 |
+
if thing not in self.relationships:
|
| 53 |
+
return f"No actions defined for thing '{thing}'"
|
| 54 |
+
|
| 55 |
+
results = []
|
| 56 |
+
for action_name in self.relationships[thing]:
|
| 57 |
+
if action_name in self.actions:
|
| 58 |
+
self.execution_stack.append(f"{thing}->{action_name}")
|
| 59 |
+
result = self.actions[action_name](*args)
|
| 60 |
+
results.append(result)
|
| 61 |
+
|
| 62 |
+
# Check if result should cascade
|
| 63 |
+
if isinstance(result, str) and result in self.relationships:
|
| 64 |
+
cascade_result = self.execute_on_thing(result)
|
| 65 |
+
results.append(f"CASCADE: {cascade_result}")
|
| 66 |
+
|
| 67 |
+
self.execution_stack.pop()
|
| 68 |
+
|
| 69 |
+
return results
|
| 70 |
+
|
| 71 |
+
def _execute_cascade(self, trigger, depth=3):
|
| 72 |
+
"""Explicit cascade execution"""
|
| 73 |
+
if depth <= 0:
|
| 74 |
+
return "Max cascade depth reached"
|
| 75 |
+
|
| 76 |
+
results = []
|
| 77 |
+
current = trigger
|
| 78 |
+
for i in range(depth):
|
| 79 |
+
if current in self.relationships:
|
| 80 |
+
action_results = self.execute_on_thing(current)
|
| 81 |
+
results.append(f"Level {i}: {action_results}")
|
| 82 |
+
# Use last result as next trigger
|
| 83 |
+
if action_results and isinstance(action_results[-1], str):
|
| 84 |
+
current = action_results[-1]
|
| 85 |
+
else:
|
| 86 |
+
break
|
| 87 |
+
return results
|
| 88 |
+
|
| 89 |
+
def _compose_actions(self, new_action_name, *action_sequence):
|
| 90 |
+
"""Compose multiple actions into one new action"""
|
| 91 |
+
def composed(*args):
|
| 92 |
+
results = []
|
| 93 |
+
for action_name in action_sequence:
|
| 94 |
+
if action_name in self.actions:
|
| 95 |
+
result = self.actions[action_name](*args)
|
| 96 |
+
results.append(result)
|
| 97 |
+
return results
|
| 98 |
+
|
| 99 |
+
self.actions[new_action_name] = composed
|
| 100 |
+
return f"Composed action '{new_action_name}' from {action_sequence}"
|