Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *
   *  *  Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
   *  *
   *  *  Licensed under the Apache License, Version 2.0 (the "License");
   *  *  you may not use this file except in compliance with the License.
   *  *  You may obtain a copy of the License at
   *  *
   *  *       http://www.apache.org/licenses/LICENSE-2.0
  *  *
  *  *  Unless required by applicable law or agreed to in writing, software
  *  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  *  See the License for the specific language governing permissions and
  *  *  limitations under the License.
  *  *
  *  * For more information: http://www.orientechnologies.com
  *  
  */
 
 package com.orientechnologies.orient.graph.console;
 
 
 import java.util.List;
 import java.util.Map;

GraphMLReader writes the data from a GraphML stream to a graph. Derived from Blueprints GraphMLReader. Supports also vertex labels.

Author(s):
Luca Garulli (l.garulli(at)orientechnologies.com)
Marko A. Rodriguez (http://markorodriguez.com)
Alex Averbuch (alex.averbuch-at-gmail.com)
Joshua Shinavier (http://fortytwo.net)
 
 public class OGraphMLReader {
   private static final String   LABELS           = "labels";
   private final OrientBaseGraph graph;
   private int                   vertexLabelIndex = 0;
   private String                vertexIdKey      = "id";
   private String                edgeIdKey        = null;
   private String                edgeLabelKey     = null;
   private boolean               storeVertexIds   = false;

  

Parameters:
graph the graph to populate with the GraphML data
 
   public OGraphMLReader(OrientBaseGraph graph) {
     this. = graph;
   }

  
Input the GraphML stream data into the graph. In practice, usually the provided graph is empty.

Parameters:
inputGraph the graph to populate with the GraphML data
graphMLInputStream an InputStream of GraphML data
Throws:
java.io.IOException thrown when the GraphML data is not correctly formatted
 
   public void inputGraph(final Graph inputGraphfinal InputStream graphMLInputStreamthrows IOException {
     inputGraph(inputGraphgraphMLInputStream, 1000, nullnullnull);
   }

  
Input the GraphML stream data into the graph. In practice, usually the provided graph is empty.

Parameters:
inputGraph the graph to populate with the GraphML data
filename name of a file containing GraphML data
Throws:
java.io.IOException thrown when the GraphML data is not correctly formatted
 
   public void inputGraph(final Graph inputGraphfinal String filenamethrows IOException {
     inputGraph(inputGraphfilename, 1000, nullnull);
   }

  
Input the GraphML stream data into the graph. More control over how data is streamed is provided by this method.

Parameters:
inputGraph the graph to populate with the GraphML data
filename name of a file containing GraphML data
bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
vertexIdKey if the id of a vertex is a <data/> property, fetch it from the data property.
edgeIdKey if the id of an edge is a <data/> property, fetch it from the data property.
edgeLabelKey if the label of an edge is a <data/> property, fetch it from the data property.
Throws:
java.io.IOException thrown when the GraphML data is not correctly formatted
  public void inputGraph(final Graph inputGraphfinal String filenameint bufferSizeString vertexIdKeyString edgeIdKey,
      String edgeLabelKeythrows IOException {
    FileInputStream fis = new FileInputStream(filename);
    try {
      inputGraph(inputGraphfisbufferSizevertexIdKeyedgeIdKeyedgeLabelKey);
    } finally {
      fis.close();
    }
  }

  
Input the GraphML stream data into the graph. More control over how data is streamed is provided by this method.

Parameters:
inputGraph the graph to populate with the GraphML data
graphMLInputStream an InputStream of GraphML data
bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
vertexIdKey if the id of a vertex is a <data/> property, fetch it from the data property.
edgeIdKey if the id of an edge is a <data/> property, fetch it from the data property.
edgeLabelKey if the label of an edge is a <data/> property, fetch it from the data property.
Throws:
java.io.IOException thrown when the GraphML data is not correctly formatted
  public void inputGraph(final Graph inputGraphfinal InputStream graphMLInputStreamint bufferSizeString vertexIdKey,
      String edgeIdKeyString edgeLabelKeythrows IOException {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {
      XMLStreamReader reader = inputFactory.createXMLStreamReader(graphMLInputStream);
      final OrientBaseGraph graph = (OrientBaseGraphinputGraph;
      if ()
        graph.setSaveOriginalIds();
      Map<StringStringkeyIdMap = new HashMap<StringString>();
      Map<StringStringkeyTypesMaps = new HashMap<StringString>();
      // <Mapped ID String, ID Object>
      // <Default ID String, Mapped ID String>
      Map<StringORIDvertexMappedIdMap = new HashMap<StringORID>();
      // Buffered Vertex Data
      String vertexId = null;
      Map<StringObjectvertexProps = null;
      boolean inVertex = false;
      // Buffered Edge Data
      String edgeId = null;
      String edgeLabel = null;
      String vertexLabel = null;
      Vertex[] edgeEndVertices = null// [0] = outVertex , [1] = inVertex
      Map<StringObjectedgeProps = null;
      boolean inEdge = false;
      int bufferCounter = 0;
      while (reader.hasNext()) {
        Integer eventType = reader.next();
        if (eventType.equals(.)) {
          String elementName = reader.getName().getLocalPart();
          if (elementName.equals(.)) {
            String id = reader.getAttributeValue(null.);
            String attributeName = reader.getAttributeValue(null.);
            String attributeType = reader.getAttributeValue(null.);
            keyIdMap.put(idattributeName);
            keyTypesMaps.put(idattributeType);
          } else if (elementName.equals(.)) {
            vertexId = reader.getAttributeValue(null.);
            vertexLabel = reader.getAttributeValue(null);
            if (vertexLabel != null) {
              if (vertexLabel.startsWith(":"))
                // REMOVE : AS PREFIX
                vertexLabel = vertexLabel.substring(1);
              final String[] vertexLabels = vertexLabel.split(":");
              // GET ONLY FIRST LABEL AS CLASS
              vertexLabel = vertexId + ",class:" + vertexLabels[];
            } else
              vertexLabel = vertexId;
            inVertex = true;
            vertexProps = new HashMap<StringObject>();
          } else if (elementName.equals(.)) {
            edgeId = reader.getAttributeValue(null.);
            edgeLabel = reader.getAttributeValue(null.);
            edgeLabel = edgeLabel == null ? . : edgeLabel;
            String[] vertexIds = new String[2];
            vertexIds[0] = reader.getAttributeValue(null.);
            vertexIds[1] = reader.getAttributeValue(null.);
            edgeEndVertices = new Vertex[2];
            for (int i = 0; i < 2; i++) { // i=0 => outVertex, i=1 => inVertex
              if (vertexIdKey == null) {
                edgeEndVertices[i] = null;
              } else {
                edgeEndVertices[i] = graph.getVertex(vertexMappedIdMap.get(vertexIds[i]));
              }
              if (null == edgeEndVertices[i]) {
                edgeEndVertices[i] = graph.addVertex(vertexLabel);
                if (vertexIdKey != null) {
                  mapId(vertexMappedIdMapvertexIds[i], (ORIDedgeEndVertices[i].getId());
                }
                bufferCounter++;
              }
            }
            inEdge = true;
            vertexLabel = null;
            edgeProps = new HashMap<StringObject>();
          } else if (elementName.equals(.)) {
            String key = reader.getAttributeValue(null.);
            String attributeName = keyIdMap.get(key);
            if (attributeName != null) {
              String value = reader.getElementText();
              if (inVertex == true) {
                if ((vertexIdKey != null) && (key.equals(vertexIdKey))) {
                  // Should occur at most once per Vertex
                  vertexId = value;
                } else
                  vertexProps.put(attributeNametypeCastValue(keyvaluekeyTypesMaps));
              } else if (inEdge == true) {
                if ((edgeLabelKey != null) && (key.equals(edgeLabelKey)))
                  edgeLabel = value;
                else if ((edgeIdKey != null) && (key.equals(edgeIdKey)))
                  edgeId = value;
                else
                  edgeProps.put(attributeNametypeCastValue(keyvaluekeyTypesMaps));
              }
            }
          }
        } else if (eventType.equals(.)) {
          String elementName = reader.getName().getLocalPart();
          if (elementName.equals(.)) {
            ORID currentVertex = null;
            if (vertexIdKey != null)
              vertexMappedIdMap.get(vertexId);
            if (currentVertex == null) {
              final OrientVertex v = graph.addVertex(vertexLabelvertexProps);
              if (vertexIdKey != null)
                mapId(vertexMappedIdMapvertexIdv.getIdentity());
              bufferCounter++;
            }
            vertexId = null;
            vertexLabel = null;
            vertexProps = null;
            inVertex = false;
          } else if (elementName.equals(.)) {
            Edge currentEdge = ((OrientVertexedgeEndVertices[0]).addEdge(null, (OrientVertexedgeEndVertices[1], edgeLabel,
                nulledgeProps);
            bufferCounter++;
            edgeId = null;
            edgeLabel = null;
            edgeEndVertices = null;
            edgeProps = null;
            inEdge = false;
          }
        }
        if (bufferCounter > bufferSize) {
          graph.commit();
          bufferCounter = 0;
        }
      }
      reader.close();
      graph.commit();
    } catch (XMLStreamException xse) {
      throw new IOException(xse);
    }
  }
  public int getVertexLabelIndex() {
    return ;
  }
  public void setVertexLabelIndex(int vertexLabelIndex) {
    this. = vertexLabelIndex;
  }

  

Parameters:
vertexIdKey if the id of a vertex is a <data/> property, fetch it from the data property.
  public void setVertexIdKey(String vertexIdKey) {
    this. = vertexIdKey;
  }

  

Parameters:
edgeIdKey if the id of an edge is a <data/> property, fetch it from the data property.
  public void setEdgeIdKey(String edgeIdKey) {
    this. = edgeIdKey;
  }

  

Parameters:
edgeLabelKey if the label of an edge is a <data/> property, fetch it from the data property.
  public void setEdgeLabelKey(String edgeLabelKey) {
    this. = edgeLabelKey;
  }

  
Input the GraphML stream data into the graph. In practice, usually the provided graph is empty.

Parameters:
graphMLInputStream an InputStream of GraphML data
Throws:
java.io.IOException thrown when the GraphML data is not correctly formatted
  public void inputGraph(final InputStream graphMLInputStreamthrows IOException {
    inputGraph(this.graphMLInputStream, 1000, this.this.this.);
  }

  
Input the GraphML stream data into the graph. In practice, usually the provided graph is empty.

Parameters:
filename name of a file containing GraphML data
Throws:
java.io.IOException thrown when the GraphML data is not correctly formatted
  public void inputGraph(final String filenamethrows IOException {
    inputGraph(this.filename, 1000, this.this.this.);
  }

  
Input the GraphML stream data into the graph. In practice, usually the provided graph is empty.

Parameters:
graphMLInputStream an InputStream of GraphML data
bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
Throws:
java.io.IOException thrown when the GraphML data is not correctly formatted
  public void inputGraph(final InputStream graphMLInputStreamint bufferSizethrows IOException {
    inputGraph(this.graphMLInputStreambufferSizethis.this.this.);
  }

  
Input the GraphML stream data into the graph. In practice, usually the provided graph is empty.

Parameters:
filename name of a file containing GraphML data
bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
Throws:
java.io.IOException thrown when the GraphML data is not correctly formatted
  public void inputGraph(final String filenameint bufferSizethrows IOException {
    inputGraph(this.filenamebufferSizethis.this.this.);
  }
  public OGraphMLReader setOptions(final Map<StringList<String>> opts) {
    for (Map.Entry<StringList<String>> opt : opts.entrySet()) {
      if (opt.getKey().equalsIgnoreCase("storeVertexIds")) {
         = Boolean.parseBoolean(opt.getValue().get(0));
      }
    }
    return this;
  }
  protected void mapId(final Map<StringORIDvertexMappedIdMapfinal String vertexIdfinal ORID rid) {
    if (vertexMappedIdMap.containsKey(vertexId))
      throw new IllegalArgumentException("Vertex with id '" + vertexId + "' has been already loaded");
    vertexMappedIdMap.put(vertexIdrid);
  }
  private Object typeCastValue(String keyString valueMap<StringStringkeyTypes) {
    String type = keyTypes.get(key);
    if (null == type || type.equals(.))
      return value;
    else if (type.equals(.))
      return Float.valueOf(value);
    else if (type.equals(.))
      return Integer.valueOf(value);
    else if (type.equals(.))
      return Double.valueOf(value);
    else if (type.equals(.))
      return Boolean.valueOf(value);
    else if (type.equals(.))
      return Long.valueOf(value);
    else
      return value;
  }
New to GrepCode? Check out our FAQ X