MicroModelicaCCompiler  4.5.3
event.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #include "event.h"
21 
22 #include <assert.h>
23 #include <sstream>
24 
25 #include <ast/expression.hpp>
26 #include <ir/helpers.hpp>
27 #include <util/model_config.hpp>
28 #include <util/util.hpp>
29 #include <util/visitors/convert_condition.hpp>
30 
31 namespace MicroModelica {
32 using namespace Util;
33 namespace IR {
34 
36  : _zero_crossing(),
37  _positive_handler(),
38  _negative_handler(),
39  _type(EVENT::Zero),
40  _current(EVENT::Zero),
41  _zc_relation(EVENT::GE),
42  _range(),
43  _positive_handler_id(0),
44  _negative_handler_id(0),
45  _id(0),
46  _offset(0),
47  _event_id()
48 {
49 }
50 
51 Event::Event(AST_Expression cond, int id, int offset, Option<Range> range, string event_id)
52  : _zero_crossing(),
53  _positive_handler(),
54  _negative_handler(),
55  _type(EVENT::Zero),
56  _current(EVENT::Zero),
57  _zc_relation(EVENT::GE),
58  _range(range),
59  _positive_handler_id(0),
60  _negative_handler_id(0),
61  _id(id),
62  _offset(offset),
63  _event_id(event_id)
64 {
65  ConvertCondition cc;
66  _zero_crossing = Equation(cc.apply(getExpression(cond)), range, EQUATION::ZeroCrossing, id, _offset);
67  _type = cc.zeroCrossing();
68  _current = _type;
69  _zc_relation = cc.zeroCrossingRelation();
70 }
71 
72 void Event::add(AST_Statement stm)
73 {
74  Statement s(stm, _range);
75  if (_current == EVENT::Positive) {
77  } else if (_current == EVENT::Negative) {
79  }
80 }
81 
82 bool Event::compare(AST_Expression zc)
83 {
84  ConvertCondition cc;
85  AST_Expression c = cc.apply(getExpression(zc));
86  EqualExp ee;
87  bool cr = ee.equalTraverse(c, _zero_crossing.equation());
88  if (cr) {
92  } else if (_current == EVENT::Negative) {
95  }
96  }
97  return cr;
98 }
99 
100 AST_Expression Event::getExpression(AST_Expression exp)
101 {
102  if (exp->expressionType() == EXPOUTPUT) {
103  return getExpression(AST_ListFirst(exp->getAsOutput()->expressionList()));
104  }
105  return exp;
106 }
107 
108 string Event::handler(EVENT::Type type) const
109 {
111  if (stms.empty()) {
112  return "";
113  };
115  stringstream buffer;
116  string block = "";
118  string arguments;
119  block += TAB;
120  if (_range) {
121  block = _range->block();
122  arguments = _range->getDimensionVarsString();
123  }
124  block += TAB;
128  for (Statement stm = stms.begin(it); !stms.end(it); stm = stms.next(it)) {
129  buffer << block << stm << endl;
130  }
131  buffer << block << fp.endDimGuards(_range);
132  buffer << block << fp.endExpression(_range);
134  return buffer.str();
135 }
136 
137 string Event::macro() const { return _zero_crossing.macro(); }
138 
139 Expression Event::exp()
140 {
141  assert(isValid());
142  return _zero_crossing.lhs();
143 }
144 
145 string Event::config() const
146 {
147  stringstream buffer;
148  int direction = (_type == EVENT::Negative) ? -1 : _type;
149  string tabs = "";
150  if (_range) {
151  buffer << _range.get();
152  tabs = _range->block();
153  }
154  buffer << tabs << "modelData->event[" << _zero_crossing.index() << "].direction = " << direction << ";" << endl;
155  buffer << tabs << "modelData->event[" << _zero_crossing.index() << "].relation = " << _zc_relation << ";" << endl;
156  if (_range) {
157  buffer << _range->end();
158  tabs = _range->block();
159  }
160  return buffer.str();
161 }
162 
163 bool Event::compareEventID(std::string event_id) { return _event_id.compare(event_id) == 0; }
164 
166 {
167  EquationTable zero_crossings;
168  EventTable::iterator ev_it;
169  for (Event ev = events.begin(ev_it); !events.end(ev_it); ev = events.next(ev_it)) {
170  zero_crossings.insert(ev.id(), ev.zeroCrossing());
171  }
172  return zero_crossings;
173 }
174 
175 } // namespace IR
176 } // namespace MicroModelica
MicroModelica::IR::FunctionPrinter
Definition: helpers.hpp:147
EXPOUTPUT
@ EXPOUTPUT
Definition: ast_types.hpp:182
MicroModelica::IR::EVENT::Positive
@ Positive
HND_POSITIVE.
Definition: event.hpp:102
ModelTable< int, Statement >
ModelTable::begin
iterator begin()
Definition: table.hpp:68
MicroModelica::IR::Event::macro
std::string macro() const
Definition: event.cpp:154
MicroModelica::IR::Event::_negative_handler_id
int _negative_handler_id
Definition: event.hpp:123
MicroModelica::IR::Equation::macro
std::string macro() const
Definition: equation.cpp:209
MicroModelica::IR::Event::_zc_relation
EVENT::Relation _zc_relation
Definition: event.hpp:120
MicroModelica::IR::FunctionPrinter::endExpression
std::string endExpression(Option< Range > range, FUNCTION_PRINTER::ReturnStatementType ret=FUNCTION_PRINTER::ReturnStatementType::Return) const
Definition: helpers.cpp:232
MicroModelica::IR::Event::compareEventID
bool compareEventID(std::string event_id)
Definition: event.cpp:180
MicroModelica::IR::Event::_negative_handler
StatementTable _negative_handler
Definition: event.hpp:117
MicroModelica::IR::Event::Event
Event()
Definition: event.cpp:52
MicroModelica::IR::Equation::equation
AST_Expression equation()
Definition: equation.hpp:81
EqualExp::equalTraverse
bool equalTraverse(AST_Expression a, AST_Expression b)
Definition: ast_util.cpp:93
helpers.hpp
Option
Definition: util_types.hpp:32
MicroModelica::IR::FunctionPrinter::endDimGuards
std::string endDimGuards(Option< Range > range) const
Definition: helpers.cpp:256
MicroModelica::IR::Event::_event_id
std::string _event_id
Definition: event.hpp:126
model_config.hpp
MicroModelica::IR::zeroCrossingTable
EquationTable zeroCrossingTable(EventTable events)
Helper function to to generate a equation table for zero crossing equations.
Definition: event.cpp:182
MicroModelica::IR::EVENT::Zero
@ Zero
HND_ZERO.
Definition: event.hpp:101
ModelTable::end
iterator end()
Definition: table.hpp:70
MicroModelica::IR::Equation::lhs
Expression lhs() const
Definition: equation.hpp:78
MicroModelica::IR::Event::_range
Option< Range > _range
Definition: event.hpp:121
MicroModelica::IR::Event::compare
bool compare(AST_Expression zc)
Definition: event.cpp:99
MicroModelica::IR::EVENT::Type
Type
Definition: event.hpp:83
MicroModelica::IR::Event::_positive_handler_id
int _positive_handler_id
Definition: event.hpp:122
MicroModelica::IR::Equation
Definition: equation.hpp:67
MicroModelica::IR::Event
Definition: event.hpp:84
MicroModelica::IR::EVENT::GE
@ GE
ZC_GE.
Definition: event.hpp:97
TAB
#define TAB
Definition: util.hpp:79
ModelTable< int, Statement >::iterator
std::map< int, Statement >::iterator iterator
Definition: table.hpp:61
MicroModelica::Util::ModelConfig::instance
static ModelConfig & instance()
Definition: model_config.hpp:87
MicroModelica::IR::Event::_positive_handler
StatementTable _positive_handler
Definition: event.hpp:116
MicroModelica::IR::Event::config
std::string config() const
Definition: event.cpp:162
MicroModelica::Util::ModelConfig::setAlgorithm
void setAlgorithm(bool algorithm)
Definition: model_config.hpp:112
AST_ListFirst
T1 AST_ListFirst(list< T1 > *l)
Definition: ast_types.hpp:271
MicroModelica::IR::Event::exp
Expression exp()
This method returns the range expression of the event.
Definition: event.cpp:156
MicroModelica::IR::Event::_type
EVENT::Type _type
Definition: event.hpp:118
MicroModelica::IR::EVENT::Negative
@ Negative
HND_NEGATIVE.
Definition: event.hpp:103
MicroModelica::IR::Equation::identifier
std::string identifier() const
Definition: equation.cpp:161
MicroModelica::IR::Event::handler
std::string handler(EVENT::Type type) const
Definition: event.cpp:125
block
int block
Definition: ast_builder.cpp:41
MicroModelica
Definition: files.cpp:45
ModelTable::empty
bool empty()
Definition: table.hpp:108
MicroModelica::IR::Equation::index
Index index() const
Definition: equation.cpp:191
MicroModelica::IR::EQUATION::ZeroCrossing
@ ZeroCrossing
Definition: equation.hpp:50
ModelTable::insert
void insert(Key k, Value v)
Definition: table.hpp:48
MicroModelica::IR::Statement
Definition: statement.hpp:54
MicroModelica::IR::FunctionPrinter::beginDimGuards
std::string beginDimGuards(std::string token, string args, Option< Range > range, std::multimap< std::string, int > used_variables) const
Definition: helpers.cpp:266
MicroModelica::IR::FunctionPrinter::beginExpression
std::string beginExpression(std::string token, Option< Range > range) const
Definition: helpers.cpp:219
MicroModelica::IR::Equation::usedVariables
std::multimap< std::string, int > usedVariables() const
Definition: equation.cpp:250
MicroModelica::IR::Event::_zero_crossing
Equation _zero_crossing
Definition: event.hpp:115
EqualExp
Definition: ast_util.hpp:253
MicroModelica::IR::Event::isValid
bool isValid() const
Definition: event.hpp:108
MicroModelica::IR::Event::add
void add(AST_Statement)
Definition: event.cpp:89
MicroModelica::IR::Event::getExpression
AST_Expression getExpression(AST_Expression zc)
Definition: event.cpp:117
util.hpp
MicroModelica::IR::Event::_current
EVENT::Type _current
Definition: event.hpp:119
ModelTable::next
Value next(iterator &it)
Definition: table.hpp:78
expression.hpp