예제 #1
0
    def test_graph_isomorphic(self):
        from Goap.Planner import Node
        from Goap.Planner import Edge

        acts = Actions()
        acts.add(name='CreateTmpDir',
                 pre_conditions={
                     'tmp_dir_state': False,
                     'tmp_dir_content': False
                 },
                 effects={
                     'tmp_dir_state': True,
                     'tmp_dir_content': False
                 },
                 shell='mkdir -p /tmp/goap_tmp')
        acts.add(name='CreateToken',
                 pre_conditions={
                     'tmp_dir_state': True,
                     'tmp_dir_content': False
                 },
                 effects={
                     'tmp_dir_state': True,
                     'tmp_dir_content': True
                 },
                 shell='touch /tmp/goap_tmp/.token')
        node1 = Node(attributes={
            'tmp_dir_state': False,
            'tmp_dir_content': False
        })
        node2 = Node(attributes={
            'tmp_dir_state': True,
            'tmp_dir_content': False
        })
        node3 = Node(attributes={
            'tmp_dir_state': True,
            'tmp_dir_content': True
        })
        edge1 = Edge(name='CreateTmpDir',
                     predecessor=node1,
                     successor=node2,
                     obj=acts.get('CreateTmpDir'))
        edge2 = Edge(name='CreateToken',
                     predecessor=node2,
                     successor=node3,
                     obj=acts.get('CreateToken'))
        g1 = nx.DiGraph(nodes=[node1, node2, node3], edges=[edge1, edge2])
        g2 = self.planner.graph.directed
        assert nx.is_isomorphic(g1, g2) is True
예제 #2
0
def setup_actions():
    actions = Actions()
    actions.add(name='CreateTmpDir',
                pre_conditions={
                    'tmp_dir_state': 'not_exist',
                    'tmp_dir_content': 'token_not_found'
                },
                effects={
                    'tmp_dir_state': 'exist',
                    'tmp_dir_content': 'token_not_found'
                },
                shell='mkdir -p /tmp/goap_tmp')
    actions.add(name='CreateToken',
                pre_conditions={
                    'tmp_dir_state': 'exist',
                    'tmp_dir_content': 'token_not_found'
                },
                effects={
                    'tmp_dir_state': 'exist',
                    'tmp_dir_content': 'token_found'
                },
                shell='touch /tmp/goap_tmp/.token')
    return actions
예제 #3
0
def setup_actions():
    actions = Actions()
    actions.add(name='CloneGitRepo',
                pre_conditions={},
                effects={},
                shell='mkdir -p /tmp/goap_tmp')
    actions.add(name='BuildProject',
                pre_conditions={},
                effects={},
                shell='touch /tmp/goap_tmp/.token')
    actions.add(name='NotifyBuild',
                pre_conditions={},
                effects={},
                shell='touch /tmp/goap_tmp/.token')
    return actions
예제 #4
0
 def setupPlanner():
     acts = Actions()
     acts.add(
         name='ExpandLV',
         pre_conditions={
             'lv_need_expansion': True,
             'vg_need_expansion': False,
         },
         effects={
             'lv_need_expansion': False,
         },
         shell='echo expand_lv',
         cost=1.5
     )
     acts.add(
         name='ExpandVG',
         pre_conditions={
             'vg_need_expansion': True,
             'pv_need_expansion': False,
         },
         effects={
             'vg_need_expansion': False,
         },
         shell='echo expand_vg',
         cost=1.0,
     )
     acts.add(
         name='ExpandPV',
         pre_conditions={
             'pv_need_expansion': True,
         },
         effects={
             'pv_need_expansion': False,
         },
         shell='echo expand_pv',
         cost=0.5,
     )
     return Planner(actions=acts)
