1
|
|
|
from django.core.management.base import BaseCommand |
2
|
|
|
from ore.models import Graph, Node, Edge, Property |
3
|
|
|
from django.contrib.auth.models import User |
4
|
|
|
|
5
|
|
|
import os.path |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class Command(BaseCommand): |
9
|
|
|
args = '<owner> <textfile>' |
10
|
|
|
help = 'Imports a fault tree in European Benchmark Fault Trees Format, see http://bit.ly/UrAxM1' |
11
|
|
|
got_root_gate = False |
12
|
|
|
nodes = {} |
13
|
|
|
graph = None |
14
|
|
|
gate_x = 0 |
15
|
|
|
event_x = 0 |
16
|
|
|
current_id = 0 |
17
|
|
|
|
18
|
|
|
def client_id(self): |
19
|
|
|
self.current_id += 1 |
20
|
|
|
return self.current_id |
21
|
|
|
|
22
|
|
|
def get_id(self, title): |
23
|
|
|
if '/' in title: |
24
|
|
|
return title[title.find(')') + 1:] |
25
|
|
|
else: |
26
|
|
|
return filter(lambda x: x.isdigit(), title) |
27
|
|
|
|
28
|
|
|
def addNode(self, title, lineno): |
29
|
|
|
lineno += 10 |
30
|
|
|
node_id = self.get_id(title) |
31
|
|
|
kind = None |
32
|
|
|
|
33
|
|
|
if node_id not in self.nodes.keys(): |
34
|
|
|
# TODO: Use some reasonable X / Y coordinates |
35
|
|
|
self.gate_x += 1 |
36
|
|
|
|
37
|
|
|
if '*' in title: |
38
|
|
|
kind = 'andGate' |
39
|
|
|
elif '+' in title: |
40
|
|
|
kind = 'orGate' |
41
|
|
|
elif '/' in title: |
42
|
|
|
kind = 'votingOrGate' |
43
|
|
|
elif title.startswith('T'): |
44
|
|
|
kind = 'basicEvent' |
45
|
|
|
|
46
|
|
|
node = Node( |
47
|
|
|
graph=self.g, |
48
|
|
|
x=self.gate_x, |
49
|
|
|
y=lineno, |
50
|
|
|
kind=kind, |
51
|
|
|
client_id=self.client_id()) |
52
|
|
|
self.nodes[node_id] = node |
53
|
|
|
node.save() |
54
|
|
|
|
55
|
|
|
prob = Property(key='title', value=title, node=node) |
56
|
|
|
prob.save() |
57
|
|
|
|
58
|
|
|
if not self.got_root_gate: |
59
|
|
|
# connect the very first gate to the top event |
60
|
|
|
root_node = Node.objects.get( |
61
|
|
|
kind__exact='topEvent', |
62
|
|
|
graph=self.graph) |
63
|
|
|
edge = Edge( |
64
|
|
|
graph=self.graph, |
65
|
|
|
source=root_node, |
66
|
|
|
target=self.nodes[node_id], |
67
|
|
|
client_id=self.client_id()) |
68
|
|
|
edge.save() |
69
|
|
|
|
70
|
|
|
self.got_root_gate = True |
71
|
|
|
|
72
|
|
|
def handle(self, *args, **options): |
73
|
|
|
user_name = 'admin' |
74
|
|
|
file_name = 'ore/fixtures/europe-1.txt' |
75
|
|
|
argc = len(args) |
76
|
|
|
|
77
|
|
|
if argc == 1: |
78
|
|
|
user_name = args[0] |
79
|
|
|
|
80
|
|
|
elif argc == 2: |
81
|
|
|
user_name = args[0] |
82
|
|
|
file_name = args[1] |
83
|
|
|
|
84
|
|
|
owner = User.objects.get(username__exact=user_name) |
85
|
|
|
|
86
|
|
|
with open(file_name) as file_handle: |
87
|
|
|
self.graph = Graph( |
88
|
|
|
name=os.path.split(file_name)[-1], kind='fuzztree', owner=owner) |
89
|
|
|
self.graph.save() |
90
|
|
|
|
91
|
|
|
for lineno, line in enumerate(file_handle): |
92
|
|
|
if line.startswith('G'): |
93
|
|
|
# Gate node |
94
|
|
|
nodes = [ |
95
|
|
|
node for node in line.rstrip('\n').split(' ') if node != ''] |
96
|
|
|
for node in nodes: |
97
|
|
|
self.addNode(node, lineno) |
98
|
|
|
# Add edges now, since all nodes in the line are in the DB |
99
|
|
|
parent = self.nodes[self.get_id(nodes[0])] |
100
|
|
|
for node in nodes[1:]: |
101
|
|
|
edge = Edge(graph=self.graph, source=parent, |
102
|
|
|
target=self.nodes[ |
103
|
|
|
self.get_id(node)], client_id=self.client_id() |
104
|
|
|
) |
105
|
|
|
edge.save() |
106
|
|
|
|
107
|
|
|
elif line.startswith('T'): |
108
|
|
|
# Basic event node with probability |
109
|
|
|
title, probability = line.split(' ')[0:2] |
110
|
|
|
|
111
|
|
|
self.addNode(title, lineno) |
112
|
|
|
node = self.nodes[self.get_id(title)] |
113
|
|
|
|
114
|
|
|
prop = Property( |
115
|
|
|
key='probability', |
116
|
|
|
value=str( |
117
|
|
|
float(probability)), |
118
|
|
|
node=node) |
119
|
|
|
prop.save() |
120
|
|
|
|