How to Save Port Constraints to a GraphML File

Questions & Answers

Summary

This article describes how port constraints can be saved in a GraphML file.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

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.

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.

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

Categories this article belongs to:
GraphML > GraphML Extension Package > GraphML
yFiles for Java > yFiles Layout > Automatic Graph Layout > Advanced Layout Features
yFiles for Java > yFiles Viewer > GraphML Extension Package > GraphML
Applies to:
yFiles for Java 2: 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
GraphML: 2.4, 3.0
Keywords:
GraphML - PortConstraint - store - save - file - PortConstraintInputHandler - PortConstraintOutputHandler - GraphMLIOHandler