* [Caml-list] Trouble with subtyping
@ 2011-04-07 16:48 Pierre-Alexandre Voye
2011-04-07 21:44 ` Guillaume Yziquel
0 siblings, 1 reply; 2+ messages in thread
From: Pierre-Alexandre Voye @ 2011-04-07 16:48 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 5526 bytes --]
Hi all, I'm working on a lib to modelize a Multi-Agent System where each
agent are driven by a Hierarchical Finite State Machine (HFSM). Hierarchical
means that each state could contain a HFSM which is runned when the machine
come into the state.
Transitions are made by evaluating condition and events which arrives in a
mail inbox.
The goal is to define dynamically all HFSM we want, just by defining states
and transitions
This little grammar represents all possibilities of mix of condition and
event handling
type ('myAgentType, 'event) transition =
Condition of ('myAgentType -> bool)
| Event of ('event -> bool)
| EventOr of ('event -> bool) * ('myAgentType,'event) transition
| EventAnd of ('event -> bool) * ('myAgentType,'event) transition
| EventXor of ('event -> bool) * ('myAgentType,'event) transition
| EventNot of ('event -> bool)
| ConditionOr of ('myAgentType -> bool) * ('myAgentType,'event)
transition
| ConditionAnd of ('myAgentType -> bool) * ('myAgentType,'event)
transition
| ConditionXor of ('myAgentType -> bool) * ('myAgentType,'event)
transition
| ConditionNot of ('myAgentType -> bool);;
I have two classes : agent and state
"state" manage current state and is able to search, according to the agent
given in argument, what is the following state.
So state have a transition list which is (state * ('myAgentType, 'event)
transition ) list (see before).
in this list, state is the (candidate) next state, and ('myAgentType,
'event) transition the expression which allows to validate or not the
transition.
'event will always be the same object, but I want 'myAgentType to be every
object which inherit from agent.
The agent have a current state, and a 'cycle' method which searchs what is
the next state of the agent according to its (the current state) transition
list
You use it like it :
let mydwarf = new agent;;
let (ctdwarf:(dwarf,masEvent) transition) = Condition (fun (e:agent) ->
true);;
let aa = new state mydwarf;;
let aaa = new state mydwarf;;
aaa#setParent aa;;
let aab = new state mydwarf;;
aab#isIn aa;;
aaa#addTransition aab (Condition (fun (e:agent) -> true));;
mydwarf#setStartState aa;;
So we have defined an agent mydwarf which have a start state "aa"
here a simplified prototype of agent and state class :
class ['myAgentType] state :
'myAgentType ->
object
constraint 'myAgentType =
< getCurrentState : < getNom : string; .. > ... >;
val mutable action : 'myAgentType -> unit
val mutable begin_action : 'myAgentType -> unit
val mutable end_action : 'myAgentType -> unit
val mutable transitions :
('myAgentType state option * ('myAgentType, masEvent) transition
option) list
method searchTransition :
'myAgentType -> bool * 'myAgentType state * 'myAgentType state *
'myAgentType state * 'myAgentType state
method has : 'myAgentType state -> unit
method isTransitionValide : bool
method setAction : ('myAgentType -> unit) -> unit
method setBeginAction : ('myAgentType -> unit) -> unit
method setEndAction : ('myAgentType -> unit) -> unit
method setTransitions :
('myAgentType state option * ('myAgentType, masEvent) transition
option) list -> unit
end
class agent :
object
val mutable birthtime : int
val mutable currentState : agent state option
val mutable gid : int
val mutable id : int
val mutable emailStack : masEvent option list
val mutable pv : int
val mutable state_time : int
method cycle : agent -> unit
method getCurrentState : agent state
method setStartState : agent state -> unit
method setState : agent state -> agent state -> agent state -> agent ->
unit
end
Now, all the logic works fine.
My big problem, is I'm unable to make it working with a class which inherit
from agent. I'm forced to copy all the code for each type of agent I want.
For instance, if i define :
class inheritedDwarf = object (self)
inherit agent
val mutable currentState = ( None : inheritedDwarf state option)
end;;
it works.
But if I define :
class inheritedDwarf = object (self)
inherit agent
val mutable currentState = ( None : inheritedDwarf state option)
val specialValue=0
method getSpecialValue = specialValue
end;;
From the toplevel I get :
Error: The abbreviation inheritedDwarf expands to type
< cycle : dwarf -> unit; getCurrentState : dwarf state;
getMail : masEvent -> unit; getPileEmail : masEvent option list;
getPv : int; getSpecialValue : int;
receiveEMail : masEvent option -> unit; removeOldEmail : unit;
setPileEmail : masEvent option list -> unit; setPv : int -> unit;
setStartState : dwarf state -> unit;
setState : dwarf state -> dwarf state -> dwarf state -> dwarf ->
unit >
but is used with type
< cycle : dwarf -> unit; getCurrentState : dwarf state;
getMail : masEvent -> unit; getPileEmail : masEvent option list;
getPv : int; receiveEMail : masEvent option -> unit;
removeOldEmail : unit; setPileEmail : masEvent option list -> unit;
setPv : int -> unit; setStartState : dwarf state -> unit;
setState : dwarf state -> dwarf state -> dwarf state -> dwarf ->
unit >
So, it doesn't want to accept currentState to become a inheritedDwarf state
option and thus, I can't use my inheritedDwarf
Is there a solution ?
Thans for your help,
Regards,
Pierre-Alexandre
--
---------------------
Isaac Project - http://www.lisaac.org/
[-- Attachment #2: Type: text/html, Size: 6796 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Trouble with subtyping
2011-04-07 16:48 [Caml-list] Trouble with subtyping Pierre-Alexandre Voye
@ 2011-04-07 21:44 ` Guillaume Yziquel
0 siblings, 0 replies; 2+ messages in thread
From: Guillaume Yziquel @ 2011-04-07 21:44 UTC (permalink / raw)
To: Pierre-Alexandre Voye; +Cc: caml-list
Le Thursday 07 Apr 2011 à 18:48:45 (+0200), Pierre-Alexandre Voye a écrit :
> Hi all, I'm working on a lib to modelize a Multi-Agent System where
> each agent are driven by a Hierarchical Finite State Machine (HFSM).
Hi.
> class agent :
> object
> val mutable birthtime : int
> val mutable currentState : agent state option
OK.
> So, it doesn't want to accept currentState to become a inheritedDwarf
> state option and thus, I can't use my inheritedDwarf
Not surprising.
'class agent' defines a class, i.e. a piece of code that can be
inherited.
As of now, the currentState field mentions the type 'agent', so if you
inherit from 'agent', currentState field will keep 'agent'.
If you want the field to refer to a subclass, do, in the signature:
class agent : object ('self)
val mutable birthtime : int
val mutable currentState : 'self state option
...
> Pierre-Alexandre
> --
> ---------------------
> Isaac Project - [1]http://www.lisaac.org/
--
Guillaume Yziquel
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-04-07 21:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-07 16:48 [Caml-list] Trouble with subtyping Pierre-Alexandre Voye
2011-04-07 21:44 ` Guillaume Yziquel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox