diff --git a/go/TrafficLights/TrafficLight.go b/go/TrafficLights/TrafficLight.go index cd7cf5e..d0fb958 100644 --- a/go/TrafficLights/TrafficLight.go +++ b/go/TrafficLights/TrafficLight.go @@ -1,5 +1,11 @@ package main +import ( + "fmt" +) + +var debuggingAllowed = true + type CardinalDirection string const ( @@ -13,28 +19,12 @@ var Directions = []CardinalDirection{ north, south, east, west, } -func nextDirection(currentDirection CardinalDirection) CardinalDirection { - switch currentDirection { - case north: - return east - case south: - return west - case east: - return south - case west: - return north - default: - return north - //TODO: dont return this here, return nil instead - } -} - type Colour string const ( - red = Colour("Red") - green = Colour("Green") - yellow = Colour("Yellow") + red = Colour("\u001B[31m Red \u001B[0m") + green = Colour("\u001B[33m Green \u001B[0m") + yellow = Colour("\u001B[32m Yellow \u001B[0m") ) var Colours = []Colour{ @@ -55,35 +45,53 @@ func nextColour(currentColour Colour) Colour { } func main() { - var directionChannel = make(chan CardinalDirection) - var colourChannel = make(chan Colour) - var waitChannel = make(chan string) + var quitChannel = make(chan string) - for _, direction := range Directions { - go TrafficLight(direction, directionChannel, colourChannel, waitChannel) - } + var northColour = make(chan Colour, 2) + var southColour = make(chan Colour, 2) + var eastColour = make(chan Colour, 2) + var westColour = make(chan Colour, 2) - directionChannel <- north - directionChannel <- south + var northDirection = make(chan string, 4) + var southDirection = make(chan string, 4) + var eastDirection = make(chan string, 4) + var westDirection = make(chan string, 4) + go TrafficLight(north, northColour, southColour, northDirection, eastDirection, southDirection) + go TrafficLight(south, southColour, northColour, southDirection, westDirection, northDirection) + go TrafficLight(east, eastColour, westColour, eastDirection, southDirection, westDirection) + go TrafficLight(west, westColour, eastColour, westDirection, northDirection, eastDirection) + + <-quitChannel } -func TrafficLight(direction CardinalDirection, directionChannel chan CardinalDirection, colourChannel chan Colour, waitChannel chan string) { - var colour Colour +func TrafficLight(direction CardinalDirection, currentColour <-chan Colour, oppositeColour chan<- Colour, currentDirection <-chan string, nextDirection chan<- string, oppositeDirection chan<- string) { + var colour = red + + if direction != north && direction != south { + nextDirection <- "go" + } + for { - switch msg := <-waitChannel; { - case msg == "proceed": - directionChannel <- nextDirection(direction) + show(direction, colour) + oppositeColour <- nextColour(colour) + select { + case msg := <-currentColour: + if msg == nextColour(colour) { + colour = msg + } } - switch msg := <-directionChannel; { - case msg == direction: - } - colourChannel <- nextColour(colour) - switch msg := <-colourChannel; { - case msg == nextColour(colour): - - } + } +} + +func show(direction CardinalDirection, colour Colour) { + fmt.Println(string(direction), " : ", string(colour)) +} + +func debug(message string) { + if debuggingAllowed { + fmt.Println("Debug:", message) } } diff --git a/mCRL2/TrafficLights/v2/v2_spec.lps b/mCRL2/TrafficLights/v2/v2_spec.lps index 39315fe..18a5d01 100644 Binary files a/mCRL2/TrafficLights/v2/v2_spec.lps and b/mCRL2/TrafficLights/v2/v2_spec.lps differ diff --git a/mCRL2/TrafficLights/v2/v2_spec.lts b/mCRL2/TrafficLights/v2/v2_spec.lts index 77fc0b9..8efcb03 100644 Binary files a/mCRL2/TrafficLights/v2/v2_spec.lts and b/mCRL2/TrafficLights/v2/v2_spec.lts differ diff --git a/mCRL2/TrafficLights/v2/v2_spec.mcrl2 b/mCRL2/TrafficLights/v2/v2_spec.mcrl2 index 5d8cb08..6858fdb 100644 --- a/mCRL2/TrafficLights/v2/v2_spec.mcrl2 +++ b/mCRL2/TrafficLights/v2/v2_spec.mcrl2 @@ -3,6 +3,22 @@ sort CardinalDirection = struct north | east | south | west; +map + nextDirection: CardinalDirection -> CardinalDirection; + oppositeDirection : CardinalDirection -> CardinalDirection; + +eqn + + nextDirection(north) = east; + nextDirection(east) = south; + nextDirection(south) = west; + nextDirection(west) = north; + + oppositeDirection(north) = south; + oppositeDirection(south) = north; + oppositeDirection(east) = west; + oppositeDirection(west) = east; + %----------------------------------------------------------------------- @@ -40,7 +56,19 @@ sort map combinations: Map; +%------------------------------------------------------------------------ +map + unsafe : Map # CardinalDirection -> Bool; + +var + status: Map; + direction: CardinalDirection; + +eqn + unsafe(status, direction) = ((status(direction) in {green, yellow} || status(oppositeDirection(direction)) in {green, yellow}) + && (status(nextDirection(direction)) in {green, yellow} + || status(oppositeDirection(nextDirection(direction))) in {green, yellow})); %------------------------------------------------------------------------ @@ -60,7 +88,7 @@ proc ; Monitor(status : Map) = - ((status(north) in {green, yellow} || status(south) in {green, yellow}) && (status(east) in {green, yellow} || status(west) in {green, yellow})) %ist aktueller Zustand unsicher ? + (unsafe(status, north)) %ist aktueller Zustand unsicher ? -> crossingUnsafe(status(north), status(east), status(south), status(west)) . delta %unsicherer Zustand, deadlock <>%wenn aktueller Zustand sicher: diff --git a/mCRL2/TrafficLights/v3/v3_spec.lps b/mCRL2/TrafficLights/v3/v3_spec.lps index 7165696..caafb14 100644 Binary files a/mCRL2/TrafficLights/v3/v3_spec.lps and b/mCRL2/TrafficLights/v3/v3_spec.lps differ diff --git a/mCRL2/TrafficLights/v3/v3_spec.lts b/mCRL2/TrafficLights/v3/v3_spec.lts index dbb6758..c13b08a 100644 Binary files a/mCRL2/TrafficLights/v3/v3_spec.lts and b/mCRL2/TrafficLights/v3/v3_spec.lts differ diff --git a/mCRL2/TrafficLights/v3/v3_spec.mcrl2 b/mCRL2/TrafficLights/v3/v3_spec.mcrl2 index 3804c8d..160c592 100644 --- a/mCRL2/TrafficLights/v3/v3_spec.mcrl2 +++ b/mCRL2/TrafficLights/v3/v3_spec.mcrl2 @@ -3,6 +3,22 @@ sort CardinalDirection = struct north | east | south | west; +map + nextDirection: CardinalDirection -> CardinalDirection; + oppositeDirection : CardinalDirection -> CardinalDirection; + +eqn + + nextDirection(north) = east; + nextDirection(east) = south; + nextDirection(south) = west; + nextDirection(west) = north; + + oppositeDirection(north) = south; + oppositeDirection(south) = north; + oppositeDirection(east) = west; + oppositeDirection(west) = east; + %----------------------------------------------------------------------- sort @@ -38,7 +54,20 @@ sort map combinations: Map; +%------------------------------------------------------------------------ +map + unsafe : Map # CardinalDirection # Colour -> Bool; + +var + status: Map; + direction: CardinalDirection; + colour: Colour; + +eqn + unsafe(status, direction, colour) = (colour in {green, yellow} || status(oppositeDirection(direction)) in {green, yellow}) + && (status(nextDirection(direction)) in {green, yellow} + || status(oppositeDirection(nextDirection(direction))) in {green, yellow}); %------------------------------------------------------------------------ @@ -59,17 +88,9 @@ proc Monitor(status : Map) = sum direction : CardinalDirection.( sum c : Colour . - (c == green) -> - (direction == north || direction == south) -> - ((status(east) == red && status(west) == red) -> % sind die anderen Ampeln rot ? - seeColour(direction, c) . Monitor(status= status[direction -> c])) % dann darf fortgefahren werden - <> - ((status(north) == red && status(south) == red) -> - seeColour(direction, c) . Monitor(status= status[direction -> c])) - <> %soll nicht grün gezeigt werden kann einfach fortgefahren werden, es wurde ja bereits überprüft - seeColour(direction, c) . - Monitor(status= status[direction -> c]) - + (unsafe(status, direction, c)) + -> delta + <> seeColour(direction, c) . Monitor(status= status[direction -> c]) ); diff --git a/mCRL2/TrafficLights/v4/v4_spec.lps b/mCRL2/TrafficLights/v4/v4_spec.lps index a184cb0..4884bf0 100644 Binary files a/mCRL2/TrafficLights/v4/v4_spec.lps and b/mCRL2/TrafficLights/v4/v4_spec.lps differ diff --git a/mCRL2/TrafficLights/v4/v4_spec.lts b/mCRL2/TrafficLights/v4/v4_spec.lts index a3fd64c..b8da6f9 100644 Binary files a/mCRL2/TrafficLights/v4/v4_spec.lts and b/mCRL2/TrafficLights/v4/v4_spec.lts differ diff --git a/mCRL2/TrafficLights/v4/v4_spec.mcrl2 b/mCRL2/TrafficLights/v4/v4_spec.mcrl2 index 59c2fbe..0257d0e 100644 --- a/mCRL2/TrafficLights/v4/v4_spec.mcrl2 +++ b/mCRL2/TrafficLights/v4/v4_spec.mcrl2 @@ -40,12 +40,13 @@ act %------------------------------------------------------------------------ proc - TrafficLight(direction : CardinalDirection) = TL(direction, red, true); + TrafficLight(direction : CardinalDirection) = + (direction == north || direction == south) + -> TL(direction, red) + <> changeDirection(direction) . TL(direction, red) + ; - TL(direction : CardinalDirection, colour : Colour, firstStart : Bool) = - (firstStart) %beim ersten Start nur das Licht umschalten - -> changeDirection(direction) . TL(direction, colour, false) - <> %ansonsten + TL(direction : CardinalDirection, colour : Colour) = show(direction, colour) . %aktuellen Zustand zeigen (colour == red) %wenn rot -> wait . ChangeDirection(direction) %Richtungswechsel, wenn wieder zurück @@ -61,7 +62,7 @@ proc ChangeLight(direction : CardinalDirection, colour : Colour) = changeLight(nextColour(colour)) %zur nächsten Farbe wechseln, wenn beide Richtungen dies autorisieren - . TL(direction, nextColour(colour), false) %fortfahren + . TL(direction, nextColour(colour)) %fortfahren ; System = @@ -83,8 +84,8 @@ proc }, - TrafficLight(north) || TrafficLight(east) || TrafficLight(south) || TrafficLight(west) || changeDirection(north) || changeDirection(south) - %starten der Prozesse und auslösen des ersten Richtungswechsels + TrafficLight(north) || TrafficLight(east) || TrafficLight(south) || TrafficLight(west) + %starten der Prozesse ))); init