ConditionExpression.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. Namespace.register("com.hotent.platform.system");
  2. /**
  3. * 表达式计算。
  4. * var con=com.hotent.platform.system.ConditionExpression();
  5. * 计算节点
  6. * con.evaluate(node);
  7. * 获取结果
  8. * con.getResult();
  9. * 清除计算
  10. * con.clear();
  11. * @returns {com.hotent.platform.system.ConditionExpression}
  12. */
  13. com.hotent.platform.system.ConditionExpression = function(){
  14. {
  15. this.sb=new StringBuffer();
  16. this.hasError=false;
  17. this.index=0;
  18. }
  19. /**
  20. * 清除重新计算
  21. */
  22. this.reset=function(){
  23. this.sb=new StringBuffer();
  24. this.hasError=false;
  25. window.status="";
  26. };
  27. /**
  28. * 获取结果
  29. */
  30. this.getResult=function(){
  31. if(this.hasError){
  32. return "当前构建条件不符合规则,请仔细检查表达式!";
  33. }
  34. return this.sb.toString();
  35. };
  36. // this.getNextNode=function(node){
  37. // node.hasNextNode=false;
  38. // var nextNode=node.getNextNode();
  39. // if(!nextNode){
  40. // return;
  41. // }
  42. // if(nextNode.type=="3"){
  43. // node.hasNextNode= true;
  44. // return;
  45. // }
  46. // this.recurNode(node,nextNode);
  47. //
  48. // };
  49. //
  50. //
  51. //
  52. // this.recurNode=function(root,node){
  53. // var childs=node.childs;
  54. // if(!childs) return;
  55. //
  56. // for(var i=0;i<childs.length;i++){
  57. // var tNode=childs[i];
  58. // if(tNode.type=="3"){
  59. // root.hasNextNode=true;
  60. // break;
  61. // }
  62. // else{
  63. // if(tNode.childs){
  64. // this.recurNode(root,tNode);
  65. // }
  66. // }
  67. // }
  68. // };
  69. this.validNode=function(node){
  70. var type=node.type;
  71. var childs=node.children;
  72. //当前节点的类型
  73. var parentNode=node.getParentNode();
  74. var preNode=node.getPreNode();
  75. var nextNode=node.getNextNode();
  76. //当前节点为条件表达式节点
  77. if(type=="3" ){
  78. if(parentNode){
  79. return;
  80. }
  81. //有下一个节点
  82. if(nextNode){
  83. //当前节点为条件节点,下一个节点是条件节点
  84. if(nextNode.type=="3"){
  85. this.hasError=true;
  86. window.status=("type 3 nextNode.type=='3'");
  87. return;
  88. }
  89. //下一个节点为条件节点
  90. else{
  91. //条件节点有子节点
  92. if(nextNode.children && nextNode.children.length>0)
  93. {
  94. window.status=("type 3 nextNode.children");
  95. this.hasError=true;
  96. return;
  97. }
  98. }
  99. }
  100. //有前一个节点
  101. if(preNode){
  102. if(preNode.type=="3"){
  103. this.hasError=true;
  104. window.status=("type 3 preNode.type=='3'");
  105. return;
  106. }
  107. //下一个节点为条件节点
  108. else{
  109. //条件节点有子节点
  110. if(preNode.children && preNode.children.length>0)
  111. {
  112. this.hasError=true;
  113. window.status=("type 3 preNode.children");
  114. return;
  115. }
  116. }
  117. }
  118. }
  119. //条件节点,有子节点
  120. else if(type!="3" && childs && childs.length>0){
  121. if(parentNode){
  122. return;
  123. }
  124. //有下一个节点
  125. if(nextNode){
  126. //当前节点为条件节点,下一个节点是条件表达式节点
  127. if(nextNode.type=="3"){
  128. this.hasError=true;
  129. window.status=("条件节点,有子节点 nextNode.type=='3'" +node.name +"," +node.id);
  130. return;
  131. }
  132. //下一个节点为条件节点
  133. else{
  134. //条件节点有子节点
  135. if(nextNode.children && nextNode.children.length>0)
  136. {
  137. window.status=(node.id +"," + node.name + "条件节点,有子节点 nextNode.children");
  138. this.hasError=true;
  139. return;
  140. }
  141. }
  142. }
  143. //有前一个节点
  144. if(preNode){
  145. if(preNode.type=="3"){
  146. this.hasError=true;
  147. window.status=("条件节点,有子节点 preNode.type=='3'");
  148. return;
  149. }
  150. //下一个节点为条件节点
  151. else{
  152. //条件节点有子节点
  153. if(preNode.children && preNode.children.length>0)
  154. {
  155. window.status=("条件节点,有子节点 preNode.children");
  156. this.hasError=true;
  157. return;
  158. }
  159. }
  160. }
  161. }
  162. //条件节点 ,当前节点无子节点
  163. else
  164. {
  165. //没有下面的节点
  166. if(nextNode==null){
  167. window.status=("条件节点 ,当前节点无子节点 没有下面的节点," + childs + "," +node.name +","+ node.id);
  168. this.hasError=true;
  169. return;
  170. }
  171. //有下面的节点
  172. else{
  173. //没有子节点说明是一个条件节点
  174. if(nextNode.children==null && nextNode.type!='3'){
  175. window.status=("条件节点 ,nextNode.type!='3' childs:" + childs +"," + node.type +"," + node.name +"," + nextNode.type+","+nextNode.id);
  176. this.hasError=true;
  177. return;
  178. }
  179. }
  180. //没有前一个节点
  181. if(preNode==null){
  182. window.status=("条件节点 ,preNode==null" +node.id +"," +node.name);
  183. this.hasError=true;
  184. return;
  185. }
  186. //前面有的节点
  187. else{
  188. //没有子节点说明是一个条件节点
  189. if(preNode.children==null && preNode.type!='3'){
  190. window.status=("条件节点 ,preNode.childs==null");
  191. this.hasError=true;
  192. return;
  193. }
  194. }
  195. }
  196. };
  197. /**
  198. * 构建条件表达式。
  199. */
  200. this.evaluate=function(ex){
  201. this.validNode(ex);
  202. var childLen=0;
  203. if(ex.children)
  204. childLen=ex.children.length;
  205. if(ex.type=="3"){
  206. this.sb.append( " "+ ex.expression +" ");
  207. }
  208. else if((ex.type=="1"|| ex.type=="2") && childLen==0)
  209. {
  210. // this.getNextNode(ex);
  211. // var rtn=ex.hasNextNode;
  212. // if(rtn)
  213. this.sb.append(" "+ex.typeName+" ");
  214. }
  215. else if((ex.type=="1" || ex.type=="2") && childLen>0)
  216. {
  217. this.sb.append("(");
  218. var childList=ex.children;
  219. var len=childList.length;
  220. for(var i=0;i<len;i++){
  221. var child=childList[i];
  222. this.validNode(child);
  223. var tmpLen=0;
  224. if(child.children)
  225. tmpLen=child.children.length;
  226. if(child.type!="3"){
  227. if((ex.type=="1" || ex.type=="2") && tmpLen==0){
  228. if(i>0 && i<len-1)
  229. this.sb.append(" " +child.typeName +" ");
  230. }
  231. else{
  232. this.sb.append(" " +ex.typeName +" ");
  233. //递归
  234. this.evaluate(child);
  235. }
  236. }
  237. else{
  238. if(i>0 && i<len)
  239. this.sb.append(" " + ex.typeName +" " +" "+ child.expression +" " );
  240. else
  241. this.sb.append(" "+ child.expression +" " );
  242. }
  243. }
  244. this.sb.append(")");
  245. }
  246. };
  247. /**
  248. * 生成节点。
  249. */
  250. this.genNode=function(type,expression){
  251. this.index++;
  252. var ctx=__ctx;
  253. var type=$("input[name=action]:checked").val();
  254. var txt=expression;
  255. var node=new Object();
  256. node.expression="";
  257. node.id=this.index;
  258. var typeName="";
  259. //1 或2 并 3,条件
  260. switch(type){
  261. case "1":
  262. node.name="OR(或)";
  263. typeName="Or";
  264. node.icon=ctx+ "/themes/img/commons/or.gif";
  265. node.image="/themes/img/commons/or.gif";
  266. break;
  267. case "2":
  268. node.name="AND(和)";
  269. typeName="And";
  270. node.icon=ctx+ "/themes/img/commons/and.gif";
  271. node.image="/themes/img/commons/and.gif";
  272. break;
  273. case "3":
  274. node.name=txt;
  275. node.icon=ctx+ "/themes/img/commons/code.gif";
  276. node.expression=txt;
  277. node.image="/themes/img/commons/code.gif";
  278. break;
  279. }
  280. node.type=type;
  281. node.typeName=typeName;
  282. return node;
  283. };
  284. };