/ src /
/src/INRules.hs
1 {-| Module : INRules
2 Maitainer : jmvilaca@di.uminho.pt
3
4
5 -}
6
7 module INRules
8 ( INRules
9 , empty
10
11 , updateRule
12 , rulesNames
13 , findRule
14 , fromRule
15 , addNewRule
16 , addNewEmptyRule
17 , removeRule
18
19 , mapRules
20 ) where
21
22 import INRule
23 import InfoKind
24
25 import Data.List
26
27 type INRules g n e = [INRule g n e]
28
29 -- | Empty set of rules.
30 empty :: (InfoKind e g, InfoKind n g) => g -> n -> e -> INRules g n e
31 empty g n e = []
32
33
34 updateRule :: String -> (INRule g n e -> INRule g n e)
35 -> INRules g n e -> INRules g n e
36 updateRule ruleName ruleFunc rules = map f rules
37 where f rule | ruleName == getName rule = ruleFunc rule
38 | otherwise = rule
39
40 rulesNames :: INRules g n e -> [String]
41 rulesNames = map getName
42
43 sameName :: String -> INRule g n e -> Bool
44 sameName ruleName rule = (getName rule) == ruleName
45
46 findRule :: String -> INRules g n e -> Maybe (INRule g n e)
47 findRule ruleName rules = find (sameName ruleName) rules
48
49 fromRule :: (INRule g n e -> x) -> String -> INRules g n e -> Maybe x
50 fromRule func ruleName rules =
51 case findRule ruleName rules of
52 Just rule -> Just $ func rule
53 Nothing -> Nothing
54
55 addNewRule :: INRule g n e -> INRules g n e -> INRules g n e
56 addNewRule newRule rules = rules ++ [newRule]
57
58 addNewEmptyRule :: (InfoKind e g, InfoKind n g) =>
59 String -> g -> n -> e -> INRules g n e -> INRules g n e
60 addNewEmptyRule ruleName g n e rules =
61 let newRule = setName ruleName $ initial g n e
62 in addNewRule newRule rules
63
64 removeRule :: (InfoKind e g, InfoKind n g) =>
65 String -> INRules g n e -> INRules g n e
66 removeRule ruleName rules = filter (not . sameName ruleName) rules
67
68 mapRules :: (INRule g n e -> x) -> INRules g n e -> [x]
69 mapRules = map