From 707eecc3d1eccb313aa1d9da6749707554b4e1fb Mon Sep 17 00:00:00 2001 From: lobnaelnahas10 Date: Sat, 23 Apr 2022 05:00:39 +0200 Subject: [PATCH 1/3] first commit --- code/core/modules/network/act.py | 94 +++++++++++++++++++++++++++++ code/core/modules/network/agenda.py | 11 ++++ 2 files changed, 105 insertions(+) create mode 100644 code/core/modules/network/act.py create mode 100644 code/core/modules/network/agenda.py diff --git a/code/core/modules/network/act.py b/code/core/modules/network/act.py new file mode 100644 index 0000000..37f08a4 --- /dev/null +++ b/code/core/modules/network/act.py @@ -0,0 +1,94 @@ +from pickle import FALSE, TRUE +from pydoc import resolve +from code.core.modules.network.agenda import agenda + + +class act(Entity) : + def __init__(self) -> None: + super().__init__() + self.stage = agenda.start + + def start(act): + #find a plan for supported complex acts or exexcute + #if the act is primitive else terminate the system + if is_supported(act): + if(act.is_primitive): + act.stage = agenda.find_preconditions + find_preconditions(act) + else: + act.stage = agenda.find_plan + find_plan(act) + else: + print("This action is not supported") + return + + def find_plan(act) : + #find a plan if exists, push the plan to the stack + #then pull the first act and start again (to check if primitve) + plans = backward_chain(act) + if(plans.isEmpty()): + print("No possible plans were found") + return FALSE + else: + plan = plans.do_one + actStack.push(plan) + act.stage = agenda.find_preconditions + new_act = actStack.pull() + new_act.stage = agenda.start + start(new_act) + + def execute(act) : + #execute primitive acts + if(act.isPrimitive): + act.executePrimitive() + act.stage = agenda.find_effects + find_effects(act) + #find a plan for complex acts + else: + act.stage = agenda.start + start(act) + + def test_precondition(precondition): + #check that the preconditions are asserted else return false + if precondition.isAsserted(): + return TRUE + else : + return FALSE + + def find_preconditions(act) : + #check for preconditions and test them else execute + preconditions = backward_chain(act) + if(not preconditions.isEmpty()): + act.stage = agenda.test + for precondition in preconditions: + isSatisfied = test_precondition(precondition) + #if preconditions are not satisfied find another plan, else continue executing + if not isSatisfied: + #try finding another way to reach this act + act.stage = agenda.find_plan + planFound = find_plan(act) + #if no plan is found try finding a plan for the initial act + if(not planFound): + while not actStack.isEmpty(): + act = actStack.pop() + find_plan(act) + else: + act.stage = agenda.execute + execute(act) + else: + stage = agenda.execute + + def find_effects(act) : + #check effects by traversing the support & precond or from the user's input + external_effect = input("Enter effects") + internal_effect = forward_chain(act) + #believe both effects then solve contradiction if found + believe(internal_effect) + believe(external_effect) + if(check_contradiction(external_effect)): + resolve_contradiction(external_effect) + if(check_contradiction(internal_effect)): + resolve_contradiction(internal_effect) + act.stage = done + + diff --git a/code/core/modules/network/agenda.py b/code/core/modules/network/agenda.py new file mode 100644 index 0000000..ab01427 --- /dev/null +++ b/code/core/modules/network/agenda.py @@ -0,0 +1,11 @@ +import enum + + +class agenda(enum.Enum): + start = 1 + find_plan = 2 + find_preconditions = 3 + test_precondition = 4 + find_effects = 5 + execute = 6 + done = 7 \ No newline at end of file From b7eb789d6d0e023869e0117a24fd43d3a6467be4 Mon Sep 17 00:00:00 2001 From: lobnaelnahas10 Date: Sat, 23 Apr 2022 05:02:15 +0200 Subject: [PATCH 2/3] first commit --- code/core/modules/network/act.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/core/modules/network/act.py b/code/core/modules/network/act.py index 37f08a4..cb67054 100644 --- a/code/core/modules/network/act.py +++ b/code/core/modules/network/act.py @@ -1,6 +1,6 @@ from pickle import FALSE, TRUE from pydoc import resolve -from code.core.modules.network.agenda import agenda +from agenda import agenda class act(Entity) : From 1abeff82d070db945a516d2d11bd723154e5af9a Mon Sep 17 00:00:00 2001 From: lobnaelnahas10 Date: Sat, 14 May 2022 23:29:59 +0200 Subject: [PATCH 3/3] --- code/core/modules/network/act.py | 59 ++++++++++++++++---------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/code/core/modules/network/act.py b/code/core/modules/network/act.py index cb67054..76c31d2 100644 --- a/code/core/modules/network/act.py +++ b/code/core/modules/network/act.py @@ -1,23 +1,22 @@ -from pickle import FALSE, TRUE -from pydoc import resolve from agenda import agenda class act(Entity) : + def __init__(self) -> None: super().__init__() self.stage = agenda.start def start(act): - #find a plan for supported complex acts or exexcute + #find a plan for supported complex acts or excute #if the act is primitive else terminate the system - if is_supported(act): + if isSupported(act): if(act.is_primitive): act.stage = agenda.find_preconditions - find_preconditions(act) + act.find_preconditions(act) else: - act.stage = agenda.find_plan - find_plan(act) + act.stage = agenda.find_plan + act.find_plan(act) else: print("This action is not supported") return @@ -25,70 +24,70 @@ def start(act): def find_plan(act) : #find a plan if exists, push the plan to the stack #then pull the first act and start again (to check if primitve) - plans = backward_chain(act) - if(plans.isEmpty()): + plans = BackwardChain(act) + if(plans.count()==0): print("No possible plans were found") - return FALSE + return False else: plan = plans.do_one actStack.push(plan) act.stage = agenda.find_preconditions new_act = actStack.pull() new_act.stage = agenda.start - start(new_act) + act.start(new_act) def execute(act) : #execute primitive acts if(act.isPrimitive): act.executePrimitive() act.stage = agenda.find_effects - find_effects(act) + act.find_effects(act) #find a plan for complex acts else: act.stage = agenda.start - start(act) + act.start(act) def test_precondition(precondition): #check that the preconditions are asserted else return false if precondition.isAsserted(): - return TRUE + return True else : - return FALSE + return False def find_preconditions(act) : #check for preconditions and test them else execute preconditions = backward_chain(act) - if(not preconditions.isEmpty()): - act.stage = agenda.test + if(preconditions.count()!=0): + act.stage = agenda.test_precondition for precondition in preconditions: - isSatisfied = test_precondition(precondition) + isSatisfied = act.test_precondition(precondition) #if preconditions are not satisfied find another plan, else continue executing if not isSatisfied: #try finding another way to reach this act act.stage = agenda.find_plan - planFound = find_plan(act) + planFound = act.find_plan(act) #if no plan is found try finding a plan for the initial act if(not planFound): - while not actStack.isEmpty(): + while actStack.count()>0: act = actStack.pop() - find_plan(act) + act.find_plan(act) else: act.stage = agenda.execute - execute(act) + act.execute(act) else: stage = agenda.execute def find_effects(act) : #check effects by traversing the support & precond or from the user's input external_effect = input("Enter effects") - internal_effect = forward_chain(act) + internal_effect = BackwardChain(act) #believe both effects then solve contradiction if found - believe(internal_effect) - believe(external_effect) - if(check_contradiction(external_effect)): - resolve_contradiction(external_effect) - if(check_contradiction(internal_effect)): - resolve_contradiction(internal_effect) - act.stage = done + ForwardChain(internal_effect) + ForwardChain(external_effect) + checkContradiction(external_effect) + # resolve_contradiction(external_effect) + checkContradiction(internal_effect) + # resolveContradiction(internal_effect) + act.stage = agenda.done