com.ibm.streamsx.plumbing > switches 1.0.0 > com.ibm.streamsx.plumbing.sample.switches > ControlledSwitchSample.spl
Sample showing use of com.ibm.streamsx.plumbing.switches::ControlledSwitch.
A DataFlow containing a ControlledSwitch that is controlled by a separate subgraph using the composite ControlFlow.
The ControlFlow composite randomly opens and closes the switch with the switch being open (no tuples flowing) about 30% of the time.
The ControlledSwitch's state is changed by execution of the com.ibm.streamsx.plumbing.switches::setSwitchState(rstring,boolean) function. The setSwitchState is tied to the instance of ControlledSwitch by the switch's name: sampleSwitch.
When running the sample the tuple flow rate can be visualized using Streams console or Streams studio.
For example here is a view of ControlledSwitchSample in Streams console. The graph shows that the tuple flow in DataFlow (light blue) stops due to the ControlledSwitch from time to time.
public composite ControlledSwitchSample { graph () as JCP = JobControlPlane() { } // DataFlow with controlled switch @spl_category(name = "data") () as DF = DataFlow() { } // Flow that controls DataFlow // Note they are not connected using streams, // only indirectly through the control mechanisms // provided by JobControlPlane @spl_note(id = "0", text = "Flow that controls DataFlow by opening and closing the switch.") @spl_category(name = "control") () as CF = ControlFlow() { } }
The data flow that will demonstrate use of ControlledSwitch. A Beacon operator submits tuples around 100 per second through a com.ibm.streamsx.plumbing.switches::ControlledSwitch and out to a sink operator. Using Streams Studio or the Streams Console you can watch the rate of tuples into the sink operator change based upon the control signals from the separate ControlFlow composite.
public composite DataFlow { graph stream<int64 a> Data = Beacon() { param period : 0.01 ; } stream<Data> ManagedData = ControlledSwitch(Data) { param name : "sampleSwitch" ; initialState : true; } () as Sink = Custom(ManagedData) { } }
The flow that controls DataFlow in ControlledSwitchSample. Every twenty seconds a random value between 0.0 and 1.0 is submitted by a Beacon. If it is less that 0.3 the switch is opened otherwise it is closed to allow tuples to flow.
public composite ControlFlow() { graph stream<float64 rv> Control = Beacon() { param initDelay : 10.0 ; period : 20.0 ; output Control : rv = random() ; } () as ChangeShedding = Custom(Control) { logic state : { boolean __unused = registerSwitch("sampleSwitch", true ) ; } onTuple Control : { setSwitchState("sampleSwitch", rv >= 0.3) ; } } }