PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drools-compiler/src/main/resources/org/drools/rule/builder/dialect/java/javaInvokers.mvel

https://github.com/mdproctor/drools
Unknown | 415 lines | 334 code | 81 blank | 0 comment | 0 complexity | 8e609a75824059d0ad80fb939c7bb6de MD5 | raw file
Possible License(s): Apache-2.0
  1. @declare{"getMethodBytecode"}
  2. public java.util.List getMethodBytecode() {
  3. return org.drools.rule.Rule.getMethodBytecode(getClass(), "@{ruleClassName}", "@{package}", "@{methodName}", "@{ (package + '.' + ruleClassName).replace( '.', '/' ) + '.class' }" );
  4. }
  5. @end{}
  6. @declare{"equals"}
  7. public boolean equals(Object object) {
  8. if ( object == null || !(object instanceof org.drools.spi.CompiledInvoker) ) {
  9. return false;
  10. }
  11. return org.drools.core.util.asm.MethodComparator.compareBytecode( getMethodBytecode(), (( org.drools.spi.CompiledInvoker ) object).getMethodBytecode() );
  12. }
  13. @end{}
  14. @declare{"hashCode"}
  15. public int hashCode() {
  16. return @{hashCode};
  17. }
  18. @end{}
  19. @declare{"replaceDeclaration"}
  20. public void replaceDeclaration(org.drools.rule.Declaration declaration,
  21. org.drools.rule.Declaration resolved) {
  22. // this class does not cache declarations
  23. }
  24. @end{}
  25. @declare{"returnValueInvoker"}
  26. package @{package};
  27. public class @{invokerClassName} implements org.drools.spi.ReturnValueExpression, org.drools.spi.CompiledInvoker
  28. {
  29. private static final long serialVersionUID = 510l;
  30. public Object createContext() { return null; }
  31. public org.drools.spi.FieldValue evaluate(java.lang.Object object,
  32. org.drools.spi.Tuple tuple,
  33. org.drools.rule.Declaration[] previousDeclarations,
  34. org.drools.rule.Declaration[] localDeclarations,
  35. org.drools.WorkingMemory workingMemory,
  36. Object context ) throws Exception {
  37. @code{i0=0}@foreach{declr : declarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) previousDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( previousDeclarations[@{i0}] ) ).getObject() );
  38. @code{i0++}
  39. @end{}
  40. @if{readLocalsFromTuple}
  41. @code{i0=0}@foreach{declr : localDeclarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) localDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( localDeclarations[@{i0}] ) ).getObject() );
  42. @code{i0++}
  43. @end{}
  44. @else{}
  45. @code{i0=0}@foreach{declr : localDeclarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) localDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
  46. @code{i0++}
  47. @end{}
  48. @end{}
  49. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  50. @end{}
  51. return @{ruleClassName}.@{methodName}(
  52. @foreach{declr : declarations} @{declr.identifier}
  53. @end{","} @if{localDeclarations != empty && declarations != empty},@end{}
  54. @foreach{declr :localDeclarations} @{declr.identifier}
  55. @end{","}@if{globals != empty && (localDeclarations != empty || declarations != empty)},@end{}
  56. @foreach{identifier : globals}@{identifier}
  57. @end{","} );
  58. }
  59. @includeNamed{"replaceDeclaration"}
  60. @includeNamed{"hashCode"}
  61. @includeNamed{"getMethodBytecode"}
  62. @includeNamed{"equals"}
  63. }
  64. @end{}
  65. @declare{"predicateInvoker"}
  66. package @{package};
  67. public class @{invokerClassName} implements org.drools.spi.PredicateExpression, org.drools.spi.CompiledInvoker
  68. {
  69. private static final long serialVersionUID = 510l;
  70. public Object createContext() { return null; }
  71. public boolean evaluate(java.lang.Object object,
  72. org.drools.spi.Tuple tuple,
  73. org.drools.rule.Declaration[] previousDeclarations,
  74. org.drools.rule.Declaration[] localDeclarations,
  75. org.drools.WorkingMemory workingMemory,
  76. Object context ) throws Exception {
  77. @code{i0=0}@foreach{declr : declarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) previousDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( previousDeclarations[@{i0}] ) ).getObject() );
  78. @code{i0++}
  79. @end{}
  80. @code{i0=0}@foreach{declr : localDeclarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) localDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
  81. @code{i0++}
  82. @end{}
  83. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  84. @end{}
  85. return @{ruleClassName}.@{methodName}(
  86. @foreach{declr : declarations} @{declr.identifier}
  87. @end{","} @if{localDeclarations != empty && declarations != empty},@end{}
  88. @foreach{declr : localDeclarations} @{declr.identifier}
  89. @end{","}@if{globals != empty && (localDeclarations != empty || declarations != empty)},@end{}
  90. @foreach{identifier : globals}@{identifier}
  91. @end{","} );
  92. }
  93. @includeNamed{"hashCode"}
  94. @includeNamed{"getMethodBytecode"}
  95. @includeNamed{"equals"}
  96. }
  97. @end{}
  98. @declare{"evalInvoker"}
  99. package @{package};
  100. public class @{invokerClassName} implements org.drools.spi.EvalExpression, org.drools.spi.CompiledInvoker
  101. {
  102. private static final long serialVersionUID = 510l;
  103. // no need for context
  104. public Object createContext() { return null; }
  105. public boolean evaluate(org.drools.spi.Tuple tuple,
  106. org.drools.rule.Declaration[] declarations,
  107. org.drools.WorkingMemory workingMemory,
  108. Object context ) throws Exception {
  109. @code{i0=0}@foreach{declr : declarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( declarations[@{i0}] ) ).getObject() );
  110. @code{i0++}
  111. @end{}
  112. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  113. @end{}
  114. return @{ruleClassName}.@{methodName}(
  115. @foreach{declr : declarations} @{declr.identifier}
  116. @end{","} @if{globals != empty && declarations != empty},@end{}
  117. @foreach{identifier : globals}@{identifier}
  118. @end{","} );
  119. }
  120. public org.drools.spi.EvalExpression clone() {
  121. // this class is stateless, so no cloning needed
  122. return this;
  123. }
  124. @includeNamed{"replaceDeclaration"}
  125. @includeNamed{"hashCode"}
  126. @includeNamed{"getMethodBytecode"}
  127. @includeNamed{"equals"}
  128. }
  129. @end{}
  130. @declare{"accumulateInvoker"}
  131. package @{package};
  132. import org.mvel2.asm.ClassReader;
  133. import org.mvel2.asm.util.TraceMethodVisitor;
  134. import org.drools.core.util.asm.MethodComparator.Tracer;
  135. import java.util.Collections;
  136. public class @{invokerClassName} implements org.drools.spi.Accumulator, org.drools.spi.CompiledInvoker
  137. {
  138. private static final long serialVersionUID = 510l;
  139. public java.io.Serializable createContext() {
  140. return new @{ruleClassName}.@{className}();
  141. }
  142. public void init(java.lang.Object workingMemoryContext,
  143. java.lang.Object context,
  144. org.drools.spi.Tuple leftTuple,
  145. org.drools.rule.Declaration[] declarations,
  146. org.drools.WorkingMemory workingMemory) throws Exception {
  147. @code{i0=0}@foreach{declr : declarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
  148. @code{i0++}
  149. @end{}
  150. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  151. @end{}
  152. ((@{ruleClassName}.@{className})context).init(
  153. @foreach{declr : declarations} @{declr.identifier}@end{","}@if{globals != empty && declarations != empty},@end{}
  154. @foreach{identifier : globals} @{identifier}@end{","} );
  155. }
  156. public void accumulate(java.lang.Object workingMemoryContext,
  157. java.lang.Object context,
  158. org.drools.spi.Tuple leftTuple,
  159. org.drools.common.InternalFactHandle handle,
  160. org.drools.rule.Declaration[] declarations,
  161. org.drools.rule.Declaration[] innerDeclarations,
  162. org.drools.WorkingMemory workingMemory) throws Exception {
  163. @code{ i0 = 0 }@foreach{declr : declarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
  164. @code{ i0++ }
  165. @end{}
  166. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  167. @end{}
  168. @if{isMultiPattern}
  169. @code{ i0 = 0 }@foreach{declr : innerDeclarations} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( innerDeclarations[@{i0}] ) ).getObject() );
  170. @code{ i0++ }
  171. @end{}
  172. @else{}
  173. @code{ i0 = 0 }@foreach{declr : innerDeclarations} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, handle.getObject() );
  174. @code{ i0++ }
  175. @end{}
  176. @end{}
  177. ((@{ruleClassName}.@{className})context).accumulate(
  178. workingMemory,
  179. handle,
  180. innerDeclarations,
  181. handle.getObject()@if{declarations != empty},@end{}
  182. @foreach{declr : declarations} @{declr.identifier}@end{","}@if{globals != empty},@end{}
  183. @foreach{identifier: globals} @{identifier}@end{","}@if{innerDeclarations != empty},@end{}
  184. @foreach{declr : innerDeclarations} @{declr.identifier}@end{","});
  185. }
  186. public void reverse(java.lang.Object workingMemoryContext,
  187. java.lang.Object context,
  188. org.drools.spi.Tuple leftTuple,
  189. org.drools.common.InternalFactHandle handle,
  190. org.drools.rule.Declaration[] declarations,
  191. org.drools.rule.Declaration[] innerDeclarations,
  192. org.drools.WorkingMemory workingMemory) throws Exception {
  193. @code{ i0 = 0 }@foreach{declr : declarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
  194. @code{ i0++ }
  195. @end{}
  196. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  197. @end{}
  198. @if{isMultiPattern}
  199. @code{ i0 = 0}@foreach{declr : innerDeclarations} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( innerDeclarations[@{i0}] ) ).getObject() );
  200. @code{ i0++ }
  201. @end{}
  202. @else{}
  203. @code{ i0 = 0 }@foreach{declr : innerDeclarations} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, handle.getObject() );
  204. @code{ i0++ }
  205. @end{}
  206. @end{}
  207. ((@{ruleClassName}.@{className})context).reverse(
  208. workingMemory,
  209. handle,
  210. handle.getObject()@if{globals != empty},@end{}
  211. @foreach{identifier : globals} @{identifier}@end{","});
  212. }
  213. public Object getResult(java.lang.Object workingMemoryContext,
  214. java.lang.Object context,
  215. org.drools.spi.Tuple leftTuple,
  216. org.drools.rule.Declaration[] declarations,
  217. org.drools.WorkingMemory workingMemory) throws Exception {
  218. @code{ i0 = 0 }@foreach{declr : declarations} @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
  219. @code{ i0++ }
  220. @end{}
  221. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  222. @end{}
  223. return ((@{ruleClassName}.@{className})context).getResult(
  224. @foreach{declr : declarations} @{declr.identifier}@end{","}@if{globals != empty && declarations != empty},@end{}
  225. @foreach{identifier : globals} @{identifier}@end{","} );
  226. }
  227. public boolean supportsReverse() {
  228. return @{supportsReverse};
  229. }
  230. public Object createWorkingMemoryContext() {
  231. return null;
  232. }
  233. @includeNamed{"hashCode"}
  234. @includeNamed{"equals"}
  235. public java.util.List getMethodBytecode() {
  236. java.io.InputStream is = @{ruleClassName}.class.getClassLoader().getResourceAsStream( "@{package}.@{ruleClassName}".replace( '.', '/' ) + "$@{className}" + ".class" );
  237. java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
  238. byte[] data = new byte[1024];
  239. int byteCount;
  240. try {
  241. while ( (byteCount = is.read( data,
  242. 0,
  243. 1024 )) > -1 )
  244. {
  245. bos.write(data, 0, byteCount);
  246. }
  247. } catch ( java.io.IOException e ) {
  248. throw new org.drools.RuntimeDroolsException("Unable getResourceAsStream for Class '@{ruleClassName}$@{className}' ");
  249. }
  250. return Collections.singletonList( bos );
  251. }
  252. }
  253. @end{}
  254. @declare{"consequenceInvoker"}
  255. package @{package};
  256. public class @{invokerClassName} implements org.drools.spi.Consequence, org.drools.spi.CompiledInvoker
  257. {
  258. private static final long serialVersionUID = 510l;
  259. private final String consequenceName = "@{consequenceName}";
  260. public String getName() {
  261. return this.consequenceName;
  262. }
  263. public void evaluate(org.drools.spi.KnowledgeHelper knowledgeHelper,
  264. org.drools.WorkingMemory workingMemory) throws Exception {
  265. org.drools.spi.Tuple tuple = knowledgeHelper.getTuple();
  266. org.drools.rule.Declaration[] declarations = ((org.drools.reteoo.RuleTerminalNode)knowledgeHelper.getActivation().getTuple().getLeftTupleSink()).getDeclarations();
  267. @foreach{declr : declarations, index : indexes, notPattern : notPatterns}
  268. org.drools.common.InternalFactHandle @{declr.identifier}__Handle__ = ( org.drools.common.InternalFactHandle ) tuple.get( declarations[@{index} ] );
  269. @{declr.typeName} @{declr.identifier} = ( @{declr.boxedTypeName} ) declarations[@{index}].@{declr.nativeReadMethod.name}((org.drools.common.InternalWorkingMemory) workingMemory, @{declr.identifier}__Handle__.getObject() );
  270. @if{notPattern}@{declr.identifier}__Handle__ = (org.drools.common.InternalFactHandle) knowledgeHelper.getWorkingMemory().getFactHandle( @{declr.identifier} );@end{}
  271. @end{}
  272. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  273. @end{}
  274. @{ruleClassName}.@{methodName} (
  275. knowledgeHelper@if{declarations != empty},@end{}
  276. @foreach{declr : declarations} @{declr.identifier}, @{declr.identifier}__Handle__
  277. @end{","}@if{globals != empty},@end{}
  278. @foreach{identifier : globals} @{identifier}
  279. @end{","} );
  280. }
  281. @includeNamed{"hashCode"}
  282. @includeNamed{"getMethodBytecode"}
  283. @includeNamed{"equals"}
  284. }
  285. @end{}
  286. @declare{"actionInvoker"}
  287. package @{package};
  288. public class @{invokerClassName} implements org.drools.spi.Action, org.drools.spi.CompiledInvoker
  289. {
  290. private static final long serialVersionUID = 510l;
  291. public void execute(org.drools.spi.KnowledgeHelper knowledgeHelper,
  292. org.drools.WorkingMemory workingMemory) throws Exception {
  293. execute(knowledgeHelper, workingMemory, null);
  294. }
  295. public void execute(org.drools.spi.KnowledgeHelper knowledgeHelper,
  296. org.drools.WorkingMemory workingMemory, org.drools.spi.ProcessContext context ) throws Exception {
  297. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  298. @end{}
  299. @foreach{type : variableTypes, identifier : variables} @{type} @{identifier} = ( @{type} ) context.getVariable( "@{identifier}" );
  300. @end{}
  301. @{processClassName}.@{methodName} (
  302. knowledgeHelper@if{globals != empty},@end{}
  303. @foreach{identifier : globals} @{identifier}
  304. @end{","}@if{variables != empty},@end{}
  305. @foreach{identifier : variables} @{identifier}
  306. @end{","}, context );
  307. }
  308. public java.util.List getMethodBytecode() {
  309. return null;
  310. }
  311. }
  312. @end{}
  313. @declare{"returnValueEvaluatorInvoker"}
  314. package @{package};
  315. public class @{invokerClassName} implements org.drools.spi.ReturnValueEvaluator, org.drools.spi.CompiledInvoker
  316. {
  317. private static final long serialVersionUID = 510l;
  318. public Object evaluate(org.drools.WorkingMemory workingMemory, org.drools.spi.ProcessContext processContext) throws Exception {
  319. @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
  320. @end{}
  321. @foreach{type : variableTypes, identifier : variables} @{type} @{identifier} = ( @{type} ) processContext.getVariable( "@{identifier}" );
  322. @end{}
  323. return @{processClassName}.@{methodName} (
  324. processContext@if{globals != empty},@end{}
  325. @foreach{identifier : globals} @{identifier}
  326. @end{","}@if{variables != empty},@end{}
  327. @foreach{identifier : variables} @{identifier}
  328. @end{","} );
  329. }
  330. public java.util.List getMethodBytecode() {
  331. return null;
  332. }
  333. }
  334. @end{}