读书人

Fixflow发动机解析(四)(模型) - 通过

发布时间: 2014-04-23 16:23:57 作者: rapoo

Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素

官方也是清楚的,他们给的那点肯定是不够用的,所以就有了上面的扩展,3.0出来了肯定会提供更多支持。

?

3.扩展 BPMN2.0 EMF模型的思路

Fixflow在读写BPMN2.0模型的时候使用了Eclipse官方提供的EMF模型,扩展元素的最好的方式就是不修改原始Eclipse提供的模型,

采用外部注入的方式修改给EMF模型添加新的元素。我们需要重新建立一个EMF模型来放置Fixflow引擎自己扩展的元素定义,

通过EMF提供的注入方法添加到元素的模型保存中去,下面我们一步步来完成这个过程。

?

4.创建一个EMF模型

这里我们不再详细介绍创建模型文件的过程,不清楚的请参考第三章创建模型。

下面的截图是 fixflow 的扩展元素模型:

Fixflow发动机解析(四)(模型)  - 通过EMF扩展BPMN2.0元素

?

这个扩展模型有些需要注意的地方,模型 整体的 Ns Prefix 和 Ns URI ?这个两个属性决定了你扩展元素的名称和命名空间。

?

Fixflow发动机解析(四)(模型)  - 通过EMF扩展BPMN2.0元素

?

Attributes 的设置需要注意的事项。

?

Fixflow发动机解析(四)(模型)  - 通过EMF扩展BPMN2.0元素?

?

EReference 引用设置需要主要的地方。

?

?Fixflow发动机解析(四)(模型)  - 通过EMF扩展BPMN2.0元素

?

创建完毕生成三个模型文件,并生成对应java代码。

Fixflow发动机解析(四)(模型)  - 通过EMF扩展BPMN2.0元素

?

5.读写扩展元素

下面的代码就是在web环境下读写BPMN2.0模型扩展元素的代码:

