1/* Copyright (c) 2012 Massachusetts Institute of Technology
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22#ifndef __DSENT_MODEL_MODEL_H__
23#define __DSENT_MODEL_MODEL_H__
24
25#include <vector>
26
27#include "util/CommonType.h"
28
29namespace DSENT
30{
31    using std::vector;
32
33    class TechModel;
34    class Result;
35
36    // Base class for all the models
37    class Model
38    {
39        protected:
40            class SubModel
41            {
42                public:
43                    SubModel(Model* model_, double num_models_);
44                    ~SubModel();
45
46                public:
47                    Model* getModel();
48                    const Model* getModel() const;
49                    double getNumModels() const;
50
51                    SubModel* clone() const;
52
53                protected:
54                    SubModel(const SubModel& sub_model_);
55
56                private:
57                    // Pointer to the actual model instance
58                    Model* m_model_;
59                    // Number of models are added
60                    double m_num_models_;
61            };
62
63        public:
64            // Model Constants
65            static const char TYPE_SEPARATOR[];
66            static const char HIERARCHY_SEPARATOR[];
67            static const char SUBFIELD_SEPARATOR[];
68            static const char DETAIL_SEPARATOR[];
69
70        public:
71            Model(const String& instance_name_, const TechModel* tech_model_);
72            virtual ~Model();
73
74        public:
75            // Set the name of this instance
76            void setInstanceName(const String& instance_name_);
77            // Get the name of this instance
78            const String& getInstanceName() const;
79
80            // Set if the model is the top model
81            void setIsTopModel(bool is_top_model_);
82            bool getIsTopModel() const;
83
84            // Add a parameter name (with and without default)
85            void addParameterName(const String& parameter_name_);
86            void addParameterName(const String& parameter_name_, const String& parameter_default_);
87            const vector<String>* getParameterNames() const;
88
89            // Add a property name
90            void addPropertyName(const String& property_name_);
91            void addPropertyName(const String& property_name_, const String& property_default_);
92            const vector<String>* getPropertyNames() const;
93
94            // Check if all parameters needed exist in the m_parameters_
95            void checkParameters() const;
96            // Check if all properties needed exist in the m_properties_
97            void checkProperties() const;
98
99            // Get the pointer to parameters
100            const ParameterMap* getParameters() const;
101            const String getParameter(const String& parameter_name_) const;
102            void setParameter(const String& parameter_name_, const String& parameter_value_);
103
104            // Get the pointer to properties
105            const PropertyMap* getProperties() const;
106            const String getProperty(const String& property_name_) const;
107            void setProperty(const String& property_name_, const String& property_value_);
108
109            // Get the pointer to generated properties
110            PropertyMap* getGenProperties();
111            const PropertyMap* getGenProperties() const;
112
113            // Add an instance to this model. num_sub_instances_ specifies the
114            // number of the same instance is added.
115            void addSubInstances(Model* sub_instance_, double num_sub_instances_);
116            // Get an instance by name.
117            Model* getSubInstance(const String& sub_instance_name_);
118            const Model* getSubInstance(const String& sub_instance_name_) const;
119            // Check if an instance exists
120            bool hasSubInstance(const String& sub_instance_name_) const;
121
122            // Add a new area
123            void addAreaResult(Result* area_);
124            // Get the pointer to an area. The area is specified by name.
125            Result* getAreaResult(const String& area_name_);
126            const Result* getAreaResult(const String& area_name_) const;
127            // Check if an area exists
128            bool hasAreaResult(const String& area_name_) const;
129
130            // Add a new ndd_power
131            void addNddPowerResult(Result* ndd_power_);
132            // Get the pointer to an ndd_power. The ndd_power is specified by name.
133            Result* getNddPowerResult(const String& ndd_power_name_);
134            const Result* getNddPowerResult(const String& ndd_power_name_) const;
135            // Check if a ndd_power exists
136            bool hasNddPowerResult(const String& ndd_power_name_) const;
137
138            // Add a new event
139            void addEventResult(Result* event_);
140            // Get the pointer to an event. The event is specified by name.
141            Result* getEventResult(const String& event_name_);
142            const Result* getEventResult(const String& event_name_) const;
143            // Check if an event exists
144            bool hasEventResult(const String& event_name_) const;
145
146            // Get the pointer to the TechModel.
147            const TechModel* getTechModel() const;
148
149            // Clone and return a new instance
150            virtual Model* clone() const;
151
152            // Checks to make sure all required parameters are present, makes sure that the model
153            // has not been constructed yet, and calls constructModel. This function is not meant
154            // to be overwritten by child classes; constructModel should be overwritten instead
155            void construct();
156            // Update checks whether the model needs updating, whether all properties have been specified,
157            // and calls updateModel if update is necessary. This function is not meant
158            // to be overwritten by child classes; updateModel should be overwritten instead
159            void update();
160            // Evaluate checks whether the model needs to be evaluated. Note that this function is
161            // not meant to be overwritten by child classes; evaluateModel should be overwritten
162            // instead
163            void evaluate();
164            void use(const String& event_name_);
165            void use();
166
167            // Resolve query hierarchy and process query
168            const void* parseQuery(const String& query_type_, const String& query_hier_, const String& query_sub_field_);
169            // Process the query
170            virtual const void* processQuery(const String& query_type_, const String& query_sub_field_);
171
172            // Print hierarchically
173            void printHierarchy(const String& query_type_, const String& query_sub_field_, const String& prepend_str_, int detail_level_, ostream& ost_) const;
174            void printInstHierarchy(const String& prepend_str_, int detail_level_, ostream& ost_) const;
175
176        protected:
177            // Query area
178            const Result* queryArea(const String& area_name_) const;
179            // Query non-data-dependent power
180            const Result* queryNddPower(const String& ndd_power_name_);
181            // Query event energy cost
182            const Result* queryEventEnergyCost(const String& event_name_);
183
184            // Constructs the model
185            virtual void constructModel() = 0;
186            // Updates timing related information of the model
187            virtual void updateModel();
188            // Evaluate non data dependent power of the model
189            virtual void evaluateModel();
190            virtual void useModel(const String& event_name_);
191            virtual void useModel();
192
193        private:
194            // Private copy constructor. Use clone to perform copy operation.
195            Model(const Model& model_);
196
197        private:
198            // Name of this instance
199            String m_instance_name_;
200            // Set if this model is the top model
201            bool m_is_top_model_;
202
203            // Contains the parameters of a model
204            // Parameters are needed in order to constructModel() and CANNOT be
205            // changed after constructModel() has been called
206            ParameterMap* m_parameters_;
207            // Contains the names of all model parameters
208            vector<String>* m_parameter_names_;
209            // Contains the properties of a model
210            // Properties are required in order to updateModel() and CAN be
211            // changed be changed after constructModel(). Call updateModel()
212            // after properties have been changed to use the new properties
213            PropertyMap* m_properties_;
214            // Contains the property names needed to update the model
215            vector<String>* m_property_names_;
216            // Contains generated properties of the model
217            // Generated properties are used mostly as a scratch space for
218            // variables used in the model that must be passed from one
219            // function to another
220            PropertyMap* m_generated_properties_;
221
222            // Contains the instances of this model
223            Map<SubModel*>* m_sub_instances_;
224            // Contains the area resulst of a model
225            Map<Result*>* m_area_map_;
226            // Contains the noo power results of a model
227            Map<Result*>* m_ndd_power_map_;
228            // Contains the event results of a model
229            Map<Result*>* m_event_map_;
230            // Pointer to a TechModel which contains the technology information
231            const TechModel* m_tech_model_;
232
233            // Set when a model is constructed
234            bool m_constructed_;
235            // Set when a model is updated
236            bool m_updated_;
237            // Set when a model is evaluated
238            bool m_evaluated_;
239
240    }; // class Model
241} // namespace DSENT
242
243#endif // __DSENT_MODEL_MODEL_H__
244
245