Verbesserungen nach Testatversuch
This commit is contained in:
parent
53c9a8aba0
commit
dbe8fcbf68
|
@ -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)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
directionChannel <- north
|
||||
directionChannel <- south
|
||||
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"
|
||||
}
|
||||
|
||||
func TrafficLight(direction CardinalDirection, directionChannel chan CardinalDirection, colourChannel chan Colour, waitChannel chan string) {
|
||||
var colour Colour
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -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:
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -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])
|
||||
);
|
||||
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue