{-# LANGUAGE TypeSynonymInstances, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-}

{-
--------------------------------------------------------------------------------
--
-- Copyright (C) 2008 Martin Sulzmann, Edmund Lam. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-}

module MultiSetRewrite.StoreRepresentation where


-- An actual representation of the store

import IO
import Data.IORef
import Control.Concurrent
import Control.Concurrent.STM

import System.IO.Unsafe

import Data.List

import qualified MultiSetRewrite.ConcurrentList as L
import qualified MultiSetRewrite.ConcurrentBag as B

import MultiSetRewrite.Base
import MultiSetRewrite.RuleSyntax
import MultiSetRewrite.RuleCompiler


----------------------------------------
-- primitives for hashing messages

type HashIdx = Int

data HashOp msg = 
   HashOp { numberOfTables :: HashIdx
          , hashMsg :: msg -> HashIdx }

-- store representation

data Store msg = Store { msgTable :: [B.Bag (InternalMsg msg)],
                       hashOp :: HashOp msg
                     }

type Location msg = IORef (L.List (InternalMsg msg))
-- we need the location (pointer ref) of the message in internal form
-- for quick access (eg logical delete)


-- internal details of the store

distribution = 7 -- we assume that store is 
                 -- a bag of linked lists
                 
-- NOTE: we could assume a different distribution for
-- the individual messages

-- The run-time distribution is currently monolithic
-- We simply take the threadId 'mod' distribution


instance (EMatch msg, Eq msg, Show msg) => RuleCompiler (Store msg) 
                                       msg (Location msg)
                                       HashIdx (B.Iterator (InternalMsg msg)) where
--atomicVerifyAndDeleteCnt :: Store msg -> Code_RHS a 
--                         -> [(Check (Location msg),IO (Maybe (Code_RHS a)))] -> IO (Maybe (Code_RHS a))
  -- we don't need the store here, but otherwise the method's type is ambiguous
  atomicVerifyAndDeleteCnt _ body checks =
   let 
       -- reverify that all verify fields are True
       reVerify [] = return True
       reVerify (n:ns) = do
            let n' = case n of
                       Verify n -> n
                       VerifyAndDelete n -> n
            b <- readTVar n'
            if b then reVerify ns
             else return False

       -- set verify fields to False
       -- only those which actually need to be deleted (the simp heads)
       setFalse [] = return ()
       setFalse ((Verify _):ns) = setFalse ns
       setFalse ((VerifyAndDelete n):ns) = do
            writeTVar n False
            setFalse ns

       -- accumulates all deletes (only simp heads)
       accDeletes [] = return ()
       accDeletes ((Verify _,_):ns) = accDeletes ns
       accDeletes ((VerifyAndDelete msg,_):ns) = do
           node <- readIORef msg
           writeIORef msg (L.DelNode {L.verify = L.verify node,
                                         L.next = L.next node})
           accDeletes ns
                            
       
       -- in case of failure, pick the continuation of the node
       -- which failed (the earliest from left to right)
       checkNode (msg,cnt) = do
          node <- readIORef msg
          b <- atomically $ readTVar (L.verify node)
          if b then return Nothing
           else return (Just cnt)         
                 
       pickCont [] = error "pickCont: impossible, there must be a failed node"
       pickCont (c:rest) = do
          let (msg,cnt) = case c of
                            (Verify msg, cnt) -> (msg, cnt)
                            (VerifyAndDelete msg, cnt) -> (msg, cnt)
          res <- checkNode (msg,cnt)
          case res of
            Nothing -> pickCont rest
            Just cnt -> cnt


   in do ns <- -- extract the to be verified fields
               mapM (\ (m,_) ->
                     case m of
                       Verify msg -> do node <- readIORef msg
                                        return (Verify (L.verify node))
                       VerifyAndDelete msg -> do node <- readIORef msg
                                                 return (VerifyAndDelete (L.verify node)) )
                    checks

               -- perform atomic re-verification
               --   check that nodes are still there, and if successful
               --     set their flags to False
               --     we continue by actually deleting the nodes followed by
               --     executing the body
               --   otherwise, we abort and pick the continuation of the first
               --     node which couldn't be reverified   
         next <- atomically $
                   (do r <- reVerify ns
                       if r then do   
                          setFalse ns 
                          return (return (Just (do {accDeletes checks; body})))
                        else retry
                    )
                     `orElse`
                    return (pickCont checks)
         next



--getIndex :: Store msg -> msg  -> IO HashIdx
  getIndex act m = return ((hashMsg (hashOp act)) m)

--initSearch :: Store msg -> HashIdx -> IO (B.Iterator (InternalMsg msg))
  initSearch  act idx =
     do let bag = (msgTable act) !! (idx -1) 
        t_id <- myThreadId
        let no =  threadIdToInt t_id
        let bagEntryIdx = no `mod` distribution
        it <- B.newIterator bag bagEntryIdx
        return it

-- we use the iterator to scan through the queue 
--nextMsg :: Store msg -> (B.Iterator (InternalMsg msg)) -> IO (Maybe (Location msg))
  nextMsg act curIterator =
    do res <- B.iterateBag curIterator
       return res

--extractMsg :: Location msg -> IO (Maybe (InternalMsg msg))
  extractMsg ptr =
    do node <- readIORef ptr
       case node of
          L.Node {} -> return (Just (L.val node))
          _         -> return Nothing

  printMsg _ ptr = do
    do m <- readIORef ptr
       return (show (L.val m))


  printReachMsg _ it = error "TODO"


-- HACK!!!!!
-- FIX
-- length "ThreadId " = 9
threadIdToInt x = read (drop 9 (show x)) :: Int

newStore :: (EMatch msg, Eq msg, Show msg) => 
           HashOp msg -> IO (Store msg)
newStore hash  = 
      do let n = numberOfTables hash
         mt <- mapM (\ _ -> B.newBag (distribution-1)) [1..n]
                -- distribution starts with 0 !!!
         return (Store {msgTable = mt,                       
                       hashOp = hash})


compileRulePattern :: (EMatch msg, Eq msg, Show msg) => 
                      [([MatchTask msg], Code_RHS ())] -> [CompClause (Store msg) (Location msg) ()]
compileRulePattern prog =
   let build [] = []  
       build ((tasks,body):rest) = 
               (compileCnt body tasks) 
               ++ (build rest)
   in build prog

-- adds the message in external format and
-- returns (added) message in internal format
-- this is the format we need to perform the search      
addMsg :: (EMatch msg, Eq msg, Show msg) => 
               (Store msg) -> msg -> IO (Location msg)
addMsg (Store {msgTable = mt,
              hashOp = hash}) 
       msg =
  do new_tag <- newTag
     let new_msg = InternalMsg {message = msg, 
                                msg_tag = new_tag}
     let hashIdx = (hashMsg hash) msg
     let bag = mt !! (hashIdx - 1)
     t_id <- myThreadId
     let no =  threadIdToInt t_id
     let bagEntryIdx = no `mod` distribution
     loc <- B.addToBag bag bagEntryIdx new_msg 
     return loc


   
executeRules :: (EMatch msg, Eq msg, Show msg) => 
               Store msg -> Location msg -> [CompClause (Store msg) (Location msg) ()] -> IO (Maybe (Code_RHS ()))
executeRules store active_msg prog =
   select store active_msg prog