/jbpm-flow/src/main/java/org/jbpm/process/instance/impl/ReturnValueConstraintEvaluator.java

https://github.com/mariofusco/jbpm · Java · 172 lines · 120 code · 31 blank · 21 comment · 3 complexity · ec4cfc8162e5b40e19d7813cd8cc8aea MD5 · raw file

  1. /**
  2. * Copyright 2005 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.jbpm.process.instance.impl;
  17. import java.io.Externalizable;
  18. import java.io.IOException;
  19. import java.io.ObjectInput;
  20. import java.io.ObjectOutput;
  21. import org.kie.api.definition.process.Connection;
  22. import org.drools.core.spi.CompiledInvoker;
  23. import org.drools.core.spi.ProcessContext;
  24. import org.drools.core.spi.Wireable;
  25. import org.jbpm.process.instance.ProcessInstance;
  26. import org.jbpm.workflow.core.Constraint;
  27. import org.jbpm.workflow.instance.NodeInstance;
  28. /**
  29. * Default implementation of a constraint.
  30. *
  31. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  32. */
  33. public class ReturnValueConstraintEvaluator
  34. implements
  35. Constraint,
  36. ConstraintEvaluator,
  37. Wireable,
  38. Externalizable {
  39. private static final long serialVersionUID = 510l;
  40. private String name;
  41. private String constraint;
  42. private int priority;
  43. private String dialect;
  44. private String type;
  45. private boolean isDefault = false;
  46. public ReturnValueConstraintEvaluator() {
  47. }
  48. private ReturnValueEvaluator evaluator;
  49. public String getConstraint() {
  50. return this.constraint;
  51. }
  52. public void setConstraint(final String constraint) {
  53. this.constraint = constraint;
  54. }
  55. public String getName() {
  56. return this.name;
  57. }
  58. public void setName(final String name) {
  59. this.name = name;
  60. }
  61. public String toString() {
  62. return this.name;
  63. }
  64. public int getPriority() {
  65. return this.priority;
  66. }
  67. public void setPriority(final int priority) {
  68. this.priority = priority;
  69. }
  70. public String getDialect() {
  71. return dialect;
  72. }
  73. public void setDialect(String dialect) {
  74. this.dialect = dialect;
  75. }
  76. public String getType() {
  77. return type;
  78. }
  79. public void setType(String type) {
  80. this.type = type;
  81. }
  82. public boolean isDefault() {
  83. return isDefault;
  84. }
  85. public void setDefault(boolean isDefault) {
  86. this.isDefault = isDefault;
  87. }
  88. public void wire(Object object) {
  89. setEvaluator( (ReturnValueEvaluator) object );
  90. }
  91. public void setEvaluator(ReturnValueEvaluator evaluator) {
  92. this.evaluator = evaluator;
  93. }
  94. public ReturnValueEvaluator getReturnValueEvaluator() {
  95. return this.evaluator;
  96. }
  97. public boolean evaluate(NodeInstance instance,
  98. Connection connection,
  99. Constraint constraint) {
  100. Object value;
  101. try {
  102. ProcessContext context = new ProcessContext(((ProcessInstance)instance.getProcessInstance()).getKnowledgeRuntime());
  103. context.setNodeInstance( instance );
  104. value = this.evaluator.evaluate( context );
  105. } catch ( Exception e ) {
  106. throw new RuntimeException( "unable to execute ReturnValueEvaluator: ",
  107. e );
  108. }
  109. if ( !(value instanceof Boolean) ) {
  110. throw new RuntimeException( "Constraints must return boolean values: " + value + " for expression " + constraint);
  111. }
  112. return ((Boolean) value).booleanValue();
  113. }
  114. public void readExternal(ObjectInput in) throws IOException,
  115. ClassNotFoundException {
  116. this.evaluator = (ReturnValueEvaluator) in.readObject();
  117. this.name = in.readUTF();
  118. this.constraint = (String) in.readObject();
  119. this.priority = in.readInt();
  120. this.dialect = in.readUTF();
  121. this.type = (String) in.readObject();
  122. }
  123. public void writeExternal(ObjectOutput out) throws IOException {
  124. if ( this.evaluator instanceof CompiledInvoker ) {
  125. out.writeObject( null );
  126. } else {
  127. out.writeObject( this.evaluator );
  128. }
  129. out.writeUTF( this.name );
  130. out.writeObject( this.constraint );
  131. out.writeInt( this.priority );
  132. out.writeUTF( dialect );
  133. out.writeObject( type );
  134. }
  135. public void setMetaData(String name, Object value) {
  136. // Do nothing
  137. }
  138. public Object getMetaData(String name) {
  139. return null;
  140. }
  141. }