PageRenderTime 25ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/drools-examples/src/main/java/org/drools/examples/datadriventemplate/DataDrivenTemplateExample.java

https://github.com/esteban-aliverti/drools
Java | 184 lines | 139 code | 17 blank | 28 comment | 0 complexity | 43436326b6387b4859317b4db59a5e47 MD5 | raw file
  1. /*
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.examples.datadriventemplate;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import org.drools.examples.templates.ActivityType;
  22. import org.drools.examples.templates.FeeScheduleRule;
  23. import org.drools.examples.templates.FeeScheduleType;
  24. import org.drools.examples.templates.FeeType;
  25. import org.drools.examples.templates.ProductType;
  26. import org.drools.template.DataProvider;
  27. import org.drools.template.DataProviderCompiler;
  28. /**
  29. * This example shows how to use Data-driven rule templates. It assumes that the FeeScheduleRule
  30. * objects have been retrieved from a database using some form of Object-Relational Mapper (such
  31. * as Hibernate or Toplink).
  32. *
  33. * An alternative is to use the ResultSetGenerator from the org.drools.templates.jdbc package, where you can use
  34. * vanilla JDBC to generate rules from (you just select the table names as the field named from the template) - this means no ORM
  35. * is required.
  36. *
  37. * Some things to note:
  38. * - at the moment the templates require all parameters to come in a Strings
  39. * - any row that references a parameter that is empty will not be generated.
  40. */
  41. public class DataDrivenTemplateExample {
  42. private class TestDataProvider
  43. implements
  44. DataProvider {
  45. private Iterator<FeeScheduleRule> iterator;
  46. TestDataProvider(List<FeeScheduleRule> rows) {
  47. this.iterator = rows.iterator();
  48. }
  49. public boolean hasNext() {
  50. return iterator.hasNext();
  51. }
  52. public String[] next() {
  53. FeeScheduleRule nextRule = iterator.next();
  54. String[] row = new String[]{ String.valueOf( nextRule.getFeeEventId() ),
  55. nextRule.getType().getCode(),
  56. nextRule.getEntityBranch(),
  57. nextRule.getProductType().getCode(),
  58. nextRule.getActivityType().getName(),
  59. nextRule.getFeeType().getCode(),
  60. nextRule.getOwningParty(),
  61. nextRule.getCurrency(),
  62. nextRule.getComparator(),
  63. String.valueOf( nextRule.getCompareAmount() ),
  64. String.valueOf( nextRule.getAmount() ),
  65. String.valueOf( nextRule.isLogEvent() )};
  66. return row;
  67. }
  68. }
  69. public static void main(String[] args) {
  70. DataDrivenTemplateExample example = new DataDrivenTemplateExample();
  71. example.testCompiler();
  72. }
  73. public void testCompiler() {
  74. ArrayList<FeeScheduleRule> rules = new ArrayList<FeeScheduleRule>();
  75. FeeScheduleType standard = new FeeScheduleType( "STANDARD" );
  76. FeeScheduleType flat = new FeeScheduleType( "FLAT" );
  77. ProductType sblc = new ProductType( "SBLC" );
  78. ProductType rrc = new ProductType( "RRC" );
  79. ActivityType iss = new ActivityType( "ISS" );
  80. ActivityType osx = new ActivityType( "OSX" );
  81. FeeType commission = new FeeType( "Commission" );
  82. FeeType postage = new FeeType( "Postage" );
  83. FeeType telex = new FeeType( "Telex" );
  84. rules.add( createRule( 1,
  85. flat,
  86. "",
  87. sblc,
  88. iss,
  89. commission,
  90. "Party 1",
  91. "USD",
  92. "",
  93. 0,
  94. 750,
  95. true ) );
  96. rules.add( createRule( 2,
  97. standard,
  98. "Entity Branch 1",
  99. rrc,
  100. iss,
  101. commission,
  102. "",
  103. "YEN",
  104. "",
  105. 0,
  106. 1600,
  107. false ) );
  108. rules.add( createRule( 3,
  109. standard,
  110. "",
  111. sblc,
  112. iss,
  113. postage,
  114. "",
  115. "YEN",
  116. "",
  117. 0,
  118. 40,
  119. true ) );
  120. rules.add( createRule( 4,
  121. flat,
  122. "",
  123. sblc,
  124. osx,
  125. telex,
  126. "",
  127. "YEN",
  128. "<",
  129. 30000,
  130. 45,
  131. false ) );
  132. TestDataProvider tdp = new TestDataProvider( rules );
  133. final DataProviderCompiler converter = new DataProviderCompiler();
  134. final String drl = converter.compile( tdp,
  135. getTemplate() );
  136. System.out.println( drl );
  137. }
  138. private InputStream getTemplate() {
  139. return DataDrivenTemplateExample.class.getResourceAsStream( "FeeScheduleRules.drt" );
  140. }
  141. private FeeScheduleRule createRule(long feeEventId,
  142. FeeScheduleType type,
  143. String entityBranch,
  144. ProductType productType,
  145. ActivityType activityType,
  146. FeeType feeType,
  147. String owningParty,
  148. String currency,
  149. String comparator,
  150. long compareAmount,
  151. long amount,
  152. boolean logEvent) {
  153. FeeScheduleRule rule = new FeeScheduleRule( feeEventId,
  154. activityType,
  155. productType,
  156. type,
  157. feeType,
  158. owningParty,
  159. entityBranch,
  160. comparator,
  161. compareAmount,
  162. amount,
  163. currency,
  164. logEvent );
  165. return rule;
  166. }
  167. }