Skip to content

Instantly share code, notes, and snippets.

@mttrbrts
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mttrbrts/b5bef3c1a8823d00ccd4 to your computer and use it in GitHub Desktop.
Save mttrbrts/b5bef3c1a8823d00ccd4 to your computer and use it in GitHub Desktop.
Routing with External Business Rules

This example uses an external Decision Service hosted by IBM Operational Decision Manager (on Bluemix) to perform switch routing based on data retrieved in a previous step.

Here we use a trivial example to replace logic written in code in a scenario taken from (http://flows.nodered.org/flow/6572686) to provide business editable rules which are managed and governed separately from the integration flow.

For example, when deciding the thresholds and logic required to raise alerts and message for the UK Power Demand (http://flows.nodered.org/flow/6572686), the logic is constructed in business friendly (and business-editable) phrases hosted by the ODM platform:

Example 1 - Demand Threshold Rule

if
    the value in GW of 'the power demand' is more than 56
then
    set message to "High Power Usage" ;

Example 2 - Frequency Threshold Decision Table

Frequency Message
< 49.5 Demand not met
[49.5, 50.5] Normal
> 50.5 Exceeding demand
[{"id":"f4b7a0b7.f71ad8","type":"http request","name":"Make Decision","method":"POST","url":"https://ds-46e0b.ng.bluemix.net/DecisionService/rest/v1/energyRuleApp/energyAgent/","x":316,"y":176,"z":"52182ff4.9d4a98","wires":[["513c556e.d8b97c"]]},{"id":"6ebae978.aa315","type":"function","name":"Build Decision Payload","func":"msg.payload = '{\"Power\":{\"valueInWatts\":'+\nmsg.payload.demand*1000.0\n+'}}';\nmsg.headers = {\n \"content-length\": msg.payload.length,\n \"content-type\": \"application/json\"\n};\nreturn msg;","outputs":1,"x":132,"y":176,"z":"52182ff4.9d4a98","wires":[["f4b7a0b7.f71ad8"]]},{"id":"6b712a4b.03ba04","type":"switch","name":"Route based on Decision","property":"payload.message","rules":[{"t":"eq","v":"Nothing unusual here"},{"t":"eq","v":"High Power Usage"}],"checkall":"false","outputs":2,"x":189,"y":249,"z":"52182ff4.9d4a98","wires":[[],["f370acd1.28da1"]]},{"id":"a536b886.396e48","type":"function","name":"Parse UK Power Demand","func":"// does a simple text extract parse of the http output to provide an\n// object containing the uk power demand, frequency and time\n\nif (~msg.payload.indexOf('<BR')) {\nvar words = msg.payload.split(\"div\")[1].split(\"<BR\");\nif (words.length >= 3) {\nmsg.payload = {};\nmsg.payload.demand = parseInt(words[0].split(\":\")[1]);\nmsg.payload.frequency = parseFloat(words[2].split(\":\")[1]);\nmsg.payload.time = words[1].split(\">\")[1];\nmsg2 ={};\nmsg2.payload = (msg.payload.frequency >= 50) ? true : false;\n\nreturn [msg,msg2];\n}\n}\nreturn null;","outputs":"2","x":427,"y":105,"z":"52182ff4.9d4a98","wires":[["6ebae978.aa315","f370acd1.28da1"],[]]},{"id":"3b899889.7859b","type":"httpget","name":"Get UK Power","baseurl":"http://www.nationalgrid.com/ngrealtime/realtime/systemdata.aspx","append":"","x":237,"y":105,"z":"52182ff4.9d4a98","wires":[["a536b886.396e48"]]},{"id":"f370acd1.28da1","type":"debug","name":"","active":true,"console":"true","complete":"false","x":492,"y":246,"z":"52182ff4.9d4a98","wires":[]},{"id":"b2d7417.9c71d4","type":"inject","name":"Tick","topic":"","payload":" ","repeat":"","crontab":"*/5 * * * *","once":false,"x":107,"y":106,"z":"52182ff4.9d4a98","wires":[["3b899889.7859b"]]},{"id":"513c556e.d8b97c","type":"function","name":"Parse Decision Response","func":"msg.payload = JSON.parse(msg.payload);\nreturn msg;","outputs":1,"x":519,"y":176,"z":"52182ff4.9d4a98","wires":[["6b712a4b.03ba04"]]}]
@robertodijo
Copy link

Hi, I am trying this out, but your decision server is not live anymore. Would you like to be able to add the energyruleapp package to this repository, so I can load this into my own Bluemix Business Rules services.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment