Rules SDK IBM WW BPM Forum March 2013

20
© 2009 IBM Corporation Using the Rules SDK v8.0.1 Business Rules Embedded Daniel Selman – ODM Product Architect March 2013

description

A description of the ODM Rules SDK, a set of APIs and components to embedd business rules authoring and execution functionality inside custom applications.

Transcript of Rules SDK IBM WW BPM Forum March 2013

Page 1: Rules SDK IBM WW BPM Forum March 2013

© 2009 IBM Corporation

Using the Rules SDK v8.0.1Business Rules Embedded

Daniel Selman – ODM Product Architect March 2013

Page 2: Rules SDK IBM WW BPM Forum March 2013

2 © 2013 IBM Corporation

Agenda

■ What is the Rules SDK?

■ When to use it?

■ Code Samples

■ Q&A

Page 3: Rules SDK IBM WW BPM Forum March 2013

3 © 2013 IBM Corporation

What is the Rules SDK?

■ APIs and embeddable components for:–Defining business object models and vocabularies–Authoring rules in Eclipse and on the Web–Building ruleset archives–Executing rules

■ APIs are significantly simpler than full ODM APIs

■ Full ODM APIs are available and may be used if necessary

Source If Applicable

IBM Presentation Template Full Version

Page 4: Rules SDK IBM WW BPM Forum March 2013

4 © 2013 IBM Corporation

When should it be used?

■ To allow business users to customize the behavior of an application using rules

■ Provide a highly customized and limited authoring and management environment:

–Relatively small numbers of rules (< 500?)–XSD models–Only If/Then rules and Decision Tables may be used

Source If Applicable

IBM Presentation Template Full Version

Page 5: Rules SDK IBM WW BPM Forum March 2013

5 © 2013 IBM Corporation

When should it not be used?

■ To re-implement a Decision Management Platform or Business Rules Management Platform

–Just use ODM! :-)

■ With great power comes great responsibility...

Source If Applicable

IBM Presentation Template Full Version

Page 6: Rules SDK IBM WW BPM Forum March 2013

6 © 2013 IBM Corporation

Getting Access

■ The Rules SDK ships with ODM and requires an ODM license.

■ After installing ODM you can find the components and samples within the rules-sdk directory.

Page 7: Rules SDK IBM WW BPM Forum March 2013

7 © 2013 IBM Corporation

Rules SDK vs ODM

Capability Rules SDK ODM

If/Then Rule (SWT) Yes Yes

If/Then Rule (Web) Yes Yes

Project export to ODM Yes Yes

Decision Tables (Web) Yes Yes

Decision Tables (Swing) No Yes

# rules per ruleset <100 <100,000

Execution algorithms Sequential Sequential, Rete, Fastpath

Governance / Lifecycle / Simulation

No Yes

Vocabulary/model editing No Yes

ODM project model: Decision Trees, Ruleflow etc.

No Yes

JEE execution support No Yes

Rule refactoring No Yes

Page 8: Rules SDK IBM WW BPM Forum March 2013

8 © 2013 IBM Corporation

Defining a Vocabulary and a BAL Rule

// the locale / encoding for the testfinal Locale locale = Locale.US;final String encoding = "UTF-8";

// create the ObjectModelBuilderObjectModelBuilder omBuilder = new ObjectModelBuilder();

// add an XSDString xsd = "customer.xsd";InputStream is = this.getClass().getClassLoader().getResourceAsStream( xsd );omBuilder.addXmlSchema(new XmlSchema(xsd, encoding, is));

// create the RuleLanguageServiceRuleLanguageService ruleLanguageService = new RuleLanguageService();

// create the vocabulary for the localeruleLanguageService.registerVocabulary( omBuilder.createVocabulary(locale) );

// define a parameterruleLanguageService.addParameter("com.ilog.rules.customer.Customer", "cust", "the customer", locale);

// create the RulesetBuilderRulesetBuilder rsBuilder = new RulesetBuilder(ruleLanguageService);

// add a rulersBuilder.addBusinessRule(locale, "rule1", "if the name of 'the customer' contains \"Smith\" then print \"Hello John!\";");

Page 9: Rules SDK IBM WW BPM Forum March 2013

9 © 2013 IBM Corporation

Using the DecisionTableBuilder

// add a decision tableDecisionTableBuilder dtBuilder = new DecisionTableBuilder(ruleLanguageService, "dt1", locale, readDTXmlModel("dt1.xml"));rsBuilder.addDecisionTable(Locale.US, "dt1", dtBuilder.getDTModel());

