| |
"Does Your Project Need a Rule Engine"
Vol. 9, Issue 6, p. 41
Listing 1
1 public void init(HttpServletRequest request)
2 {
3 // Initialize the ruleserver
4 m_ruleRuntime = new Rules4JServer(System.getProperty("DataDirectory"),
"rules4JServer.xml");
5
6 person = getPersonData();
7 m_currentRuleSetName = "TestServer:Test:EligibilityRuleset";
8 // Get a rulesession object from the ruleserver
9 m_currentRuleSession = (StatefulRuleSession) m_ruleServer.createRuleSession
(m_currentRuleSetName, null, RuleRuntime.STATEFUL_SESSION_TYPE);
10
11 List personList = new ArrayList();
12 personList.add(person);
13 // Pass the person object to the rule engine
14 m_currentRuleSession.addObjects(personList);
15 // execute the rules in the rulesession
16 m_currentRuleSession.executeRules();
17 personList = m_currentRuleSession.getObjects();
18 m_currentRuleSession.reset();
19
20 }
21
22 private RuleRuntime m_ruleRuntime = null;
23 private Collection m_ruleSetNames = null;
24 private String m_currentRuleSetName = "";
25 private com.rules4j.RuleSet m_currentRuleSet = null;
26 private com.rules4j.StatefulRuleSession m_currentRuleSession = null;
Listing 2
1 public void install(Installparams params)
2 {
3 if (params.getContainerType().equals("Tomcat 4.1.27") ) {
4 m_osCommand.copyDir("linux/war_files", workingDir);
5 // Modify InsuranceDemoPage.java to point to the correct rulebase directory
6 m_osCommand.replaceString("<data directory>",
params.getRuleBaseDirectory(),
"com/rules4j/demo/InsuranceDemoPage.java");
7 // Compile InsuranceDemoPage.java
8 m_osCommand.compileJavaFile("build",
"com/rules4j/demo/InsuranceDemoPage.java");
10
11 } else if (params.getContainerType().equals("WebLogic 8.1")) {
12 m_osCommand.copyDir("linux/ear_files", workingDir);
13 // Modify InsuranceDemo.java to contain the correct port and connection
properties
14 // replace <context1>, <context2>
15 m_osCommand.replaceString("<context1>",
"p.put(Context.INITIAL_CONTEXT_FACTORY,
\"weblogic.jndi.WLInitialContextFactory\");");
16 m_osCommand.replaceString("<context2>",
"p.put(Context.PROVIDER_URL, \"t3://localhost:80\");");
17 // Compile InsuranceDemo.java
18 m_osCommand.compileJavaFile("build",
"com/rules4j/demo/InsuranceDemo.java");
19
20 } else if (params.getContainerType().equals("JBoss 3.3.2")) {
21 m_osCommand.copyDir("linux/ear_files", workingDir);
22 // Modify InsuranceDemo.java to contain the correct port and connection
properties
23 m_osCommand.replaceString("<context1>",
"p.put(Context.INITIAL_CONTEXT_FACTORY,
\"org.jnp.interfaces.NamingContextFactory\");");
24 m_osCommand.replaceString("<context2>",
"p.put(Context.URL_PKG_PREFIXES,
\"jboss.naming:org.jnp.interfaces\");");
25 m_osCommand.replaceString("<context3>",
"p.put(Context.PROVIDER_URL, \"localhost:1099\");");
26 // Compile InsuranceDemo.java
27 m_osCommand.compileJavaFile("build",
"com/rules4j/demo/InsuranceDemo.java");
28 }
29 }
Listing 3
1 <?xml version="1.0" encoding="UTF-8"?>
2 <Rulebase xmlns="http://xml.netbeans.org/examples/targetNS">
3 <RuleBaseName>demo_install</RuleBaseName>
4 <Ruleset>
5 <RuleSetName>BuildRules</RuleSetName>
6 <Rule>
7 <RuleName>Container Type rule</RuleName>
8 <If>
9 <Condition LogicalType="and">
10 <Expression>
11 <ObjectId>this</ObjectId>
12 <Attribute Type="string">containerType</Attribute>
13 <EqualTo>Tomcat 4.1.27</EqualTo>
14 </Expression>
15 </Condition>
16 <Then>
17 <Do>
18 <DoExpression>
19 <ObjectId>OSCommand</ObjectId>
20 <Method>copyDir</Method>
21 <Param Type="string">linux/war_files</Param>
22 <Param>
23 <ParamExpression>
24 <ObjectId>WorkingDirectory</ObjectId>
25 <Attribute Type="string">value</Attribute>
26 </ParamExpression>
27 </Param>
28 </DoExpression>
29 <DoExpression>
30 <ObjectId>OSCommand</ObjectId>
31 <Method>replaceString</Method>
32 <Param Type="string"><data directory></Param>
33 <Param>
34 <ParamExpression>
35 <ObjectId>this</ObjectId>
36 <Attribute Type="string">ruleBaseDirectory</Attribute>
37 </ParamExpression>
38 </Param>
39 <Param
Type="string">com/rules4j/demo/InsuranceDemoPage.java</Param>
40 </DoExpression>
41 <DoExpression>
42 <ObjectId>OSCommand</ObjectId>
43 <Method>compileJavaFile</Method>
44 <Param Type="string">build</Param>
45 <Param
Type="string">com/rules4j/demo/InsuranceDemoPage.java</Param>
46 </DoExpression>
47 </Do>
48 </Then>
49 </If>
50
51 <Spare_you_all_the_details>you get the
idea</Spare_you_all_the_details>
52
53 </Rule>
54 </Ruleset>
55 </Rulebase>
Listing 4
1 public void install(Object params)
2 {
3 String currentRuleSetName = "Installer:demo_install:BuildRules";
4 // Get a rulesession object from the ruleserver
5 StatefulRuleSession ruleSession =
(StatefulRuleSession) m_ruleServer.createRuleSession
(currentRuleSetName, null, RuleRuntime.STATEFUL_SESSION_TYPE);
7
8 List paramList = new ArrayList();
9 personList.add(params);
10 // Pass the person object to the rule engine
11 ruleSession.addObjects(paramList);
12
13 // Load a reference to the OSCommand class into the rule engine
14 OSCommand osCommand = new OSCommand();
15 RuleSession.loadResourceObject("OSCommand", osCommand);
16 // Load a reference to the workingDir variable into the rule engine
17 RuleSession.loadResourceObject("WorkingDirectory", workingDir);
18
19 // execute the rules in the rulesession
20 ruleSession.executeRules();
21 ruleSession.reset();
22
23 }
|
|