OMCompiler/SimulationRuntime/cpp/Core/DataExchange/XmlPropertyReader.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include <Core/ModelicaDefine.h> | ||
| 29 | #include <Core/Modelica.h> | ||
| 30 | #include <Core/DataExchange/FactoryExport.h> | ||
| 31 | #include <Core/Utils/extension/logger.hpp> | ||
| 32 | #include <Core/DataExchange/XmlPropertyReader.h> | ||
| 33 | #include <expat.h> | ||
| 34 | #include <fstream> | ||
| 35 | #include <iostream> | ||
| 36 | #include <list> | ||
| 37 | #include <locale> | ||
| 38 | #include <optional> | ||
| 39 | #include <regex> | ||
| 40 | #include <sstream> | ||
| 41 | #include <type_traits> | ||
| 42 | #include <vector> | ||
| 43 | |||
| 44 | namespace { | ||
| 45 | |||
| 46 | struct XmlElement | ||
| 47 | { | ||
| 48 | std::string name; | ||
| 49 | std::map<std::string, std::string> attributes; | ||
| 50 | std::list<XmlElement> children; | ||
| 51 | |||
| 52 | const std::string* attribute(const std::string& key) const | ||
| 53 | { | ||
| 54 | std::map<std::string, std::string>::const_iterator it = attributes.find(key); | ||
| 55 |
14/18✓ Branch 0 taken 406 times.
✓ Branch 1 taken 267 times.
✓ Branch 2 taken 2574 times.
✓ Branch 3 taken 3548 times.
✓ Branch 4 taken 11620 times.
✓ Branch 5 taken 197 times.
✓ Branch 6 taken 9749 times.
✓ Branch 7 taken 1451 times.
✓ Branch 8 taken 11200 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 11200 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 4 times.
✓ Branch 14 taken 49 times.
✓ Branch 15 taken 8 times.
✓ Branch 16 taken 33600 times.
✗ Branch 17 not taken.
|
85873 | return it == attributes.end() ? nullptr : &it->second; |
| 56 | } | ||
| 57 | |||
| 58 |
1/2✓ Branch 0 taken 33600 times.
✗ Branch 1 not taken.
|
33600 | const std::string& requiredAttribute(const std::string& key) const |
| 59 | { | ||
| 60 | const std::string* value = attribute(key); | ||
| 61 | if (!value) | ||
| 62 | ✗ | throw std::runtime_error("missing attribute " + key + " of " + name); | |
| 63 | 33600 | return *value; | |
| 64 | } | ||
| 65 | |||
| 66 | 80 | const XmlElement& child(const std::string& childName) const | |
| 67 | { | ||
| 68 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | for (const XmlElement& c : children) |
| 69 | if (c.name == childName) | ||
| 70 | 80 | return c; | |
| 71 | ✗ | throw std::runtime_error("missing element " + childName + " in " + name); | |
| 72 | } | ||
| 73 | |||
| 74 | // The whole attribute has to parse as a T. | ||
| 75 | template <class T> | ||
| 76 |
6/6std::optional<bool> (anonymous namespace)::XmlElement::attributeAs<bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 267 times.
std::optional<double> (anonymous namespace)::XmlElement::attributeAs<double>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✓ Branch 0 taken 2574 times.
✓ Branch 1 taken 3548 times.
std::optional<int> (anonymous namespace)::XmlElement::attributeAs<int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✓ Branch 0 taken 11620 times.
✓ Branch 1 taken 197 times.
|
18612 | std::optional<T> attributeAs(const std::string& key) const |
| 77 | { | ||
| 78 | const std::string* str = attribute(key); | ||
| 79 | if (!str) | ||
| 80 | 4012 | return std::nullopt; | |
| 81 | 14600 | std::istringstream is(*str); | |
| 82 |
8/8std::optional<bool> (anonymous namespace)::XmlElement::attributeAs<bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✓ Branch 1 taken 406 times.
✓ Branch 4 taken 406 times.
✓ Branch 8 taken 406 times.
std::optional<double> (anonymous namespace)::XmlElement::attributeAs<double>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✓ Branch 1 taken 2574 times.
✓ Branch 4 taken 2574 times.
✓ Branch 8 taken 2574 times.
std::optional<int> (anonymous namespace)::XmlElement::attributeAs<int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✓ Branch 1 taken 11620 times.
✓ Branch 4 taken 11620 times.
|
14600 | is.imbue(std::locale::classic()); |
| 83 | T value; | ||
| 84 |
1/1✓ Branch 1 taken 11620 times.
|
11620 | is >> value; |
| 85 |
3/6std::optional<bool> (anonymous namespace)::XmlElement::attributeAs<bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✓ Branch 0 taken 406 times.
✗ Branch 1 not taken.
std::optional<double> (anonymous namespace)::XmlElement::attributeAs<double>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 2574 times.
std::optional<int> (anonymous namespace)::XmlElement::attributeAs<int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 11620 times.
|
14600 | if (is.fail()) { |
| 86 | if constexpr (!std::is_same_v<T, bool>) | ||
| 87 | ✗ | return std::nullopt; | |
| 88 |
1/1✓ Branch 1 taken 406 times.
|
406 | is.clear(); |
| 89 | is.str(*str); | ||
| 90 | is >> std::boolalpha >> value; | ||
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
|
406 | if (is.fail()) |
| 92 | ✗ | return std::nullopt; | |
| 93 | } | ||
| 94 | is >> std::ws; | ||
| 95 |
4/6std::optional<bool> (anonymous namespace)::XmlElement::attributeAs<bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 406 times.
std::optional<double> (anonymous namespace)::XmlElement::attributeAs<double>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2572 times.
std::optional<int> (anonymous namespace)::XmlElement::attributeAs<int>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const:
✗ Branch 0 not taken.
✓ Branch 1 taken 11620 times.
|
14600 | if (!is.eof()) |
| 96 | 2 | return std::nullopt; | |
| 97 | 14598 | return value; | |
| 98 | 14600 | } | |
| 99 | }; | ||
| 100 | |||
| 101 | 22484 | void XMLCALL startElement(void* userData, const XML_Char* name, const XML_Char** atts) | |
| 102 | { | ||
| 103 | std::vector<XmlElement*>& stack = *static_cast<std::vector<XmlElement*>*>(userData); | ||
| 104 | 22484 | stack.back()->children.emplace_back(); | |
| 105 | 22484 | XmlElement& e = stack.back()->children.back(); | |
| 106 | 22484 | e.name = name; | |
| 107 |
2/2✓ Branch 0 taken 148038 times.
✓ Branch 1 taken 22484 times.
|
170522 | for (int i = 0; atts[i]; i += 2) |
| 108 |
1/1✓ Branch 2 taken 148038 times.
|
296076 | e.attributes[atts[i]] = atts[i + 1]; |
| 109 | 22484 | stack.push_back(&e); | |
| 110 | 22484 | } | |
| 111 | |||
| 112 | 22484 | void XMLCALL endElement(void* userData, const XML_Char*) | |
| 113 | { | ||
| 114 | static_cast<std::vector<XmlElement*>*>(userData)->pop_back(); | ||
| 115 | 22484 | } | |
| 116 | |||
| 117 | 40 | void readXml(std::istream& in, XmlElement& document) | |
| 118 | { | ||
| 119 | 40 | std::vector<XmlElement*> stack(1, &document); | |
| 120 |
1/1✓ Branch 1 taken 40 times.
|
40 | XML_Parser parser = XML_ParserCreate(NULL); |
| 121 |
1/1✓ Branch 1 taken 40 times.
|
40 | XML_SetUserData(parser, &stack); |
| 122 |
1/1✓ Branch 1 taken 40 times.
|
40 | XML_SetElementHandler(parser, startElement, endElement); |
| 123 |
1/3✓ Branch 1 taken 40 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
40 | std::vector<char> buf(65536); |
| 124 | bool done = false; | ||
| 125 |
2/2✓ Branch 0 taken 89 times.
✓ Branch 1 taken 40 times.
|
129 | while (!done) { |
| 126 |
1/1✓ Branch 1 taken 89 times.
|
89 | in.read(buf.data(), buf.size()); |
| 127 | 89 | done = in.gcount() < (std::streamsize)buf.size(); | |
| 128 |
2/3✓ Branch 1 taken 89 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 89 times.
|
89 | if (XML_Parse(parser, buf.data(), (int)in.gcount(), done) == XML_STATUS_ERROR) { |
| 129 | ✗ | std::stringstream ss; | |
| 130 | ✗ | ss << XML_ErrorString(XML_GetErrorCode(parser)) << " at line " << XML_GetCurrentLineNumber(parser); | |
| 131 | ✗ | XML_ParserFree(parser); | |
| 132 | ✗ | throw std::runtime_error(ss.str()); | |
| 133 | ✗ | } | |
| 134 | } | ||
| 135 |
1/1✓ Branch 1 taken 40 times.
|
40 | XML_ParserFree(parser); |
| 136 | 40 | } | |
| 137 | |||
| 138 | } | ||
| 139 | |||
| 140 | ✗ | static std::string realToString(double value) | |
| 141 | { | ||
| 142 | ✗ | std::ostringstream os; | |
| 143 | os.precision(17); | ||
| 144 | os << value; | ||
| 145 | ✗ | return os.str(); | |
| 146 | ✗ | } | |
| 147 | |||
| 148 | // Offset in column-major storage of the element at the given row-major offset. | ||
| 149 | ✗ | static int rowMajorToColumnMajor(const std::vector<int>& dims, int rowMajorOffset) | |
| 150 | { | ||
| 151 | ✗ | std::vector<int> idx(dims.size()); | |
| 152 | int rem = rowMajorOffset; | ||
| 153 | ✗ | for (int k = (int)dims.size() - 1; k >= 0; k--) { idx[k] = rem % dims[k]; rem /= dims[k]; } | |
| 154 | int off = 0, stride = 1; | ||
| 155 | ✗ | for (size_t k = 0; k < dims.size(); k++) { off += idx[k] * stride; stride *= dims[k]; } | |
| 156 | ✗ | return off; | |
| 157 | } | ||
| 158 | |||
| 159 | // Build the Modelica name of a scalar element of a column-major array, the | ||
| 160 | // storage order of the arrays of this runtime (StatArrayDimN/DynArrayDimN), | ||
| 161 | // e.g. arrayElementName("a", {2,3}, 4) -> "a[1,3]". Used to expose the | ||
| 162 | // elements of a non-scalarized array variable as individual result signals. | ||
| 163 | 9 | static std::string arrayElementName(const std::string& base, const std::vector<int>& dims, int linearOffset) | |
| 164 | { | ||
| 165 | 9 | std::vector<int> idx(dims.size()); | |
| 166 | int rem = linearOffset; | ||
| 167 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | for (size_t k = 0; k < dims.size(); k++) { idx[k] = rem % dims[k]; rem /= dims[k]; } |
| 168 |
1/1✓ Branch 1 taken 9 times.
|
9 | std::stringstream ss; |
| 169 |
1/1✓ Branch 1 taken 9 times.
|
9 | ss << base << "["; |
| 170 |
4/7✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 6 taken 9 times.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 9 times.
|
18 | for (size_t k = 0; k < idx.size(); k++) { if (k) ss << ","; ss << (idx[k] + 1); } |
| 171 |
1/1✓ Branch 1 taken 9 times.
|
9 | ss << "]"; |
| 172 | 9 | return ss.str(); | |
| 173 | 9 | } | |
| 174 | |||
| 175 | 40 | XmlPropertyReader::XmlPropertyReader(IGlobalSettings *globalSettings, std::string propertyFile) | |
| 176 | : IPropertyReader() | ||
| 177 | 40 | ,_globalSettings(globalSettings) | |
| 178 |
1/1✓ Branch 1 taken 40 times.
|
80 | ,_propertyFile(globalSettings->getInputPath() + propertyFile) |
| 179 | 40 | ,_isInitialized(false) | |
| 180 | { | ||
| 181 | 40 | } | |
| 182 | 80 | XmlPropertyReader::~XmlPropertyReader() | |
| 183 | { | ||
| 184 | 80 | } | |
| 185 | |||
| 186 | 40 | void XmlPropertyReader::readInitialValues(IContinuous& system, shared_ptr<ISimVars> sim_vars) | |
| 187 | { | ||
| 188 | 40 | std::ifstream file; | |
| 189 |
1/1✓ Branch 1 taken 40 times.
|
40 | file.open (_propertyFile.c_str(), std::ifstream::in); |
| 190 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (file.good()) |
| 191 | { | ||
| 192 |
1/1✓ Branch 1 taken 40 times.
|
40 | double *realVars = sim_vars->getRealVarsVector(); |
| 193 |
1/1✓ Branch 1 taken 40 times.
|
40 | int *intVars = sim_vars->getIntVarsVector(); |
| 194 |
1/1✓ Branch 1 taken 40 times.
|
40 | bool *boolVars = sim_vars->getBoolVarsVector(); |
| 195 |
1/1✓ Branch 1 taken 40 times.
|
40 | string *stringVars = sim_vars->getStringVarsVector(); |
| 196 |
1/1✓ Branch 1 taken 40 times.
|
40 | double *derVars= sim_vars-> getDerStateVector(); |
| 197 | int refIdx = -1; | ||
| 198 | 40 | std::optional<int> refIdxOpt; | |
| 199 |
2/2✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
|
40 | std::regex filterRegex(_globalSettings->getVariableFilter()); |
| 200 |
1/1✓ Branch 1 taken 40 times.
|
40 | EmitResults emitResults = _globalSettings->getEmitResults(); |
| 201 | try | ||
| 202 | { | ||
| 203 | XmlElement document; | ||
| 204 |
1/1✓ Branch 1 taken 40 times.
|
40 | readXml(file, document); |
| 205 | |||
| 206 |
3/4✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
|
80 | const XmlElement& modelDescription = document.child("ModelDescription"); |
| 207 | |||
| 208 | ✗ | LOGGER_WRITE_BEGIN("Initialize start values:", LC_INIT, LL_DEBUG); | |
| 209 |
4/4✓ Branch 1 taken 40 times.
✓ Branch 4 taken 40 times.
✓ Branch 6 taken 11200 times.
✓ Branch 7 taken 40 times.
|
11280 | for (const XmlElement& vars : modelDescription.child("ModelVariables").children) |
| 210 | { | ||
| 211 |
3/4✓ Branch 1 taken 4 times.
✓ Branch 2 taken 11196 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
11204 | if (vars.name == "ScalarVariable" || vars.name == "ArrayVariable") |
| 212 | { | ||
| 213 |
4/6✓ Branch 1 taken 11200 times.
✓ Branch 4 taken 11200 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 11200 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 11200 times.
|
11200 | refIdxOpt = vars.attributeAs<int>("valueReference"); |
| 214 | |||
| 215 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11200 times.
|
11200 | if (!refIdxOpt) |
| 216 | ✗ | continue; | |
| 217 | |||
| 218 |
2/2✓ Branch 1 taken 11200 times.
✓ Branch 4 taken 11200 times.
|
11200 | string name = vars.requiredAttribute("name"); |
| 219 |
3/5✓ Branch 1 taken 11200 times.
✓ Branch 3 taken 9749 times.
✓ Branch 4 taken 1451 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
22400 | const string* descriptonOpt = vars.attribute("description"); |
| 220 | string descripton; | ||
| 221 |
2/2✓ Branch 0 taken 9749 times.
✓ Branch 1 taken 1451 times.
|
11200 | if (descriptonOpt) |
| 222 | descripton = *descriptonOpt; | ||
| 223 | |||
| 224 | refIdx = *refIdxOpt; | ||
| 225 |
2/4✓ Branch 1 taken 11200 times.
✓ Branch 4 taken 11200 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
11200 | std::string aliasInfo = vars.requiredAttribute("alias"); |
| 226 |
2/4✓ Branch 1 taken 11200 times.
✓ Branch 4 taken 11200 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
11200 | std::string variabilityInfo = vars.requiredAttribute("variability"); |
| 227 | 11200 | bool isParameter = (variabilityInfo.compare("parameter") == 0); | |
| 228 | //If a start value is given for the alias and the referred variable, skip the alias declaration | ||
| 229 | 11200 | bool isAlias = aliasInfo.compare("alias") == 0; | |
| 230 | 11200 | bool isNegatedAlias = aliasInfo.compare("negatedAlias") == 0; | |
| 231 | |||
| 232 | // For a non-scalarized array (kept un-expanded with simCodeScalarize=false), | ||
| 233 | // collect the per-dimension sizes so each scalar element a[i,j,...] can be | ||
| 234 | // registered as its own result signal at the contiguous reference refIdx+offset. | ||
| 235 | bool isArray = (vars.name == "ArrayVariable"); | ||
| 236 | std::vector<int> arrayDims; | ||
| 237 | int arraySize = 1; | ||
| 238 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11196 times.
|
11200 | if (isArray) |
| 239 | { | ||
| 240 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | for (const XmlElement& dimNode : vars.children) |
| 241 | { | ||
| 242 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
|
8 | if (dimNode.name == "Dimension") |
| 243 | { | ||
| 244 |
3/4✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
|
8 | std::optional<int> d = dimNode.attributeAs<int>("start"); |
| 245 |
2/3✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (d) { arrayDims.push_back(*d); arraySize *= *d; } |
| 246 | } | ||
| 247 | } | ||
| 248 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
4 | if (arrayDims.empty()) { arrayDims.push_back(1); } |
| 249 | } | ||
| 250 | |||
| 251 | bool emitResult = false; | ||
| 252 |
1/2✓ Branch 0 taken 11200 times.
✗ Branch 1 not taken.
|
11200 | if (emitResults != EMIT_NONE) { |
| 253 | emitResult = std::regex_match(name, filterRegex); | ||
| 254 |
1/2✓ Branch 0 taken 11200 times.
✗ Branch 1 not taken.
|
11200 | if (emitResults != EMIT_ALL) { |
| 255 |
1/1✓ Branch 1 taken 11200 times.
|
11200 | const string* isProtectedOpt = vars.attribute("isProtected"); |
| 256 |
3/4✓ Branch 0 taken 11200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8002 times.
✓ Branch 3 taken 3198 times.
|
22400 | bool isProtected = isProtectedOpt && *isProtectedOpt == "true"; |
| 257 |
1/1✓ Branch 1 taken 11200 times.
|
11200 | const string* hideResultOpt = vars.attribute("hideResult"); |
| 258 |
3/4✓ Branch 0 taken 11200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10688 times.
✓ Branch 3 taken 512 times.
|
22400 | bool hideResultIsTrue = hideResultOpt && *hideResultOpt == "true"; |
| 259 |
3/4✓ Branch 0 taken 11200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11196 times.
✓ Branch 3 taken 4 times.
|
22400 | bool hideResultIsFalse = hideResultOpt && *hideResultOpt == "false"; |
| 260 | 11200 | emitResult &= emitResults == EMIT_HIDDEN || !hideResultIsTrue; | |
| 261 |
3/4✓ Branch 0 taken 631 times.
✓ Branch 1 taken 10569 times.
✓ Branch 2 taken 631 times.
✗ Branch 3 not taken.
|
11200 | emitResult &= emitResults == EMIT_PROTECTED || (!isProtected || (emitResults != EMIT_HIDDEN && hideResultIsFalse)); |
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 |
2/2✓ Branch 0 taken 11204 times.
✓ Branch 1 taken 11200 times.
|
22404 | for (const XmlElement& var : vars.children) |
| 266 | { | ||
| 267 |
2/2✓ Branch 1 taken 9348 times.
✓ Branch 2 taken 1856 times.
|
11204 | if ((var.name == "Real") /* Todo: this is needed for reduce dae method but breaks tests*/ /*&& (name.substr(0, 3) != "der")*/) |
| 268 | { | ||
| 269 | //If a start value is given for the alias and the referred variable, skip the alias declaration | ||
| 270 |
2/2✓ Branch 0 taken 6122 times.
✓ Branch 1 taken 3226 times.
|
9348 | if (!(isAlias || isNegatedAlias)) |
| 271 | { | ||
| 272 |
4/4✓ Branch 1 taken 6122 times.
✓ Branch 4 taken 6122 times.
✓ Branch 6 taken 2572 times.
✓ Branch 7 taken 3550 times.
|
12244 | std::optional<double> v = var.attributeAs<double>("start"); |
| 273 |
2/2✓ Branch 0 taken 2572 times.
✓ Branch 1 taken 3550 times.
|
6122 | if (v) { |
| 274 |
1/2✓ Branch 0 taken 2572 times.
✗ Branch 1 not taken.
|
2572 | double value = *v; |
| 275 | ✗ | LOGGER_WRITE("XMLPropertyReader: Setting real variable for " + name + " with reference " + to_string(refIdx) + " to " + realToString(value), LC_INIT, LL_DEBUG); | |
| 276 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 5144 times.
✓ Branch 2 taken 2572 times.
✓ Branch 3 taken 2572 times.
|
5144 | for (int off = 0; off < (isArray ? arraySize : 1); off++) |
| 277 |
1/1✓ Branch 1 taken 2572 times.
|
2572 | system.setRealStartValue(realVars[refIdx + off], value); |
| 278 | } | ||
| 279 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3546 times.
|
3550 | else if (isArray) |
| 280 | { | ||
| 281 | // an array start value with one entry per element, in row-major order | ||
| 282 |
1/1✓ Branch 1 taken 4 times.
|
4 | const string* startStr = var.attribute("start"); |
| 283 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (startStr) { |
| 284 | std::vector<double> values; | ||
| 285 | ✗ | std::istringstream is(*startStr); | |
| 286 | double d; | ||
| 287 | ✗ | while (is >> d) values.push_back(d); | |
| 288 | ✗ | if ((int)values.size() == arraySize) { | |
| 289 | ✗ | for (int pos = 0; pos < arraySize; pos++) | |
| 290 | ✗ | system.setRealStartValue(realVars[refIdx + rowMajorToColumnMajor(arrayDims, pos)], values[pos]); | |
| 291 | } | ||
| 292 | ✗ | } | |
| 293 | } | ||
| 294 | } | ||
| 295 |
2/2✓ Branch 0 taken 1949 times.
✓ Branch 1 taken 7399 times.
|
9348 | if (emitResult) |
| 296 | { | ||
| 297 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1946 times.
|
1949 | if (isArray) |
| 298 | { | ||
| 299 | // expose each scalar element a[i,j,...] as its own result signal | ||
| 300 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | for (int off = 0; off < arraySize; off++) |
| 301 | { | ||
| 302 |
1/1✓ Branch 1 taken 9 times.
|
9 | const double* p = &sim_vars->getRealVar(refIdx + off); |
| 303 |
1/1✓ Branch 1 taken 9 times.
|
9 | std::string elname = arrayElementName(name, arrayDims, off); |
| 304 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | if (isParameter) |
| 305 |
1/1✓ Branch 1 taken 3 times.
|
3 | _realVars.addParameter(elname, descripton, p); |
| 306 | else | ||
| 307 |
1/1✓ Branch 1 taken 6 times.
|
6 | _realVars.addOutputVar(elname, descripton, p, isNegatedAlias); |
| 308 | } | ||
| 309 | } | ||
| 310 | else | ||
| 311 | { | ||
| 312 |
1/1✓ Branch 1 taken 1946 times.
|
1946 | const double* realVarPtr = &sim_vars->getRealVar(refIdx); |
| 313 |
2/2✓ Branch 0 taken 540 times.
✓ Branch 1 taken 1406 times.
|
1946 | if (isParameter) |
| 314 |
1/1✓ Branch 1 taken 540 times.
|
540 | _realVars.addParameter(name, descripton, realVarPtr); |
| 315 | else | ||
| 316 |
1/1✓ Branch 1 taken 1406 times.
|
1406 | _realVars.addOutputVar(name, descripton, realVarPtr, isNegatedAlias); |
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 |
2/2✓ Branch 0 taken 867 times.
✓ Branch 1 taken 989 times.
|
1856 | else if (var.name == "Integer") |
| 321 | { | ||
| 322 | //If a start value is given for the alias and the referred variable, skip the alias declaration | ||
| 323 |
2/2✓ Branch 0 taken 613 times.
✓ Branch 1 taken 254 times.
|
867 | if (!(isAlias || isNegatedAlias)) |
| 324 | { | ||
| 325 |
4/4✓ Branch 1 taken 613 times.
✓ Branch 4 taken 613 times.
✓ Branch 6 taken 416 times.
✓ Branch 7 taken 197 times.
|
1226 | std::optional<int> v = var.attributeAs<int>("start"); |
| 326 |
2/2✓ Branch 0 taken 416 times.
✓ Branch 1 taken 197 times.
|
613 | if (v) { |
| 327 |
1/2✓ Branch 0 taken 416 times.
✗ Branch 1 not taken.
|
416 | int value = *v; |
| 328 | ✗ | LOGGER_WRITE("XMLPropertyReader: Setting int variable for " + name + " with reference " + to_string(refIdx) + " to " + to_string(value), LC_INIT, LL_DEBUG); | |
| 329 |
1/1✓ Branch 1 taken 416 times.
|
416 | system.setIntStartValue(intVars[refIdx], value); |
| 330 | } | ||
| 331 | } | ||
| 332 |
1/1✓ Branch 1 taken 867 times.
|
867 | const int& intVar = sim_vars->getIntVar(refIdx); |
| 333 | const int* intVarPtr = &intVar; | ||
| 334 |
2/2✓ Branch 0 taken 448 times.
✓ Branch 1 taken 419 times.
|
867 | if (emitResult) |
| 335 | { | ||
| 336 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 252 times.
|
448 | if (isParameter) |
| 337 |
1/1✓ Branch 1 taken 196 times.
|
196 | _intVars.addParameter(name, descripton, intVarPtr); |
| 338 | else | ||
| 339 |
1/1✓ Branch 1 taken 252 times.
|
252 | _intVars.addOutputVar(name, descripton, intVarPtr, isNegatedAlias); |
| 340 | } | ||
| 341 | } | ||
| 342 |
2/2✓ Branch 0 taken 928 times.
✓ Branch 1 taken 61 times.
|
989 | else if (var.name == "Boolean") |
| 343 | { | ||
| 344 | //If a start value is given for the alias and the referred variable, skip the alias declaration | ||
| 345 |
2/2✓ Branch 0 taken 673 times.
✓ Branch 1 taken 255 times.
|
928 | if (!(isAlias || isNegatedAlias)) |
| 346 | { | ||
| 347 |
4/4✓ Branch 1 taken 673 times.
✓ Branch 4 taken 673 times.
✓ Branch 6 taken 406 times.
✓ Branch 7 taken 267 times.
|
1346 | std::optional<bool> v = var.attributeAs<bool>("start"); |
| 348 |
2/2✓ Branch 0 taken 406 times.
✓ Branch 1 taken 267 times.
|
673 | if (v) { |
| 349 |
1/2✓ Branch 0 taken 406 times.
✗ Branch 1 not taken.
|
406 | bool value = *v; |
| 350 | ✗ | LOGGER_WRITE("XMLPropertyReader: Setting bool variable for " + name + " with reference " + to_string(refIdx) + " to " + to_string(value), LC_INIT, LL_DEBUG); | |
| 351 |
1/1✓ Branch 1 taken 406 times.
|
406 | system.setBoolStartValue(boolVars[refIdx], value); |
| 352 | } | ||
| 353 | } | ||
| 354 |
1/1✓ Branch 1 taken 928 times.
|
928 | const bool& boolVar = sim_vars->getBoolVar(refIdx); |
| 355 | const bool* boolVarPtr = &boolVar; | ||
| 356 |
2/2✓ Branch 0 taken 271 times.
✓ Branch 1 taken 657 times.
|
928 | if (emitResult) |
| 357 | { | ||
| 358 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 208 times.
|
271 | if (isParameter) |
| 359 |
1/1✓ Branch 1 taken 63 times.
|
63 | _boolVars.addParameter(name, descripton, boolVarPtr); |
| 360 | else | ||
| 361 |
1/1✓ Branch 1 taken 208 times.
|
208 | _boolVars.addOutputVar(name, descripton, boolVarPtr, isNegatedAlias); |
| 362 | } | ||
| 363 | } | ||
| 364 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 4 times.
|
61 | else if (var.name == "String") |
| 365 | { | ||
| 366 | //If a start value is given for the alias and the referred variable, skip the alias declaration | ||
| 367 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | if (!(isAlias || isNegatedAlias)) |
| 368 | { | ||
| 369 |
1/3✓ Branch 1 taken 57 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
57 | const string* v = var.attribute("start"); |
| 370 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 8 times.
|
57 | if (v) { |
| 371 | string value = *v; | ||
| 372 | ✗ | LOGGER_WRITE("XMLPropertyReader: Setting string variable for " + name + " with reference " + to_string(refIdx) + " to " + value, LC_INIT, LL_DEBUG); | |
| 373 |
4/6✓ Branch 1 taken 49 times.
✓ Branch 4 taken 49 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 47 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
147 | system.setStringStartValue(stringVars[refIdx], value); |
| 374 | } | ||
| 375 | } | ||
| 376 | } | ||
| 377 | } | ||
| 378 | } | ||
| 379 | } | ||
| 380 | |||
| 381 |
1/1✓ Branch 1 taken 40 times.
|
40 | size_t derSize = sim_vars->getDimStateVars(); |
| 382 |
1/1✓ Branch 1 taken 40 times.
|
40 | string name = "der"; |
| 383 |
1/3✓ Branch 1 taken 40 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
40 | string descripton = "der"; |
| 384 |
2/2✓ Branch 0 taken 167 times.
✓ Branch 1 taken 40 times.
|
207 | for (size_t i = 0; i < derSize; i++) |
| 385 | { | ||
| 386 |
1/1✓ Branch 1 taken 167 times.
|
167 | _derVars.addOutputVar(name, descripton, derVars + i, false); |
| 387 | } | ||
| 388 | |||
| 389 | ✗ | LOGGER_WRITE_END(LC_INIT, LL_DEBUG); | |
| 390 | 40 | } | |
| 391 | ✗ | catch(exception &ex) | |
| 392 | { | ||
| 393 | ✗ | std::stringstream sstream; | |
| 394 | ✗ | sstream << "Could not read start values. Current variable reference is " << refIdx; | |
| 395 | ✗ | throw ModelicaSimulationError(UTILITY, sstream.str()); | |
| 396 | ✗ | } | |
| 397 | 40 | _isInitialized = true; | |
| 398 |
1/1✓ Branch 1 taken 40 times.
|
40 | file.close(); |
| 399 | |||
| 400 | 40 | } | |
| 401 | 40 | } | |
| 402 | |||
| 403 | 340459 | const output_int_vars_t& XmlPropertyReader::getIntOutVars() | |
| 404 | { | ||
| 405 |
1/2✓ Branch 0 taken 340459 times.
✗ Branch 1 not taken.
|
340459 | if (_isInitialized) |
| 406 | 340459 | return _intVars; | |
| 407 | else | ||
| 408 | ✗ | throw ModelicaSimulationError(UTILITY, "init xml file has not been read"); | |
| 409 | } | ||
| 410 | |||
| 411 | 340459 | const output_real_vars_t& XmlPropertyReader::getRealOutVars() | |
| 412 | { | ||
| 413 |
1/2✓ Branch 0 taken 340459 times.
✗ Branch 1 not taken.
|
340459 | if (_isInitialized) |
| 414 | 340459 | return _realVars; | |
| 415 | else | ||
| 416 | ✗ | throw ModelicaSimulationError(UTILITY, "init xml file has not been read"); | |
| 417 | } | ||
| 418 | |||
| 419 | 340459 | const output_bool_vars_t& XmlPropertyReader::getBoolOutVars() | |
| 420 | { | ||
| 421 |
1/2✓ Branch 0 taken 340459 times.
✗ Branch 1 not taken.
|
340459 | if (_isInitialized) |
| 422 | 340459 | return _boolVars; | |
| 423 | else | ||
| 424 | ✗ | throw ModelicaSimulationError(UTILITY, "init xml file has not been read"); | |
| 425 | } | ||
| 426 | |||
| 427 | 340459 | const output_der_vars_t& XmlPropertyReader::getDerOutVars() | |
| 428 | { | ||
| 429 |
1/2✓ Branch 0 taken 340459 times.
✗ Branch 1 not taken.
|
340459 | if (_isInitialized) |
| 430 | 340459 | return _derVars; | |
| 431 | else | ||
| 432 | ✗ | throw ModelicaSimulationError(UTILITY, "Derivatives xml file has not been read"); | |
| 433 | } | ||
| 434 | |||
| 435 | 340459 | const output_res_vars_t& XmlPropertyReader::getResOutVars() | |
| 436 | { | ||
| 437 |
1/2✓ Branch 0 taken 340459 times.
✗ Branch 1 not taken.
|
340459 | if (_isInitialized) |
| 438 | 340459 | return _resVars; | |
| 439 | else | ||
| 440 | ✗ | throw ModelicaSimulationError(UTILITY, "Residues xml file has not been read"); | |
| 441 | } | ||
| 442 | |||
| 443 | ✗ | std::string XmlPropertyReader::getPropertyFile() | |
| 444 | { | ||
| 445 | ✗ | return _propertyFile; | |
| 446 | } | ||
| 447 | |||
| 448 | ✗ | void XmlPropertyReader::setPropertyFile(std::string file) | |
| 449 | { | ||
| 450 | ✗ | _propertyFile = file; | |
| 451 | ✗ | } | |
| 452 |