-
Notifications
You must be signed in to change notification settings - Fork 0
/
rulesToNeo4j.py
55 lines (42 loc) · 1.94 KB
/
rulesToNeo4j.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from py2neo import Graph, Node, Relationship
import sys
graph = Graph()
globalPreconditions = {}
globalConclusions = {}
relationships = []
def parseFile():
global globalPreconditions, globalConclusions, relationships
file = open(sys.argv[1], "r")
attacker = Node("Attacker", name="attacker")
for line in file:
currentPreconditions = []
preconditions, conclusions = line.split('==>')
preconditions = preconditions.strip()
startVertices = preconditions.split(',')
for vertex in startVertices:
if vertex not in globalPreconditions:
globalPreconditions[vertex] = Node("Precondition", name=vertex)
relationships.append(Relationship(attacker, "_", globalPreconditions[vertex]))
currentPreconditions.append(globalPreconditions[vertex])
endVertices, supp, conf = conclusions.split("#")
endVertices = endVertices.strip()
endVertices = endVertices.split(',')
for vertex in endVertices:
if vertex not in globalConclusions:
globalConclusions[vertex] = []
globalConclusions[vertex].append(Node("Conclusion", name=vertex, support=supp, confidence=conf))
for precondition in currentPreconditions:
relationships.append(Relationship(precondition, "_" , globalConclusions[vertex][-1]))
def addBackwardEdges():
global globalPreconditions, globalConclusions, relationships
for conclusion in globalConclusions:
if conclusion in globalPreconditions:
for vertex in globalConclusions[conclusion]:
relationships.append(Relationship(vertex, "backward", globalPreconditions[conclusion]))
def createGraph():
for relationship in relationships:
graph.create(relationship)
graph.delete_all()
parseFile()
addBackwardEdges()
createGraph()