Package com.tinkerpop.blueprints.util.wrappers.batch

Examples of com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph


      Vertex vertexIn, vertexOut;
      long faunusIdIn, faunusIdOut, blueprintsIdIn, blueprintsIdOut;
      HashMap<Long, Long> faunusToBlueprintsId = new HashMap<Long, Long>();

      // if this is a transactional graph then we're buffering
      final BatchGraph graph = BatchGraph.wrap(inputGraph, bufferSize);

      // load list of vertices
      List<FaunusVertex> faunusVertexList = FaunusGraphSONUtility.fromJSON(inputStream);

      // add vertices w/ properties to graph, also saving id->id mapping for edge creation
      Vertex blueprintsVertex;
      for (FaunusVertex faunusVertex : faunusVertexList) {
        blueprintsVertex = graph.addVertex(faunusVertex.getIdAsLong());
        for (String property : faunusVertex.getPropertyKeys()) {
          blueprintsVertex.setProperty(property, faunusVertex.getProperty(property));
        }
        faunusToBlueprintsId.put(faunusVertex.getIdAsLong(), (Long) blueprintsVertex.getId());
      }

      // add edges between vertices
      for (FaunusVertex faunusVertex : faunusVertexList) {
        for (Edge edge : faunusVertex.getEdges(Direction.BOTH)) {
            fe = (FaunusEdge) edge;

            // retrieve the vertices stored in the graph
            faunusIdIn = fe.getVertexId(Direction.IN);
            blueprintsIdIn = faunusToBlueprintsId.get(faunusIdIn);
            faunusIdOut = fe.getVertexId(Direction.OUT);
            blueprintsIdOut = faunusToBlueprintsId.get(faunusIdOut);
            vertexIn = graph.getVertex(blueprintsIdIn);
            vertexOut = graph.getVertex(blueprintsIdOut);

            // save the edge to the graph
            graph.addEdge(null, vertexIn, vertexOut, fe.getLabel());
        }
      }

      // commit changes to the graph
      graph.commit();
    }
View Full Code Here


     * Initialise the handler using the graph passed as arguments.
     *
     * @param graph
     */
    public TitanRDFHandler(TitanGraph graph, String indexes) {
        this.graph = new BatchGraph(graph);

        if (!graph.getIndexedKeys(Vertex.class).contains(VALUE)) {
            graph.createKeyIndex(VALUE, Vertex.class);
        }

View Full Code Here

                                  final Set<String> edgePropertyKeys, final Set<String> vertexPropertyKeys) throws IOException {

        final JsonParser jp = jsonFactory.createJsonParser(jsonInputStream);

        // if this is a transactional graph then we're buffering
        final BatchGraph graph = BatchGraph.wrap(inputGraph, bufferSize);

        final ElementFactory elementFactory = new GraphElementFactory(graph);
        GraphSONUtility graphson = new GraphSONUtility(GraphSONMode.NORMAL, elementFactory,
                vertexPropertyKeys, edgePropertyKeys);

        while (jp.nextToken() != JsonToken.END_OBJECT) {
            final String fieldname = jp.getCurrentName() == null ? "" : jp.getCurrentName();
            if (fieldname.equals(GraphSONTokens.MODE)) {
                jp.nextToken();
                final GraphSONMode mode = GraphSONMode.valueOf(jp.getText());
                graphson = new GraphSONUtility(mode, elementFactory, vertexPropertyKeys, edgePropertyKeys);
            } else if (fieldname.equals(GraphSONTokens.VERTICES)) {
                jp.nextToken();
                while (jp.nextToken() != JsonToken.END_ARRAY) {
                    final JsonNode node = jp.readValueAsTree();
                    graphson.vertexFromJson(node);
                }
            } else if (fieldname.equals(GraphSONTokens.EDGES)) {
                jp.nextToken();
                while (jp.nextToken() != JsonToken.END_ARRAY) {
                    final JsonNode node = jp.readValueAsTree();
                    final Vertex inV = graph.getVertex(GraphSONUtility.getTypedValueFromJsonNode(node.get(GraphSONTokens._IN_V)));
                    final Vertex outV = graph.getVertex(GraphSONUtility.getTypedValueFromJsonNode(node.get(GraphSONTokens._OUT_V)));
                    graphson.edgeFromJson(node, outV, inV);
                }
            }
        }

        jp.close();

        graph.commit();
        ;
    }
