/// <summary> /// 构造函数 /// </summary> /// <param name="gm"></param> /// <param name="v0"></param> public Dijkstra(GraphManager gm, int v0) { G = gm; final = new bool[gm.nodeNum]; D = new int[gm.nodeNum]; this.v0 = v0; count = 0; pre = new int[gm.nodeNum]; }
SteinerTree st; // #endregion Fields #region Constructors //int buttonMode; //const int BUTTON_MODE = 2; //主按钮模式切换 0画数据图 1画Steiner树 /// <summary> /// 主窗口 /// </summary> public MainWindow() { InitializeComponent(); // 初始化 dbm = new DBManager(); count = dbm.getNodeNum(); gm = new GraphManager(count); dbm.initGraph(gm); datagraph = new DataGraphGeometry(); textBlockInfo.Text = "Ready."; //状态信息显示 }
/// <summary> /// 构造函数 /// </summary> /// <param name="gm">图结构</param> /// <param name="dbm">数据库管理类</param> /// <param name="keyword">关键词集合</param> public SteinerTree(GraphManager gm, DBManager dbm, string[] keyword) { //this.gm = gm; isReached = new bool[gm.nodeNum]; for (int i = 0; i < isReached.Length; i++) { isReached[i] = false; } dictVL = new Dictionary<int, List<int>>(); dictVL2 = new Dictionary<int, List<int>>(); dictDijkstra = new Dictionary<int, Dijkstra>(); iterator = new List<Dijkstra>(); keywordSet = new List<List<int>>(); keywordIndex = dbm.getKeywordSets(keyword, gm, keywordSet); foreach (int item in keywordIndex) { Dijkstra dkInterator = new Dijkstra(gm, item); dictDijkstra.Add(item, dkInterator); iterator.Add(dkInterator); dkInterator.execute(); dkInterator.getNextNode(); } steinerTreeNode = new List<int>(); steinerTreeEdge = new List<edge>(); }
/// <summary> /// 根据关键词获得节点集合 /// </summary> /// <param name="keyword">关键词数组</param> /// <param name="gm">GraphManager数据图</param> /// <param name="keywordSet">???</param> /// <returns></returns> public List<int> getKeywordSets(string[] keyword, GraphManager gm, List<List<int>> keywordSet) { string sql = ""; List<int> keywordIndexList = new List<int>(); string id = ""; int index = 0; #region 获取关键字节点集合 try { myConnection.Open(); MySqlDataReader dr; foreach (string item in keyword) { List<int> nodeList = new List<int>(); sql = "select PID from paper where Title like '%" + item + "%'"; using (MySqlCommand commandPaper = new MySqlCommand(sql, myConnection)) { dr = commandPaper.ExecuteReader(); while (dr.Read()) { id = dr.GetString("PID"); index = gm.dicKey[id]; nodeList.Add(index); if (!keywordIndexList.Contains(index)) { keywordIndexList.Add(index); } } dr.Close(); } sql = "select AID from author where Name like '%" + item + "%'"; using (MySqlCommand commandAuthor = new MySqlCommand(sql, myConnection)) { dr = commandAuthor.ExecuteReader(); while (dr.Read()) { id = dr.GetString("AID"); index = gm.dicKey[id]; nodeList.Add(index); if (!keywordIndexList.Contains(index)) { keywordIndexList.Add(index); } } dr.Close(); } keywordSet.Add(nodeList); } myConnection.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } #endregion //foreach (int i in keywordIndexList) //{ // Console.WriteLine(i); //} return keywordIndexList; }
/// <summary> /// 初始化数据图 /// </summary> /// <param name="gm"></param> public void initGraph(GraphManager gm) { try { myConnection.Open(); } catch (MySqlException e) { Console.WriteLine(e.Message); App.Current.Shutdown(); } MySqlDataReader dr; int index = 0; string sql = "select * from paper"; using (MySqlCommand paperCommand = new MySqlCommand(sql, myConnection)) { dr = paperCommand.ExecuteReader(); while (dr.Read()) { gm.dicKey[dr.GetString("PID")] = index; gm.arrayKey.SetValue(dr.GetString("PID"), index); index++; } dr.Close(); } sql = "select * from author"; using (MySqlCommand authorCommand = new MySqlCommand(sql, myConnection)) { dr = authorCommand.ExecuteReader(); while (dr.Read()) { gm.dicKey[dr.GetString("AID")] = index; gm.arrayKey.SetValue(dr.GetString("AID"), index); index++; } dr.Close(); } sql = "select * from \"paper-author\""; using (MySqlCommand paper_authorCommand = new MySqlCommand(sql, myConnection)) { dr = paper_authorCommand.ExecuteReader(); int paperIndex; int authorIndex; int count = 0; string key = ""; while (dr.Read()) { key = string.Format("W{0}", count); count++; gm.arrayKey.SetValue(key, index); paperIndex = gm.dicKey[dr.GetString("PID")]; authorIndex = gm.dicKey[dr.GetString("AID")]; gm.graph[paperIndex, index] = 1; gm.graph[index, paperIndex] = 1; gm.graph[authorIndex, index] = 1; gm.graph[index, authorIndex] = 1; index++; } dr.Close(); } sql = "select * from citation"; using (MySqlCommand citationCommand = new MySqlCommand(sql, myConnection)) { dr = citationCommand.ExecuteReader(); int citeIndex; int citedIndex; int count = 0; string key = ""; while (dr.Read()) { key = string.Format("C{0}", count); count++; gm.arrayKey.SetValue(key, index); citeIndex = gm.dicKey[dr.GetString("Cite")]; citedIndex = gm.dicKey[dr.GetString("Cited")]; gm.graph[citeIndex, index] = 1; gm.graph[index, citeIndex] = 1; gm.graph[citedIndex, index] = 1; gm.graph[index, citedIndex] = 1; index++; } dr.Close(); } myConnection.Close(); }
/// <summary> /// 构造steiner树的几何图形 /// </summary> /// <param name="st">steinertree类</param> /// <param name="gm">GraphManager类</param> /// <param name="radius">每个点的半径</param> /// <param name="fontsize">每个点说明文字的大小</param> /// <returns>几何图形组。[0]为点集,[1]为边集</returns> public PathGeometry[] ConstructSteinterGeometry(SteinerTree st, GraphManager gm, int radius, int fontsize) { steinertreeShape = new PathGeometry(); steinertreeLine = new PathGeometry(); //i即要绘制的节点 foreach (int i in st.steinerTreeNode) { steinertreeShape.AddGeometry(new EllipseGeometry((Point)gm.arrayPos.GetValue(i), radius, radius)); Point ptext = new Point(((Point)gm.arrayPos.GetValue(i)).X - H_GAP, ((Point)gm.arrayPos.GetValue(i)).Y - V_GAP); steinertreeShape.AddGeometry(DataGraphPrintText(gm.arrayKey.GetValue(i).ToString(), ptext, fontsize).BuildGeometry()); } //画线 foreach (edge i in st.steinerTreeEdge) { steinertreeLine.AddGeometry(new LineGeometry((Point)gm.arrayPos.GetValue(i.x), (Point)gm.arrayPos.GetValue(i.y))); } return new PathGeometry[] { steinertreeShape, steinertreeLine }; }
/// <summary> /// not implemented yet /// </summary> /// <param name="st"></param> /// <param name="gm"></param> /// <param name="radius"></param> /// <param name="fontsize"></param> /// <returns></returns> public PathGeometry[] ConstructSteinterExactGeometry(SteinerTree st, GraphManager gm, int radius, int fontsize) { throw new NotImplementedException(); }
/// <summary> /// 从数据图构造几何图形 /// </summary> /// <param name="gm">数据图GraphManager</param> /// <param name="radius">每個點的半徑</param> /// <param name="fontsize">字体大小</param> /// <returns></returns> public PathGeometry[] ConstructGeometrySimple(GraphManager gm, int radius, int fontsize) { #region 画点 int PCount = 0; int ACount = 0; int WCount = 0; int CCount = 0; dataGraphShape = new PathGeometry(); dataGraphLine = new PathGeometry(); for (int i = 0; i < gm.nodeNum; i++) { if (gm.arrayKey.GetValue(i).ToString()[0] == 't') { Point centerP = new Point(H_DIST * PCount + H_DIST, 2 * V_DIST); dataGraphShape.AddGeometry(new EllipseGeometry(centerP, radius, radius)); gm.arrayPos.SetValue(new Point(H_DIST * PCount + H_DIST, 2 * V_DIST), i); Point ptextP = new Point(H_DIST * PCount + H_DIST - H_GAP, 2 * V_DIST - V_GAP); dataGraphShape.AddGeometry(DataGraphPrintText(gm.arrayKey.GetValue(i).ToString(), ptextP, fontsize).BuildGeometry()); PCount++; } if (gm.arrayKey.GetValue(i).ToString()[0] == 'a') { Point centerA = new Point(H_DIST * ACount + H_DIST, 4 * V_DIST); dataGraphShape.AddGeometry(new EllipseGeometry(centerA, radius, radius)); gm.arrayPos.SetValue(new Point(H_DIST * ACount + H_DIST, 4 * V_DIST), i); Point ptextA = new Point(H_DIST * ACount + H_DIST - H_GAP, 4 * V_DIST - V_GAP); dataGraphShape.AddGeometry(DataGraphPrintText(gm.arrayKey.GetValue(i).ToString(), ptextA, fontsize).BuildGeometry()); ACount++; } if (gm.arrayKey.GetValue(i).ToString()[0] == 'C') { Point centerC = new Point(H_DIST * CCount + H_DIST, V_DIST); dataGraphShape.AddGeometry(new EllipseGeometry(centerC, radius, radius)); gm.arrayPos.SetValue(new Point(H_DIST * CCount + H_DIST, V_DIST), i); Point ptextC = new Point(H_DIST * CCount + H_DIST - H_GAP, V_DIST - V_GAP); dataGraphShape.AddGeometry(DataGraphPrintText(gm.arrayKey.GetValue(i).ToString(), ptextC, fontsize).BuildGeometry()); CCount++; } if (gm.arrayKey.GetValue(i).ToString()[0] == 'W') { Point centerW = new Point(H_DIST * WCount + H_DIST, 3 * V_DIST); dataGraphShape.AddGeometry(new EllipseGeometry(centerW, radius, radius)); gm.arrayPos.SetValue(new Point(H_DIST * WCount + H_DIST, 3 * V_DIST), i); Point ptextW = new Point(H_DIST * WCount + H_DIST - H_GAP, 3 * V_DIST - V_GAP); dataGraphShape.AddGeometry(DataGraphPrintText(gm.arrayKey.GetValue(i).ToString(), ptextW, fontsize).BuildGeometry()); WCount++; } } #endregion #region 画线 for (int i = 0; i < gm.nodeNum; i++) { for (int j = 0; j < i; j++) { if (gm.graph[i, j] == 1) { dataGraphLine.AddGeometry(new LineGeometry((Point)gm.arrayPos.GetValue(i), (Point)gm.arrayPos.GetValue(j))); } } } #endregion return new PathGeometry[] { dataGraphShape, dataGraphLine }; }
/// <summary> /// 从数据图构造几何图形 /// </summary> /// <param name="gm">数据图GraphManager</param> /// <param name="fontsize">字体大小</param> /// <param name="radius">点半径</param> /// <returns>返回几何图形集</returns> public PathGeometry[] ConstructGeometry(GraphManager gm, int radius, int fontsize) { tableIndex = new int[gm.nodeNum, 2]; string currentKey = null; //获得第一个主键 nodeNo = tableNo = 0; GeometryGroup[] group = new GeometryGroup[1]; //遍历用临时group,只有一个组 for (int i = 0; i < gm.nodeNum; i++) { //遍历所有主键 if (gm.arrayKey.GetValue(i).Equals(currentKey)) { //仍然是相同key tables.CopyTo(tables.Count - 1, group, 0, 1); //取出最后一个,插入点 group[0].Children.Add(new EllipseGeometry(new Point(tableNo * H_DIST, nodeNo * H_DIST), radius, radius)); tableIndex[i, 1] = nodeNo++; //计数 } else { currentKey = (string)gm.arrayKey.GetValue(i); //更换key keys.Add(currentKey); //计入key nodeNo = 0; tableIndex[i, 1] = nodeNo; //记录node编号 tables.Add(group); //放入旧group tableIndex[i, 0] = tableNo++; //记录新表编号 group = new GeometryGroup[1]; //建立新group,插入下一组的第一个node,group只有一个元素 group[0].Children.Add(new EllipseGeometry(new Point(tableNo * H_DIST, nodeNo * H_DIST), radius, radius)); } } EllipseGeometry[] cp = new EllipseGeometry[1]; //临时用ellipse,只有一个元素 for (int i = 0; i < gm.nodeNum; i++) { for (int j = 0; j < gm.nodeNum; j++) { #region DEBUG Console.Write(gm.graph[i, j]); Console.Write(" "); #endregion if (gm.graph[i, j] == 1) { tables.CopyTo(tableIndex[i, 0], group, 0, 1);//找到对应表 group[0].Children.CopyTo(cp, tableIndex[i, 1]);//取出对应形状 Point s = cp[0].Center; //取点 tables.CopyTo(tableIndex[j, 0], group, 0, 1); group[0].Children.CopyTo(cp, tableIndex[j, 1]); Point d = cp[0].Center; edges.Children.Add(new LineGeometry(s, d)); } } } for (int i = 0; i < tables.Count; i++) { tables.CopyTo(i, group, 0, 1); //提取第i个表的group dataGraphShape.AddGeometry(group[0]); //放入总图 } dataGraphLine.AddGeometry(edges); return new PathGeometry[] { dataGraphShape, dataGraphLine }; }