jbpm4.4根据.jpdl.xml流程定义文件,得到流程图.png

前端之家收集整理的这篇文章主要介绍了jbpm4.4根据.jpdl.xml流程定义文件,得到流程图.png前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

jbpm4.4根据.jpdl.xml流程定义文件,得到流程图.png

只需将.jpdl.xml文件传过来,就可以根据这个文件解析出来.png图片

先看节点类的定义:
Java代码
  1. publicclassNode{
  2. privateStringname;
  3. privateStringtype;
  4. privateRectanglerectangle;
  5. privateList<Transition>transitions=newArrayList<Transition>();
  6. publicNode(Stringname,Stringtype){
  7. this.name=name;
  8. this.type=type;
  9. }
  10. publicNode(Stringname,Stringtype,intx,inty,intw,inth){
  11. this.name=name;
  12. this.type=type;
  13. this.rectangle=newRectangle(x,y,w,h);
  14. }
  15. publicRectanglegetRectangle(){
  16. returnrectangle;
  17. }
  18. publicvoidsetRectangle(Rectanglerectangle){
  19. this.rectangle=rectangle;
  20. }
  21. publicStringgetType(){
  22. returntype;
  23. }
  24. publicvoidsetType(Stringtype){
  25. this.type=type;
  26. }
  27. publicStringgetName(){
  28. returnname;
  29. }
  30. publicvoidsetName(Stringname){
  31. this.name=name;
  32. }
  33. publicvoidaddTransition(Transitiontransition){
  34. transitions.add(transition);
  35. }
  36. publicList<Transition>getTransitions(){
  37. returntransitions;
  38. }
  39. publicvoidsetTransitions(List<Transition>transitions){
  40. this.transitions=transitions;
  41. }
  42. publicintgetX(){
  43. returnrectangle.x;
  44. }
  45. publicintgetY(){
  46. returnrectangle.y;
  47. }
  48. publicintgetCenterX(){
  49. return(int)rectangle.getCenterX();
  50. }
  51. publicintgetCenterY(){
  52. return(int)rectangle.getCenterY();
  53. }
  54. publicintgetWitdth(){
  55. returnrectangle.width;
  56. }
  57. publicintgetHeight(){
  58. returnrectangle.height;
  59. }
  60. }


其次是Transition的定义:
Java代码
  1. publicclassTransition{
  2. privatePointlabelPosition;
  3. privateList<Point>lineTrace=newArrayList<Point>();
  4. privateStringlabel;
  5. privateStringto;
  6. publicTransition(Stringlabel,Stringto){
  7. this.label=label;
  8. this.to=to;
  9. }
  10. publicPointgetLabelPosition(){
  11. returnlabelPosition;
  12. }
  13. publicvoidsetLabelPosition(PointlabelPosition){
  14. this.labelPosition=labelPosition;
  15. }
  16. publicList<Point>getLineTrace(){
  17. returnlineTrace;
  18. }
  19. publicvoidsetLineTrace(List<Point>lineTrace){
  20. this.lineTrace=lineTrace;
  21. }
  22. publicvoidaddLineTrace(PointlineTrace){
  23. if(lineTrace!=null){
  24. this.lineTrace.add(lineTrace);
  25. }
  26. }
  27. publicStringgetLabel(){
  28. returnlabel;
  29. }
  30. publicvoidsetLabel(Stringlabel){
  31. this.label=label;
  32. }
  33. publicStringgetTo(){
  34. returnto;
  35. }
  36. publicvoidsetTo(Stringto){
  37. this.to=to;
  38. }
  39. }