// define a decision table via APIDecisionTableBuilder builder = new DecisionTableBuilder(ruleLanguageService, "test", locale);builder.addConditionColumn( "cust", "Category", "the category");builder.addConditionColumn( "cust", "Age", "the age");builder.addActionColumn( "cust", "Category", "the resulting category");builder.addContent(new String[][] {{ "Gold", "< 20", "Silver" }, { "Gold", "[20..30]", "Gold" }, { "Gold", ">= 30", "Platinum" }, { "Bronze", "< 30", "Silver" }, { "Bronze", ">= 30", "Platinum" }});

Page 10: Rules SDK IBM WW BPM Forum March 2013

10 © 2013 IBM Corporation

Generating and Executing the Ruleset Archive

// generate the archivebyte[] rs = rsBuilder.buildRuleset("ruleset.jar");Assert.assertEquals( "Unexpected build issues", 0, rsBuilder.getBuildIssues().size() );

// create the rule serviceIlrRuleService ruleService = new XMLRuleService(new PrintWriter(System.out));IlrPath path = new IlrPath("RuleApp", "Ruleset");

// undeploy the ruleset if requiredif (ruleService.isRulesetDeployed(path)) {ruleService.undeployRuleset(path);}

// deploy the rulesetMap<String, String> properties = new HashMap<String, String>();properties.put(IlrRulesetArchiveProperties.KEY_SEQUENTIAL_TRACE_ENABLED, Boolean.toString(true));ruleService.deployRuleset(path, rs, properties);

// load instance documentis = this.getClass().getClassLoader().getResourceAsStream( "sample-customer.xml" );

// execute the engineMap<String, Object> params = new HashMap<String, Object>();params.put( "cust", inputStreamToString( encoding, is ));

// retrieve the resultsIlrSessionResponse response = ruleService.executeRuleset(path, params, true);IlrBusinessExecutionTrace businessTrace = new IlrBusinessExecutionTrace(response.getRulesetExecutionTrace());

Page 11: Rules SDK IBM WW BPM Forum March 2013

11 © 2013 IBM Corporation

Export the RuleSetBuilder to a Zipped Rule Project

// generate the archive that contains the rule projectByteArrayOutputStream bytes = new ByteArrayOutputStream();ZipOutputStream out = new ZipOutputStream( bytes );// The following line of code can be used to dump that project on disk//ZipOutputStream out = new ZipOutputStream( new FileOutputStream(new File(System.getProperty("user.dir"), "textProject.zip")) );rsBuilder.export(out, "testProject");

Page 12: Rules SDK IBM WW BPM Forum March 2013

12 © 2013 IBM Corporation

SWT BAL Editor

Page 13: Rules SDK IBM WW BPM Forum March 2013

13 © 2013 IBM Corporation

Using the SWT BAL Editor

// creation and display of the rule editor component IntelliTextDefaultEnvironment environment = new IntelliTextDefaultEnvironment(); // set some prediction configuration environment.getPreferenceStore().setValue(IntelliTextPreferences.FILTER_UNREACHABLE, true); // create the JFace element final IntelliTextViewer ruleEditor = new IntelliTextViewer(composite, SWT.NONE, environment); // create a document to hold the edited text IntelliTextDocument document = new IntelliTextDocument(IlrBAL.BAL_DEFINITION, rule.getLocale(), ruleLanguageService.getParserManager()); // register the variables document.setVariableProvider(ruleLanguageService.getVariableProvider(rule.getLocale())); // set the text document.set(rule.getContents()); // add a listener to react to text changes document.addDocumentListener(new IDocumentListener() { @Override public void documentChanged(DocumentEvent event) { rule.setContents(event.getDocument().get()); }

