Skip to content

Commit

Permalink
Generate next per config
Browse files Browse the repository at this point in the history
  • Loading branch information
bugarela committed Jul 1, 2022
1 parent a7f17af commit 8e942fb
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 16 deletions.
11 changes: 9 additions & 2 deletions ConfigParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

module ConfigParser where

import qualified Head as H
import Parser

import Data.Aeson
import GHC.Generics
import qualified Data.ByteString.Lazy as B

type Call = (String, [H.Value])

jsonFile :: FilePath
jsonFile = "config-sample.json"

data DistributionConfig = Config [ProcessConfig] [String] deriving (Show,Generic)

data ProcessConfig = PConfig String [String] deriving (Show,Generic)
data ProcessConfig = PConfig String [Call] deriving (Show,Generic)

instance FromJSON DistributionConfig where
parseJSON = withObject "DistribuitionConfig" $ \obj -> do
Expand All @@ -24,7 +29,9 @@ instance FromJSON ProcessConfig where
parseJSON = withObject "ProcessConfig" $ \obj -> do
i <- obj .: "process_id"
as <- obj .: "actions"
return (PConfig i as)
case mapM parseCall as of
Left e -> fail("Invalid action calls in:" ++ show as ++ ". Error: " ++ show e)
Right cs -> return (PConfig i cs)

parseConfig :: FilePath -> IO (Either String DistributionConfig)
parseConfig file = do content <- B.readFile file
Expand Down
13 changes: 4 additions & 9 deletions Elixir.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ generate (Spec m i n ds) (Config ps shared) = let defs = filter (not . (specialD
cs = findConstants ds
vs = findVariables ds
defInit = findIdentifier i ds
defNext = findIdentifier n ds
in map (\(PConfig p as) -> ((moduleName m) ++ "_" ++ p, spec m cs vs (filterDefs as defs) defInit defNext)) ps
-- defNext = findIdentifier n ds
in map (\(PConfig p as) -> ((moduleName m) ++ "_" ++ p, spec m cs vs (filterDefs as defs) defInit (ActionDefinition n [] [] (ActionOr (map (\(i, ps) -> ActionCall i ps) as))))) ps

filterDefs :: [String] -> [Definition] -> [Definition]
filterDefs is ds = let actionNames = map stripParameters is
filterDefs :: [Call] -> [Definition] -> [Definition]
filterDefs is ds = let actionNames = map fst is
in filter (\d -> name d `elem` actionNames) ds

stripParameters :: String -> String
stripParameters [] = ""
stripParameters ('(':_) = ""
stripParameters (s:ss) = s:stripParameters ss

{-- \vdash --}
-- (MOD)
spec :: Module -> [Constant] -> [Variable] -> [Definition] -> Init -> Next -> ElixirCode
Expand Down
3 changes: 1 addition & 2 deletions Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import Helpers
import Parser (parseTla)
import JSONParser (parseJson)
import ConfigParser (parseConfig)
import Control.Applicative
import Control.Applicative

filename p = "elixir/lib/generated_code/" ++ snake p ++ ".ex"

writeCode m (p, code) = let f = filename p in writeFile f code >> return f

convert name init next config (m, ds) = mapM (writeCode name) (generate (Spec m init next ds) config)
-- in writeFile f (generate (Spec m init next ds) config) >> return f

main = do (mode:name:i:n:configFile:_) <- getArgs
config <- parseConfig configFile
Expand Down
2 changes: 2 additions & 0 deletions Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ parseTla a = do f <- readFile a
let e = parse specification "Error:" f
return (left show e)

parseCall c = parse call ("Error parsing " ++ c ++ ":") c

specification = do (n, d) <- moduleHeader
ws
ds <- many definition
Expand Down
10 changes: 9 additions & 1 deletion elixir/lib/generated_code/apaew_d840_node0.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ defmodule ApaewD840 do
def main(variables) do
IO.puts (inspect variables)

main(%{})
main((
decide_action(
List.flatten([
%{ action: "InitiateProbe()", condition: initiate_probe_condition(variables), state: initiate_probe(variables) },
%{ action: "SendMsg(Lit (Num 0))", condition: send_msg_condition(variables, 0), state: send_msg(variables, 0) },
%{ action: "Deactivate(Lit (Num 0))", condition: deactivate_condition(variables, 0), state: deactivate(variables, 0) }
])
)
))
end

def decide_action(actions) do
Expand Down
10 changes: 9 additions & 1 deletion elixir/lib/generated_code/apaew_d840_node1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ defmodule ApaewD840 do
def main(variables) do
IO.puts (inspect variables)

main(%{})
main((
decide_action(
List.flatten([
%{ action: "PassToken(Lit (Num 1))", condition: pass_token_condition(variables, 1), state: pass_token(variables, 1) },
%{ action: "SendMsg(Lit (Num 1))", condition: send_msg_condition(variables, 1), state: send_msg(variables, 1) },
%{ action: "Deactivate(Lit (Num 1))", condition: deactivate_condition(variables, 1), state: deactivate(variables, 1) }
])
)
))
end

def decide_action(actions) do
Expand Down
10 changes: 9 additions & 1 deletion elixir/lib/generated_code/apaew_d840_node2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ defmodule ApaewD840 do
def main(variables) do
IO.puts (inspect variables)

main(%{})
main((
decide_action(
List.flatten([
%{ action: "PassToken(Lit (Num 2))", condition: pass_token_condition(variables, 2), state: pass_token(variables, 2) },
%{ action: "SendMsg(Lit (Num 2))", condition: send_msg_condition(variables, 2), state: send_msg(variables, 2) },
%{ action: "Deactivate(Lit (Num 2))", condition: deactivate_condition(variables, 2), state: deactivate(variables, 2) }
])
)
))
end

def decide_action(actions) do
Expand Down

0 comments on commit 8e942fb

Please sign in to comment.