类JpdlModel
Java代码
  1. /**
  2. *CopyRight(C)2006-2009yy
  3. *@authoryy
  4. *@projectjbpm
  5. *@version1.0
  6. *@mailyy629_86at163dotcom
  7. *@date2009-9-6下午06:00:14
  8. *@description
  9. */
  10. packagesofocus.bpm.jbpm.jpdl.model;
  11. importjava.awt.Point;
  12. importjava.io.InputStream;
  13. importjava.util.HashMap;
  14. importjava.util.LinkedHashMap;
  15. importjava.util.List;
  16. importjava.util.Map;
  17. importorg.dom4j.Element;
  18. importorg.dom4j.io.SAXReader;
  19. publicclassJpdlModel{
  20. privateMap<String,Node>nodes=newLinkedHashMap<String,Node>();
  21. publicstaticfinalintRECT_OFFSET_X=-7;
  22. publicstaticfinalintRECT_OFFSET_Y=-8;
  23. publicstaticfinalintDEFAULT_PIC_SIZE=48;
  24. privatefinalstaticMap<String,Object>nodeInfos=newHashMap<String,Object>();
  25. static{
  26. nodeInfos.put("start","start_event_empty.png");
  27. nodeInfos.put("end","end_event_terminate.png");
  28. nodeInfos.put("end-cancel","end_event_cancel.png");
  29. nodeInfos.put("end-error","end_event_error.png");
  30. nodeInfos.put("decision","gateway_exclusive.png");
  31. nodeInfos.put("fork","gateway_parallel.png");
  32. nodeInfos.put("join","gateway_parallel.png");
  33. nodeInfos.put("state",null);
  34. nodeInfos.put("hql",null);
  35. nodeInfos.put("sql",null);
  36. nodeInfos.put("java",null);
  37. nodeInfos.put("script",null);
  38. nodeInfos.put("task",null);
  39. nodeInfos.put("sub-process",null);
  40. nodeInfos.put("custom",null);
  41. }
  42. publicJpdlModel(InputStreamis)throwsException{
  43. this(newSAXReader().read(is).getRootElement());
  44. }
  45. @SuppressWarnings("unchecked")
  46. privateJpdlModel(ElementrootEl)throwsException{
  47. for(Elementel:(List<Element>)rootEl.elements()){
  48. Stringtype=el.getQName().getName();
  49. if(!nodeInfos.containsKey(type)){//不是可展示的节点
  50. continue;
  51. }
  52. Stringname=null;
  53. if(el.attribute("name")!=null){
  54. name=el.attributeValue("name");
  55. }
  56. String[]location=el.attributeValue("g").split(",");
  57. intx=Integer.parseInt(location[0]);
  58. inty=Integer.parseInt(location[1]);
  59. intw=Integer.parseInt(location[2]);
  60. inth=Integer.parseInt(location[3]);
  61. if(nodeInfos.get(type)!=null){
  62. w=DEFAULT_PIC_SIZE;
  63. h=DEFAULT_PIC_SIZE;
  64. }else{
  65. x-=RECT_OFFSET_X;
  66. y-=RECT_OFFSET_Y;
  67. w+=(RECT_OFFSET_X+RECT_OFFSET_X);
  68. h+=(RECT_OFFSET_Y+RECT_OFFSET_Y);
  69. }
  70. Nodenode=newNode(name,type,x,h);
  71. parserTransition(node,el);
  72. nodes.put(name,node);
  73. }
  74. }
  75. @SuppressWarnings("unchecked")
  76. privatevoidparserTransition(Nodenode,ElementnodeEl){
  77. for(Elementel:(List<Element>)nodeEl.elements("transition")){
  78. Stringlabel=el.attributeValue("name");
  79. Stringto=el.attributeValue("to");
  80. Transitiontransition=newTransition(label,to);
  81. Stringg=el.attributeValue("g");
  82. if(g!=null&&g.length()>0){
  83. if(g.indexOf(":")<0){
  84. transition.setLabelPosition(getPoint(g));
  85. }else{
  86. String[]p=g.split(":");
  87. transition.setLabelPosition(getPoint(p[1]));
  88. String[]lines=p[0].split(";");
  89. for(Stringline:lines){
  90. transition.addLineTrace(getPoint(line));
  91. }
  92. }
  93. }
  94. node.addTransition(transition);
  95. }
  96. }
  97. privatePointgetPoint(Stringexp){
  98. if(exp==null||exp.length()==0){
  99. returnnull;
  100. }
  101. String[]p=exp.split(",");
  102. returnnewPoint(Integer.valueOf(p[0]),Integer.valueOf(p[1]));
  103. }
  104. publicMap<String,Node>getNodes(){
  105. returnnodes;
  106. }
  107. publicstaticMap<String,Object>getNodeInfos(){
  108. returnnodeInfos;
  109. }
  110. }


