<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.1//EN' 'http://www.it.uu.se/research/group/darts/uppaal/flat-1_2.dtd'>
<nta>
	<declaration>/*
OR - 2019
Adapted by Jovian HERSEMEULE - 2020
 */

const int NAB = 3;
const int NBA = 3;
const int N = 6;

typedef int[0,NAB-1] id_tAB;
typedef int[0,NBA-1] id_tBA;

int ab=0, ba=0;

chan        accesAB[NAB], accesBA[NBA], libAB[NAB], libBA[NBA], requestAB[NAB], requestBA[NBA];

</declaration>
	<template>
		<name>TrainBA</name>
		<parameter>const id_tBA id</parameter>
		<declaration>clock x;</declaration>
		<location id="id0" x="-51" y="0">
			<name x="-51" y="17">A</name>
		</location>
		<location id="id1" x="93" y="0">
			<name x="84" y="17">BA</name>
			<label kind="invariant" x="110" y="-8">x&lt;=10</label>
		</location>
		<location id="id2" x="93" y="-127">
			<name x="93" y="-161">B</name>
		</location>
		<location id="id3" x="-51" y="-127">
			<name x="-59" y="-161">Safe</name>
		</location>
		<init ref="id3"/>
		<transition>
			<source ref="id0"/>
			<target ref="id3"/>
			<label kind="synchronisation" x="-51" y="-68">libBA[id] !</label>
		</transition>
		<transition>
			<source ref="id1"/>
			<target ref="id0"/>
		</transition>
		<transition>
			<source ref="id2"/>
			<target ref="id1"/>
			<label kind="synchronisation" x="101" y="-85">accesBA[id] ?</label>
			<label kind="assignment" x="101" y="-68">x:=0</label>
		</transition>
		<transition>
			<source ref="id3"/>
			<target ref="id2"/>
			<label kind="synchronisation" x="-33" y="-144">requestBA[id] !</label>
		</transition>
	</template>
	<template>
		<name x="40" y="16">TrainAB</name>
		<parameter>const id_tAB id</parameter>
		<declaration>clock x;</declaration>
		<location id="id4" x="127" y="93">
			<name x="116" y="65">Safe</name>
		</location>
		<location id="id5" x="280" y="93">
			<name x="279" y="65">A</name>
		</location>
		<location id="id6" x="127" y="229">
			<name x="124" y="243">B</name>
		</location>
		<location id="id7" x="280" y="229">
			<name x="270" y="243">AB</name>
			<label kind="invariant" x="296" y="218">x&lt;=10</label>
		</location>
		<init ref="id4"/>
		<transition>
			<source ref="id6"/>
			<target ref="id4"/>
			<label kind="synchronisation" x="127" y="144">libAB[id] !</label>
		</transition>
		<transition>
			<source ref="id7"/>
			<target ref="id6"/>
		</transition>
		<transition>
			<source ref="id5"/>
			<target ref="id7"/>
			<label kind="synchronisation" x="280" y="144">accesAB[id] ?</label>
			<label kind="assignment" x="280" y="161">x:=0</label>
		</transition>
		<transition>
			<source ref="id4"/>
			<target ref="id5"/>
			<label kind="synchronisation" x="145" y="76">requestAB[id] !</label>
		</transition>
	</template>
	<template>
		<name x="40" y="16">CTRL</name>
		<declaration>id_tAB list[N+1];
int[0,N] len;

// Put an element at the end of the queue
void enqueue(id_tAB element)
{
        list[len++] = element;
}

// Remove the front element of the queue
void dequeue()
{
        int i = 0;
        len -= 1;
        while (i &lt; len)
        {
                list[i] = list[i + 1];
                i++;
        }
        list[i] = 0;
}

// Returns the front element of the queue
id_tAB front()
{
   return list[0];
}

// Returns the last element of the queue
id_tAB tail()
{
   return list[len - 1];
}</declaration>
		<location id="id8" x="314" y="102">
			<name x="331" y="94">Free</name>
		</location>
		<location id="id9" x="212" y="-68">
			<name x="238" y="-59">OnlyAB</name>
			<label kind="comments" x="297" y="-59">At least one train on AB ;
and no train blocked on B</label>
		</location>
		<location id="id10" x="212" y="263">
			<name x="229" y="238">OnlyBA</name>
		</location>
		<location id="id11" x="-153" y="-68">
			<name x="-272" y="-68">RequestFromB</name>
			<label kind="comments" x="-484" y="-42">Waiting for AB trains to leave shared section;
