-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackingState.py
More file actions
96 lines (83 loc) · 3.4 KB
/
HackingState.py
File metadata and controls
96 lines (83 loc) · 3.4 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
# -*- coding: utf-8 -*-
from Observation import Observation
class HackingState():
class ObsState():
def __init__(self, obs, state):
self.obs = obs
self.state = state
"""Hacking State class models a state"""
# def __init__(self, name, description, isFinalState):
def __init__(self, name, isFinalState):
self.actions = []
self.observationStateMap = {}
self.__name = name
# self.__description = description
self.finalState = isFinalState
def __eq__(self, other):
""" a state is equal
"""
# return self.__class__ == other.__class__ and self.__name == other.__name and other.__description == self.__description
return self.__class__ == other.__class__ and self.__name == other.__name
def getName(self):
"""returns the state name
:returns: name of the state
"""
return self.__name
def addAction(self, action):
"""Add the action for the current state
:action: the current action to do
"""
noContain = len([a for a in self.actions if a.getID() == action.getID()]) == 0
# if action not in self.actions:
if noContain:
self.actions.append(action)
def isFinalState(self):
"""Returns True if it is a final state
:returns: a boolean value
"""
return self.finalState
def addObservationState(self, observation, state):
self.observationStateMap[observation.getID()] = self.ObsState(observation, state)
def setObservations(self, observations, states):
"""set the observations for this state """
for i in len(observations):
self.addObservationState(observations[i], states[i])
def suggestActions(self):
"""Returns the action to do
:returns: action
"""
return self.actions
def availableObservations(self):
observations = []
"""Returns the list of available observations"
:returns: <list> observations
"""
for k , v in self.observationStateMap.iteritems():
observations.append(v.obs)
return observations
def getObservationStateMap(self):
return self.observationStateMap
def acquireObservation(self, hg, obsID):
"""Acquire the observation from the state set the new state"""
obsState = self.observationStateMap[obsID]
print "Update state to %s" % (obsState.state.getName())
newState = obsState.state
hg.setState(newState)
def __str__(self):
"""returns the string description of state
:returns: a string
"""
# strRet = "Name: %s \nDescr: %s\nAction: %s\nisFinalState: %s\n" % (self.__name, self.__description, self.action, self.finalState)
strRet = "Name: %s \nActions: %s\nisFinalState: %s\n" % (self.__name, self.actions, self.finalState)
observations = "Observations: "
for k,v in self.observationStateMap.iteritems():
observations = observations + str(v.obs) + ", "
return strRet + observations
def __eq__(self, theState):
"""Overrides the default implementation"""
if isinstance(theState, HackingState):
return self.getName() == theState.getName()
return False
def __ne__(self, other):
"""Overrides the default implementation (unnecessary in Python 3)"""
return not self.__eq__(other)