Verbesserungen nach Testatversuch

This commit is contained in:
Johannes Theiner 2020-05-17 11:28:29 +02:00
parent 53c9a8aba0
commit dbe8fcbf68
10 changed files with 118 additions and 60 deletions

View File

@ -1,5 +1,11 @@
package main package main
import (
"fmt"
)
var debuggingAllowed = true
type CardinalDirection string type CardinalDirection string
const ( const (
@ -13,28 +19,12 @@ var Directions = []CardinalDirection{
north, south, east, west, 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 type Colour string
const ( const (
red = Colour("Red") red = Colour("\u001B[31m Red \u001B[0m")
green = Colour("Green") green = Colour("\u001B[33m Green \u001B[0m")
yellow = Colour("Yellow") yellow = Colour("\u001B[32m Yellow \u001B[0m")
) )
var Colours = []Colour{ var Colours = []Colour{
@ -55,35 +45,53 @@ func nextColour(currentColour Colour) Colour {
} }
func main() { func main() {
var directionChannel = make(chan CardinalDirection) var quitChannel = make(chan string)
var colourChannel = make(chan Colour)
var waitChannel = make(chan string)
for _, direction := range Directions { var northColour = make(chan Colour, 2)
go TrafficLight(direction, directionChannel, colourChannel, waitChannel) var southColour = make(chan Colour, 2)
} var eastColour = make(chan Colour, 2)
var westColour = make(chan Colour, 2)
directionChannel <- north var northDirection = make(chan string, 4)
directionChannel <- south 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) { func TrafficLight(direction CardinalDirection, currentColour <-chan Colour, oppositeColour chan<- Colour, currentDirection <-chan string, nextDirection chan<- string, oppositeDirection chan<- string) {
var colour Colour var colour = red
if direction != north && direction != south {
nextDirection <- "go"
}
for { for {
switch msg := <-waitChannel; { show(direction, colour)
case msg == "proceed":
directionChannel <- nextDirection(direction)
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.

View File

@ -3,6 +3,22 @@
sort sort
CardinalDirection = struct north | east | south | west; 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 map
combinations: 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) = 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 -> crossingUnsafe(status(north), status(east), status(south), status(west)) . delta %unsicherer Zustand, deadlock
<>%wenn aktueller Zustand sicher: <>%wenn aktueller Zustand sicher:

Binary file not shown.

Binary file not shown.

View File

@ -3,6 +3,22 @@
sort sort
CardinalDirection = struct north | east | south | west; 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 sort
@ -38,7 +54,20 @@ sort
map map
combinations: 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) = Monitor(status : Map) =
sum direction : CardinalDirection.( sum direction : CardinalDirection.(
sum c : Colour . sum c : Colour .
(c == green) -> (unsafe(status, direction, c))
(direction == north || direction == south) -> -> delta
((status(east) == red && status(west) == red) -> % sind die anderen Ampeln rot ? <> seeColour(direction, c) . Monitor(status= status[direction -> c])
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])
); );

Binary file not shown.

Binary file not shown.

View File

@ -40,12 +40,13 @@ act
%------------------------------------------------------------------------ %------------------------------------------------------------------------
proc 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) = TL(direction : CardinalDirection, colour : Colour) =
(firstStart) %beim ersten Start nur das Licht umschalten
-> changeDirection(direction) . TL(direction, colour, false)
<> %ansonsten
show(direction, colour) . %aktuellen Zustand zeigen show(direction, colour) . %aktuellen Zustand zeigen
(colour == red) %wenn rot (colour == red) %wenn rot
-> wait . ChangeDirection(direction) %Richtungswechsel, wenn wieder zurück -> wait . ChangeDirection(direction) %Richtungswechsel, wenn wieder zurück
@ -61,7 +62,7 @@ proc
ChangeLight(direction : CardinalDirection, colour : Colour) = ChangeLight(direction : CardinalDirection, colour : Colour) =
changeLight(nextColour(colour)) %zur nächsten Farbe wechseln, wenn beide Richtungen dies autorisieren 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 = System =
@ -83,8 +84,8 @@ proc
}, },
TrafficLight(north) || TrafficLight(east) || TrafficLight(south) || TrafficLight(west) || changeDirection(north) || changeDirection(south) TrafficLight(north) || TrafficLight(east) || TrafficLight(south) || TrafficLight(west)
%starten der Prozesse und auslösen des ersten Richtungswechsels %starten der Prozesse
))); )));
init init