packages feed

EEConfig-1.0: EEConfig.hs

-- ==================================
-- Module name: EEConfig
-- Copyright (C) 2008  Bartosz Wójcik
-- Created on: 26.10.2008
-- Last update: 29.10.2008
-- License: BSD

-- =========================================================
-- |
-- This module is a very simple parser for parameters recognition.
-- It recognizes given parameters and their values.
-- As input it becomes list of parameters and their values in format @[ParameterInput]@
-- and input String where parameters will be searched.
-- As output it deliveres recognized parameters and values in format @[ParameterOutput]@,
-- where only these parameters are present which have been found in the input string.
-- In output list each exisitng parameter has exactly one value - this one that has
-- been recognized in the input string.
-- Parameters in the input string have to be always given in following format:
-- (\<flagname> \<parameter>)*
--
-- Example how to use it.
--
-- @
-- let paramsList = matchParamsL params inputText
-- let paramsTree = matchParamsT paramsList
-- params :: [ParameterInput]
-- params = [("-pl1",algNum),("-pl2",algNum),
--          ("-br1","0":(map show [5..30])),("-br2","0":(map show [5..30])),
--          ("-dp1","0":(map show [3..8])),("-dp2","0":(map show [3..8])),
--          ("-verbose",["0","1","2"]),
--          ("-evaluator",["only","all"])]
-- @

module EEConfig (ParameterInput,
                  ParameterOutput,
                  ParameterTree,
                  matchParamsL,
                  matchParamsT,
                  member,
                  (!)
                  )
where

import qualified Data.Map as Map (Map,
                                  (!),
                                  member,
                                  insert,
                                  empty)

-- =========================================================
-- |
-- ParameterInput is a pair of parameter label and all its valid values.
type ParameterInput = (String,[String])

-- |
-- Parameter's label and its value formated for output.
type ParameterOutput = (String,String)

-- |
-- Dictionary is a map of keywords which are parameter labels,
-- and values that are maps of all possible values for given parameter.
-- It is possible to rewrite Dictionary to Trie if anybody whishes.
type Dictionary = Map.Map String (Map.Map String String)

-- |
-- Read parameters [ParameterOutput] formated can be transformed into
-- binary tree for search purposes.
type ParameterTree = Map.Map String String
-- =========================================================

-- =========================================================
matchParamsL :: [ParameterInput]   -- ^ List of parameters' labels and their all possible values
             -> String             -- ^ Input
             -> [ParameterOutput]  -- ^ Output - list of matched parameters and values.
matchParamsL params = matchAgainstDict (buildDict params) [] . words
-- =========================================================

-- =========================================================
matchAgainstDict :: Dictionary -> [ParameterOutput] -> [String] -> [ParameterOutput]
-- ---------------------------------------------------------
matchAgainstDict _    os []     = os
matchAgainstDict _    os (w:[]) = os
matchAgainstDict dict os (w:val:ws)
                 | w `Map.member` dict && val `Map.member` values = matchAgainstDict dict ((w,val):os) ws
                 | otherwise                                      = matchAgainstDict dict os (val:ws)
                 where values = dict Map.! w
-- =========================================================

-- =========================================================
buildDict :: [ParameterInput] -> Dictionary
-- ---------------------------------------------------------
buildDict = foldl (\map (key,args) -> Map.insert key (insertValues args) map) Map.empty
-- =========================================================

-- =========================================================
insertValues :: [String] -> Map.Map String String
-- ---------------------------------------------------------
insertValues = foldl (\map key -> Map.insert key [] map) Map.empty
-- =========================================================

-- =========================================================
-- | Transfers list of regonized parameters into a tree.
matchParamsT :: [ParameterOutput] -> ParameterTree
-- ---------------------------------------------------------
matchParamsT = foldl (\map (key,arg) -> Map.insert key arg map) Map.empty
-- =========================================================

-- =========================================================
-- | Following functions are supposed to be parameters dictionary access functions.
-- If this modul used another data structure these 2 functions would need to 
-- be rewriten to get data from this new data structure.
(!) dictionary key = dictionary Map.! key
-- =========================================================
-- | Following functions are supposed to be parameters dictionary access functions.
member key dictionary = key `Map.member` dictionary
-- =========================================================