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/