根据JpdlModel绘制出流程图
Java代码
  1. /**
  2. *CopyRight(C)2006-2009yy
  3. *@authoryy
  4. *@projectJbpm
  5. *@version1.0
  6. *@mailyy629_86at163dotcom
  7. *@date2009-9-6下午06:00:14
  8. *@description
  9. */
  10. packagesofocus.bpm.jbpm.jpdl;
  11. importjava.awt.*;
  12. importjava.awt.font.FontRenderContext;
  13. importjava.awt.geom.Rectangle2D;
  14. importjava.awt.image.BufferedImage;
  15. importjava.io.IOException;
  16. importjava.util.List;
  17. importjava.util.Map;
  18. importjavax.imageio.ImageIO;
  19. importsofocus.bpm.jbpm.jpdl.model.JpdlModel;
  20. importsofocus.bpm.jbpm.jpdl.model.Node;
  21. importsofocus.bpm.jbpm.jpdl.model.Transition;
  22. /**
  23. *@authoryeyong
  24. *
  25. */
  26. publicclassJpdlModelDrawer{
  27. publicstaticfinalintRECT_OFFSET_X=JpdlModel.RECT_OFFSET_X;
  28. publicstaticfinalintRECT_OFFSET_Y=JpdlModel.RECT_OFFSET_Y;
  29. publicstaticfinalintRECT_ROUND=25;
  30. publicstaticfinalintDEFAULT_FONT_SIZE=12;
  31. publicstaticfinalColorDEFAULT_STROKE_COLOR=Color.decode("#03689A");
  32. publicstaticfinalStrokeDEFAULT_STROKE=newBasicStroke(2);
  33. publicstaticfinalColorDEFAULT_LINE_STROKE_COLOR=Color.decode("#808080");
  34. publicstaticfinalStrokeDEFAULT_LINE_STROKE=newBasicStroke(1);
  35. publicstaticfinalColorDEFAULT_FILL_COLOR=Color.decode("#F6F7FF");
  36. privatefinalstaticMap<String,Object>nodeInfos=JpdlModel.getNodeInfos();
  37. publicBufferedImagedraw(JpdlModeljpdlModel)throwsIOException{
  38. Rectangledimension=getCanvasDimension(jpdlModel);
  39. BufferedImagebi=newBufferedImage(dimension.width,dimension.height,BufferedImage.TYPE_INT_ARGB);
  40. Graphics2Dg2=bi.createGraphics();
  41. g2.setColor(Color.WHITE);
  42. g2.fillRect(0,0,dimension.width,dimension.height);
  43. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  44. Fontfont=newFont("宋体",Font.PLAIN,DEFAULT_FONT_SIZE);
  45. g2.setFont(font);
  46. Map<String,Node>nodes=jpdlModel.getNodes();
  47. drawNode(nodes,g2,font);
  48. drawTransition(nodes,g2);
  49. returnbi;
  50. }
  51. /**
  52. *获得图片的矩形大小
  53. *
  54. *@return
  55. */
  56. privateRectanglegetCanvasDimension(JpdlModeljpdlModel){
  57. Rectanglerectangle=newRectangle();
  58. Rectanglerect;
  59. for(Nodenode:jpdlModel.getNodes().values()){
  60. rect=node.getRectangle();
  61. if(rect.getMaxX()>rectangle.getMaxX()){
  62. rectangle.width=(int)rect.getMaxX();
  63. }
  64. if(rect.getMaxY()>rectangle.getMaxY()){
  65. rectangle.height=(int)rect.getMaxY();
  66. }
  67. for(Transitiontransition:node.getTransitions()){
  68. List<Point>trace=transition.getLineTrace();
  69. for(Pointpoint:trace){
  70. if(rectangle.getMaxX()<point.x){
  71. rectangle.width=point.x;
  72. }
  73. if(rectangle.getMaxY()<point.y){
  74. rectangle.height=point.y;
  75. }
  76. }
  77. }
  78. }
  79. rectangle.width+=60;
  80. rectangle.height+=20;
  81. returnrectangle;
  82. }
  83. /**
  84. *@paramg2
  85. *@throwsIOException
  86. */
  87. privatevoiddrawTransition(Map<String,Node>nodes,Graphics2Dg2)throwsIOException{
  88. g2.setStroke(DEFAULT_LINE_STROKE);
  89. g2.setColor(DEFAULT_LINE_STROKE_COLOR);
  90. for(Nodenode:nodes.values()){
  91. for(Transitiontransition:node.getTransitions()){
  92. Stringto=transition.getTo();
  93. NodetoNode=nodes.get(to);
  94. List<Point>trace=newLinkedList<Point>(transition.getLineTrace());
  95. intlen=trace.size()+2;
  96. trace.add(0,newPoint(node.getCenterX(),node.getCenterY()));
  97. trace.add(newPoint(toNode.getCenterX(),toNode.getCenterY()));
  98. int[]xPoints=newint[len];
  99. int[]yPoints=newint[len];
  100. for(inti=0;i<len;i++){
  101. xPoints[i]=trace.get(i).x;
  102. yPoints[i]=trace.get(i).y;
  103. }
  104. finalinttaskGrow=4;
  105. finalintsmallGrow=-2;
  106. intgrow=0;
  107. if(nodeInfos.get(node.getType())!=null){
  108. grow=smallGrow;
  109. }else{
  110. grow=taskGrow;
  111. }
  112. Pointp=GeometryUtils.getRectangleLineCrossPoint(node.getRectangle(),newPoint(xPoints[1],
  113. yPoints[1]),grow);
  114. if(p!=null){
  115. xPoints[0]=p.x;
  116. yPoints[0]=p.y;
  117. }
  118. if(nodeInfos.get(toNode.getType())!=null){
  119. grow=smallGrow;
  120. }else{
  121. grow=taskGrow;
  122. }
  123. p=GeometryUtils.getRectangleLineCrossPoint(toNode.getRectangle(),newPoint(xPoints[len-2],
  124. yPoints[len-2]),grow);
  125. if(p!=null){
  126. xPoints[len-1]=p.x;
  127. yPoints[len-1]=p.y;
  128. }
  129. g2.drawPolyline(xPoints,yPoints,len);
  130. drawArrow(g2,xPoints[len-2],yPoints[len-2],xPoints[len-1],yPoints[len-1]);
  131. Stringlabel=transition.getLabel();
  132. if(label!=null&&label.length()>0){
  133. intcx,cy;
  134. if(len%2==0){
  135. cx=(xPoints[len/2-1]+xPoints[len/2])/2;
  136. cy=(yPoints[len/2-1]+yPoints[len/2])/2;
  137. }else{
  138. cx=xPoints[len/2];
  139. cy=yPoints[len/2];
  140. }
  141. PointlabelPoint=transition.getLabelPosition();
  142. if(labelPoint!=null){
  143. cx+=labelPoint.x;
  144. cy+=labelPoint.y;
  145. }
  146. cy-=RECT_OFFSET_Y+RECT_OFFSET_Y/2;
  147. g2.drawString(label,cx,cy);
  148. }
  149. }
  150. }
  151. }
  152. privatevoiddrawArrow(Graphics2Dg2,intx1,inty1,intx2,inty2){
  153. finaldoublelen=8.0;
  154. doubleslopy=Math.atan2(y2-y1,x2-x1);
  155. doublecosy=Math.cos(slopy);
  156. doublesiny=Math.sin(slopy);
  157. int[]xPoints={0,x2,0};
  158. int[]yPoints={0,y2,0};
  159. doublea=len*siny,b=len*cosy;
  160. doublec=len/2.0*siny,d=len/2.0*cosy;
  161. xPoints[0]=x2-(int)(b+c);
  162. yPoints[0]=y2-(int)(a-d);
  163. xPoints[2]=x2-(int)(b-c);
  164. yPoints[2]=y2-(int)(d+a);
  165. g2.fillPolygon(xPoints,3);
  166. }
  167. /**
  168. *@paramg2
  169. *@throwsIOException
  170. */
  171. privatevoiddrawNode(Map<String,Graphics2Dg2,Fontfont)throwsIOException{
  172. for(Nodenode:nodes.values()){
  173. Stringname=node.getName();
  174. if(nodeInfos.get(node.getType())!=null){
  175. BufferedImagebi2=ImageIO.read(getClass().getResourceAsStream(
  176. "/icons/48/"+nodeInfos.get(node.getType())));
  177. g2.drawImage(bi2,node.getX(),node.getY(),null);
  178. }else{
  179. intx=node.getX();
  180. inty=node.getY();
  181. intw=node.getWitdth();
  182. inth=node.getHeight();
  183. g2.setColor(DEFAULT_FILL_COLOR);
  184. g2.fillRoundRect(x,h,RECT_ROUND,RECT_ROUND);
  185. g2.setColor(DEFAULT_STROKE_COLOR);
  186. g2.setStroke(DEFAULT_STROKE);
  187. g2.drawRoundRect(x,RECT_ROUND);
  188. FontRenderContextfrc=g2.getFontRenderContext();
  189. Rectangle2Dr2=font.getStringBounds(name,frc);
  190. intxLabel=(int)(node.getX()+((node.getWitdth()-r2.getWidth())/2));
  191. intyLabel=(int)((node.getY()+((node.getHeight()-r2.getHeight())/2))-r2.getY());
  192. g2.setStroke(DEFAULT_LINE_STROKE);
  193. g2.setColor(Color.black);
  194. g2.drawString(name,xLabel,yLabel);
  195. }
  196. }
  197. }
  198. }