예제 #5
0
class AutomatonTest(unittest.TestCase):
    @staticmethod
    def __reset_environment():
        if path.isdir('/tmp/goap_tmp'):
            subprocess.call(['rm', '-rf', '/tmp/goap_tmp'])

    def __print(self):
        self.print.pprint(
            'Acknowledge world: {}, Action Plan: {}, Result: {}'.format(
                self.automaton.world_state, self.automaton.action_plan,
                self.automaton.actions_response))

    def setUp(self):
        self.print = PrettyPrinter(indent=4)

        self.goal = {
            "tmp_dir_state": "exist",
            "tmp_dir_content": "token_found",
        }
        self.actions = Actions()
        self.sensors = Sensors()
        self.sensors.add(
            name='SenseTmpDirState',
            shell=
            'if [ -d "/tmp/goap_tmp" ]; then echo -n "exist"; else echo -n "not_exist"; fi',
            binding='tmp_dir_state')
        self.sensors.add(
            name='SenseTmpDirContent',
            shell=
            '[ -f /tmp/goap_tmp/.token ] && echo -n "token_found" || echo -n "token_not_found"',
            binding='tmp_dir_content')
        self.actions.add(name='CreateTmpDir',
                         pre_conditions={
                             'tmp_dir_state': 'not_exist',
                             'tmp_dir_content': 'token_not_found'
                         },
                         effects={
                             'tmp_dir_state': 'exist',
                             'tmp_dir_content': 'token_not_found'
                         },
                         shell='mkdir -p /tmp/goap_tmp')
        self.actions.add(name='CreateToken',
                         pre_conditions={
                             'tmp_dir_state': 'exist',
                             'tmp_dir_content': 'token_not_found'
                         },
                         effects={
                             'tmp_dir_state': 'exist',
                             'tmp_dir_content': 'token_found'
                         },
                         shell='touch /tmp/goap_tmp/.token')
        world_state_matrix = {
            "tmp_dir_state": 'Unknown',
            "tmp_dir_content": 'Unknown',
        }
        self.automaton = Automaton(name='directory_watcher',
                                   actions=self.actions,
                                   sensors=self.sensors,
                                   world_state=world_state_matrix)

    def test_sensing(self):
        self.__reset_environment()
        self.automaton.input_goal(self.goal)
        self.automaton.sense()
        assert self.automaton.world_state == {
            'tmp_dir_state': 'not_exist',
            'tmp_dir_content': 'token_not_found'
        }

    def test_planning(self):
        create_tmp_dir = self.actions.get('CreateTmpDir')
        create_token = self.actions.get('CreateToken')
        self.__reset_environment()
        self.automaton.input_goal(self.goal)
        self.automaton.sense()
        self.automaton.plan()
        action_plan = [
            action[2]['object'] for action in self.automaton.action_plan
        ]
        assert action_plan == [create_tmp_dir, create_token]

    def test_acting(self):
        self.__reset_environment()
        self.automaton.input_goal(self.goal)
        self.automaton.sense()
        self.automaton.plan()
        self.automaton.act()
        assert path.isdir('/tmp/goap_tmp') and path.isfile(
            '/tmp/goap_tmp/.token')
예제 #6
0
class ActionTest(unittest.TestCase):
    def setUp(self):
        # ACTIONS
        self.actions = Actions()

    def test_add_action_success(self):
        self.actions.add(name='CreateVPC',
                         pre_conditions={
                             'vpc': False,
                             'db': False,
                             'app': False
                         },
                         effects={
                             'vpc': True,
                             'db': False,
                             'app': False
                         },
                         shell='awscli vpc create')
        self.actions.add(name='CreateDB',
                         pre_conditions={
                             'vpc': True,
                             'db': False,
                             'app': False
                         },
                         effects={
                             'vpc': True,
                             'db': True,
                             'app': False
                         },
                         shell='awscli vpc create')
        assert "CreateVPC" == str(self.actions.get(name='CreateVPC'))
        assert "CreateDB" == str(self.actions.get(name='CreateDB'))

    def test_remove_action_success(self):
        self.actions.add(name='CreateVPC',
                         pre_conditions={
                             'vpc': False,
                             'db': False,
                             'app': False
                         },
                         effects={
                             'vpc': True,
                             'db': False,
                             'app': False
                         },
                         shell='awscli vpc create')
        self.actions.add(name='CreateDB',
                         pre_conditions={
                             'vpc': True,
                             'db': False,
                             'app': False
                         },
                         effects={
                             'vpc': True,
                             'db': True,
                             'app': False
                         },
                         shell='awscli vpc create')
        self.actions.remove(name='CreateVPC')
        assert "CreateDB" == str(self.actions.get(name='CreateDB'))

    def test_remove_action_error(self):
        self.actions.add(name='CreateVPC',
                         pre_conditions={
                             'vpc': False,
                             'db': False,
                             'app': False
                         },
                         effects={
                             'vpc': True,
                             'db': False,
                             'app': False
                         },
                         shell='awscli vpc create')
        self.actions.add(name='CreateDB',
                         pre_conditions={
                             'vpc': True,
                             'db': False,
                             'app': False
                         },
                         effects={
                             'vpc': True,
                             'db': True,
                             'app': False
                         },
                         shell='awscli vpc create')
        self.actions.remove(name='CreateAPP')
        assert 'None' == str(self.actions.get(name='CreateAPP'))
