赞
踩
插件是唯一方式。
Plugins are the only one way to change the functionality of a modeling tool.
插件运行过程示意图:
插件管理器会扫描plugins下所有文件夹,文件夹内需要有插件描述文件,根据文件中的描述找到插件类,判断是否继承com.nomagic.magicdraw.plugins.Plugin
,并实现了init()
方法。
注:必须有一个类继承com.nomagic.magicdraw.plugins.Plugin
The plugin must contain at least one class derived from the com.nomagic.magicdraw.plugins.Plugin class.
通过settings > Appearance & Behavior > Path Variables添加变量MAGIC_DRAW_INSTALL_DIRECTORY
指向安装目录(便于多人开发时解决各计算机依赖包位置不同的问题);
修改配置文件MagicDraw\bin\magicdraw.properties,在JAVA_ARGS
添加属性
-Dmd.plugins.dir="absolute path to plugins directory1;absolute path to
plugins directory2"
目录直接写当前正在开发插件目录,这样启动MagicDraw后会到该目录加载插件(经过测试发现不修改配置,在启动IDEA调试后MagicDraw也可以加载插件);
注:在.properties中需要使用转义字符,否则会导致配置不生效,所有插件丢失,示例如下
-Dmd.plugins.dir\="\C:\\Program Files\\MagicDraw\\plugins;D\:\\09_Project\\Simulation\\SYSML\\ide\\intellij\\MagicDraw development\\plugins"
创建空项目;
添加Libraries
,并在Modules
中设置Dependencies
引用;
注:Libraries指向MAGIC_DRAW_INSTALL_DIRECTORY\lib整个文件夹,并且需要手动设置递归(否则会出现依赖缺失),在配置文件.idea\libraries\xxx.xml文件下直接手动修改
<jarDirectory url="file://$MAGIC_DRAW_INSTALL_DIRECTORY$/lib" recursive="true" />
Edit Configurations;
具体参数如下:
MagicDraw with all plugins -Xmx2000M -Xss1024K -XX:PermSize=60M -XX:MaxPermSize=200M -DLOCALCONFIG=true -Dmd.plugins.dir=${MAGIC_DRAW_INSTALL_DIRECTORY}/plugins;.. "-javaagent:${MAGIC_DRAW_INSTALL_DIRECTORY}/openapi/ide/lib/com.nomagic.magicdraw.intellij.launcher.jar" -jar "${MAGIC_DRAW_INSTALL_DIRECTORY}/openapi/ide/lib/com.nomagic.magicdraw.intellij.launcher.jar" com.nomagic.magicdraw.LaunchGateway -verbose $MODULE_DIR$
创建插件类;
继承com.nomagic.magicdraw.plugins.Plugin
,并实现init()
、close()
、isSupported()
方法。
package simulation; import com.nomagic.magicdraw.core.Application; import com.nomagic.magicdraw.plugins.Plugin; /** * 插件 */ public class SimulationPlugin extends Plugin { public static boolean initialized; @Override public void init() { initialized = true; Application.getInstance().getGUILog().showMessage("Simulation plugin initialized."); } @Override public boolean close() { return true; } @Override public boolean isSupported() { return true; } }
创建plugin.xml;
src同级目录下创建。
<?xml version="1.0" encoding="UTF-8"?> <plugin id="SimulationPlugin" name="SimulationPlugin" version="1.0" provider-name="SYSWARE" class="simulation.SimulationPlugin"> <requires> <api version="1.2"/> </requires> <runtime> <library name="SimulationPlugin.jar"/> </runtime> </plugin>
引用MagicDraw\lib\bundles
下的某些jar包时,编译没有问题,但是插件运行时报错,如Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
报错信息:
2019-10-16 16:36:09,996 [main] ERROR PLUGINS - Can not start plugin My Plug-in 1 java.lang.NoClassDefFoundError: org/json/JSONObject at myplugin1.MyPlugin1.init(MyPlugin1.java:17) at com.nomagic.magicdraw.plugins.d.b(d.java:719) at com.nomagic.magicdraw.plugins.d.a(d.java:556) at com.nomagic.magicdraw.plugins.d.y(d.java:487) at com.nomagic.magicdraw.core.h.t(h.java:45) at com.nomagic.rcpf.product.o.a(o.java:283) at com.nomagic.rcpf.product.p.b(p.java:93) at com.nomagic.rcpf.product.p.a(p.java:77) at com.nomagic.magicdraw.core.Application.internalStart(Application.java:685) at com.nomagic.magicdraw.core.Application.start(Application.java:454) at com.nomagic.magicdraw.MagicDrawApplicationLauncher.launch(MagicDrawApplicationLauncher.java:24) at com.nomagic.magicdraw.ApplicationGateway.start(ApplicationGateway.java:46) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388) at com.nomagic.osgi.launcher.FrameworkLauncher$DefaultApplication.run(FrameworkLauncher.java:227) at com.nomagic.osgi.launcher.FrameworkLauncher.runFrameworkApplication(FrameworkLauncher.java:177) at com.nomagic.osgi.launcher.FrameworkLauncher.run(FrameworkLauncher.java:114) at com.nomagic.osgi.launcher.FrameworkLauncher.run(FrameworkLauncher.java:93) at com.nomagic.osgi.launcher.ProductionFrameworkLauncher.run(ProductionFrameworkLauncher.java:66) at com.nomagic.osgi.launcher.ProductionFrameworkLauncher.main(ProductionFrameworkLauncher.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.nomagic.launcher.Launcher.startMainClass(Launcher.java:337) at com.nomagic.launcher.Launcher.start(Launcher.java:108) at com.nomagic.launcher.Launcher.main(Launcher.java:70) Caused by: java.lang.ClassNotFoundException: org.json.JSONObject at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 29 more
解决方案:
在plugin.xml同级目录下创建lib文件夹,将引用的jar包复制到该目录下,同时在Project Structure/Libraries下添加lib目录,并将顺序调到最高,然后在plugin.xml中添加runtime>library引用
截图如下:
<?xml version="1.0" encoding="UTF-8"?>
<plugin
id="myplugin1"
name="My Plug-in 1"
version="1.0"
ownClassloader="true"
provider-name="No Magic"
class="myplugin1.MyPlugin1">
<runtime>
<library name="lib/org.json_1.0.0.255610281323.jar"/>
<library name="myplugin1.jar"/>
</runtime>
</plugin>
需要在仿真开始前注册监听器,否则监听回调函数没有作用。
自定义监听类:CustomSimulationExecutionListener
import com.nomagic.magicdraw.simulation.execution.SimulationExecutionListener;
import com.nomagic.magicdraw.simulation.fuml.data.i;
import fUML.Semantics.CommonBehaviors.Communications.SignalInstance;
public class CustomSimulationExecutionListener extends SimulationExecutionListener {
@Override
public void eventTriggered(SignalInstance signal){
i data = signal.getData();
System.out.println("监听到信号被触发");
}
}
注册监听器:MainAction
@Override
public void actionPerformed(ActionEvent e)
{
// TODO: 监听仿真过程中的事件
CustomSimulationExecutionListener executionListener = new CustomSimulationExecutionListener();
SimulationManager.registerSimulationExecutionListener(executionListener);
// ...开始仿真等操作
}
必须基于JDK11进行开发,否则编译时会报空指针异常,project lanuage level设置为10;
Modules中按照MagicDraw Patch[安装目录/lib/path.jar]、MagicDradraw Brand[安装目录/lib/brand.jar brand_api.jar]、MagicDraw Libs[安装目录/lib]的顺序设置依赖;
引用其他的依赖,如simulation.jar,需要在发布时在plugin.xml中制指定runtime/library/name设置具体位置,否则会出现开发环境下正常,但是发布后找不到类而加载失败的情况;
特殊情况:通过plugin.xml设置依赖simulation.jar后,造成活动图的decision、状态图的choice在仿真过程中突然中断,需要通过插件依赖的方式设置依赖。
<?xml version="1.0" encoding="UTF-8"?> <plugin id="MatlabEngine" name="Matlab Engine" version="19.0 SP3" internalVersion="1900010" provider-name="No Magic" class="com.nomagic.magicdraw.simulation.matlab.MatlabPlugin"> <requires> <api version="1.0"/> <required-plugin id="SimulationToolkit" name="Cameo Simulation Toolkit" version="19.0 SP3" internalVersion="1900010"/> </requires> <runtime> <library name="matlab_api.jar"/> <library name="lib/jmatengine.jar"/> </runtime> </plugin>
当使用IDEA进行调试开发时,可能会在enviroment/plugins下面出现多个版本的同一插件,原因是设置的启动配置中:
-Dmd.plugins.dir=${MAGIC_DRAW_INSTALL_DIRECTORY}/plugins;..
MagicDraw会到安装目录下的plugins文件夹以及当前项目的上级目录寻找所有的plugin.xml文件,然后加载对应插件,如果有多个版本的plugin.xml就会造成插件多次加载。
当正在开始的插件依赖于其他插件时,通常需要在Modules/Dependencies下设置依赖的jar包,部署发布时需要在plugin.xml中设置requires对插件的依赖。
有时会出现问题:部署发布后,插件以及MagicDraw都没有问题;但是开发调试过程中,MagicDraw本身功能却出现问题,如原本的仿真功能菜单为Run
,当开发插件依赖仿真插件时,菜单变成了CONTEXT_TOOLBAR
。
原因在于:发布后的插件,由于已经在plugin.xml中设置了插件依赖关系,MagicDraw软件本身会解决依赖问题;调试中的插件,虽然引用了所依赖插件的jar包,但是并没有把其下的所有jar包引进来,还可能是所依赖的插件本身又依赖于另一个插件,这些jar包都要包含进来。
开发过程中需要保证所有依赖的插件及依赖插件的父插件目录下的jar包,都需要引入进来。
[1] MagicDraw\manual\MagicDraw UserManual.pdf :Developer Guide
部分
[3] Jar包反编译后修改源码再编译
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。