Exemplo n.º 1
0
        public void Create_When_T_Is_ISymmetricMatrix_of_double()
        {
            //Arrange
            var provider = new NetworkFactoryProvider();

            //Act
            INetworkFactory factory = provider.Create<ISymmetricMatrix<double>>();

            //Assert
            Assert.NotNull(factory);
            Assert.IsType<SymmetricMatrixFactory<double>>(factory);
        }
Exemplo n.º 2
0
        public void Create_When_T_Is_IBasicAdjList()
        {
            //Arrange
            var provider = new NetworkFactoryProvider();

            //Act
            INetworkFactory factory = provider.Create<IBasicAdjList>();

            //Assert
            Assert.NotNull(factory);
            Assert.IsType<BasicAdjListFactory>(factory);
        }
Exemplo n.º 3
0
        public void Create_Where_Param_Is_IFullMatrix_of_double()
        {
            //Arrange
            var provider = new NetworkFactoryProvider();
            var mockNetwork = new Mock<IFullMatrix<double>>();

            //Act
            var factory = provider.Create(mockNetwork.Object);

            //Assert
            Assert.NotNull(factory);
            Assert.IsType<FullMatrixFactory<double>>(factory);
        }
Exemplo n.º 4
0
        public void Create_Where_Param_Is_IBasicAdjList()
        {
            //Arrange
            var provider = new NetworkFactoryProvider();
            var mockNetwork = new Mock<IBasicAdjList>();

            //Act
            var factory = provider.Create(mockNetwork.Object);

            //Assert
            Assert.NotNull(factory);
            Assert.IsType<BasicAdjListFactory>(factory);
        }