예제 #7
0
파일: Automaton.py 프로젝트: leopepe/GOApy
     {'vpc_state': 'available', 'db_state': 'available', 'app_state': 'running'}
 ])
 world_state_matrix = {
     "vpc_state": 'Unknown',
     "db_state": 'Unknown',
     "app_state": 'Unknown',
 }
 goal = {
     "vpc_state": "available",
     "db_state": "available",
     "app_state": "running",
 }
 aws_actions = Actions()
 aws_actions.add(
     name='CreateVPC',
     pre_conditions={'vpc_state': 'unavailable', 'db_state': 'unavailable', 'app_state': 'unavailable'},
     effects={'vpc_state': 'available', 'db_state': 'unavailable', 'app_state': 'unavailable'},
     shell='echo "vpc created"'
 )
 aws_actions.add(
     name='CreateDB',
     pre_conditions={'vpc_state': 'available', 'db_state': 'unavailable', 'app_state': 'unavailable'},
     effects={'vpc_state': 'available', 'db_state': 'available', 'app_state': 'unavailable'},
     shell='echo "db created"'
 )
 aws_actions.add(
     name='CreateApp',
     pre_conditions={'vpc_state': 'available', 'db_state': 'available', 'app_state': 'unavailable'},
     effects={'vpc_state': 'available', 'db_state': 'available', 'app_state': 'running'},
     shell='echo "app created" > /tmp/CreateApp.out'
 )
 aws_sensors = Sensors()
예제 #8
0
class PlannerTest(unittest.TestCase):
    @staticmethod
    def __reset_environment():
        if path.isdir('/tmp/goap_tmp'):
            subprocess.call(['rm', '-rf', '/tmp/goap_tmp'])

    def __print(self):
        self.print.pprint('Nodes: {}, Edges: {}'.format(
            self.planner.graph.nodes, self.planner.graph.edges))

    def setUp(self):
        self.print = PrettyPrinter(indent=4)
        self.setUpDirHandlerCMD()
        self.setUpLvmCMD()
        self.init_ws = WorldState({
            "tmp_dir_state": False,
            "tmp_dir_content": False,
        })
        self.gs = WorldState({
            "tmp_dir_state": True,
            "tmp_dir_content": True,
        })
        self.planner = Planner(actions=self.dir_handler_cmd)

    def setUpDirHandlerCMD(self):
        self.dir_handler_cmd = Actions()
        self.dir_handler_cmd.add(name='CreateTmpDir',
                                 pre_conditions={
                                     'tmp_dir_state': False,
                                     'tmp_dir_content': False
                                 },
                                 effects={
                                     'tmp_dir_state': True,
                                     'tmp_dir_content': False
                                 },
                                 shell='mkdir -p /tmp/goap_tmp')
        self.dir_handler_cmd.add(name='CreateToken',
                                 pre_conditions={
                                     'tmp_dir_state': True,
                                     'tmp_dir_content': False
                                 },
                                 effects={
                                     'tmp_dir_state': True,
                                     'tmp_dir_content': True
                                 },
                                 shell='touch /tmp/goap_tmp/.token')

    def setUpLvmCMD(self):
        self.lv_act = Actions()
        self.lv_act.add(name='ExpandLV',
                        pre_conditions={
                            'lv_need_expansion': True,
                            'vg_need_expansion': False,
                        },
                        effects={
                            'lv_need_expansion': False,
                        },
                        shell='echo expand_lv',
                        cost=1.0)
        self.lv_act.add(name='ExpandVG',
                        pre_conditions={
                            'vg_need_expansion': True,
                            'pv_need_expansion': False,
                        },
                        effects={
                            'vg_need_expansion': False,
                        },
                        shell='echo expand_vg')
        self.lv_act.add(
            name='ExpandPV',
            pre_conditions={
                'pv_need_expansion': True,
            },
            effects={
                'pv_need_expansion': False,
            },
            shell='echo purge_old_files',
            cost=1.5,
        )

    def test_set_goal(self):
        self.planner.goal = self.gs
        assert self.gs == self.planner.goal

    def test_graph_isomorphic(self):
        from Goap.Planner import Node
        from Goap.Planner import Edge

        acts = Actions()
        acts.add(name='CreateTmpDir',
                 pre_conditions={
                     'tmp_dir_state': False,
                     'tmp_dir_content': False
                 },
                 effects={
                     'tmp_dir_state': True,
                     'tmp_dir_content': False
                 },
                 shell='mkdir -p /tmp/goap_tmp')
        acts.add(name='CreateToken',
                 pre_conditions={
                     'tmp_dir_state': True,
                     'tmp_dir_content': False
                 },
                 effects={
                     'tmp_dir_state': True,
                     'tmp_dir_content': True
                 },
                 shell='touch /tmp/goap_tmp/.token')
        node1 = Node(attributes={
            'tmp_dir_state': False,
            'tmp_dir_content': False
        })
        node2 = Node(attributes={
            'tmp_dir_state': True,
            'tmp_dir_content': False
        })
        node3 = Node(attributes={
            'tmp_dir_state': True,
            'tmp_dir_content': True
        })
        edge1 = Edge(name='CreateTmpDir',
                     predecessor=node1,
                     successor=node2,
                     obj=acts.get('CreateTmpDir'))
        edge2 = Edge(name='CreateToken',
                     predecessor=node2,
                     successor=node3,
                     obj=acts.get('CreateToken'))
        g1 = nx.DiGraph(nodes=[node1, node2, node3], edges=[edge1, edge2])
        g2 = self.planner.graph.directed
        assert nx.is_isomorphic(g1, g2) is True

    def test_plan(self):
        create_tmp_dir = self.planner.actions.get('CreateTmpDir')
        create_token = self.planner.actions.get('CreateToken')
        plan = self.planner.plan(state=self.init_ws, goal=self.gs)
        action_plan = [action[2]['object'] for action in plan]
        assert action_plan == [create_tmp_dir, create_token]
