Using Rules Engine for Enterprise iOS Apps — Powered by Gameplaykit

Archan Ganguly
3 min readJan 20, 2016

I believe enterprise mobile apps have introduced new dimensions to mobile app design & development. More and more, we are seeing businesses require mobile apps to do more things rather than just be an enabler in the multi-channel context.

Transforming the way the enterprise works entails making enterprise mobile apps “powerful enough” to drive business processes efficiently with lower cycle times. A suite of mobile apps may work in tandem to fulfill the business processes within a LOB. Typically, global enterprises have large fieldforce/workforce who need to be on the field in patchy networks, many times without any network connectivity. This makes Offline capability the most sought after feature in Enterprise mobility space. And, with it, it introduces the complexity of executing business processes on mobile. Mobile no more remains a client just to initiate a business process. While offline, the mobile device and the app has a major role play as far as BPM is concerned.

Business Processes & Rules

No doubt, business processes have to deal with Rules. For example, a field force may require to execute a sales order in offline mode, generate invoice, take payments(cash, cheque etc.) and print receipt via a mobile printer.

Off late, I was researching on designing and building a light weight rules engine for iOS. While building something grounds-up is always an option (motivated from many such open source java based rules engine such as EasyRules etc.), I stumbled upon a new kid in the town — GameplayKit’s Rule subsystem.

Although, part of Gameplaykit, I believe this system can be readily used for any Enterprise iOS app.

So, let us step back and think, as to what is really expected out of a Rules engine ? For a business process, it needs the Rules engine to evaluate a set of rules. A rule would typically evaluate a predicate and then take an action. The below schematic provides a high level view of processes & rules

The Rule Engine and its sets of Rules.

iOS GameplayKit Rule sub-system

iOS 9 introduced Gameplaykit and with it came the rule subsystem. You can create one or more instance of the Rulesystem, create rules and add the rules to the subsystem. Then, when you do an evaluate on the rule system, all rules within it would get executed in a certain order. By default, the rules get executed in the order it was added to the rule system. You can also customize the order of execution via the “salience” property of the rule.

For one my requirements, I have to apply certain rules per a “sequence” configuration. The salience property really helps here, keeps your code clean!

Let us jump into code now…We would attempt to realise the above RulesEngine block.

Below are two examples of rules related to tax and discount respectively.

Next, I specify the salience order indicating that first, the discount is applied and then the tax is calculated! The rules are added to a GKRuleSystem instance. And, on invocation of the evaluate() function, the rules are executed per the salience order.

Pretty handy and simple huh!!

You will find more details on the GameplayKit Rule Systems here : https://developer.apple.com/library/ios/documentation/General/Conceptual/GameplayKit_Guide/RuleSystems.html

We need to delve more in to this and look forward to package the rule engine implementation into a custom framework. To keep it simple, the design should enable the mobile app core component to just feed in with data sets and receive an output based on rule system execution. Also, any change to the rules would imply that the framework undergoes updates and not the enterprise mobile app core components — pretty standard process for mobile app change management.

--

--