Exemplo n.º 5
0
        internal IBasicAdjList InitializeNetwork(int nodeCount, int edgeCount)
        {
            // check for and remove duplicates before data attrib tables are created
            RemoveDuplicatesFromSecondList(NodeDataHeaders, NodePropertyHeaders);
            RemoveDuplicatesFromSecondList(EdgeDataHeaders, EdgePropertyHeaders);
            //----
            var helper = new KnownHeaderNamesReaderHelper();
            if(DisableDataConversions)
            {
                TranslateHeadersToStrings(NodeDataHeaders);
                TranslateHeadersToStrings(NodePropertyHeaders);
                TranslateHeadersToStrings(EdgeDataHeaders);
                TranslateHeadersToStrings(EdgePropertyHeaders);
            }
            else
            {
                TranslateNodeHeaderNames(NodeDataHeaders, helper);
                TranslateNodeHeaderNames(NodePropertyHeaders, helper);
                TranslateEdgeHeaderNames(EdgeDataHeaders, helper);
                TranslateEdgeHeaderNames(EdgePropertyHeaders, helper);
                CheckForAndManageNodeSizeCase(NodePropertyHeaders);
            }

            IBasicAdjList net = null;
            using (var p = new NetworkFactoryProvider())
            {
                using (var fac = p.Create<IBasicAdjList>())
                {
                    fac.EnableNodeDataAttributes = true;
                    fac.EnableEdgeDataAttributes = true;
                    fac.NetworkName = Path.GetFileNameWithoutExtension(File);
                    net = fac.CreateNetwork(Guid.NewGuid(), nodeCount) as IBasicAdjList;
                }
            }

            CreateNodeAttribs(net, NodeDataHeaders);
            CreateNodeAttribs(net, NodePropertyHeaders);
            CreateEdgeAttribs(net, EdgeDataHeaders);
            CreateEdgeAttribs(net, EdgePropertyHeaders);

            return net;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reads the graph element attributes and creates the network, then does 1 pass to read the nodes.
        /// </summary>
        /// <param name="network"></param>
        /// <param name="reader"></param>
        /// <remarks>Assumes reader.Name == "graph" and reader.IsStartElement() == true</remarks>
        internal void ReadNetwork(out IAdjList network, XmlReader reader)
        {
            network = null;
            reader.MoveToAttribute("id");
            string idStr = reader["id"];
            Guid id = ToGuid(idStr);

            reader.MoveToAttribute("edgedefault");
            string directedStr = reader["edgedefault"];
            bool isDirected = (string.Compare(directedStr, BlueSpider.GraphML.XMLConstants._Directed) == 0);

            string name = null;
            if (reader.MoveToAttribute("name"))
            {
                name = reader["name"];
            }
            else
                name = Path.GetFileNameWithoutExtension(File);

            int n = 0;
            if (reader.MoveToAttribute("parse.nodes"))
            {
                string numNodesStr = reader["parse.nodes"];
                if (!int.TryParse(numNodesStr, out n))
                    n = 0;
            }
            reader.MoveToElement();

            using (var provider = new NetworkFactoryProvider())
            {
                using (var fac = provider.Create<IBasicAdjList>())
                {
                    fac.EnableNodeDataAttributes = true;
                    fac.EnableEdgeDataAttributes = true;
                    network = fac.CreateNetwork(id, n) as IBasicAdjList;
                    network.Name = name;
                    network.IsDirected = isDirected;
                }
            }
            ReadGraphElements(network, reader);
        }
Exemplo n.º 7
0
        internal INetwork CreateFromFullMatrix(string networkName)
        {
            // symmetry determines directedness.
            _IsSymmetric = CheckForSymmetry();

            IAdjList network = null;

            using (var provider = new NetworkFactoryProvider())
            {
                using (var fac = provider.Create<IBasicAdjList>())
                {
                    fac.EnableNodeDataAttributes = true;
                    fac.EnableEdgeDataAttributes = true;
                    network = fac.CreateNetwork(_NetworkId, _n) as IBasicAdjList;
                    network.Name = networkName;
                    network.IsDirected = !_IsSymmetric;
                }
            }

            int edgeWeightsColIndex = -1;
            var edgeAttribNetwork = network as IEdgeAttributes;
            if (IncludeValsAsEdgeData)
            {
                edgeWeightsColIndex = edgeAttribNetwork.EdgeData.AddColumn("Edge weights", typeof(double), 0.0);
            }

            #region Set the node labels
            // set the node labels
            if (IncludeNodeLabels)
            {
                if (_Labels != null && _Labels.Count == _n)
                {
                    SetNodeLabels(network, _Labels);
                }
                else if (_RowLabels != null && _RowLabels.Count == _n)
                {
                    // if cant use regular labels, maybe we can use row labels
                    SetNodeLabels(network, _RowLabels);
                }
                else if (_ColLabels != null && _ColLabels.Count == _n)
                {
                    // as a last resort, try using col labels
                    SetNodeLabels(network, _ColLabels);
                }
            }
            #endregion

            IEdge edge = null;

            int k;
            if (_IsSymmetric)
            {
                #region Symmetric matrix

                // by default take upper half
                for (int i = 0; i < _n; i++)
                {
                    for (int j = i + 1; j < _n; j++)
                    {
                        k = i * _n + j;
                        if (_vals[k] != 0)
                        {
                            if (i == j)
                                AppendMssg(string.Format("Self-looped edge: node[{0}] ->  node[{1}].", i, j));

                            edge = network.CreateEdge(network.Nodes[i], network.Nodes[j]);
                            if (IncludeValsAsEdgeData)
                                edgeAttribNetwork.EdgeData.SetValue<double>(edge, edgeWeightsColIndex, _vals[k]);
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region Asymmetric matrix

                for (int i = 0; i < _n; i++)
                {
                    for (int j = 0; j < _n; j++)
                    {
                        k = i * _n + j;
                        if (_vals[k] != 0)
                        {
                            if (i == j)
                                AppendMssg(string.Format("Self-looped edge: node[{0}] ->  node[{1}].", i, j));

                            edge = network.CreateEdge(network.Nodes[i], network.Nodes[j]);
                            if (IncludeValsAsEdgeData)
                                edgeAttribNetwork.EdgeData.SetValue<double>(edge, edgeWeightsColIndex, _vals[k]);
                        }
                    }
                }
                #endregion
            }

            return network;
        }