## Activity 13: Define component and add functional constraint

Create in the main project directory a file VendingMachine.component using File > New > File, with the VendingMachine project as parent. Insert the following text (you may copy the text below):

    import "IUser/IUser.interface"
    import "IService/IService.interface"
    import "ICoinCheck/ICoinCheck.interface"

    component VendingMachine

    provided port IUser vmUserPort
    provided port IService vmServicePort
    required port ICoinCheck vmCoinPort

    functional constraints

    OperationalConstraintInsertCoin {
        /* InsertCoin Command of IUser returns NOT_OPERATIONAL
         * when IService is not in state Operational
         * Otherwise it returns ACCEPTED or NOT_ACCEPTED
         */
        use events
        vmUserPort::reply to command InsertCoin

        initial state CoinReply {
            vmUserPort::reply (*,CoinResult::NOT_OPERATIONAL) to command InsertCoin
                 where NOT vmServicePort in Operational
            next state: CoinReply

            vmUserPort::reply (*,CoinResult::ACCEPTED) to command InsertCoin
                 where vmServicePort in Operational
            next state: CoinReply

            vmUserPort::reply (*,CoinResult::NOT_ACCEPTED) to command InsertCoin
                 where vmServicePort in Operational
            next state: CoinReply
        }
    }

Improve the lay-out using <CTRL>-<SHIFT>-F.

This specification assumes that interface IService has a state called Operational to represent that the machine is on and not in an error. Replace it with the state name of your model.

The functional constraint restricts the reply to command InsertCoin as indicated in the use events part. The specification expresses that when a reply to InsertCoin occurs, one of the following cases should hold:

- the result is NOT_OPERATIONAL and the state machine of the vmServicePort in not in state Operational, or

- the result is ACCEPTED and the state machine of the vmServicePort in in state Operational, or

- the result is NOT_ACCEPTED and the state machine of the vmServicePort in in state Operational.

To monitor the component constraints, change the file VendingMachine.prj as follows:

- Import VendingMachine.component

- Remove the current monitoring tasks

- Add the following monitoring task:

      Generate Monitors {
         monitoring for component VendingMachine {
            trace directories "VendingMachineEventsFiles"
         }
      }

  Component monitoring checks the components constraints and all interfaces of the component.

Run monitoring and inspect the dashboard with the results in folder comma-gen.