Item Update Counter Rule

The item update counter rule is a rule that counts the number of updates of any M3 item. For every 10th update, an event "10ItemUpdates" is posted to the Event Hub with the current counter value. The actual counter (EventCounterTest) is a declared fact type.

package com.lawson.eventhub.analytics.drools;
 
import com.lawson.eventhub.analytics.drools.model.Event;
import com.lawson.eventhub.analytics.drools.model.HubEvent;
import com.lawson.eventhub.EventOperation;
 
declare EventCounterTest
	value : int
end
 
rule "Insert_EventCounterTest"
	when
		HubEvent()
		not(EventCounterTest())
	then
		EventCounterTest counter = new EventCounterTest();
		insert(counter);
end
 
rule "MITMAS_Update_Counter"
	@subscription(M3:MITMAS:U)
	no-loop
	when
		event: HubEvent(publisher == "M3", documentName == "MITMAS", operation == EventOperation.UPDATE)
		counter: EventCounterTest()
	then
		counter.setValue(counter.getValue() + 1);
		if (counter.getValue() % 10 == 0) {
			// Post counter event
			Event event2 = new Event();
			event2.setDocumentName("10ItemUpdates");
			event2.setOperation(EventOperation.CREATE);
			event2.setTrackingId(event.getTrackingId());
			event2.addElement("TotalEvents", String.valueOf(counter.getValue()));
			event2.postEvent();
		}
end