模型文件下载地址:??SampleProcess.bpmn.7z

  1 import java.util.List;  2   3 import org.eclipse.bpmn2.BaseElement;  4 import org.eclipse.bpmn2.Bpmn2Factory;  5 import org.eclipse.bpmn2.Bpmn2Package;  6 import org.eclipse.bpmn2.Definitions;  7 import org.eclipse.bpmn2.ExtensionAttributeValue;  8 import org.eclipse.bpmn2.RootElement;  9 import org.eclipse.bpmn2.di.BpmnDiPackage; 10 import org.eclipse.bpmn2.util.Bpmn2ResourceFactoryImpl; 11 import org.eclipse.dd.dc.DcPackage; 12 import org.eclipse.dd.di.DiPackage; 13 import org.eclipse.emf.common.util.URI; 14 import org.eclipse.emf.ecore.EPackage; 15 import org.eclipse.emf.ecore.EReference; 16 import org.eclipse.emf.ecore.impl.EStructuralFeatureImpl.SimpleFeatureMapEntry; 17 import org.eclipse.emf.ecore.resource.Resource; 18 import org.eclipse.emf.ecore.resource.ResourceSet; 19 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 20 import org.eclipse.emf.ecore.util.FeatureMap; 21  22 import com.founder.fix.bpmn2extensions.fixflow.ConnectorInstance; 23 import com.founder.fix.bpmn2extensions.fixflow.FixFlowFactory; 24 import com.founder.fix.bpmn2extensions.fixflow.FixFlowPackage; 25  26  27 public class test { 28  29     public static void main(String[] args) { 30         //创建资源集 31         ResourceSet resourceSet = getResourceSet(); 32         //创建资源 33         Resource resource = resourceSet.createResource(URI.createURI("SampleProcess.bpmn")); 34          35          36         try { 37             //加载资源 38             resource.load(null); 39         } catch (Exception e) { 40             e.printStackTrace(); 41         } 42         //获取根元素 43         Definitions definitions = (Definitions) resource.getContents().get(0).eContents().get(0); 44         //获取process对象 45         org.eclipse.bpmn2.Process process=null; 46         for (RootElement rootElement : definitions.getRootElements()) { 47             if (rootElement instanceof org.eclipse.bpmn2.Process) { 48                 process = (org.eclipse.bpmn2.Process) rootElement;                 49             } 50         } 51          52         if(process==null){ 53             return; 54         } 55          56         //设置扩展 EAttribute fixflow:dbid 57         process.eSet(FixFlowPackage.Literals.DOCUMENT_ROOT__DBID, "SampleProcess_1"); 58         //读取扩展 EAttribute fixflow:dbid 59         process.eGet(FixFlowPackage.Literals.DOCUMENT_ROOT__DBID); 60          61          62         ConnectorInstance connectorInstance=FixFlowFactory.eINSTANCE.createConnectorInstance(); 63         //添加扩展元素 64         addExtensionElement(process,FixFlowPackage.Literals.DOCUMENT_ROOT__CONNECTOR_INSTANCE,connectorInstance); 65         //读取扩展元素 66         getExtensionElementList(ConnectorInstance.class,process,FixFlowPackage.Literals.DOCUMENT_ROOT__CONNECTOR_INSTANCE); 67          68  69     } 70      71     /** 72      * 创建资源集 73      * @return 74      */ 75     private static ResourceSet getResourceSet() { 76          77         //以下的方式是脱离Eclipse环境在web下使用EMF的时候使用的方式,Eclipse里使用要简单的多。 78          79         ResourceSet resourceSet = new ResourceSetImpl(); 80         (EPackage.Registry.INSTANCE).put("http://www.omg.org/spec/BPMN/20100524/MODEL", Bpmn2Package.eINSTANCE); 81         //注意这里的注册没有这个注册是没有办法通过直接获取扩展元素的 82         (EPackage.Registry.INSTANCE).put("http://www.founderfix.com/fixflow", FixFlowPackage.eINSTANCE); 83         (EPackage.Registry.INSTANCE).put("http://www.omg.org/spec/DD/20100524/DI", DiPackage.eINSTANCE); 84         (EPackage.Registry.INSTANCE).put("http://www.omg.org/spec/DD/20100524/DC", DcPackage.eINSTANCE); 85         (EPackage.Registry.INSTANCE).put("http://www.omg.org/spec/BPMN/20100524/DI", BpmnDiPackage.eINSTANCE); 86         FixFlowPackage.eINSTANCE.eClass(); 87         FixFlowPackage xxxPackage = FixFlowPackage.eINSTANCE; 88         EPackage.Registry.INSTANCE.put(xxxPackage.getNsURI(), xxxPackage); 89         Bpmn2ResourceFactoryImpl ddd = new Bpmn2ResourceFactoryImpl(); 90         //注意这里的注册没有这个注册是没有办法通过直接获取扩展元素的 91         Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("fixflow", ddd); 92         resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("bpmn", ddd); 93         resourceSet.getPackageRegistry().put(xxxPackage.getNsURI(), xxxPackage); 94         Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("bpmn", ddd); 95         return resourceSet; 96     } 97      98     /** 99      * 读取扩展元素 Element100      * @param t 扩展元素的java类型101      * @param baseElement 主元素102      * @param eReference 引用103      * @return104      */105     @SuppressWarnings("unchecked")106     public static <T> List<T> getExtensionElementList( Class<T> t ,BaseElement baseElement,EReference eReference){107         108         //BPMN2.0官方定义每个BaseElement元素都含有扩展元素ExtensionValues109         if (baseElement.getExtensionValues().size() > 0) {110             for (ExtensionAttributeValue extensionAttributeValue : baseElement.getExtensionValues()) {111                 FeatureMap extensionElements = extensionAttributeValue.getValue();112                 Object objectElement = extensionElements.get(eReference, true);113                 if (objectElement != null) {114 115                     List<T> tObjList = (List<T>) objectElement;116                     return tObjList;117                 118 119                 }120             }121         }122         123         124         return (List<T>)null;125     }126     127     /**128      * 添加扩展元素129      * @param baseElement 主元素130      * @param eReference 引用131      * @param o 扩展元素对象132      * @return133      */134     public static boolean addExtensionElement(BaseElement baseElement,EReference eReference,Object o){135         final FeatureMap.Entry extensionElementEntry = new SimpleFeatureMapEntry((org.eclipse.emf.ecore.EStructuralFeature.Internal) eReference, o);136             if(baseElement.getExtensionValues().size() > 0){137                 baseElement.getExtensionValues().get(0).getValue().add(extensionElementEntry);138             }else{139                 ExtensionAttributeValue extensionElement = Bpmn2Factory.eINSTANCE.createExtensionAttributeValue();140                 extensionElement.getValue().add(extensionElementEntry);141                 baseElement.getExtensionValues().add(extensionElement);142             } 143         return false;144     }145 146 }

?

读书人网 >软件架构设计

热点推荐