예제 #9
0
파일: Agent.py 프로젝트: leopepe/GOApy
                print('[INFO] Plan executed: {}'.format(actions_result))

            print('Sleeping 7 sec from {}'.format(datetime.now()))
            sleep(7)


if __name__ == '__main__':
    from Goap.Action import Actions
    # import pprint

    # ACTIONS
    actions = Actions()
    # VPC/Network set
    actions.add(
        name='CreateVPC',
        pre_conditions={'vpc': False, 'db': False, 'app': False},
        effects={'vpc': True, 'db': False, 'app': False}
    )
    actions.add(
        name='DestroyVPC',
        pre_conditions={'vpc': True, 'db': False, 'app': False},
        effects={'vpc': False, 'db': False, 'app': False}
    )
    # DB set
    actions.add(
        name='CreateDB',
        pre_conditions={'vpc': True, 'db': False, 'app': False},
        effects={'vpc': True, 'db': True, 'app': False}
    )
    actions.add(
        name='DestroyDB',
예제 #10
0
        self.stop()
        return result


if __name__ == '__main__':
    import random
    # import pprint
    from time import sleep
    from datetime import datetime

    # ACTIONS
    actions = Actions()
    # monitor state
    actions.add(
        name='CheckModules',
        pre_conditions={'vpc_checked': False, 'db_check': False, 'app_checked': False},
        effects={'vpc_checked': True, 'db_check': True, 'app_checked': True}
    )
    # VPC/Network set
    actions.add(
        name='CreateVPC',
        pre_conditions={'vpc': False, 'db': False, 'app': False},
        effects={'vpc': True, 'db': False, 'app': False}
    )
    # DB set
    actions.add(
        name='CreateDB',
        pre_conditions={'vpc': True, 'db': False, 'app': False},
        effects={'vpc': True, 'db': True, 'app': False}
    )
    actions.add(
예제 #11
0
if __name__ == '__main__':
    import random
    # import pprint
    from time import sleep
    from datetime import datetime

    # ACTIONS
    actions = Actions()
    # monitor state
    actions.add(name='CheckModules',
                pre_conditions={
                    'vpc_checked': False,
                    'db_check': False,
                    'app_checked': False
                },
                effects={
                    'vpc_checked': True,
                    'db_check': True,
                    'app_checked': True
                })
    # VPC/Network set
    actions.add(name='CreateVPC',
                pre_conditions={
                    'vpc': False,
                    'db': False,
                    'app': False
                },
                effects={
                    'vpc': True,
                    'db': False,
예제 #12
0
     "vpc_state": 'Unknown',
     "db_state": 'Unknown',
     "app_state": 'Unknown',
 }
 goal = {
     "vpc_state": "available",
     "db_state": "available",
     "app_state": "running",
 }
 aws_actions = Actions()
 aws_actions.add(name='CreateVPC',
                 pre_conditions={
                     'vpc_state': 'unavailable',
                     'db_state': 'unavailable',
                     'app_state': 'unavailable'
                 },
                 effects={
                     'vpc_state': 'available',
                     'db_state': 'unavailable',
                     'app_state': 'unavailable'
                 },
                 shell='echo "vpc created"')
 aws_actions.add(name='CreateDB',
                 pre_conditions={
                     'vpc_state': 'available',
                     'db_state': 'unavailable',
                     'app_state': 'unavailable'
                 },
                 effects={
                     'vpc_state': 'available',
                     'db_state': 'available',
                     'app_state': 'unavailable'