some other trains are waiting on B.</label>
		</location>
		<location id="id12" x="-153" y="263">
			<name x="-272" y="246">RequestFromA</name>
		</location>
		<init ref="id8"/>
		<transition>
			<source ref="id11"/>
			<target ref="id11"/>
			<label kind="select" x="-263" y="-221">e:id_tAB</label>
			<label kind="synchronisation" x="-263" y="-204">accesAB[e]!</label>
			<nail x="-187" y="-187"/>
			<nail x="-263" y="-187"/>
		</transition>
		<transition>
			<source ref="id12"/>
			<target ref="id12"/>
			<label kind="select" x="-255" y="323">e:id_tBA</label>
			<label kind="synchronisation" x="-255" y="340">accesBA[e] !</label>
			<nail x="-196" y="323"/>
			<nail x="-256" y="323"/>
		</transition>
		<transition>
			<source ref="id8"/>
			<target ref="id9"/>
			<label kind="select" x="144" y="17">e:id_tAB</label>
			<label kind="synchronisation" x="102" y="34">requestAB[e] ?</label>
			<label kind="assignment" x="161" y="51">ab ++</label>
			<nail x="212" y="59"/>
		</transition>
		<transition>
			<source ref="id8"/>
			<target ref="id10"/>
			<label kind="select" x="144" y="136">e:id_tBA</label>
			<label kind="synchronisation" x="102" y="153">requestBA[e] ?</label>
			<label kind="assignment" x="161" y="170">ba ++</label>
			<nail x="212" y="144"/>
		</transition>
		<transition>
			<source ref="id12"/>
			<target ref="id9"/>
			<label kind="guard" x="-221" y="136">ba == 0</label>
			<nail x="-153" y="144"/>
		</transition>
		<transition>
			<source ref="id10"/>
			<target ref="id8"/>
			<label kind="guard" x="263" y="186">ba == 0</label>
		</transition>
		<transition>
			<source ref="id12"/>
			<target ref="id12"/>
			<label kind="select" x="-153" y="322">e:id_tBA</label>
			<label kind="synchronisation" x="-153" y="339">libBA[e] ?</label>
			<label kind="assignment" x="-153" y="356">ba --</label>
			<nail x="-85" y="322"/>
			<nail x="-153" y="322"/>
		</transition>
		<transition>
			<source ref="id10"/>
			<target ref="id12"/>
			<label kind="select" x="-42" y="212">e:id_tAB</label>
			<label kind="synchronisation" x="-42" y="229">requestAB[e] ?</label>
			<label kind="assignment" x="-42" y="246">ab ++</label>
		</transition>
		<transition>
			<source ref="id10"/>
			<target ref="id10"/>
			<label kind="select" x="323" y="263">e:id_tBA</label>
			<label kind="synchronisation" x="323" y="280">requestBA[e] ?</label>
			<label kind="assignment" x="323" y="298">ba ++</label>
			<nail x="314" y="323"/>
			<nail x="314" y="263"/>
		</transition>
		<transition>
			<source ref="id9"/>
			<target ref="id9"/>
			<label kind="select" x="331" y="-153">e:id_tAB</label>
			<label kind="synchronisation" x="331" y="-136">requestAB[e] ?</label>
			<label kind="assignment" x="331" y="-119">ab ++</label>
			<nail x="323" y="-153"/>
			<nail x="323" y="-68"/>
		</transition>
		<transition>
			<source ref="id11"/>
			<target ref="id10"/>
			<label kind="guard" x="-221" y="42">ab == 0</label>
			<nail x="-153" y="50"/>
		</transition>
		<transition>
			<source ref="id11"/>
			<target ref="id11"/>
			<label kind="select" x="-144" y="-238">e:id_tAB</label>
			<label kind="synchronisation" x="-144" y="-221">libAB[e] ?</label>
			<label kind="assignment" x="-144" y="-204">ab--</label>
			<nail x="-68" y="-187"/>
			<nail x="-153" y="-187"/>
		</transition>
		<transition>
			<source ref="id9"/>
			<target ref="id11"/>
			<label kind="select" x="-34" y="-119">e:id_tBA</label>
			<label kind="synchronisation" x="-34" y="-102">requestBA[e] ?</label>
			<label kind="assignment" x="-34" y="-85">ba ++</label>
		</transition>
		<transition>
			<source ref="id10"/>
			<target ref="id10"/>
			<label kind="select" x="221" y="339">e:id_tBA</label>
			<label kind="synchronisation" x="221" y="356">libBA[e] ?</label>
			<label kind="assignment" x="221" y="373">ba --</label>
			<nail x="280" y="331"/>
			<nail x="212" y="331"/>
		</transition>
		<transition>
			<source ref="id10"/>
			<target ref="id10"/>
			<label kind="select" x="110" y="340">e:id_tBA</label>
			<label kind="synchronisation" x="110" y="357">accesBA[e] !</label>
			<nail x="195" y="331"/>
			<nail x="110" y="331"/>
		</transition>
		<transition>
			<source ref="id9"/>
			<target ref="id9"/>
			<label kind="select" x="85" y="-229">e:id_tAB</label>
			<label kind="synchronisation" x="85" y="-212">accesAB[e] !</label>
			<nail x="187" y="-187"/>
			<nail x="85" y="-187"/>
		</transition>
		<transition>
			<source ref="id9"/>
			<target ref="id9"/>
			<label kind="select" x="212" y="-238">e:id_tAB</label>
			<label kind="synchronisation" x="212" y="-221">libAB[e] ?</label>
			<label kind="assignment" x="212" y="-204">ab--</label>
			<nail x="289" y="-187"/>
			<nail x="212" y="-187"/>
		</transition>
		<transition>
			<source ref="id9"/>
			<target ref="id8"/>
			<label kind="guard" x="272" y="8">ab == 0</label>
		</transition>
	</template>
	<system>system TrainAB, TrainBA, CTRL;
</system>
	<queries>
		<query>
			<formula>E&lt;&gt; TrainBA(1).BA</formula>
			<comment></comment>
		</query>
		<query>
			<formula>E&lt;&gt; TrainBA(2).BA
			</formula>
			<comment>
			</comment>
		</query>
		<query>
			<formula>A[] forall (i : id_tBA) forall (j : id_tAB) TrainBA(i).BA &amp;&amp; TrainAB(j).AB imply i == j
			</formula>
			<comment>
			</comment>
		</query>
		<query>
			<formula>TrainBA(1).B --&gt; TrainBA(1).BA
			</formula>
			<comment>
			</comment>
		</query>
		<query>
			<formula>A&lt;&gt; TrainBA(1).BA
			</formula>
			<comment>
			</comment>
		</query>
		<query>
			<formula>A[] not deadlock</formula>
			<comment></comment>
		</query>
		<query>
			<formula>E&lt;&gt; TrainBA(1).BA &amp; TrainBA(2).BA
			</formula>
			<comment>
			</comment>
		</query>
	</queries>
</nta>