How to Save Port Constraints to a GraphML File
Applies to: GraphML 3.0, GraphML 2.4, yFiles for Java 2.7, yFiles for Java 2.6, yFiles for Java 2.5, yFiles for Java 2.4 print article email article

Type: Questions & Answers

This article describes how port constraints can be saved in a GraphML file.

When saving a graph to a GraphML file, by default PortConstraints are not stored in this file. Nevertheless, the integrated GraphML support (for yFiles 2.7 and higher) as well as the GraphML package already provide input and output handlers that can easily be registered to the GraphMLIOHandler as follows:

If you are using the integrated GraphML support for yFiles for Java 2.7 and higher:

y.io.GraphMLIOHandler ioh = new y.io.GraphMLIOHandler();

ioh.getGraphMLHandler().addInputHandlerProvider(new InputHandlerProvider() {
 public void onQueryInputHandler(QueryInputHandlersEvent event) throws GraphMLParseException {
   Element keyDefinition = event.getKeyDefinition();
   PortConstraintInputHandler handler = new PortConstraintInputHandler();

   //If we have encountered a graphml attribute that defines port constraints: 
   if (handler.acceptKey(keyDefinition)) {
     //Create the data providers if they don't exist yet
     initDataProvider(PortConstraintKeys.SOURCE_GROUPID_KEY, event.getContext().getGraph());
     initDataProvider(PortConstraintKeys.TARGET_GROUPID_KEY, event.getContext().getGraph());
     initDataProvider(PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY, event.getContext().getGraph());
     initDataProvider(PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY, event.getContext().getGraph());
     //Actually register the input handler for the port constraint data
     event.addInputHandler(handler);
   }
 }

 private void initDataProvider(Object key, Graph graph) {
   DataProvider dp = graph.getDataProvider(key);
   if (dp == null || !(dp instanceof EdgeMap)) {
     dp = Maps.createEdgeMap(new WeakHashMap());
     graph.addDataProvider(key, dp);
   }
 }
});

ioh.getGraphMLHandler().addOutputHandlerProvider(new OutputHandlerProvider() {
  public void onQueryOutputHandler(QueryOutputHandlersEvent event) throws GraphMLWriteException {
    boolean isRegistered = false;
    Graph g = event.getContext().getGraph();
    Object[] keys = g.getDataProviderKeys();
    for (int i = 0; i < keys.length; i++) {
      Object key = keys[i];
      //Check if any of the port constraint relevant dataproviders is registered
      if(PortConstraintKeys.SOURCE_GROUPID_KEY.equals(key)  ||
         PortConstraintKeys.TARGET_GROUPID_KEY.equals(key) ||
         PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY.equals(key) ||
         PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY.equals(key)) {
        isRegistered = true;
        break;
       }
    }
    if (isRegistered) {
      //Only use the output handler if the data providers exist
      event.addOutputHandler(new PortConstraintOutputHandler(), KeyScope.EDGE);
    }
  }
});
Using this GraphMLIOHandler will now guarantee that set PortConstraints will be stored and reloaded.
Note
If you don't want to create the port constraint related DataProviders automatically when reading, you can leave out the initDataProvider calls in the sample. PortConstraintInputHandler itself will ignore port constraint data for which no DataProvider is resgistered.

If you are using the GraphML extension package for yFiles for Java:

GraphMLIOHandler ioh = new GraphMLIOHandler();
//yext.graphml.graph2D.PortConstraintInputHandler
ioh.addInputHandler( new PortConstraintInputHandler());
//yext.graphml.graph2D.PortConstraintOutputHandler
ioh.addOutputHandler(new PortConstraintOutputHandler(), GraphMLConstants.SCOPE_EDGE);
Using this GraphMLIOHandler will now guarantee that set PortConstraints will be stored and reloaded.
Note
PortConstraintInputHandler from the extension package will always add new DataProviders to the graph for the source and target port constraints if they are not yet registered. Also PortConstraintOutputHandler from the extension package expects that the respective data providers are registered

Keywords: GraphML - PortConstraint - store - save - file - PortConstraintInputHandler - PortConstraintOutputHandler - GraphMLIOHandler

Provide feedback:
How useful was this article?    less 1 2 3 4 5 more
Email address (optional):
COPYRIGHT © 2010 yWorks · ALL RIGHTS RESERVED imprint | top | home