|
| 1 | + |
| 2 | +within TestKeywords; |
| 3 | + |
| 4 | +package TestKeywords |
| 5 | + // ----------------------------------------- |
| 6 | + // Basic classes and types |
| 7 | + // ----------------------------------------- |
| 8 | + record MyRecord |
| 9 | + parameter Real r = 1.0; // 'parameter', 'Real' |
| 10 | + Integer i; // 'Integer' |
| 11 | + end MyRecord; // 'record', 'end' |
| 12 | + |
| 13 | + connector MyConnector |
| 14 | + flow Real f; // 'connector', 'flow', 'Real' |
| 15 | + stream String s; // 'stream', 'String' |
| 16 | + input Real inVar; // 'input' |
| 17 | + output Real outVar; // 'output' |
| 18 | + end MyConnector; |
| 19 | + |
| 20 | + type MyType = Real; // 'type', 'Real' |
| 21 | + |
| 22 | + // ----------------------------------------- |
| 23 | + // Reserved words |
| 24 | + // ----------------------------------------- |
| 25 | + model FullModel |
| 26 | + extends TestKeywords; // 'extends' |
| 27 | + inner MyConnector mc; // 'inner' |
| 28 | + outer MyConnector mcOuter; // 'outer' |
| 29 | + constant Real c = 3.14; // 'constant', 'Real' |
| 30 | + public Real pubVar; // 'public' |
| 31 | + protected Real protVar; // 'protected' |
| 32 | + discrete Integer dInt; // 'discrete', 'Integer' |
| 33 | + Boolean flag = true; // 'Boolean', 'true' |
| 34 | + Boolean off = false; // 'false' |
| 35 | + parameter Integer p = 2; // 'parameter', 'Integer' |
| 36 | + MyRecord rec; // 'record' |
| 37 | + input Real inputVar; // 'input' |
| 38 | + output Real outputVar; // 'output' |
| 39 | + Real arr[2]; // arrays (no keyword) |
| 40 | + equation |
| 41 | + der(inputVar) = -p * inputVar + c; // 'der' |
| 42 | + when time > 1.0 then // 'when', 'then' |
| 43 | + outputVar = inputVar * 2.0; |
| 44 | + end when; // 'end' (when) |
| 45 | + |
| 46 | + // Ejemplos de funciones matemáticas comunes |
| 47 | + Real minVal = Modelica.Math.min(inputVar, outputVar); |
| 48 | + Real maxVal = Modelica.Math.max(inputVar, outputVar); |
| 49 | + Real absVal = abs(inputVar); |
| 50 | + Real sinVal = sin(inputVar); |
| 51 | + Real cosVal = cos(inputVar); |
| 52 | + Real tanVal = tan(inputVar); |
| 53 | + Real expVal = exp(inputVar); |
| 54 | + Real logVal = log(inputVar); |
| 55 | + Real sqrtVal = sqrt(inputVar); |
| 56 | + end FullModel; |
| 57 | + |
| 58 | + // ----------------------------------------- |
| 59 | + // Functions, operators, and control flow |
| 60 | + // ----------------------------------------- |
| 61 | + function myFunc |
| 62 | + input Real x; // 'input' |
| 63 | + output Real y; // 'output' |
| 64 | + algorithm |
| 65 | + if x > 0 then // 'if', 'then' |
| 66 | + y := x; |
| 67 | + elseif x < 0 then // 'elseif', 'then' |
| 68 | + y := -x; |
| 69 | + else // 'else' |
| 70 | + y := 0; |
| 71 | + end if; // 'end' (if) |
| 72 | + end myFunc; // 'end', 'function' |
| 73 | + |
| 74 | + operator function addOp "operator example" |
| 75 | + input Real a; |
| 76 | + input Real b; |
| 77 | + output Real r; |
| 78 | + algorithm |
| 79 | + r := a + b; |
| 80 | + return; // 'return' |
| 81 | + end addOp; // 'operator', 'function', 'return' |
| 82 | + |
| 83 | + impure function impureFunc |
| 84 | + input Real x; |
| 85 | + output Real y; |
| 86 | + algorithm |
| 87 | + y := x + rand(); // 'impure' |
| 88 | + end impureFunc; // 'impure', 'function' |
| 89 | + |
| 90 | + // ----------------------------------------- |
| 91 | + // Algorithm with loop / for / while / break |
| 92 | + // ----------------------------------------- |
| 93 | + model AlgoModel |
| 94 | + Real sum; |
| 95 | + algorithm |
| 96 | + sum := 0; |
| 97 | + for i in 1:10 loop // 'for', 'in', 'loop' |
| 98 | + if i == 5 then |
| 99 | + break; // 'break' |
| 100 | + end if; // 'end' (if) |
| 101 | + sum := sum + i; |
| 102 | + end for; // 'end' (for) |
| 103 | + while sum < 10 loop // 'while', 'loop' |
| 104 | + sum := sum + 1; |
| 105 | + end while; // 'end' (while) |
| 106 | + end AlgoModel; |
| 107 | + |
| 108 | + // ----------------------------------------- |
| 109 | + // Package, encapsulated, import, partial, final |
| 110 | + // ----------------------------------------- |
| 111 | + package InnerPackage encapsulated |
| 112 | + import Modelica.Math; // 'import' |
| 113 | + partial model AbstractModel // 'partial' |
| 114 | + end AbstractModel; |
| 115 | + model Concrete final // 'final' |
| 116 | + end Concrete; |
| 117 | + end InnerPackage; |
| 118 | + |
| 119 | + // ----------------------------------------- |
| 120 | + // Use of connect, constrainedby, expandable, protected, public |
| 121 | + // ----------------------------------------- |
| 122 | + model ConnExample |
| 123 | + MyConnector c1, c2; |
| 124 | + MyRecord r1; |
| 125 | + equation |
| 126 | + connect(c1, c2); // 'connect' |
| 127 | + end ConnExample; |
| 128 | + |
| 129 | + // ----------------------------------------- |
| 130 | + // Redeclare / replaceable / constrainedby |
| 131 | + // ----------------------------------------- |
| 132 | + model RedeclareExample |
| 133 | + replaceable model M = FullModel; // 'replaceable' |
| 134 | + redeclare model R = FullModel; // 'redeclare' |
| 135 | + constrainedby MyRecord cb; // 'constrainedby' |
| 136 | + end RedeclareExample; |
| 137 | + |
| 138 | + // ----------------------------------------- |
| 139 | + // External, annotation, within usage |
| 140 | + // ----------------------------------------- |
| 141 | + function externalExample |
| 142 | + input Real x; |
| 143 | + output Real y; |
| 144 | + external "C" y = "c_func"(x); // 'external' |
| 145 | + end externalExample; |
| 146 | + |
| 147 | + // ----------------------------------------- |
| 148 | + // Operator/flow/pure/each/annotation/initial/expandable/type/true/false |
| 149 | + // ----------------------------------------- |
| 150 | + model Misc |
| 151 | + pure function pureFunc |
| 152 | + input Real a; |
| 153 | + output Real b; |
| 154 | + algorithm |
| 155 | + b := a * 2; |
| 156 | + end pureFunc; // 'pure' |
| 157 | + each Integer eachVar[2]; // 'each', 'Integer' |
| 158 | + expandable model ExpModel // 'expandable' |
| 159 | + end ExpModel; |
| 160 | + initial equation // 'initial' |
| 161 | + ; // empty initial equation |
| 162 | + end Misc; |
| 163 | + |
| 164 | + // ----------------------------------------- |
| 165 | + // When / elsewhen / then / return example |
| 166 | + // ----------------------------------------- |
| 167 | + model WhenExample |
| 168 | + Real x; |
| 169 | + equation |
| 170 | + when x > 1 then // 'when', 'then' |
| 171 | + x = 2; |
| 172 | + elsewhen x < -1 then // 'elsewhen' |
| 173 | + x = -2; |
| 174 | + end when; |
| 175 | + end WhenExample; |
| 176 | + |
| 177 | + // ----------------------------------------- |
| 178 | + // Classes and additional language keywords |
| 179 | + // ----------------------------------------- |
| 180 | + class MyClass |
| 181 | + end MyClass; // 'class' |
| 182 | + |
| 183 | + encapsulated package MoreKeywords encapsulated |
| 184 | + final model FinalModel end FinalModel; // 'final' |
| 185 | + partial model PartialModel end PartialModel;// 'partial' |
| 186 | + expandable model ExpandableModel end ExpandableModel; // 'expandable' |
| 187 | + import Modelica.Constants; // 'import' |
| 188 | + end MoreKeywords; |
| 189 | + |
| 190 | + model VarTypes |
| 191 | + constant Real cReal = 3.14; // 'constant', 'Real' |
| 192 | + parameter Integer pInt = 10; // 'parameter', 'Integer' |
| 193 | + discrete Boolean dBool = true; // 'discrete', 'Boolean' |
| 194 | + enumeration Choices = {first, second}; // 'enumeration' |
| 195 | + end VarTypes; |
| 196 | + |
| 197 | + // ----------------------------------------- |
| 198 | + // Example of annotation with graphical icons |
| 199 | + // ----------------------------------------- |
| 200 | + model IconExample |
| 201 | + Real x; |
| 202 | + annotation ( |
| 203 | + Icon = |
| 204 | + graphics = { |
| 205 | + // Línea simple |
| 206 | + line( |
| 207 | + origin={0,0}, |
| 208 | + points={{-100,0},{100,0}}, |
| 209 | + color={255,0,0}, |
| 210 | + thickness=2 |
| 211 | + ), |
| 212 | + |
| 213 | + // Polígono cerrado |
| 214 | + polygon( |
| 215 | + origin={0,0}, |
| 216 | + points={{-50,50},{50,50},{0,-50}}, |
| 217 | + fillColor={0,255,0}, |
| 218 | + pattern=LinePattern.Solid |
| 219 | + ), |
| 220 | + |
| 221 | + // Elipse |
| 222 | + ellipse( |
| 223 | + origin={0,0}, |
| 224 | + extent={{-20,-10},{20,10}}, |
| 225 | + fillColor={0,0,255}, |
| 226 | + lineColor={0,0,0} |
| 227 | + ), |
| 228 | + |
| 229 | + // Texto |
| 230 | + text( |
| 231 | + origin={0,60}, |
| 232 | + string="Ejemplo de icono", |
| 233 | + fontSize=12, |
| 234 | + color={0,0,0} |
| 235 | + ), |
| 236 | + |
| 237 | + // dynamicSelect con opciones |
| 238 | + dynamicSelect( |
| 239 | + origin={0,-60}, |
| 240 | + extent={{-30,-20},{30,20}}, |
| 241 | + choices={"Opción1", "Opción2", "Opción3"}, |
| 242 | + selected=1 |
| 243 | + ) |
| 244 | + } |
| 245 | + ); |
| 246 | + end IconExample; |
| 247 | + |
| 248 | +end TestKeywords; |
0 commit comments