- Undocumented Matlab - https://undocumentedmatlab.com -

JGraph and BDE

Posted By Yair Altman On January 26, 2011 | 13 Comments

Last week, when I presented JFreeChart [1], I intended to follow it with other articles dedicated to JGraph, JFreeReport and other useful open-source packages that can be used from within Matlab. However, blog follower Scott Koch was quick to post a comment [2] about JGraph and even took the time to add a short code snippet illustrating JGraph’s use. So, I will honor Scott by re-posting his example here.

JGraph

JGraph [3] is an open-source Java library that helps visualize graph-theory objects and connectors. Note that in today’s article we discuss graphs in the sense of connected object trees, not graphs in the sense of x-vs.-y data charts.
To use JGraph, download [4] the JGraphx zip file, then extract the zip file. You’ll get a jgraphx/ subfolder containing separate sub-folders with docs, examples, source-code and a lib/ sub-folder with a jgraphx.jar file. To use JGraph in Matlab, load jgraphx.jar into Matlab’s Java classpath (there is no need to use the static classpath in this case, although you can of course do so if you like):

javaaddpath('jgraphx/lib/jgraphx.jar');

We can now run Scott Koch’s example of using JGraph in Matlab:

% Make the graph object
graph = com.mxgraph.view.mxGraph;
% Get the parent cell
parent = graph.getDefaultParent();
% Group update
graph.getModel().beginUpdate();
% Add some child cells
v1 = graph.insertVertex(parent, '', 'Hello', 240, 150, 80, 30);
v2 = graph.insertVertex(parent, '', 'World', 20,   20, 80, 30);
graph.insertEdge(parent, '', 'Edge', v1, v2);
graph.getModel().endUpdate();
% Get scrollpane
graphComponent = com.mxgraph.swing.mxGraphComponent(graph);
% Make a figure and stick the component on it
f = figure('units','pixels');
pos = get(f,'position');
mypanel = javax.swing.JPanel(java.awt.BorderLayout);
mypanel.add(graphComponent);
[obj, hcontainer] = javacomponent(mypanel, [0,0,pos(3:4)], f);

JGraph example within a Matlab figure (the graph is fully interactive)
JGraph example within a Matlab figure (the graph is fully interactive)

And some more complex JGraph examples, taken from the “official” samples gallery:
JGraph sample (click for details) [5]
JGraph sample (click for details) [6]
JGraph samples (click for details)

JGraphT

JGraphT [7] is another open-source Java-based library. JGraphT is a non-GUI library that provides underlying graph-theory objects and algorithms. These can be used in conjunction with the JGraph visualization library.
The Matlab File Exchange has a submission that wraps JGraphT within Matlab [8]. Apparently, this Matlab utility is a wrapper for the non-GUI JGraphT, but I see no reason not to use JGraph’s GUI visualization capabilities, since JGraphT has an extremely simple JGraph adapter.

BDE

BDE (Block Diagram Editor?) is an internal Matlab Java class library that has been included in Matlab releases for many years, to this day. Judging from the size of this library file (%matlabroot%/java/jar/bde.jar), MathWorks continues to develop BDE, which grows from one release to another.
BDE can be used to present and edit block diagrams. The diagrams themselves are stored in XML files in a BDE/ subfolder beneath the user’s prefdir folder. However, I do not know how these diagrams can be used. Here’s a simple usage example:

bde = com.mathworks.bde.clients.BDEDesktop;  % fails in R2009b+
diagram = com.mathworks.bde.diagram.Diagram;
client = com.mathworks.bde.clients.DiagramViewDTClient('YMA',diagram);
bde.addClient(client,'YMA');

BDE sample diagram (click for details) [9]
BDE sample diagram (click for details)

Note: In Matlab release R2009b, BDE apparently had a major redesign, and the above code no longer works.
I would be happy if anyone could enlighten me regarding the possible usage of BDE, or how it can be ran in one of the latest releases. Please leave a comment below [10].

Categories: GUI, High risk of breaking in future versions, Java


13 Comments (Open | Close)

13 Comments To "JGraph and BDE"

#1 Comment By Fab On February 9, 2011 @ 05:09

“JGraph example within a Matlab figure (the graph is fully interactive)”
Well I still can’t move the vorteces, same problem as Scott mentioned in his comment. Any solution to that?
(Version R2008a)

#2 Comment By Yair Altman On February 9, 2011 @ 10:40

@Fab – well, while you cannot simply drag the vortexes, you can indeed move them by dragging opposite edges. For example, to move left, first enlarge the vortex leftward by dragging the left-side resize handle, then reduce the size leftward by dragging the right-side resize handle; the end result is that the vortex moved left. Ugly, but it works…

#3 Comment By Sven Körner On February 17, 2011 @ 13:13

