Ticket #1172: DiningTableTotal.mo

File DiningTableTotal.mo, 22.3 KB (added by Karin Gustafsson, 15 years ago)
Line 
1package Modelica "Modelica Standard Library"
2 extends Icons.Library;
3 annotation(preferedView="info", version="2.2.1", versionDate="2006-03-24", conversion(from(version="1.6", ModelicaAdditions(version="1.5"), MultiBody(version="1.0.1"), MultiBody(version="1.0"), Matrices(version="0.8"), script="Scripts/ConvertModelica_from_1.6_to_2.1.mos"), from(version="2.1 Beta1", script="Scripts/ConvertModelica_from_2.1Beta1_to_2.1.mos"), noneFromVersion="2.1", noneFromVersion="2.2"), Dymola(checkSum="539989979:1143034484"), Settings(NewStateSelection=true), Documentation(info="<HTML>
4<p>
5Package <b>Modelica</b> is a <b>standardized</b> and <b>free</b> package
6that is developed together with the Modelica language from the
7Modelica Association, see <a href=\"http://www.Modelica.org\">http://www.Modelica.org</a>.
8It is also called <b>Modelica Standard Library</b>.
9It provides model components in many domains that are based on
10standardized interface definitions. Some typical examples are shown
11in the next figure:
12</p>
13
14<p>
15<img src=\"./Images/UsersGuide/ModelicaLibraries.png\">
16</p>
17
18<p>
19For an introduction, have especially a look at:
20</p>
21<ul>
22<li> <a href=\"Modelica://Modelica.UsersGuide\">Users Guide</a>
23 discusses some aspects of the Modelica Standard Library, such as
24 interface definitions and used conventions.</li>
25<li><a href=\"Modelica://Modelica.UsersGuide.ReleaseNotes\">Release Notes</a>
26 summarizes the changes of new versions of this package.</li>
27<li> Packages <b>Examples</b> in the various subpackages, demonstrate
28 how to use the components of the corresponding sublibrary.</li>
29</ul>
30
31<p>
32Copyright &copy; 1998-2006, Modelica Association.
33</p>
34<p>
35<i>This Modelica package is <b>free</b> software; it can be redistributed and/or modified
36under the terms of the <b>Modelica license</b>, see the license conditions
37and the accompanying <b>disclaimer</b>
38<a href=\"Modelica://Modelica.UsersGuide.ModelicaLicense\">here</a>.</i>
39</p>
40<p> <b>Note:</b> This is a <i>subset</i> of the official Modelica package with minor changes made by MathCore Engineering AB.
41For a complete list of changes see the <a href=\"Modelica://Modelica.UsersGuide.ReleaseNotes\">Release Notes</a>.
42</p>
43</HTML>
44", revisions=""), Icon(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})), Diagram(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
45 package Math "Mathematical functions (e.g., sin, cos) and operations on matrices (e.g., norm, solve, eig, exp)"
46 import SI = Modelica.SIunits;
47 extends Modelica.Icons.Library2;
48 annotation(preferedView="info", Invisible=true, Icon(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10}), graphics={Text(visible=true, extent={{-59,-56},{42,-9}}, textString="f(x)", fontName="Arial")}), Documentation(info="<HTML>
49<p>
50This package contains <b>basic mathematical functions</b> (such as sin(..)),
51as well as functions operating on <b>matrices</b>.
52</p>
53
54<dl>
55<dt><b>Main Author:</b>
56<dd><a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a><br>
57 Deutsches Zentrum f&uuml;r Luft und Raumfahrt e.V. (DLR)<br>
58 Institut f&uuml;r Robotik und Mechatronik<br>
59 Postfach 1116<br>
60 D-82230 Wessling<br>
61 Germany<br>
62 email: <A HREF=\"mailto:Martin.Otter@dlr.de\">Martin.Otter@dlr.de</A><br>
63</dl>
64
65<p>
66Copyright &copy; 1998-2006, Modelica Association and DLR.
67</p>
68<p>
69<i>This Modelica package is <b>free</b> software; it can be redistributed and/or modified
70under the terms of the <b>Modelica license</b>, see the license conditions
71and the accompanying <b>disclaimer</b>
72<a href=\"Modelica://Modelica.UsersGuide.ModelicaLicense\">here</a>.</i>
73</p><br>
74</HTML>
75", revisions="<html>
76<ul>
77<li><i>October 21, 2002</i>
78 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>
79 and <a href=\"http://www.robotic.dlr.de/Christian.Schweiger/\">Christian Schweiger</a>:<br>
80 Function tempInterpol2 added.</li>
81<li><i>Oct. 24, 1999</i>
82 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
83 Icons for icon and diagram level introduced.</li>
84<li><i>June 30, 1999</i>
85 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
86 Realized.</li>
87</ul>
88
89</html>"), Diagram(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
90 end Math;
91
92 package SIunits "Type and unit definitions based on SI units according to ISO 31-1992"
93 extends Modelica.Icons.Library2;
94 annotation(preferedView="info", Invisible=true, Icon(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10}), graphics={Text(visible=true, extent={{-63,-67},{45,-13}}, textString="[kg.m2]", fontName="Arial")}), Documentation(info="<html>
95<p>This package provides predefined types, such as <i>Mass</i>,
96<i>Angle</i>, <i>Time</i>, based on the international standard
97on units, e.g.,
98</p>
99
100<pre> <b>type</b> Angle = Real(<b>final</b> quantity = \"Angle\",
101 <b>final</b> unit = \"rad\",
102 displayUnit = \"deg\");
103</pre>
104
105<p>
106as well as conversion functions from non SI-units to SI-units
107and vice versa in subpackage
108<a href=\"Modelica://Modelica.SIunits.Conversions\">Conversions</a>.
109</p>
110
111<p>
112For an introduction how units are used in the Modelica standard library
113with package SIunits, have a look at:
114<a href=\"Modelica://Modelica.SIunits.UsersGuide.HowToUseSIunits\">How to use SIunits</a>.
115</p>
116
117<p>
118Copyright &copy; 1998-2006, Modelica Association and DLR.
119</p>
120<p>
121<i>This Modelica package is <b>free</b> software; it can be redistributed and/or modified
122under the terms of the <b>Modelica license</b>, see the license conditions
123and the accompanying <b>disclaimer</b>
124<a href=\"Modelica://Modelica.UsersGuide.ModelicaLicense\">here</a>.</i>
125</p>
126
127</html>", revisions="<html>
128<ul>
129<li><i>Dec. 14, 2005</i>
130 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
131 Add users guide and removed \"min\" values for Resistance and Conductance.</li>
132<li><i>October 21, 2002</i>
133 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>
134 and <a href=\"http://www.robotic.dlr.de/Christian.Schweiger/\">Christian Schweiger</a>:<br>
135 Added new package <b>Conversions</b>. Corrected typo <i>Wavelenght</i>.</li>
136<li><i>June 6, 2000</i>
137 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
138 Introduced the following new types<br>
139 type Temperature = ThermodynamicTemperature;<br>
140 types DerDensityByEnthalpy, DerDensityByPressure,
141 DerDensityByTemperature, DerEnthalpyByPressure,
142 DerEnergyByDensity, DerEnergyByPressure<br>
143 Attribute \"final\" removed from min and max values
144 in order that these values can still be changed to narrow
145 the allowed range of values.<br>
146 Quantity=\"Stress\" removed from type \"Stress\", in order
147 that a type \"Stress\" can be connected to a type \"Pressure\".</li>
148<li><i>Oct. 27, 1999</i>
149 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
150 New types due to electrical library: Transconductance, InversePotential,
151 Damping.</li>
152<li><i>Sept. 18, 1999</i>
153 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
154 Renamed from SIunit to SIunits. Subpackages expanded, i.e., the
155 SIunits package, does no longer contain subpackages.</li>
156<li><i>Aug 12, 1999</i>
157 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
158 Type \"Pressure\" renamed to \"AbsolutePressure\" and introduced a new
159 type \"Pressure\" which does not contain a minimum of zero in order
160 to allow convenient handling of relative pressure. Redefined
161 BulkModulus as an alias to AbsolutePressure instead of Stress, since
162 needed in hydraulics.</li>
163<li><i>June 29, 1999</i>
164 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
165 Bug-fix: Double definition of \"Compressibility\" removed
166 and appropriate \"extends Heat\" clause introduced in
167 package SolidStatePhysics to incorporate ThermodynamicTemperature.</li>
168<li><i>April 8, 1998</i>
169 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>
170 and Astrid Jaschinski:<br>
171 Complete ISO 31 chapters realized.</li>
172<li><i>Nov. 15, 1997</i>
173 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>
174 and <a href=\"http://www.control.lth.se/~hubertus/\">Hubertus Tummescheit</a>:<br>
175 Some chapters realized.</li>
176</ul>
177</html>"), Diagram(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10}), graphics={Rectangle(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, extent={{169,86},{349,236}}),Polygon(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, points={{169,236},{189,256},{369,256},{349,236},{169,236}}),Polygon(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, points={{369,256},{369,106},{349,86},{349,236},{369,256}}),Text(visible=true, fillColor={160,160,160}, extent={{179,196},{339,226}}, textString="Library", fontName="Arial"),Text(visible=true, extent={{206,119},{314,173}}, textString="[kg.m2]", fontName="Arial"),Text(visible=true, fillColor={255,0,0}, extent={{163,264},{406,320}}, textString="Modelica.SIunits", fontName="Arial")}));
178 end SIunits;
179
180 package Icons "Icon definitions"
181 annotation(preferedView="info", Documentation(info="<html>
182<p>
183This package contains definitions for the graphical layout of
184components which may be used in different libraries.
185The icons can be utilized by inheriting them in the desired class
186using \"extends\".
187</p>
188<dl>
189<dt><b>Main Author:</b>
190<dd><a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a><br>
191 Deutsches Zentrum fuer Luft und Raumfahrt e.V. (DLR)<br>
192 Oberpfaffenhofen<br>
193 Postfach 1116<br>
194 D-82230 Wessling<br>
195 email: <A HREF=\"mailto:Martin.Otter@dlr.de\">Martin.Otter@dlr.de</A><br>
196</dl>
197
198<p>
199Copyright &copy; 1998-2006, Modelica Association and DLR.
200</p>
201<p>
202<i>This Modelica package is <b>free</b> software; it can be redistributed and/or modified
203under the terms of the <b>Modelica license</b>, see the license conditions
204and the accompanying <b>disclaimer</b>
205<a href=\"Modelica://Modelica.UsersGuide.ModelicaLicense\">here</a>.</i>
206</p><br>
207</HTML>
208", revisions="<html>
209<ul>
210<li><i>October 21, 2002</i>
211 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>
212 and <a href=\"http://www.robotic.dlr.de/Christian.Schweiger/\">Christian Schweiger</a>:<br>
213 Added new icons <b>Function</b>, <b>Enumerations</b> and <b>Record</b>.</li>
214<li><i>June 6, 2000</i>
215 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
216 Replaced <b>model</b> keyword by <b>package</b> if the main
217 usage is for inheriting from a package.<br>
218 New icons <b>GearIcon</b> and <b>MotorIcon</b>.</li>
219<li><i>Sept. 18, 1999</i>
220 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
221 Renaming package Icon to Icons.
222 Model Advanced removed (icon not accepted on the Modelica meeting).
223 New model Library2, which is the Library icon with enough place
224 to add library specific elements in the icon. Icon also used in diagram
225 level for models Info, TranslationalSensor, RotationalSensor.</li>
226<li><i>July 15, 1999</i>
227 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
228 Model Caution renamed to Advanced, model Sensor renamed to
229 TranslationalSensor, new model RotationalSensor.</li>
230<li><i>June 30, 1999</i>
231 by <a href=\"http://www.robotic.dlr.de/Martin.Otter/\">Martin Otter</a>:<br>
232 Realized a first version.</li>
233</ul>
234<br>
235</html>"), Icon(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10}), graphics={Rectangle(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, extent={{-100,-100},{80,50}}),Polygon(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, points={{-100,50},{-80,70},{100,70},{80,50},{-100,50}}),Polygon(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, points={{100,70},{100,-80},{80,-100},{80,50},{100,70}}),Text(visible=true, fillColor={255,0,0}, extent={{-120,70},{120,135}}, textString="%name", fontName="Arial"),Text(visible=true, fillColor={160,160,160}, extent={{-90,10},{70,40}}, textString="Library", fontName="Arial"),Rectangle(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, extent={{-100,-100},{80,50}}),Polygon(visible=true, fillColor={210,210,210}, fillPattern=FillPattern.Solid, points={{-100,50},{-80,70},{100,70},{80,50},{-100,50}}),Polygon(visible=true, fillColor={210,210,210}, fillPattern=FillPattern.Solid, points={{100,70},{100,-80},{80,-100},{80,50},{100,70}}),Text(visible=true, fillColor={160,160,160}, extent={{-90,10},{70,40}}, textString="Library", fontName="Arial"),Polygon(visible=true, fillColor={192,192,192}, fillPattern=FillPattern.Solid, points={{-64,-20},{-50,-4},{50,-4},{36,-20},{-64,-20},{-64,-20}}),Rectangle(visible=true, fillColor={192,192,192}, fillPattern=FillPattern.Solid, extent={{-64,-84},{36,-20}}),Text(visible=true, fillColor={128,128,128}, extent={{-60,-38},{32,-24}}, textString="Library", fontName="Arial"),Polygon(visible=true, fillColor={192,192,192}, fillPattern=FillPattern.Solid, points={{50,-4},{50,-70},{36,-84},{36,-20},{50,-4}})}), Diagram(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
236 partial package Library "Icon for library"
237 annotation(Icon(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10}), graphics={Rectangle(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, extent={{-100,-100},{80,50}}),Polygon(visible=true, fillColor={210,210,210}, fillPattern=FillPattern.Solid, points={{-100,50},{-80,70},{100,70},{80,50},{-100,50}}),Polygon(visible=true, fillColor={210,210,210}, fillPattern=FillPattern.Solid, points={{100,70},{100,-80},{80,-100},{80,50},{100,70}}),Text(visible=true, fillColor={0,0,255}, extent={{-85,-85},{65,35}}, textString="Library", fontName="Arial"),Text(visible=true, fillColor={255,0,0}, extent={{-120,73},{120,122}}, textString="%name", fontName="Arial")}), Diagram(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
238 end Library;
239
240 partial package Library2 "Icon for library where additional icon elements shall be added"
241 annotation(Icon(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10}), graphics={Rectangle(visible=true, fillColor={235,235,235}, fillPattern=FillPattern.Solid, extent={{-100,-100},{80,50}}),Polygon(visible=true, fillColor={210,210,210}, fillPattern=FillPattern.Solid, points={{-100,50},{-80,70},{100,70},{80,50},{-100,50}}),Polygon(visible=true, fillColor={210,210,210}, fillPattern=FillPattern.Solid, points={{100,70},{100,-80},{80,-100},{80,50},{100,70}}),Text(visible=true, fillColor={255,0,0}, extent={{-120,70},{120,125}}, textString="%name", fontName="Arial"),Text(visible=true, fillColor={160,160,160}, fillPattern=FillPattern.Solid, extent={{-90,10},{70,40}}, textString="Library", fontName="Arial")}), Diagram(coordinateSystem(extent={{-100,100},{100,-100}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
242 end Library2;
243
244 end Icons;
245
246end Modelica;
247package Philosopher
248 annotation(Diagram(coordinateSystem(extent={{-100.0,-100.0},{100.0,100.0}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
249 model DiningTable
250 parameter Integer n=5 "Number of philosophers and forks";
251 parameter Real sigma=5 "Standard deviation of delay times";
252 Philosopher phil[n](sigma=fill(sigma, n));
253 Mutex mutex(n=n);
254 Fork fork[n];
255 equation
256 for i in 1:n loop
257 connect(phil[i].mutexPort,mutex.port[i]);
258 connect(phil[i].right,fork[i].left);
259 connect(fork[i].right,phil[mod(i, n) + 1].left);
260 end for;
261 end DiningTable;
262
263 connector ForkPhilosopherConnection
264 Boolean pickedUp(start=false);
265 Boolean busy;
266 end ForkPhilosopherConnection;
267
268 model Fork
269 ForkPhilosopherConnection left "Connection to the philosopher to the left of the fork";
270 ForkPhilosopherConnection right "Connection to the philosopher to the right of the fork";
271 equation
272 right.busy=left.pickedUp;
273 left.busy=right.pickedUp;
274 end Fork;
275
276 connector MutexPortOut "Application mutex port connector for access"
277 output Boolean request "Set this to request ownership of the mutex";
278 output Boolean release "Set this to release ownership of the mutex";
279 input Boolean ok "This signals that ownership was granted";
280 end MutexPortOut;
281
282 model Philosopher "A Philosopher, connected to forks and a mutex"
283 import Philosopher.Random;
284 MutexPortOut mutexPort "Connection to the global mutex";
285 discrete Real[3] startSeed={1,2,3};
286 parameter Real mu=20.0 "mean value";
287 parameter Real sigma=5 "standard dev";
288 discrete Integer state "1==thinking, 2==hungry, 3==eating";
289 ForkPhilosopherConnection left;
290 ForkPhilosopherConnection right;
291 annotation(Diagram(coordinateSystem(extent={{-100.0,-100.0},{100.0,100.0}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
292 protected
293 constant Integer thinking=0;
294 constant Integer hungry=1;
295 constant Integer eating=2;
296 discrete Real T;
297 discrete Real timeOfNextChange;
298 discrete Real[3] randomSeed;
299 Boolean canEat;
300 Boolean timeToChangeState;
301 Boolean timeToGetHungry;
302 Boolean doneEating;
303 equation
304 timeToChangeState=timeOfNextChange <= time;
305 canEat=state == hungry and not (left.busy or right.busy);
306 timeToGetHungry=state == thinking and timeToChangeState;
307 doneEating=state == eating and timeToChangeState;
308 algorithm
309 when initial() then
310 state:=thinking;
311 left.pickedUp:=false;
312 right.pickedUp:=false;
313 (T,randomSeed):=Random.normalvariate(mu, sigma, startSeed);
314 timeOfNextChange:=abs(T);
315 elsewhen pre(timeToGetHungry) then
316 state:=hungry;
317 end when;
318 when pre(canEat) then
319 mutexPort.release:=false;
320 mutexPort.request:=true;
321 end when;
322 when pre(mutexPort.ok) then
323 if pre(canEat) then
324 left.pickedUp:=true;
325 right.pickedUp:=true;
326 (T,randomSeed):=Random.normalvariate(mu, sigma, pre(randomSeed));
327 timeOfNextChange:=time + abs(T);
328 state:=eating;
329 end if;
330 mutexPort.release:=true;
331 mutexPort.request:=false;
332 end when;
333 when pre(doneEating) then
334 state:=thinking;
335 left.pickedUp:=false;
336 right.pickedUp:=false;
337 (T,randomSeed):=Random.normalvariate(mu, sigma, pre(randomSeed));
338 timeOfNextChange:=time + abs(T);
339 end when;
340 end Philosopher;
341
342 package Random
343 annotation(Diagram(coordinateSystem(extent={{-148.5,-105.0},{148.5,105.0}}, preserveAspectRatio=true, initialScale=0.1, grid={10,10})));
344 import Modelica.Math;
345 constant Real NV_MAGICCONST=4*exp(-0.5)/sqrt(2.0);
346 function random
347 input Real[3] si "input random seed";
348 output Real x "uniform random variate between 0 and 1";
349 output Real[3] so "output random seed";
350 annotation(Diagram(coordinateSystem(extent={{-100,-100},{100,100}})));
351 algorithm
352 so[1]:=abs(rem(171*si[1], 30269));
353 so[2]:=abs(rem(172*si[2], 30307));
354 so[3]:=abs(rem(170*si[3], 30323));
355 if so[1] <= 0 and so[1] >= 0 then
356 so[1]:=1;
357 end if;
358 if so[2] <= 0 and so[2] >= 0 then
359 so[2]:=1;
360 end if;
361 if so[3] <= 0 and so[3] >= 0 then
362 so[3]:=1;
363 end if;
364 x:=rem(so[1]/30269.0 + so[2]/30307.0 + so[3]/3023.0, 1.0);
365 end random;
366
367 function normalvariate "normally distributed random variable"
368 input Real mu "mean value";
369 input Real sigma "standard deviation";
370 input Real[3] si "input random seed";
371 output Real x;
372 output Real[3] so "output random seed";
373 protected
374 Real[3] s1,s2;
375 Real z,zz,u1,u2;
376 Boolean my_break=false;
377 algorithm
378 s1:=si;
379 u2:=1;
380 while (not my_break) loop
381 (u1,s2):=Random.random(s1);
382 (u2,s1):=Random.random(s2);
383 z:=NV_MAGICCONST*(u1 - 0.5)/u2;
384 zz:=z*z/4.0;
385 my_break:=zz <= -log(u2);
386 end while;
387 x:=mu + z*sigma;
388 so:=s1;
389 end normalvariate;
390
391 end Random;
392
393 connector MutexPortIn "Mutex port connector for receiveing requests"
394 input Boolean request "Set by application to request access";
395 input Boolean release "Set by application to release access";
396 output Boolean ok "Signal that ownership was granted";
397 end MutexPortIn;
398
399 model Mutex "Mutual exclusion of shared resource"
400 constant Integer n=5 "The number of connected ports";
401 MutexPortIn[n] port;
402 protected
403 Boolean request[n];
404 Boolean release[n];
405 Boolean ok[n];
406 Boolean waiting[n];
407 Boolean occupied "Mutex is locked if occupied is true";
408 equation
409 for i in 1:n loop
410 port[i].ok=ok[i];
411 request[i]=port[i].request;
412 release[i]=port[i].release;
413 end for;
414 algorithm
415 for i in 1:n loop
416 when request[i] then
417 if not occupied then
418 ok[i]:=true;
419 waiting[i]:=false;
420 else
421 ok[i]:=false;
422 waiting[i]:=true;
423 end if;
424 occupied:=true;
425 end when;
426 when pre(waiting[i]) and not occupied then
427 occupied:=true;
428 ok[i]:=true;
429 waiting[i]:=false;
430 end when;
431 when pre(release[i]) then
432 ok[i]:=false;
433 occupied:=false;
434 end when;
435 end for;
436 end Mutex;
437
438end Philosopher;
439model Philosopher_DiningTable
440 extends Philosopher.DiningTable;
441end Philosopher_DiningTable;