-- printing LTS graph functions

-- Function provides the list of all transition potentially enable in the given state.
--
enableInState :: State -> [TTransition]
enableInState s = foldr (++) [] (map (enable s) agents)

-- Function provides the list of all transition enable in the given state.
-- UWAGA: ponizsza wersja dotyczy tylko modeli bez czasu
-- 
enableTransitions :: State -> [TTransition]
enableTransitions = enableInState

-- Function searches the list for a state. 
--
findState :: State -> [Node] -> Int
findState _ [] = -1
findState z ((n,s,t):nodes) | z == s = n
                            | otherwise = findState z nodes        

-- Function generates the LTS graph
--

update :: ([Node], [Node]) -> TTransition -> [State] -> ([Node], [Node])
update (((n,s,ls):nodes), list) t (z:zs) 
  | ind == -1 = update ([(n,s,ls ++ [((show t),(lastn+1))])] ++ nodes ++ [((lastn+1),z,[])],list ++ [((lastn+1),z,[])]) t zs
  | otherwise = update ([(n,s,ls ++ [((show t),ind)])] ++ nodes,list) t zs
  where ind = findState z list
        (lastn,lasts,lastls) = last ((n,s,ls):nodes)

update (a,b) _ [] = (a,b)


ltsgen :: [Node] -> [TTransition] -> [Node] -> [Node]

ltsgen ((n,s,ls):nodes) (t:ts) list = ltsgen newLTS ts newList 
  where states = fire t s
        (lastn,lasts,lastls) = last ((n,s,ls):nodes)
        (newLTS,newList) = update (((n,s,ls):nodes), list) t states


ltsgen ((n1,s1,l1):(n2,s2,l2):nodes) [] list  
  = (n1,s1,l1) : ltsgen ((n2,s2,l2):nodes) (enableTransitions s2) list    

ltsgen [(n,s,t)] [] _ = [(n,s,t)]          

lts = ltsgen [(0,s0,[])] (enableTransitions s0) [(0,s0,[])]
