[jboss-svn-commits] JBL Code SVN: r21278 - in labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main: java/org/drools/examples/templates and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jul 29 08:07:47 EDT 2008


Author: stevearoonie
Date: 2008-07-29 08:07:47 -0400 (Tue, 29 Jul 2008)
New Revision: 21278

Added:
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/DataDrivenTemplateExample.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/ActivityType.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeScheduleRule.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeScheduleType.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeType.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/ProductType.java
   labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/FeeScheduleRules.drt
Log:
Add example for Data-driven template

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/DataDrivenTemplateExample.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/DataDrivenTemplateExample.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/DataDrivenTemplateExample.java	2008-07-29 12:07:47 UTC (rev 21278)
@@ -0,0 +1,166 @@
+package org.drools.examples;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.drools.examples.templates.ActivityType;
+import org.drools.examples.templates.FeeScheduleRule;
+import org.drools.examples.templates.FeeScheduleType;
+import org.drools.examples.templates.FeeType;
+import org.drools.examples.templates.ProductType;
+import org.drools.template.DataProvider;
+import org.drools.template.DataProviderCompiler;
+
+/**
+ * This example shows how to use Data-driven rule templates. It assumes that the FeeScheduleRule
+ * objects have been retrieved from a database using some form of Object-Relational Mapper (such
+ * as Hibernate or Toplink).
+ * 
+ * Some things to note:
+ *   - at the moment the templates require all parameters to come in a Strings
+ *   - any row that references a parameter that is empty will not be generated.
+ * @author Steve
+ *
+ */
+public class DataDrivenTemplateExample {
+
+    private class TestDataProvider
+        implements
+        DataProvider {
+
+        private Iterator<FeeScheduleRule> iterator;
+
+        TestDataProvider(List<FeeScheduleRule> rows) {
+            this.iterator = rows.iterator();
+        }
+
+        public boolean hasNext() {
+            return iterator.hasNext();
+        }
+
+        public String[] next() {
+            FeeScheduleRule nextRule = iterator.next();
+            String[] row = new String[]{ String.valueOf( nextRule.getFeeEventId() ),
+                                         nextRule.getType().getCode(), 
+                                         nextRule.getEntityBranch(), 
+                                         nextRule.getProductType().getCode(), 
+                                         nextRule.getActivityType().getName(), 
+                                         nextRule.getFeeType().getCode(), 
+                                         nextRule.getOwningParty(),
+                                         nextRule.getCurrency(), 
+                                         nextRule.getComparator(), 
+                                         String.valueOf( nextRule.getCompareAmount() ), 
+                                         String.valueOf( nextRule.getAmount() ),
+                                         String.valueOf( nextRule.isLogEvent() )};
+            return row;
+        }
+
+    }
+
+    public static void main(String[] args) throws Exception {
+        DataDrivenTemplateExample example = new DataDrivenTemplateExample();
+        example.testCompiler();
+    }
+
+    public void testCompiler() throws Exception {
+        ArrayList<FeeScheduleRule> rules = new ArrayList<FeeScheduleRule>();
+        FeeScheduleType standard = new FeeScheduleType( "STANDARD" );
+        FeeScheduleType flat = new FeeScheduleType( "FLAT" );
+        ProductType sblc = new ProductType( "SBLC" );
+        ProductType rrc = new ProductType( "RRC" );
+        ActivityType iss = new ActivityType( "ISS" );
+        ActivityType osx = new ActivityType( "OSX" );
+        FeeType commission = new FeeType( "Commission" );
+        FeeType postage = new FeeType( "Postage" );
+        FeeType telex = new FeeType( "Telex" );
+
+        rules.add( createRule( 1,
+                               flat,
+                               "",
+                               sblc,
+                               iss,
+                               commission,
+                               "Party 1",
+                               "USD",
+                               "",
+                               0,
+                               750,
+                               true ) );
+        rules.add( createRule( 2,
+                               standard,
+                               "Entity Branch 1",
+                               rrc,
+                               iss,
+                               commission,
+                               "",
+                               "YEN",
+                               "",
+                               0,
+                               1600,
+                               false ) );
+        rules.add( createRule( 3,
+                               standard,
+                               "",
+                               sblc,
+                               iss,
+                               postage,
+                               "",
+                               "YEN",
+                               "",
+                               0,
+                               40,
+                               true ) );
+        rules.add( createRule( 4,
+                               flat,
+                               "",
+                               sblc,
+                               osx,
+                               telex,
+                               "",
+                               "YEN",
+                               "<",
+                               30000,
+                               45,
+                               false ) );
+        TestDataProvider tdp = new TestDataProvider( rules );
+        final DataProviderCompiler converter = new DataProviderCompiler();
+        final String drl = converter.compile( tdp,
+                                              getTemplate() );
+        System.out.println( drl );
+
+    }
+
+    private InputStream getTemplate() {
+        return DataDrivenTemplateExample.class.getResourceAsStream( "FeeScheduleRules.drt" );
+    }
+
+    private FeeScheduleRule createRule(long feeEventId,
+                                       FeeScheduleType type,
+                                       String entityBranch,
+                                       ProductType productType,
+                                       ActivityType activityType,
+                                       FeeType feeType,
+                                       String owningParty,
+                                       String currency,
+                                       String comparator,
+                                       long compareAmount,
+                                       long amount,
+                                       boolean logEvent) {
+        FeeScheduleRule rule = new FeeScheduleRule( feeEventId,
+                                                    activityType,
+                                                    productType,
+                                                    type,
+                                                    feeType,
+                                                    owningParty,
+                                                    entityBranch,
+                                                    comparator,
+                                                    compareAmount,
+                                                    amount,
+                                                    currency,
+                                                    logEvent );
+        return rule;
+    }
+
+}

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/ActivityType.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/ActivityType.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/ActivityType.java	2008-07-29 12:07:47 UTC (rev 21278)
@@ -0,0 +1,27 @@
+package org.drools.examples.templates;
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class ActivityType {
+    private String name;
+
+    public ActivityType(String name) {
+        this.name = name;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeScheduleRule.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeScheduleRule.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeScheduleRule.java	2008-07-29 12:07:47 UTC (rev 21278)
@@ -0,0 +1,93 @@
+package org.drools.examples.templates;
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class FeeScheduleRule {
+    private ActivityType activityType;
+    private ProductType productType;
+    private FeeScheduleType type;
+    private FeeType feeType;
+    private String owningParty;
+    private String entityBranch;
+    private String comparator;
+    private long compareAmount;
+    private long amount;
+    private String currency;
+    private boolean logEvent;
+    private long feeEventId;
+    public FeeScheduleRule(long feeEventId,
+                           ActivityType activityType,
+                           ProductType productType,
+                           FeeScheduleType type,
+                           FeeType feeType,
+                           String owningParty,
+                           String entityBranch,
+                           String comparator,
+                           long compareAmount,
+                           long amount,
+                           String currency, 
+                           boolean logEvent) {
+        this.feeEventId = feeEventId;
+        this.activityType = activityType;
+        this.productType = productType;
+        this.type = type;
+        this.feeType = feeType;
+        this.owningParty = owningParty;
+        this.entityBranch = entityBranch;
+        this.comparator = comparator;
+        this.compareAmount = compareAmount;
+        this.amount = amount;
+        this.currency = currency;
+        this.logEvent = logEvent;
+    }
+    public ActivityType getActivityType() {
+        return activityType;
+    }
+    public ProductType getProductType() {
+        return productType;
+    }
+    public FeeScheduleType getType() {
+        return type;
+    }
+    public FeeType getFeeType() {
+        return feeType;
+    }
+    public String getOwningParty() {
+        return owningParty;
+    }
+    public String getEntityBranch() {
+        return entityBranch;
+    }
+    public String getComparator() {
+        return comparator;
+    }
+    public long getAmount() {
+        return amount;
+    }
+    public String getCurrency() {
+        return currency;
+    }
+    public long getCompareAmount() {
+        return compareAmount;
+    }
+    public boolean isLogEvent() {
+        return logEvent;
+    }
+    public long getFeeEventId() {
+        return feeEventId;
+    }
+    
+     
+}

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeScheduleType.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeScheduleType.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeScheduleType.java	2008-07-29 12:07:47 UTC (rev 21278)
@@ -0,0 +1,28 @@
+package org.drools.examples.templates;
+
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class FeeScheduleType {
+    private String code;
+    
+    public FeeScheduleType(String code) {
+        this.code = code;
+    }
+
+    public final String getCode() {
+        return code;
+    }
+}

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeType.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeType.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/FeeType.java	2008-07-29 12:07:47 UTC (rev 21278)
@@ -0,0 +1,27 @@
+package org.drools.examples.templates;
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class FeeType {
+    private String code;
+
+    public FeeType(String code) {
+        this.code = code;
+    }
+
+    public String getCode() {
+        return code;
+    }
+}

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/ProductType.java
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/ProductType.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/java/org/drools/examples/templates/ProductType.java	2008-07-29 12:07:47 UTC (rev 21278)
@@ -0,0 +1,27 @@
+package org.drools.examples.templates;
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class ProductType {
+    private String code;
+
+    public String getCode() {
+        return code;
+    }
+
+    public ProductType(String code) {
+        this.code = code;
+    }
+}

Added: labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/FeeScheduleRules.drt
===================================================================
--- labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/FeeScheduleRules.drt	                        (rev 0)
+++ labs/jbossrules/trunk/drools-examples/drools-examples-drl/src/main/rules/org/drools/examples/FeeScheduleRules.drt	2008-07-29 12:07:47 UTC (rev 21278)
@@ -0,0 +1,49 @@
+template header
+FEE_EVENT_ID
+FEE_SCHEDULE_TYPE
+ENTITY_BRANCH
+PRODUCT_TYPE
+ACTIVITY_TYPE
+FEE_TYPE
+OWNING_PARTY
+CCY
+COMPARATOR
+LC_AMOUNT
+AMOUNT
+LOG_EVENT: Boolean
+
+package org.drools.examples;
+
+global FeeResult result;
+global Logger logger;
+
+template "Fee Schedule"
+rule "Fee Schedule_@{row.rowNumber}"
+	agenda-group "@{FEE_SCHEDULE_TYPE}"
+	
+	when
+		FeeEvent(productType.code == "@{PRODUCT_TYPE}",
+			activityType.name == "@{ACTIVITY_TYPE}",
+			feeType.code == "@{FEE_TYPE}",
+			txParty.name == "@{OWNING_PARTY}",
+			entityBranch == "@{ENTITY_BRANCH}",
+			amount.value @{COMPARATOR} @{LC_AMOUNT},
+			amount.currency == "@{CCY}"
+		)
+	then
+		result.setSchedule(new FeeSchedule("@{FEE_SCHEDULE_TYPE}", @{AMOUNT}));
+end
+end template
+
+template "Log use"
+LOG_EVENT == true
+rule "Log Event @{row.rowNumber}"
+	when
+		FeeEvent(
+			id == @{FEE_EVENT_ID},
+			$activity: activityType.name
+		)
+	then
+		logger.addLogging($activity);
+end
+end template
\ No newline at end of file




More information about the jboss-svn-commits mailing list