Skip to content

Commit

Permalink
pr init
Browse files Browse the repository at this point in the history
  • Loading branch information
lipanpan03 authored and tugraph committed Jun 6, 2023
1 parent b7a2091 commit ed1e31a
Show file tree
Hide file tree
Showing 40 changed files with 136 additions and 15 deletions.
62 changes: 62 additions & 0 deletions three_kingdoms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 1. 简介
由于史料的缺失,一些即使为人们熟知的历史事件也往往存在着很多未解之谜。以三国历史为例,诸葛亮为什么出山帮助当时势力弱小的刘备,同为天下英雄的曹操和刘备为什么成就差距巨大等等。以往学者往往采用二维关系分析历史,这样得出的结论往往比较片面。使用TuGraph将三国的历史人物和事件导入图模型中,使用图计算方式进行分析,能够帮助我们从有限的信息中获得更有价值的知识,是一种非常有意义的跨学科尝试。
# 2. 数据建模
我们设计了5类顶点和5类边,点包括“主公”,“州”,“文臣”,“武将”,“战役”,边包括“父亲”,“兄长”,“隶属”,“籍贯”,“参战”。其具体建模信息如下所示:
5类点
![image.png](images/leader.png)
![image.png](images/states.png)
![image.png](images/officer.png)
![image.png](images/solider.png)
![image.png](images/war.png)
5类边
![image.png](images/edges.png)
# 3. 数据导入
向TuGraph导入数据,既可以使用TuGraph的`lgraph_import`工具离线导入,也可以使用页面导入。lgraph_import导入命令如下所示
```shell
lgraph_import -c import.json --overwrite true --continue_on_error true
```
# 4. Cypher分析
## 4.1 诸葛亮为什么选择刘备
通过如下cypher命令可以查看诸葛亮和曹操、刘备之间的关系
```cypher
MATCH p = (cc:主公 {name: '曹操'})-[*1..3]-(zgl:文臣 {name: '诸葛亮'}) RETURN p
```
得到的查询结果如下图所示
![image.png](images/cypher1.png)
从图中可以很直观的看出,曹操和诸葛亮之间的最短路径之一包含徐州之战,曹操曾经因父亲被杀对徐州进行过屠城,而诸葛亮是徐州琅琊郡人,任何人都断然不会选择一个屠杀过自己家乡的军阀作为主公。而相反,刘备曾经在徐州之战中阻止过曹操的暴行,这应当是诸葛亮对刘备好感的原因之一。
## 4.2 曹操为什么成就比刘备高
通过如下cypher命令可以查看家族对曹操创业的助力
```cypher
MATCH (cc:主公{name:"曹操"})<-[r:隶属]-(wj:武将) WHERE wj.name REGEXP "曹.*" OR wj.name REGEXP "夏侯.*" return cc,wj
```
得到的查询结果如下图所示
![image.png](images/cypher2.png)
平定天下最重要的就是军事人才,曹操其父本姓夏侯,过继于曹氏,曹氏和夏侯氏在谯县都属于地方大族,在曹操创业初期提供了夏侯惇,夏侯渊,曹仁,曹洪在内大量的军事人才。而刘备其父早丧,没有家族助力,年过50才凑齐了自己的五虎上将,而这时已经过了天下大乱争夺地盘的最佳时机,曹操已经天下九州居其六了。
## 4.3 三国中最强大的魏国为何最先灭亡
通过如下cypher命令可以查看曹操集团的重要文官组成
```cypher
MATCH (cc:主公)<-[r:隶属]-(wc) WHERE cc.name REGEXP "曹.*" AND (label(wc) = "文臣" OR label(wc) = "主公") return cc,wc
```
得到的查询结果如下图所示
![image.png](images/cypher3.png)
曹魏事实上于249年灭亡于高平陵之变,立国29年,少于蜀汉(43年)和东吴(51年)。三国中实力最强大的魏国最先灭亡的原因就在于曹魏的文官制度(九品中正制)使得权力很容易集中在世家大族手中。从图中可以看出,曹操曹丕父子两代的重要文臣几乎都是世家大族,颍川荀氏,颍川钟氏,颍川陈氏,武威贾氏等,甚至还出现了地区化趋势,集中于颍川,最终政权也为和颍川荀氏关系密切的河内司马氏所篡夺。
## 4.4 三国各自的实力究竟如何
通过如下cypher命令可以查看三国各集团的人口实力
```cypher
MATCH (p) WHERE (label(p)="主公" OR label(p)="文臣" OR label(p)="武将") AND p.hometown IN ["幽州","冀州","青州","并州","凉州","司州","豫州","兖州","徐州"] WITH COUNT(p) AS w
MATCH (p) WHERE (label(p)="主公" OR label(p)="文臣" OR label(p)="武将") AND p.hometown IN ["益州"] WITH COUNT(p) AS s,w
MATCH (p) WHERE (label(p)="主公" OR label(p)="文臣" OR label(p)="武将") AND p.hometown IN ["扬州","荆州","交州"] RETURN "魏",w,"蜀",s,"吴",count(p)
```
得到的查询结果如下表所示
![image.png](images/cypher4.png)
古代社会衡量一个国家实力的重要指标是人口数量,由于人口数据缺失,我们使用三国所有主公和文臣武将的籍贯数据估计每个州的人口数量。发现三国主要人物中,按籍贯有60个属于魏国,有23个属于吴国,仅有2个属于蜀国,证明魏国确实是三国中最强大的国家。
## 4.5 曹操的军事能力如何评价
通过如下cypher命令可以查看曹操参与的主要战役
```cypher
MATCH (cc:主公{name:"曹操"})-[]-(zy:战役) RETURN cc,zy
```
得到的查询结果如下图所示
![image.png](images/cypher5.png)
从图中可以看出,曹操在三国主要的15场战役中参加了8场,出场率比较高。但是曹操只获胜了徐州之战、兖州之战、官渡之战和襄樊之战,汉中之战、宛城之战、群雄讨董和赤壁之战都失败了,综合胜率50%,证明曹操并不算一个非常优秀的军事家。
# 5. 备注
更多的分析有待大家积极补充和尝试!
Binary file added three_kingdoms/images/cypher1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/cypher2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/cypher3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/cypher4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/cypher5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/edges.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/leader.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/officer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/solider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/states.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added three_kingdoms/images/war.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 15 additions & 15 deletions three_kingdoms/import.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"header": 1,
"format": "CSV",
"label": "主公",
"path": "rawdata/主公.csv"
"path": "raw_data/主公.csv"
},
{
"columns": [
Expand All @@ -24,7 +24,7 @@
"header": 1,
"format": "CSV",
"label": "文臣",
"path": "rawdata/文臣.csv"
"path": "raw_data/文臣.csv"
},
{
"columns": [
Expand All @@ -36,7 +36,7 @@
"header": 1,
"format": "CSV",
"label": "武将",
"path": "rawdata/武将.csv"
"path": "raw_data/武将.csv"
},
{
"columns": [
Expand All @@ -46,7 +46,7 @@
"header": 1,
"format": "CSV",
"label": "",
"path": "rawdata/州.csv"
"path": "raw_data/州.csv"
},
{
"columns": [
Expand All @@ -57,7 +57,7 @@
"header": 1,
"format": "CSV",
"label": "战役",
"path": "rawdata/战役.csv"
"path": "raw_data/战役.csv"
},
{
"DST_ID": "主公",
Expand All @@ -69,7 +69,7 @@
"header": 1,
"format": "CSV",
"label": "父亲",
"path": "rawdata/关系_主公_父_主公.csv"
"path": "raw_data/关系_主公_父_主公.csv"
},
{
"DST_ID": "主公",
Expand All @@ -81,7 +81,7 @@
"header": 1,
"format": "CSV",
"label": "父亲",
"path": "rawdata/关系_武将_父_主公.csv"
"path": "raw_data/关系_武将_父_主公.csv"
},
{
"DST_ID": "主公",
Expand All @@ -93,7 +93,7 @@
"header": 1,
"format": "CSV",
"label": "兄长",
"path": "rawdata/关系_主公_兄_主公.csv"
"path": "raw_data/关系_主公_兄_主公.csv"
},
{
"DST_ID": "主公",
Expand All @@ -105,7 +105,7 @@
"header": 1,
"format": "CSV",
"label": "隶属",
"path": "rawdata/关系_文臣_隶属_主公.csv"
"path": "raw_data/关系_文臣_隶属_主公.csv"
},
{
"DST_ID": "主公",
Expand All @@ -117,7 +117,7 @@
"header": 1,
"format": "CSV",
"label": "隶属",
"path": "rawdata/关系_武将_隶属_主公.csv"
"path": "raw_data/关系_武将_隶属_主公.csv"
},
{
"DST_ID": "主公",
Expand All @@ -129,7 +129,7 @@
"header": 1,
"format": "CSV",
"label": "隶属",
"path": "rawdata/关系_主公_隶属_主公.csv"
"path": "raw_data/关系_主公_隶属_主公.csv"
},
{
"DST_ID": "",
Expand All @@ -141,7 +141,7 @@
"header": 1,
"format": "CSV",
"label": "籍贯",
"path": "rawdata/关系_主公_籍贯_州.csv"
"path": "raw_data/关系_主公_籍贯_州.csv"
},
{
"DST_ID": "",
Expand All @@ -153,7 +153,7 @@
"header": 1,
"format": "CSV",
"label": "籍贯",
"path": "rawdata/关系_文臣_籍贯_州.csv"
"path": "raw_data/关系_文臣_籍贯_州.csv"
},
{
"DST_ID": "",
Expand All @@ -165,7 +165,7 @@
"header": 1,
"format": "CSV",
"label": "籍贯",
"path": "rawdata/关系_武将_籍贯_州.csv"
"path": "raw_data/关系_武将_籍贯_州.csv"
},
{
"DST_ID": "战役",
Expand All @@ -177,7 +177,7 @@
"header": 1,
"format": "CSV",
"label": "参战",
"path": "rawdata/关系_主公_参战_战役.csv"
"path": "raw_data/关系_主公_参战_战役.csv"
}
],
"schema": [
Expand Down
18 changes: 18 additions & 0 deletions three_kingdoms/lgraph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"directory" : "/var/lib/lgraph/three_kingdoms_db",

"host" : "0.0.0.0",
"port" : 27001,
"rpc_port" : 27002,
"enable_rpc" : true,
"enable_ha" : false,

"verbose" : 2,
"log_dir" : "",

"disable_auth" : false,
"ssl_auth" : false,
"server_key" : "/usr/local/etc/lgraph/server-key.pem",
"server_cert" : "/usr/local/etc/lgraph/server-cert.pem",
"web" : "/usr/local/share/lgraph/resource"
}
File renamed without changes.
1 change: 1 addition & 0 deletions three_kingdoms/query/e1.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MATCH p = (cc:主公 {name: '曹操'})-[*1..3]-(zgl:文臣 {name: '诸葛亮'}) RETURN p
1 change: 1 addition & 0 deletions three_kingdoms/query/e2.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MATCH (cc:主公{name:"曹操"})<-[r:隶属]-(wj:武将) WHERE wj.name REGEXP "曹.*" OR wj.name REGEXP "夏侯.*" return cc,wj
1 change: 1 addition & 0 deletions three_kingdoms/query/e3.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MATCH (cc:主公)<-[r:隶属]-(wc) WHERE cc.name REGEXP "曹.*" AND (label(wc) = "文臣" OR label(wc) = "主公") return cc,wc
3 changes: 3 additions & 0 deletions three_kingdoms/query/e4.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MATCH (p) WHERE (label(p)="主公" OR label(p)="文臣" OR label(p)="武将") AND p.hometown IN ["幽州","冀州","青州","并州","凉州","司州","豫州","兖州","徐州"] WITH COUNT(p) AS w
MATCH (p) WHERE (label(p)="主公" OR label(p)="文臣" OR label(p)="武将") AND p.hometown IN ["益州"] WITH COUNT(p) AS s,w
MATCH (p) WHERE (label(p)="主公" OR label(p)="文臣" OR label(p)="武将") AND p.hometown IN ["扬州","荆州","交州"] RETURN "魏",w,"蜀",s,"吴",count(p)
1 change: 1 addition & 0 deletions three_kingdoms/query/e5.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MATCH (cc:主公{name:"曹操"})-[]-(zy:战役) RETURN cc,zy
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file removed three_kingdoms/rawdata/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions three_kingdoms/run_import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
mkdir -p /var/lib/lgraph/
lgraph_import --dir /var/lib/lgraph/three_kingdoms_db --verbose 2 -c import.json --dry_run 0 --continue_on_error 1 --overwrite 1 --online false
rm -rf import_tmp

echo "IMPORT DONE."
26 changes: 26 additions & 0 deletions three_kingdoms/run_query.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

CYPHER="lgraph_cypher -u admin -P 73@TuGraph -p 27001 -f "
QUERY_DIR="./query"

cat $QUERY_DIR/e1.cypher
$CYPHER $QUERY_DIR/e1.cypher
echo

cat $QUERY_DIR/e2.cypher
$CYPHER $QUERY_DIR/e2.cypher
echo

cat $QUERY_DIR/e3.cypher
$CYPHER $QUERY_DIR/e3.cypher
echo

cat $QUERY_DIR/e4.cypher
$CYPHER $QUERY_DIR/e4.cypher
echo

cat $QUERY_DIR/e5.cypher
$CYPHER $QUERY_DIR/e5.cypher
echo

echo "QUERY DONE."
2 changes: 2 additions & 0 deletions three_kingdoms/run_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
lgraph_server -c lgraph.json

0 comments on commit ed1e31a

Please sign in to comment.