工具类,用来计算一些坐标的
Java代码
  1. /**
  2. *CopyRight(C)2006-2009yy
  3. *@authoryy
  4. *@projectjbpm
  5. *@version1.0
  6. *@mailyy629_86at163dotcom
  7. *@date2009-9-11上午06:16:26
  8. *@description
  9. */
  10. packagesofocus.bpm.jbpm.jpdl;
  11. importjava.awt.Point;
  12. importjava.awt.Rectangle;
  13. /**
  14. *@authoryeyong
  15. *
  16. */
  17. publicclassGeometryUtils{
  18. /**
  19. *获得直线(x1,y1)-(x2,y2)的斜率
  20. *
  21. *@paramx1
  22. *@paramy1
  23. *@paramx2
  24. *@paramy2
  25. *@return
  26. */
  27. publicstaticdoublegetSlope(intx1,inty2){
  28. return((double)y2-y1)/(x2-x1);
  29. }
  30. /**
  31. *获得直线(x1,y2)的y轴截距
  32. *
  33. *@paramx1
  34. *@paramy1
  35. *@paramx2
  36. *@paramy2
  37. *@return
  38. */
  39. publicstaticdoublegetYIntercep(intx1,inty2){
  40. returny1-x1*getSlope(x1,y1,y2);
  41. }
  42. /**
  43. *获得矩形的中点
  44. *
  45. *@paramrect
  46. *@return
  47. */
  48. publicstaticPointgetRectangleCenter(Rectanglerect){
  49. returnnewPoint((int)rect.getCenterX(),(int)rect.getCenterY());
  50. }
  51. /**
  52. *获得矩形中心p0与p1的线段和矩形的交点
  53. *
  54. *@paramrectangle
  55. *@paramp1
  56. *@return
  57. */
  58. publicstaticPointgetRectangleLineCrossPoint(Rectanglerectangle,Pointp1,intgrow){
  59. Rectanglerect=rectangle.getBounds();
  60. rect.grow(grow,grow);
  61. Pointp0=GeometryUtils.getRectangleCenter(rect);
  62. if(p1.x==p0.x){
  63. if(p1.y<p0.y){
  64. returnnewPoint(p0.x,rect.y);
  65. }
  66. returnnewPoint(p0.x,rect.y+rect.height);
  67. }
  68. if(p1.y==p0.y){
  69. if(p1.x<p0.x){
  70. returnnewPoint(rect.x,p0.y);
  71. }
  72. returnnewPoint(rect.x+rect.width,p0.y);
  73. }
  74. doubleslope=GeometryUtils.getSlope(p0.x,p0.y,rect.x,rect.y);
  75. doubleslopeLine=GeometryUtils.getSlope(p0.x,p1.x,p1.y);
  76. doubleyIntercep=GeometryUtils.getYIntercep(p0.x,p1.y);
  77. if(Math.abs(slopeLine)>slope-1e-2){
  78. if(p1.y<rect.y){
  79. returnnewPoint((int)((rect.y-yIntercep)/slopeLine),rect.y);
  80. }else{
  81. returnnewPoint((int)((rect.y+rect.height-yIntercep)/slopeLine),rect.y+rect.height);
  82. }
  83. }
  84. if(p1.x<rect.x){
  85. returnnewPoint(rect.x,(int)(slopeLine*rect.x+yIntercep));
  86. }else{
  87. returnnewPoint(rect.x+rect.width,(int)(slopeLine*(rect.x+rect.width)+yIntercep));
  88. }
  89. }
  90. }


测试
Java代码

public static void main(String[] args) { // TODO Auto-generated method stub try { File file = new File("D:/test.jpdl.xml"); InputStream is = new FileInputStream(file); JpdlModel jpdlModel = new JpdlModel (is); ImageIO.write(new JpdlModelDrawer().draw(jpdlModel),"png",new File("D:/test.png")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }

}

原文链接:https://www.f2er.com/xml/298792.html

猜你在找的XML相关文章