7c130ba6eafc925501640faaca98fdbd9ba249f5
[profile/ivi/ico-uxf-homescreen.git] / lib / misc / state-machine / CicoStateMachineCreator.h
1 /*
2  * Copyright (c) 2013 TOYOTA MOTOR CORPORATION.
3  *
4  * Contact: 
5  *
6  * Licensed under the Apache License, Version 2.0 (the License){}
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License. 
17  */
18
19
20 #ifndef CICOSTATEMACHINECREATOR_H
21 #define CICOSTATEMACHINECREATOR_H
22 #include <iostream>
23 #include <string>
24 #include <vector>
25 #include "CicoBlockParser.h"
26 #include "CicoEventInfo.h"
27
28 #ifndef CICOSTATECORE_H
29 class CicoStateCore;
30 #endif
31
32 #ifndef CICOSTATEMACHINE_H
33 class CicoStateMachine;
34 #endif
35
36 #ifndef CICOSTATE_H
37 class CicoState;
38 #endif
39
40 #ifndef CICOFINALSTATE_H
41 class CicoFinalState;
42 #endif
43
44 #ifndef CICOHISTORYSTATE_H
45 class CicoHistoryState;
46 #endif
47
48 #define DKstateMachine      "stateMachine"
49 #define DKstate             "state"
50 #define DKfinalState        "finalState"
51 #define DKhistoryState      "historyState"
52 #define DKevent             "event"
53
54 #define DKname              "name"
55 #define DKname2             "n"
56 #define DKvalue             "value"
57 #define DKvalue2            "v"
58 #define DKinitial           "initial"
59 #define DKinitial2          "i"
60 #define DKfinish            "finish"
61 #define DKfinish2           "f"
62 #define DKdefault           "default"
63 #define DKdefault2          "d"
64 #define DKguardCondition    "guardCondition"
65 #define DKguardCondition2   "g"
66 #define DKoperator          "operator"
67 #define DKoperator2         "o"
68 #define DKtransition        "transition"
69 #define DKtransition2       "t"
70 #define DKtype              "type"
71 #define DKtype2             "ht"
72 #define DKvalued            "valued"
73 #define DKvalued2           "vd"
74 #define DKjoin              "join"
75 #define DKjoin2              "j"
76
77 #define DVShallow           "Shallow"
78 #define DVDeep              "Deep"
79 #define DVand               "and"
80 #define DVor                "or"
81
82
83 /**
84  * StateMachine-Object creator
85  * @author m.kamoshida
86  * @version 0.1
87  */
88
89 /**
90  * @brief The CicoStateMachineCreator class
91  * @      state-machine produce from the definition file or string
92  */
93 class CicoStateMachineCreator
94 {
95 public:
96     CicoStateMachine* createFile(const std::string& filename);
97
98     CicoStateMachine* create(const char* sttdef);
99     const std::string& getError();
100
101 protected:
102     void        CreateStateObjects(CicoBlockParser& cbp,
103                                   CicoStateCore* parent = 0);
104     void        CreateStateMachine(CicoBlockParser& cbp);
105     void        CreateState(CicoBlockParser& cbp, CicoStateCore* parent);
106     void        CreateHistoryState(CicoBlockParser& cbp,
107                                    CicoStateCore* parent);
108     void        CreateFinalState(CicoBlockParser& cbp, CicoStateCore* parent);
109
110     void        setConnect(CicoBlockParser& cbp, CicoStateMachine* csm,
111                            CicoStateCore* parent=0);
112
113     void        setConnectStateMachine(CicoBlockParser& cbp,
114                                        CicoStateMachine* csm);
115     void        setConnectState(CicoBlockParser& cbp, CicoStateMachine* csm);
116     void        setConnectHistoryState(CicoBlockParser& cbp,
117                                        CicoStateMachine* csm);
118     void        setInitial(CicoBlockParser& cbp, const char* key,
119                            CicoStateMachine* csm);
120     void        addInitial(CicoStateCore* tgt, CicoStateCore* dat);
121
122     void        setFinish(CicoBlockParser& cbp, const char* key,
123                           CicoStateMachine* csm);
124
125     void        setDefault(CicoBlockParser& cbp, CicoHistoryState* chs,
126                            CicoStateMachine* csm);
127     void        addDefault(CicoHistoryState* chs, CicoStateCore* csc);
128
129
130     void        CreateEvent(CicoBlockParser& cbp, CicoStateMachine* csm,
131                             CicoStateCore* parent);
132
133     bool        regGuardCondition(picojson::value& v, CicoEventInfo& evi);
134     void        setGuardCondition(CicoEventInfo& evi, picojson::object& o);
135
136     bool        getJsonString(picojson::value& v, std::string& str);
137     bool        getJsonNumber(picojson::value& v, int& num);
138     bool        getJsonDouble(picojson::value& v, double& d);
139     bool        parseName(picojson::object& o, std::string& nm);
140
141     bool        parseValueI(picojson::object& o, int& v);
142     bool        parseValueIS(picojson::object& o, int& n, std::string& s);
143
144     bool        parseValuedD(picojson::object& o, double& v);
145
146     bool        parseHistoryType(picojson::object& o,
147                                  CicoHistoryState::E_HType& e);
148
149     bool        parseStateObject(picojson::value& v,
150                                  std::vector<CicoStateCore*>& vec,
151                                  CicoStateMachine* csm, std::string& err);
152
153     bool        parseOperator(picojson::object& o, std::string& op);
154     bool        parseJoin(picojson::object& o, std::string& j);
155
156 private:
157     std::string m_err;
158
159 };
160
161 /**
162  * @brief CicoStateMachineCreator::getError
163  * @      get error message
164  * @return error message
165  */
166 inline const std::string& CicoStateMachineCreator::getError()
167 {
168     return m_err;
169 }
170
171 #endif // CICOSTATEMACHINECREATOR_H