@Override public void documentAboutToBeChanged(DocumentEvent event) { // nothing to do } }); // assign the document to the editor ruleEditor.setInput(document);

/** * Represents a BAL rule in the model. */public class BalRule {

private Locale locale;private String name;private String contents;

Editing

Page 14: Rules SDK IBM WW BPM Forum March 2013

14 © 2013 IBM Corporation

Web BAL Editor

Page 15: Rules SDK IBM WW BPM Forum March 2013

15 © 2013 IBM Corporation

Web BAL Editor GUI (index.jsp) <script type="text/javascript"> dojo.registerModulePath("com.ibm.bdsl.web", "${pageContext.request.contextPath}/WebRuleEditor/com/ibm/bdsl/web"); dojo.registerModulePath("com.ibm.rules.bdsl.dt", "${pageContext.request.contextPath}/WebDTEditor/resources/js/com/ibm/rules/bdsl/dt");

// Decision Tables dojo.require("com.ibm.rules.bdsl.dt.DecisionTable"); dojo.require("com.ibm.rules.bdsl.dt.EditableDecisionTable");

// Rules dojo.require("com.ibm.bdsl.web.editor.dojo.DojoIntelliTextEditorDecorated");

// create a rule editorvar ruleEditor = new com.ibm.bdsl.web.editor.dojo.DojoIntelliTextEditorDecorated({ id: 'ruleEditor', handlerUrl: '${pageContext.request.contextPath}/WebRuleEditor' }, dojo.create('div', null, 'rulePane')); ruleEditor.startup();

Page 16: Rules SDK IBM WW BPM Forum March 2013

16 © 2013 IBM Corporation

Web BAL Editor Servletpublic class WebRuleEditorServlet extends IntelliTextEditorServlet { public static final String ENV_ATTRIBUTE_NAME = "intellitextEnvironment";

@Override protected IntelliTextEditorEnvironment getEnvironment(HttpServletRequest req) { HttpSession session = req.getSession(); IntelliTextEditorEnvironment env = (IntelliTextEditorEnvironment) session.getAttribute(ENV_ATTRIBUTE_NAME); if (env == null) { env = createEnvironment(session); } return env; }

public static synchronized IntelliTextEditorEnvironment createEnvironment(HttpSession session) { Locale locale = new Locale(SessionUtils.DEFAULT_LOCALE); RuleLanguageService ruleLanguageService = SessionUtils.getRuleLanguageService(session);

IlrBRLDefinition definition = IlrBAL.getDefinition(locale); IlrBRLParserManager parserManager = ruleLanguageService.getParserManager(); IlrBRLVariableProvider variableProvider = ruleLanguageService.getVariableProvider(locale);

IntelliTextEditorEnvironment environment = new DefaultIntelliTextEditorEnvironment(parserManager, variableProvider, definition, null);

session.setAttribute(ENV_ATTRIBUTE_NAME, environment);

return environment; }}

Page 17: Rules SDK IBM WW BPM Forum March 2013

17 © 2013 IBM Corporation

Web DT Editor

Page 18: Rules SDK IBM WW BPM Forum March 2013

18 © 2013 IBM Corporation

Web DT Editor GUI (index.jsp) <script type="text/javascript"> dojo.registerModulePath("com.ibm.bdsl.web", "${pageContext.request.contextPath}/WebRuleEditor/com/ibm/bdsl/web"); dojo.registerModulePath("com.ibm.rules.bdsl.dt", "${pageContext.request.contextPath}/WebDTEditor/resources/js/com/ibm/rules/bdsl/dt");

// Decision Tables dojo.require("com.ibm.rules.bdsl.dt.DecisionTable"); dojo.require("com.ibm.rules.bdsl.dt.EditableDecisionTable");

// Rules dojo.require("com.ibm.bdsl.web.editor.dojo.DojoIntelliTextEditorDecorated");

// Create a decision table editor dojo.xhrPost({ url: '${pageContext.request.contextPath}/WebDTEditor/command/create', handleAs: 'json', preventCache: true, load: function (response) { if (response) { var dt = new com.ibm.rules.bdsl.dt.EditableDecisionTable({ id: 'dt', model: response, baseUrl:'${pageContext.request.contextPath}/WebDTEditor/' }, dojo.create('div', null, 'gridPanel')); dt.startup(); dt.getSelectionService().selectCells(0, 1); } } });

Page 19: Rules SDK IBM WW BPM Forum March 2013

19 © 2013 IBM Corporation

Web DT Editor Servletpublic class WebDtEditorServlet extends DTEditorServlet {

public static final String ENV_ATTRIBUTE_NAME = "dtEditorEnv";

@Overrideprotected DTEditorEnvironment getDtEnvironment(HttpServletRequest request) {

HttpSession session = request.getSession();WebDTEditorEnvironment env = (WebDTEditorEnvironment) session

.getAttribute(WebDtEditorServlet.ENV_ATTRIBUTE_NAME);if (env == null) {

session.setAttribute(WebDtEditorServlet.ENV_ATTRIBUTE_NAME,env = new WebDTEditorEnvironment());

}return env;

}

@Overrideprotected IlrDTModel getModelTemplate(HttpServletRequest request) {

DTEditorEnvironment env = (DTEditorEnvironment) request.getSession().getAttribute(WebDtEditorServlet.ENV_ATTRIBUTE_NAME);

if (env != null) {DTHandle dtHandle = env.getDTHandle();if (dtHandle != null) {

return dtHandle.getModelTemplate();}

}return null;

}

@Overrideprotected IlrBRLDefinition getBALDefinition(HttpServletRequest request) {

return IlrBAL.getDefinition(new Locale(SessionUtils.DEFAULT_LOCALE));}

@Overrideprotected IlrBRLParserService getBALParser(HttpServletRequest request) {

return SessionUtils.getRuleLanguageService(request.getSession()).getParserManager();

}}

Page 20: Rules SDK IBM WW BPM Forum March 2013

20 © 2013 IBM Corporation

Any Questions?