@Fab: There is a way, you can move the vertex (works in 2010a, maybe also in 2008a). The trick is, when you have selected the vertex as shown in the figure after the codesnippet (green dotted border with marker and the double-cross appears) then press the left mouse button (hold on), press the right mouse button (hold on) and with both mouse buttons down move the mouse to the new place for the vertex. Then release the right mouse button first – vertex moves. It is difficult, but with a little training …

#4 Comment By Akli Benali On October 21, 2011 @ 14:30

About JgraphT and the submission link mentioned above: I tried to follow the instructions in the link and I can’t use the JgraphT in Matlab. When I do ‘which org.matjgraph.MaximalCliques.getMaximalCliques’ it retrives “not found”. First I thought the problem was in the paths to add because the “build” path does not exist. I changed it to match the build.xml file and all the jar files in the \lib\ path and still it does not work.

Does anyone know what can be the problem?

Cheers

(Version R2008a, Windows Vista)

#5 Comment By Yair Altman On October 22, 2011 @ 09:09

@Akli – did you remember to use javaaddpath for your JGraphT jar file(s)?

#6 Comment By Akli Benali On October 24, 2011 @ 11:25

Yes, I added the jar files in \lib and the build.xml using the javaaddpath.

#7 Comment By Yair Altman On October 31, 2011 @ 15:04

@Akli – try to follow the code in the following File Exchange submission: [17]

#8 Comment By Markus Behle On July 11, 2013 @ 01:58

mxGraph/JGraph – Get Drag & Drop (moving vertices), Cut & Paste working

People complained about not being able to move vertices if they start a java application built with mxGraph/JGraph. I experienced the same problem and investigated it. Here’s my solution how to get it working.
This article refers to Matlab R2013a and mxGraph 2.1.0.0 but it should also work with older versions of both.

Both Drag & Drop (which is needed for moving vertices around) and Cut & Paste rely on the mxGraphTransferHandler working correctly. The mxGraphTransferHandler in fact uses its own implementation of Transferable (java.awt.datatransfer.Transferable) called mxGraphTransferable. Have a look at the source code in jgraphx/src/com/mxgraph/swing/util/mxGraphTransferable.java. At the very end you’ll see a static initializer whose job is to construct a suitable DataFlavor (java.awt.datatransfer.DataFlavor). And here is the problem! If you start your mxGraph application from Java’s VM everything works fine. But if you start it from within Matlab’s JVM it cannot find the class com.mxgraph.swing.util.mxGraphTransferable and you’ll get a ClassNotFoundException. As you can see in the code this one is caught but nothing happens (// do nothing). So you do not notice that the Class Loader has a problem and keep wondering why moving vertices does not work (as me).

Now here comes the solution. By the way, I found it in the source code jgraphx/src/com/mxgraph/swing/util/mxGraphTransferable.java at the very top. There it says “Serialized Data Flavor. Use the following code to switch to local reference flavor: …”. In fact this did not work for me. “If you get a class not found exception, try the following instead: …” Et voila! Just insert their code

mxGraphTransferable.dataFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType
+ "; class=com.mxgraph.swing.util.mxGraphTransferable", null,
new com.mxgraph.swing.util.mxGraphTransferable(null, null).getClass().getClassLoader());

at the beginning of your java code and moving vertices, Drag & Drop and Cut & Paste works!

#9 Comment By Daniel Malmquist On October 10, 2013 @ 05:46

Hi!

I’ve been trying to get this working.

Do i need to put this code in my own program or in the jgraphx sources and rebuild it?

I’ve tried running it from my program with no success. I use jgraphx directly from matlab so i rewrote the code snippet for matlab:

%Fix for movable vertices
tempobj = javaObject('com.mxgraph.swing.util.mxGraphTransferable',[], []);
mxGraphTransferable.dataFlavor = javaObject('java.awt.datatransfer.DataFlavor', ...
   [char(java.awt.datatransfer.DataFlavor.javaJVMLocalObjectMimeType)'; class = com.mxgraph.swing.util.mxGraphTransferable'], ...
   [], tempobj.getClass().getClassLoader())

What am I doing wrong?

Regards

#10 Comment By Markus Behle On October 25, 2013 @ 06:58

@Daniel

You don’t need to patch jgraphx’ sources.
From your code I see that you use jgraphx directly on Matlab’s command line (or in a .m file).
I wrote a Java program (where I put my code snippet in right at the beginning), compiled and packed it into a jar. From Matlab’s command line, I use that jar as a kind of library.

#11 Comment By Scott Koch On June 25, 2014 @ 11:38

Thanks for the post Markus. Here’s how I implemented your suggestion for anyone interested (Daniel).

Overload the mxGraph class. Create a file, mymxGraph.java with the following code:

