Consider a simple intersection with two crossing roads and four traffic lights. Assume a simple algorithm for switching lights, so that the traffic on one road can proceed while the traffic on the other road is stopped. Model each traffic light as an instance of a TrafficLight class with a state attribute that can be either red, yellow, or green. Write invariants in OCL on the state attribute of the TrafficLight class that guarantee that the traffic cannot proceed on both roads simultaneously. Add associations to the model to navigate the system, if necessary. Note that OCL constraints are written on classes (as opposed to instances).

What will be an ideal response?

We add an association called right to denote the traffic light on the road to the right. Hence, the expression
right.right denotes the opposing light.

/* Opposing lights have always the same state */
context TrafficLight inv:
state = right.right.state
/* If the current light is green or yellow, the light on the right must be red */
context TrafficLight inv:
(state = #green or state = #yellow) implies right.state = #red

Computer Science & Information Technology

You might also like to view...

The String method substring returns ________.

a. a char b. a String c. void d. a char[]

Computer Science & Information Technology

What does the black square in the proxy indicate?

What will be an ideal response?

Computer Science & Information Technology