Пример #1
0
        public static void InputGraph(IGraph inputGraph, Stream inputStream, int bufferSize,
                                      string defaultEdgeLabel, string vertexIdKey, string edgeIdKey,
                                      string edgeLabelKey)
        {
            if (inputGraph == null)
            {
                throw new ArgumentNullException(nameof(inputGraph));
            }
            if (inputStream == null)
            {
                throw new ArgumentNullException(nameof(inputStream));
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentException("bufferSize must be greater than zero");
            }
            if (string.IsNullOrWhiteSpace(defaultEdgeLabel))
            {
                throw new ArgumentNullException(nameof(defaultEdgeLabel));
            }

            var graph = BatchGraph.Wrap(inputGraph, bufferSize);

            using (var r = new StreamReader(inputStream, Encoding.GetEncoding("ISO-8859-1")))
            {
                var st = new StreamTokenizer(r);

                try
                {
                    st.CommentChar(GmlTokens.CommentChar);
                    st.OrdinaryChar('[');
                    st.OrdinaryChar(']');

                    const string stringCharacters = "/\\(){}<>!£$%^&*-+=,.?:;@_`|~";
                    for (var i = 0; i < stringCharacters.Length; i++)
                    {
                        st.WordChars(stringCharacters.ElementAt(i), stringCharacters.ElementAt(i));
                    }

                    new GmlParser(graph, defaultEdgeLabel, vertexIdKey, edgeIdKey, edgeLabelKey).Parse(st);

                    graph.Commit();
                }
                catch (IOException e)
                {
                    throw new IOException(string.Concat("GML malformed line number ", st.LineNumber, ": "), e);
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Input the JSON stream data into the graph.
        ///     More control over how data is streamed is provided by this method.
        /// </summary>
        /// <param name="inputGraph">the graph to populate with the JSON data</param>
        /// <param name="jsonInputStream">a Stream of JSON data</param>
        /// <param name="bufferSize">the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)</param>
        /// <param name="edgePropertyKeys"></param>
        /// <param name="vertexPropertyKeys"></param>
        public static void InputGraph(IGraph inputGraph, Stream jsonInputStream, int bufferSize,
                                      IEnumerable <string> edgePropertyKeys, IEnumerable <string> vertexPropertyKeys)
        {
            if (inputGraph == null)
            {
                throw new ArgumentNullException(nameof(inputGraph));
            }
            if (jsonInputStream == null)
            {
                throw new ArgumentNullException(nameof(jsonInputStream));
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentException("bufferSize must be greater than zero");
            }

            StreamReader sr = null;

            try
            {
                sr = new StreamReader(jsonInputStream);

                using (var jp = new JsonTextReader(sr))
                {
                    sr = null;
                    // if this is a transactional graph then we're buffering
                    var graph          = BatchGraph.Wrap(inputGraph, bufferSize);
                    var elementFactory = new GraphElementFactory(graph);

                    // ReSharper disable PossibleMultipleEnumeration
                    var graphson = new GraphSonUtility(GraphSonMode.NORMAL, elementFactory, vertexPropertyKeys,
                                                       edgePropertyKeys);
                    // ReSharper restore PossibleMultipleEnumeration

                    var serializer = JsonSerializer.Create(null);

                    while (jp.Read() && jp.TokenType != JsonToken.EndObject)
                    {
                        var fieldname = Convert.ToString(jp.Value);
                        switch (fieldname)
                        {
                        case GraphSonTokens.Mode:
                        {
                            var mode = (GraphSonMode)Enum.Parse(typeof(GraphSonMode), jp.ReadAsString());
                            // ReSharper disable PossibleMultipleEnumeration
                            graphson = new GraphSonUtility(mode, elementFactory, vertexPropertyKeys,
                                                           edgePropertyKeys);
                            // ReSharper restore PossibleMultipleEnumeration
                        }
                        break;

                        case GraphSonTokens.Vertices:
                            jp.Read();
                            while (jp.Read() && jp.TokenType != JsonToken.EndArray)
                            {
                                var node = (JObject)serializer.Deserialize(jp);
                                graphson.VertexFromJson(node);
                            }
                            break;

                        case GraphSonTokens.Edges:
                            jp.Read();
                            while (jp.Read() && jp.TokenType != JsonToken.EndArray)
                            {
                                var node  = (JObject)serializer.Deserialize(jp);
                                var idIn  = GraphSonUtility.GetTypedValueFromJsonNode(node[GraphSonTokens.InV]);
                                var idOut = GraphSonUtility.GetTypedValueFromJsonNode(node[GraphSonTokens.OutV]);
                                if (idIn == null || idOut == null)
                                {
                                    continue;
                                }
                                var inV  = graph.GetVertex(idIn);
                                var outV = graph.GetVertex(idOut);
                                graphson.EdgeFromJson(node, outV, inV);
                            }
                            break;
                        }
                    }

                    graph.Commit();
                }
            }
            finally
            {
                if (sr != null)
                {
                    sr.Dispose();
                }
            }
        }
Пример #3
0
        /// <summary>
        ///     Input the GraphML stream data into the graph.
        ///     More control over how data is streamed is provided by this method.
        /// </summary>
        /// <param name="inputGraph">the graph to populate with the GraphML data</param>
        /// <param name="graphMlInputStream">a Stream of GraphML data</param>
        /// <param name="bufferSize">the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)</param>
        /// <param name="vertexIdKey">if the id of a vertex is a &lt;data/&gt; property, fetch it from the data property.</param>
        /// <param name="edgeIdKey">if the id of an edge is a &lt;data/&gt; property, fetch it from the data property.</param>
        /// <param name="edgeLabelKey">if the label of an edge is a &lt;data/&gt; property, fetch it from the data property.</param>
        public static void InputGraph(IGraph inputGraph, Stream graphMlInputStream, int bufferSize, string vertexIdKey,
                                      string edgeIdKey, string edgeLabelKey)
        {
            if (inputGraph == null)
            {
                throw new ArgumentNullException(nameof(inputGraph));
            }
            if (graphMlInputStream == null)
            {
                throw new ArgumentNullException(nameof(graphMlInputStream));
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentException("bufferSize must be greater than zero");
            }

            using (var reader = XmlReader.Create(graphMlInputStream))
            {
                var graph = BatchGraph.Wrap(inputGraph, bufferSize);

                var keyIdMap     = new Dictionary <string, string>();
                var keyTypesMaps = new Dictionary <string, string>();
                // <Mapped ID string, ID object>

                // <Default ID string, Mapped ID string>
                var vertexMappedIdMap = new Dictionary <string, string>();

                // Buffered Vertex Data
                string vertexId = null;
                Dictionary <string, object> vertexProps = null;
                bool inVertex = false;

                // Buffered Edge Data
                string    edgeId                      = null;
                string    edgeLabel                   = null;
                IVertex[] edgeEndVertices             = null; //[0] = outVertex , [1] = inVertex
                Dictionary <string, object> edgeProps = null;
                var inEdge = false;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        var elementName = reader.Name;

                        switch (elementName)
                        {
                        case GraphMlTokens.Key:
                        {
                            var id            = reader.GetAttribute(GraphMlTokens.Id);
                            var attributeName = reader.GetAttribute(GraphMlTokens.AttrName);
                            var attributeType = reader.GetAttribute(GraphMlTokens.AttrType);
                            keyIdMap[id]     = attributeName;
                            keyTypesMaps[id] = attributeType;
                        }
                        break;

                        case GraphMlTokens.Node:
                            vertexId = reader.GetAttribute(GraphMlTokens.Id);
                            if (vertexIdKey != null)
                            {
                                vertexMappedIdMap[vertexId] = vertexId;
                            }
                            inVertex    = true;
                            vertexProps = new Dictionary <string, object>();
                            break;

                        case GraphMlTokens.Edge:
                        {
                            edgeId    = reader.GetAttribute(GraphMlTokens.Id);
                            edgeLabel = reader.GetAttribute(GraphMlTokens.Label);
                            edgeLabel = edgeLabel ?? GraphMlTokens.Default;

                            var vertexIds = new string[2];
                            vertexIds[0]    = reader.GetAttribute(GraphMlTokens.Source);
                            vertexIds[1]    = reader.GetAttribute(GraphMlTokens.Target);
                            edgeEndVertices = new IVertex[2];

                            for (var 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])
                                {
                                    continue;
                                }

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

                            inEdge    = true;
                            edgeProps = new Dictionary <string, object>();
                        }
                        break;

                        case GraphMlTokens.Data:
                        {
                            var key           = reader.GetAttribute(GraphMlTokens.Key);
                            var attributeName = keyIdMap.Get(key);

                            if (attributeName != null)
                            {
                                reader.Read();
                                var value = reader.Value;

                                if (inVertex)
                                {
                                    if ((vertexIdKey != null) && (key == vertexIdKey))
                                    {
                                        // Should occur at most once per Vertex
                                        // Assumes single ID prop per Vertex
                                        vertexMappedIdMap[vertexId] = value;
                                        vertexId = value;
                                    }
                                    else if (vertexProps != null)
                                    {
                                        vertexProps[attributeName] = TypeCastValue(key, value, keyTypesMaps);
                                    }
                                }
                                else if (inEdge)
                                {
                                    if ((edgeLabelKey != null) && (key == edgeLabelKey))
                                    {
                                        edgeLabel = value;
                                    }
                                    else if ((edgeIdKey != null) && (key == edgeIdKey))
                                    {
                                        edgeId = value;
                                    }
                                    else if (edgeProps != null)
                                    {
                                        edgeProps[attributeName] = TypeCastValue(key, value, keyTypesMaps);
                                    }
                                }
                            }
                        }
                        break;
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        var elementName = reader.Name;

                        switch (elementName)
                        {
                        case GraphMlTokens.Node:
                            if (vertexId != null)
                            {
                                var currentVertex = graph.GetVertex(vertexId) ?? graph.AddVertex(vertexId);
                                if (vertexProps != null)
                                {
                                    foreach (var prop in vertexProps)
                                    {
                                        currentVertex.SetProperty(prop.Key, prop.Value);
                                    }
                                }

                                vertexId    = null;
                                vertexProps = null;
                                inVertex    = false;
                            }
                            break;

                        case GraphMlTokens.Edge:
                            if (edgeEndVertices.Length > 1)
                            {
                                var currentEdge = graph.AddEdge(edgeId, edgeEndVertices[0], edgeEndVertices[1], edgeLabel);

                                if (edgeProps != null)
                                {
                                    foreach (var prop in edgeProps)
                                    {
                                        currentEdge.SetProperty(prop.Key, prop.Value);
                                    }
                                }

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

                graph.Commit();
            }
        }
Пример #4
0
        /// <summary>
        ///     Input the GraphJson stream data into the graph.
        ///     More control over how data is streamed is provided by this method.
        /// </summary>
        /// <param name="inputGraph">the graph to populate with the GraphJson data</param>
        /// <param name="jsonInputStream">a Stream of GraphJson data</param>
        /// <param name="bufferSize">the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)</param>
        /// <param name="settings">Contains field names that the reader will use to parse the graph</param>
        public static void InputGraph(IGraph inputGraph, Stream jsonInputStream, int bufferSize, GraphJsonSettings settings)
        {
            if (inputGraph == null)
            {
                throw new ArgumentNullException(nameof(inputGraph));
            }
            if (jsonInputStream == null)
            {
                throw new ArgumentNullException(nameof(jsonInputStream));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentException("bufferSize must be greater than zero");
            }

            StreamReader sr = null;

            try
            {
                sr = new StreamReader(jsonInputStream);

                using (var jp = new JsonTextReader(sr))
                {
                    sr = null;
                    // if this is a transactional graph then we're buffering
                    var graph      = BatchGraph.Wrap(inputGraph, bufferSize);
                    var serializer = JsonSerializer.Create(null);

                    while (jp.Read() && jp.TokenType != JsonToken.EndObject)
                    {
                        var fieldname = Convert.ToString(jp.Value);
                        switch (fieldname)
                        {
                        case "nodes":
                            jp.Read();
                            while (jp.Read() && jp.TokenType != JsonToken.EndArray)
                            {
                                var    props = new Dictionary <string, object>();
                                var    node  = (JObject)serializer.Deserialize(jp);
                                object id    = null;
                                foreach (var val in node)
                                {
                                    if (val.Key == settings.IdProp)
                                    {
                                        id = val.Value.ToObject <object>();
                                    }
                                    else
                                    {
                                        props.Add(val.Key, val.Value.ToObject <object>());
                                    }
                                }
                                var vertex = graph.AddVertex(id);
                                vertex.SetProperties(props);
                            }
                            break;

                        case "edges":
                            jp.Read();
                            while (jp.Read() && jp.TokenType != JsonToken.EndArray)
                            {
                                var    props   = new Dictionary <string, object>();
                                var    node    = (JObject)serializer.Deserialize(jp);
                                object id      = null;
                                object source  = null;
                                object target  = null;
                                var    caption = string.Empty;
                                foreach (var val in node)
                                {
                                    if (val.Key == settings.IdProp)
                                    {
                                        id = val.Value.ToObject <object>();
                                    }
                                    else if (val.Key == settings.EdgeCaptionProp)
                                    {
                                        caption = val.Value.ToString();
                                    }
                                    else if (val.Key == settings.SourceProp)
                                    {
                                        source = val.Value.ToObject <object>();
                                    }
                                    else if (val.Key == settings.TargetProp)
                                    {
                                        target = val.Value.ToObject <object>();
                                    }
                                    else
                                    {
                                        props.Add(val.Key, val.Value.ToObject <object>());
                                    }
                                }
                                if (source == null)
                                {
                                    throw new IOException("Edge has no source");
                                }
                                if (target == null)
                                {
                                    throw new IOException("Edge has no target");
                                }
                                var edge = graph.AddEdge(id, graph.GetVertex(source), graph.GetVertex(target), caption);
                                edge.SetProperties(props);
                            }
                            break;
                        }
                    }

                    graph.Commit();
                }
            }
            finally
            {
                if (sr != null)
                {
                    sr.Dispose();
                }
            }
        }