import java.awt.datatransfer.DataFlavor;
import com.mxgraph.view.mxGraph;
import com.mxgraph.swing.util.mxGraphTransferable;

// Modified mxGraph to fix dragging
public class mymxGraph extends mxGraph {
  static {
    try {
      //Set enableImageSupport to false if using Mac otherwise data translation error happens.
      com.mxgraph.swing.util.mxGraphTransferable.enableImageSupport = false;
      //Following code is suggested in:
      //https://github.com/jgraph/jgraphx/blob/master/src/com/mxgraph/swing/util/mxGraphTransferable.java
      mxGraphTransferable.dataFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + 
      "; class=com.mxgraph.swing.util.mxGraphTransferable", null, 
      new com.mxgraph.swing.util.mxGraphTransferable(null, null).getClass().getClassLoader()); 
    } catch (ClassNotFoundException e) {
      System.err.println("Caught Class Not Found Exception: " + e.getMessage());;
    }
  }
}

Next, compile the class (at Matlab command line). Note that I have the jgraphx.jar in the same folder as mymxGraph.java created above which is also my Current Folder in Matlab:

% Compile the java file being sure to include jgraphx.jar
system('javac mymxGraph.java -classpath jgraphx.jar')
% Create a jar
system('jar cf mymxGraph.jar mymxGraph.class')
% Add both mymxGraph and jgraphx to java path
javaaddpath('mymxGraph.jar');
javaaddpath('jgraphx.jar')

% Replace com.mxgraph.view.mxGraph with the new mymxGraph
graph = mymxGraph;

%Run the rest of the code above and cells should now be draggable. 

I’m a java amateur so if anyone has improvements to suggest please post them.

Thanks

#12 Comment By Sam Roberts On July 2, 2015 @ 05:32

Yair – just in case you’re still interested (I appreciate that this is a very old post I’m replying to), BDE is short for block diagram editor. It’s part of SimBiology (MathWorks’ product for modelling, simulation and analysis of biological systems, such as pharmacokinetics and systems biology), and is used to graphically lay out components of biological models. If you take a look at the marketing pages on MathWorks website for SimBiology, you’ll see plenty of screenshots that show the component in action.

It’s a MathWorks-authored component, although it may (I’m not sure) be a wrapper around a third-party component. I’m not sure why it’s included as part of MATLAB rather than just SimBiology – perhaps there’s an idea that it might get reused across other products as well.

If anyone is thinking of using it, I would not rely on it to have a stable interface or behaviour across versions: MathWorks may well modify it based on requirements for SimBiology – which, as a leaf product, has much more scope to change rapidly than core MATLAB.

#13 Comment By Yair Altman On July 2, 2015 @ 05:42

@Sam – thanks for confirming that BDE does indeed stand for Block Diagram Editor as I’ve speculated. As I noted in the main text, MathWorks removed BDE from the core Matlab in R2009b, and it’s not available to the general Matlab user since then. Only users of R2009a and earlier can use it. When I wrote the article in late 2010 (for publication in Jan 2011), R2009a was still widely used. But I believe that it is not very relevant nowadays.


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/jgraph-and-bde

URLs in this post:

[1] JFreeChart: http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/

[2] post a comment: http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/#comment-30052

[3] JGraph: http://www.jgraph.com/jgraph.html

[4] download: http://www.jgraph.com/jgraphdownload.html

[5] Image: http://undocumentedmatlab.com/images/JGraph_JDOgenie.png

[6] Image: http://undocumentedmatlab.com/images/JGraph_yawleditor.png

[7] JGraphT: http://jgrapht.sourceforge.net/

[8] submission that wraps JGraphT within Matlab: http://www.mathworks.com/matlabcentral/fileexchange/27074-maximal-independent-sets-using-jgrapht

[9] Image: http://undocumentedmatlab.com/images/BDE.png

[10] below: http://undocumentedmatlab.com/blog/jgraph-and-bde/#respond

[11] JGraph in Matlab figures : https://undocumentedmatlab.com/articles/jgraph-in-matlab-figures

[12] Waterloo graphics : https://undocumentedmatlab.com/articles/waterloo-graphics

[13] JTattoo look-and-feel demo : https://undocumentedmatlab.com/articles/jtattoo-look-and-feel-demo

[14] Frameless (undecorated) figure windows : https://undocumentedmatlab.com/articles/frameless-undecorated-figure-windows

[15] Builtin PopupPanel widget : https://undocumentedmatlab.com/articles/builtin-popuppanel-widget

[16] uigetfile/uiputfile customizations : https://undocumentedmatlab.com/articles/uigetfile-uiputfile-customizations

[17] : http://www.mathworks.com/matlabcentral/fileexchange/27074

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.