View Full Code Here

     * @throws IOException thrown if the data is not valid
     */
    public static void inputGraph(final Graph inputGraph, final InputStream inputStream, final int bufferSize,
                                  final String defaultEdgeLabel, final String vertexIdKey, final String edgeIdKey,
                                  final String edgeLabelKey) throws IOException {
        final BatchGraph graph = BatchGraph.wrap(inputGraph, bufferSize);

        final Reader r = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("ISO-8859-1")));
        final StreamTokenizer st = new StreamTokenizer(r);

        try {
            st.commentChar(GMLTokens.COMMENT_CHAR);
            st.ordinaryChar('[');
            st.ordinaryChar(']');

            final String stringCharacters = "/\\(){}<>!£$%^&*-+=,.?:;@_`|~";
            for (int i = 0; i < stringCharacters.length(); i++) {
                st.wordChars(stringCharacters.charAt(i), stringCharacters.charAt(i));
            }

            new GMLParser(graph, defaultEdgeLabel, vertexIdKey, edgeIdKey, edgeLabelKey).parse(st);

            graph.commit();

        } catch (IOException e) {
            throw new IOException("GML malformed line number " + st.lineno() + ": ", e);
        } finally {
            r.close();
View Full Code Here

        XMLInputFactory inputFactory = XMLInputFactory.newInstance();

        try {
            XMLStreamReader reader = inputFactory.createXMLStreamReader(graphMLInputStream);

            final BatchGraph graph = BatchGraph.wrap(inputGraph, bufferSize);

            Map<String, String> keyIdMap = new HashMap<String, String>();
            Map<String, String> keyTypesMaps = new HashMap<String, String>();
            // <Mapped ID String, ID Object>

            // <Default ID String, Mapped ID String>
            Map<String, String> vertexMappedIdMap = new HashMap<String, String>();

            // Buffered Vertex Data
            String vertexId = null;
            Map<String, Object> vertexProps = null;
            boolean inVertex = false;

            // Buffered Edge Data
            String edgeId = null;
            String edgeLabel = null;
            Vertex[] edgeEndVertices = null; //[0] = outVertex , [1] = inVertex
            Map<String, Object> edgeProps = null;
            boolean inEdge = false;

            while (reader.hasNext()) {

                Integer eventType = reader.next();
                if (eventType.equals(XMLEvent.START_ELEMENT)) {
                    String elementName = reader.getName().getLocalPart();

                    if (elementName.equals(GraphMLTokens.KEY)) {
                        String id = reader.getAttributeValue(null, GraphMLTokens.ID);
                        String attributeName = reader.getAttributeValue(null, GraphMLTokens.ATTR_NAME);
                        String attributeType = reader.getAttributeValue(null, GraphMLTokens.ATTR_TYPE);
                        keyIdMap.put(id, attributeName);
                        keyTypesMaps.put(id, attributeType);

                    } else if (elementName.equals(GraphMLTokens.NODE)) {
                        vertexId = reader.getAttributeValue(null, GraphMLTokens.ID);
                        if (vertexIdKey != null)
                            vertexMappedIdMap.put(vertexId, vertexId);
                        inVertex = true;
                        vertexProps = new HashMap<String, Object>();

                    } else if (elementName.equals(GraphMLTokens.EDGE)) {
                        edgeId = reader.getAttributeValue(null, GraphMLTokens.ID);
                        edgeLabel = reader.getAttributeValue(null, GraphMLTokens.LABEL);
                        edgeLabel = edgeLabel == null ? GraphMLTokens._DEFAULT : edgeLabel;

                        String[] vertexIds = new String[2];
                        vertexIds[0] = reader.getAttributeValue(null, GraphMLTokens.SOURCE);
                        vertexIds[1] = reader.getAttributeValue(null, GraphMLTokens.TARGET);
                        edgeEndVertices = new Vertex[2];

                        for (int i = 0; i < 2; i++) { //i=0 => outVertex, i=1 => inVertex
                            if (vertexIdKey == null) {
                                edgeEndVertices[i] = graph.getVertex(vertexIds[i]);
                            } else {
                                edgeEndVertices[i] = graph.getVertex(vertexMappedIdMap.get(vertexIds[i]));
                            }

                            if (null == edgeEndVertices[i]) {
                                edgeEndVertices[i] = graph.addVertex(vertexIds[i]);
                                if (vertexIdKey != null)
                                    // Default to standard ID system (in case no mapped
                                    // ID is found later)
                                    vertexMappedIdMap.put(vertexIds[i], vertexIds[i]);
                            }
                        }

                        inEdge = true;
                        edgeProps = new HashMap<String, Object>();

                    } else if (elementName.equals(GraphMLTokens.DATA)) {
                        String key = reader.getAttributeValue(null, GraphMLTokens.KEY);
                        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
                                    // Assumes single ID prop per Vertex
                                    vertexMappedIdMap.put(vertexId, value);
                                    vertexId = value;
                                } else
                                    vertexProps.put(attributeName, typeCastValue(key, value, keyTypesMaps));
                            } else if (inEdge == true) {
                                if ((edgeLabelKey != null) && (key.equals(edgeLabelKey)))
                                    edgeLabel = value;
                                else if ((edgeIdKey != null) && (key.equals(edgeIdKey)))
                                    edgeId = value;
                                else
                                    edgeProps.put(attributeName, typeCastValue(key, value, keyTypesMaps));
                            }
                        }

                    }
                } else if (eventType.equals(XMLEvent.END_ELEMENT)) {
                    String elementName = reader.getName().getLocalPart();

                    if (elementName.equals(GraphMLTokens.NODE)) {
                        Vertex currentVertex = graph.getVertex(vertexId);
                        if (currentVertex == null) {
                            currentVertex = graph.addVertex(vertexId);
                        }

                        for (Entry<String, Object> prop : vertexProps.entrySet()) {
                            currentVertex.setProperty(prop.getKey(), prop.getValue());
                        }

                        vertexId = null;
                        vertexProps = null;
                        inVertex = false;
                    } else if (elementName.equals(GraphMLTokens.EDGE)) {
                        Edge currentEdge = graph.addEdge(edgeId, edgeEndVertices[0], edgeEndVertices[1], edgeLabel);

                        for (Entry<String, Object> prop : edgeProps.entrySet()) {
                            currentEdge.setProperty(prop.getKey(), prop.getValue());
                        }

                        edgeId = null;
                        edgeLabel = null;
                        edgeEndVertices = null;
                        edgeProps = null;
                        inEdge = false;
                    }

                }
            }

            reader.close();

            graph.commit();
            ;
        } catch (XMLStreamException xse) {
            throw new IOException(xse);
        }
    }
View Full Code Here

TOP

Related Classes of com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.