-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
1443 lines (1228 loc) · 51.7 KB
/
models.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
from decimal import *
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from clusters.utils import *
from clusters.tarjan import strongly_connected_components
class InputOutputCell(object):
def __init__(self, producer, consumer, resource):
self.producer = producer
self.consumer = consumer
self.resource = resource
class InputOutputTable(object):
def __init__(self, columns, rows):
self.columns = columns
self.rows = rows
class InputOutputHeader(object):
def __init__(self, function, resources):
self.function = function
self.resources = resources
def input_output_cells(cluster):
cells = []
for ef in EconomicFunction.objects.filter(cluster=cluster):
outputs = ef.outputs()
if outputs:
for output in ef.outputs():
for fn_rt in output.resource_type.functions.filter(role="consumes"):
cells.append(InputOutputCell(ef, fn_rt.function, output.resource_type))
return cells
# todo: what if one EconomicFunction provides more than one input
# to the same consuming EconomicFunction?
def input_output_table(cluster):
# todo: rows and columns shd be sorted the same
cells = input_output_cells(cluster)
rows = {}
columns = []
for cell in cells:
columns.append(cell.consumer)
columns = list(set(columns))
col_count = len(columns)
for cell in cells:
rows[cell.producer] = []
for x in range(col_count):
rows[cell.producer].append(' ')
for cell in cells:
row_cell = columns.index(cell.consumer)
rows[cell.producer][row_cell] = cell.resource
return InputOutputTable(columns, rows)
class FunctionResourceTable(object):
def __init__(self, columns, rows):
self.columns = columns
self.rows = rows
def function_resource_table(cluster, toggle):
frs = FunctionResourceType.objects.filter(function__cluster=cluster)
functions = {}
resources = []
for fr in frs:
resources.append(fr.resource_type)
resources = list(set(resources))
resources.sort(lambda x, y: cmp(x.name, y.name))
col_count = len(resources)
for fr in frs:
function = fr.function
if not function in functions:
functions[function] = [function.name,]
for x in range(col_count + 1):
functions[function].append('')
functions[function][col_count + 1] = 0
for fr in frs:
mult = 1
if fr.role == "consumes":
mult = -1
row_cell = resources.index(fr.resource_type) + 1
if toggle == "val":
functions[fr.function][row_cell] = fr.value * mult
functions[fr.function][col_count + 1] += fr.value * mult
else:
functions[fr.function][row_cell] = fr.quantity * mult
functions[fr.function][col_count + 1] += fr.quantity * mult
rows = functions.values()
rows.sort()
col_totals = ["Totals",]
for i in range(1, col_count + 2):
col_totals.append(0)
for row in rows:
for i in range(1, col_count + 2):
col_totals[i] += row[i] or 0
rows.append(col_totals)
columns = []
for r in resources:
columns.append(r.name)
columns.append("Totals")
return FunctionResourceTable(columns, rows)
def connected_functions(node, all_nodes, to_return):
to_return.append(node)
for subnode in all_nodes:
for out in subnode.outputs():
for consumer in out.resource_type.cluster_consumers(subnode.cluster):
if not consumer.function in to_return:
connected_functions(consumer.function, all_nodes, to_return)
for inp in subnode.inputs():
for producer in inp.resource_type.cluster_producers(subnode.cluster):
if not producer.function in to_return:
connected_functions(producer.function, all_nodes, to_return)
return to_return
UNIT_TYPE_CHOICES = (
('quantity', 'quantity'),
('value', 'value'),
)
class Unit(models.Model):
type = models.CharField(max_length=12, choices=UNIT_TYPE_CHOICES)
abbrev = models.CharField(max_length=8)
name = models.CharField(max_length=64)
symbol = models.CharField(max_length=1, blank=True)
def __unicode__(self):
return self.abbrev
class Community(models.Model):
name = models.CharField(max_length=128)
description = models.TextField(blank=True, null=True)
map_center = models.CharField(max_length=255, blank=True,
help_text="Map center may be an address, or latitude and longitude.")
latitude = models.FloatField(default=0.0, blank=True, null=True)
longitude = models.FloatField(default=0.0, blank=True, null=True)
map_zoom_level = models.PositiveSmallIntegerField(default=0,
help_text="0-20 - larger numbers zoom in more")
unit_of_value = models.ForeignKey(Unit, blank=True, null=True,
limit_choices_to={"type": "value"},
related_name="community_units")
resource_aspect_name = models.CharField(max_length=128, blank=True,
help_text="Name for aspect fields on Economic Resource Types in this community. If no name, aspects will not be used.")
agent_geographic_area_name = models.CharField(max_length=128, blank=True,
help_text="Name for geographic area fields for Economic Agents in this community. If no name, areas will not be used.")
when_created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
created_by = models.ForeignKey(User, verbose_name='created by',
related_name='created_community', blank=True, null=True)
when_changed = models.DateTimeField(auto_now=True, blank=True, null=True)
changed_by = models.ForeignKey(User, verbose_name='changed by',
related_name='changed_community', blank=True, null=True)
class Meta:
verbose_name_plural = "communities"
ordering = ('name',)
def __unicode__(self):
return self.name
def is_public(self):
answer = False
for cluster in self.clusters.all():
if cluster.is_public():
answer = True
break
return answer
def member_users(self):
return [m.member for m in self.members.all()]
def permits(self, perm_name, user):
if user.is_superuser:
return True
if self.created_by:
if user.id == self.created_by.id:
return True
perm = None
mbr = [mbr for mbr in self.members.all() if mbr.member.id == user.id]
if mbr:
mbr = mbr[0]
perm = mbr.permission_role
if perm_name == "delete" or perm_name == "users":
if perm:
if perm == "owner":
return True
else:
return False
else:
if perm:
return True
return False
PERMISSION_ROLE_CHOICES = (
('owner', 'Owner'),
('editor', 'Editor'),
)
class CommunityMember(models.Model):
community = models.ForeignKey(Community, related_name='members')
member = models.ForeignKey(User, related_name="communities")
permission_role = models.CharField(max_length=12, choices=PERMISSION_ROLE_CHOICES)
@property
def username(self):
return self.member.username
class AggregateFunctionResource(object):
def __init__(self, function, resource_type, role, quantity, price, value):
self.function = function
self.resource_type = resource_type
self.role = role
self.quantity = quantity
self.price = price
self.value = value
def get_value(self):
if self.value:
return self.value
if self.quantity and self.price:
return int((self.quantity * self.price).quantize(Decimal('.01'), rounding=ROUND_UP))
return 0
@property
def color(self):
return "green"
class AgentGroupFlow(object):
def __init__(self, from_function, to_function, resource_type, quantity, price, value):
self.from_function = from_function
self.to_function = to_function
self.resource_type = resource_type
self.quantity = quantity
self.price = price
self.value = value
def get_value(self):
if self.value:
return self.value
if self.quantity and self.price:
return int((self.quantity * self.price).quantize(Decimal('.01'), rounding=ROUND_UP))
return 0
@property
def color(self):
return "green"
class AgentGroupFunction(object):
def __init__(self, function, flow_dict):
self.function = function
self.flow_dict = flow_dict
def flows(self):
return self.flow_dict.values()
@property
def color(self):
return "green"
class AggregateFunction(object):
def __init__(self, function, resource_dict):
self.function = function
self.resource_dict = resource_dict
def resources(self):
return self.resource_dict.values()
def produced_function_resources(self):
answer = []
for r in self.resources():
if r.role == "produces":
answer.append(r)
return answer
def function_inputs(self):
return [res for res in self.resources() if res.role == "consumes"]
def function_outputs(self):
return [res for res in self.resources() if res.role == "produces"]
class Region(object):
def __init__(self, name, lat, lng, function_dict):
self.name = name
self.lat = lat
self.lng = lng
self.function_dict= function_dict
def all_functions(self):
return self.function_dict.values()
def lat_lng(self):
return ",".join([str(self.lat), str(self.lng)])
def color(self):
answer = "gray"
not_green = ""
for agfn in self.all_functions():
if agfn.function.color != "green":
not_green = agfn.function.color
if not_green:
answer = not_green
else:
answer = "green"
return answer
class AgentGroup(object):
def __init__(self, name, function_dict):
self.name = name
self.function_dict= function_dict
def node_id(self):
return "".join(["AgentGroup-", self.name])
def all_functions(self):
return self.function_dict.values()
def function_inputs(self):
fns = self.all_functions()
answer = []
for fn in fns:
answer.extend(fn.function_inputs())
return answer
def function_outputs(self):
fns = self.all_functions()
answer = []
for fn in fns:
answer.extend(fn.function_outputs())
return answer
@property
def color(self):
return "firebrick"
class ResourceAtStage(object):
def __init__(self, long_name):
self.long_name = long_name
self.name = long_name.split(";")[1]
def node_id(self):
return self.long_name
SHARING_CHOICES = (
('public', 'Public'),
('private', 'Private'),
)
class Cluster(models.Model):
community = models.ForeignKey(Community, related_name='clusters')
name = models.CharField(max_length=128)
description = models.TextField(blank=True, null=True)
function_aspect_name = models.CharField(max_length=128, blank=True,
help_text="Name for aspect fields on Economic Functions in this cluster. If no name, aspects will not be used.")
root_function = models.ForeignKey("EconomicFunction", blank=True, null=True,
related_name="cluster_root",
help_text="Graph root is optional - can be either function or resource but not both")
root_resource = models.ForeignKey("EconomicResourceType", blank=True, null=True,
related_name="cluster_root",
help_text="Graph root is optional - can be either function or resource but not both")
when_created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
created_by = models.ForeignKey(User, verbose_name='created by',
related_name='created_cluster', blank=True, null=True)
when_changed = models.DateTimeField(auto_now=True, blank=True, null=True)
changed_by = models.ForeignKey(User, verbose_name='changed by',
related_name='changed_cluster', blank=True, null=True)
sharing = models.CharField(max_length=12, choices=SHARING_CHOICES,
default="private")
def get_absolute_url(self):
return ('cluster', (), {"cluster_id": self.id})
def __unicode__(self):
return " ".join([self.community.name, self.name])
def permits(self, perm_name, user):
if user.is_superuser:
return True
if self.created_by:
if user.id == self.created_by.id:
return True
perm = None
mbr = [mbr for mbr in self.community.members.all() if mbr.member.id == user.id]
if mbr:
mbr = mbr[0]
perm = mbr.permission_role
if perm_name == "delete":
if perm:
if perm == "owner":
return True
else:
return False
else:
if perm:
return True
return False
def is_public(self):
return self.sharing == "public"
def root(self):
if self.root_function:
return self.root_function
elif self.root_resource:
return self.root_resource
else:
return None
def resources(self):
answer = []
for fun in self.functions.all():
for r in fun.resources.all():
answer.append(r.resource_type)
flows = FunctionResourceFlow.objects.filter(
from_function__cluster=self)
for flow in flows:
answer.append(flow.resource_type)
for agent in self.agents():
for af in agent.functions.filter(function__cluster=self):
for afrt in af.function_resources.all():
answer.append(afrt.resource_type)
return list(set(answer))
def community_resources(self):
community = self.community
resources = self.resources()
crs = []
for r in resources:
crs.append(r.communities.get(community=community))
return crs
def agents(self):
answer = []
#for fun in self.functions.all():
# for a in fun.agents.all():
# answer.append(a.agent)
#answer = list(set(answer))
answer = [cla.agent for cla in self.cluster_agents.all()]
answer.sort(lambda x, y: cmp(x.name, y.name))
return answer
def flow_connected_functions(self):
connected = []
flows = self.flows()
for flow in flows:
connected.append(flow.from_function)
connected.append(flow.to_function)
connected = list(set(connected))
return connected
def disjoints(self):
funs = self.functions.all().order_by("id")
root = self.root_function
if not root:
if funs.count():
root = funs[0]
connected = connected_functions(root, funs, [])
connected.extend(self.flow_connected_functions())
connected = list(set(connected))
disjoint = []
for fun in funs:
if not fun in connected:
disjoint.append(fun)
return disjoint
def missing_function_numbers(self):
funs = self.functions.all()
missing = []
for fun in funs:
for res in fun.resources.all():
if not res.quantity:
missing.append(res)
return missing
def missing_agent_numbers(self):
agents = self.agents()
missing = []
for agent in agents:
for res in agent.resources.all():
if not res.quantity:
missing.append(res)
return missing
def function_production_without_consumption(self):
#import pdb; pdb.set_trace()
funs = self.functions.all()
missing = []
for fun in funs:
for out in fun.outputs():
consumption_qty = 0
consumption_val = 0
for consumer in out.resource_type.cluster_consumers(self):
consumption_qty += consumer.quantity
consumption_val += consumer.value
if consumption_qty < out.quantity:
missing.append({"function_resource": out, "quantity_missing": consumption_qty - out.quantity })
if consumption_val < out.value:
missing.append({"function_resource": out, "value_missing": consumption_val - out.value })
return missing
def function_consumption_without_production(self):
#import pdb; pdb.set_trace()
funs = self.functions.all()
missing = []
for fun in funs:
for inp in fun.inputs():
production_qty = 0
production_val = 0
for producer in inp.resource_type.cluster_producers(self):
production_qty += producer.quantity
production_val += producer.value
if production_qty < inp.quantity:
missing.append({"function_resource": inp, "quantity_missing": production_qty - inp.quantity })
if production_val < inp.value:
missing.append({"function_resource": inp, "value_missing": production_val - inp.value })
return missing
def agent_functions(self):
return AgentFunction.objects.filter(function__cluster=self)
def agent_function_production_without_consumption(self):
#import pdb; pdb.set_trace()
funs = self.agent_functions()
missing = []
for fun in funs:
for out in fun.outputs():
consumption_qty = 0
consumption_val = 0
for consumer in out.resource_type.cluster_consumers(self):
consumption_qty += consumer.quantity
consumption_val += consumer.value
if consumption_qty < out.quantity:
missing.append({"function_resource": out, "quantity_missing": consumption_qty - out.quantity })
if consumption_val < out.value:
missing.append({"function_resource": out, "value_missing": consumption_val - out.value })
return missing
def agent_function_consumption_without_production(self):
#import pdb; pdb.set_trace()
funs = self.agent_functions()
missing = []
for fun in funs:
for inp in fun.inputs():
production_qty = 0
production_val = 0
for producer in inp.resource_type.cluster_producers(self):
production_qty += producer.quantity
production_val += producer.value
if production_qty < inp.quantity:
missing.append({"function_resource": inp, "quantity_missing": production_qty - inp.quantity })
if production_val < inp.value:
missing.append({"function_resource": inp, "value_missing": production_val - inp.value })
return missing
def function_io_vs_flows(self):
report = []
fns = self.functions.all()
#import pdb; pdb.set_trace()
for fn in fns:
incoming = fn.incoming_flows.all()
for inc in incoming:
inc.flow = True
inc.matched = False
outgoing = fn.outgoing_flows.all()
for og in outgoing:
og.flow = True
og.matched = False
for inp in fn.inputs():
inp.flow = False
report.append(inp)
rels = inp.resource_type.all_relatives()
for inc in incoming:
if inc.resource_type in rels:
inc.matched = True
report.append(inc)
for outp in fn.outputs():
outp.flow = False
report.append(outp)
rels = outp.resource_type.all_relatives()
for og in outgoing:
if og.resource_type in rels:
og.matched = True
report.append(og)
for inc in incoming:
if not inc.matched:
report.append(inc)
for og in outgoing:
if not og.matched:
report.append(og)
return report
def function_agent_diffs(self):
#import pdb; pdb.set_trace()
funs = self.functions.all()
diffs = []
for fun in funs:
# agents here are AgentFunction objects
agents = fun.agents.all()
for res in fun.resources.all():
agent_total_qty = 0
agent_total_val = 0
for agent in agents:
agent_total_qty += sum(
ares.quantity for ares in agent.function_resources.filter(
resource_type=res.resource_type, role=res.role)
)
agent_total_val += sum(
ares.value for ares in agent.function_resources.filter(
resource_type=res.resource_type, role=res.role)
)
for ares in agent.function_resources.filter(role=res.role):
if ares.resource_type.is_child_of(res.resource_type):
agent_total_qty += ares.quantity
if agent_total_qty != res.quantity:
diffs.append({"function_resource": res, "function_quantity": res.quantity, "agent_total_qty": agent_total_qty})
if agent_total_val != res.value:
diffs.append({"function_resource": res, "function_value": res.value, "agent_total_val": agent_total_val})
return diffs
def has_flows(self):
flows = FunctionResourceFlow.objects.filter(
from_function__cluster=self)
if not flows:
flows = AgentResourceFlow.objects.filter(
from_function__function__cluster=self)
if flows:
return True
else:
return False
def function_flows(self):
return FunctionResourceFlow.objects.filter(
from_function__cluster=self)
def agent_flows(self):
return AgentResourceFlow.objects.filter(
from_function__function__cluster=self)
def has_function_resources(self):
frts = FunctionResourceType.objects.filter(
function__cluster=self)
if not frts:
frts = AgentFunctionResourceType.objects.filter(
agent_function__function__cluster=self)
if frts:
return True
else:
return False
def fr_graph_nodes(self):
fns = list(self.functions.all())
rtypes = []
for fn in fns:
rtypes.extend([v.resource_type for v in fn.inputs()])
rtypes.extend([v.resource_type for v in fn.outputs()])
rtypes = list(set(rtypes))
fns.extend(rtypes)
return fns
def flow_graph_nodes(self):
flows = self.function_flows()
nodes = []
for flow in flows:
nodes.append(flow.from_function)
nodes.append(flow.to_function)
nodes.append(flow.resource_type)
nodes = list(set(nodes))
return nodes
def regions(self):
areas = {}
agents = self.agents()
for agent in agents:
co = self.community
ca = CommunityAgent.objects.get(
community=co, agent=agent)
key = ca.region_latitude + ca.region_longitude
if key:
if not key in areas:
areas[key] = Region(
ca.geographic_area,
ca.region_latitude,
ca.region_longitude,
{})
area = areas[key]
afs = agent.functions.filter(function__cluster=self)
for af in afs:
if not af.function.id in area.function_dict:
area.function_dict[af.function.id] = AggregateFunction(
af.function, {})
fn = area.function_dict[af.function.id]
for afrt in af.function_resources.all():
reskey = "".join([str(afrt.resource_type.id), afrt.role])
if not reskey in fn.resource_dict:
fn.resource_dict[reskey] = AggregateFunctionResource(
af.function, afrt.resource_type, afrt.role, 0.0, Decimal("0.00"), 0.0)
rt = fn.resource_dict[reskey]
rt.quantity += afrt.quantity
rt.price += afrt.price
rt.value += afrt.value
return areas.values()
def groups(self):
groups = {}
agents = self.agents()
for agent in agents:
co = self.community
ca = CommunityAgent.objects.get(
community=co, agent=agent)
key = ca.group
if key:
if not key in groups:
groups[key] = AgentGroup(
ca.group,
{})
grp = groups[key]
afs = agent.functions.filter(function__cluster=self)
#import pdb; pdb.set_trace()
for af in afs:
if not af.function.id in grp.function_dict:
grp.function_dict[af.function.id] = AggregateFunction(
af.function, {})
fn = grp.function_dict[af.function.id]
for afrt in af.function_resources.all():
reskey = "".join([str(afrt.resource_type.id), afrt.role])
if not reskey in fn.resource_dict:
fn.resource_dict[reskey] = AggregateFunctionResource(
af.function, afrt.resource_type, afrt.role, 0, Decimal("0.00"), 0)
rt = fn.resource_dict[reskey]
rt.quantity += afrt.quantity
rt.price += afrt.price
rt.value += afrt.value
return groups.values()
def group_flows(self):
groups = {}
agents = self.agents()
for agent in agents:
co = self.community
ca = CommunityAgent.objects.get(
community=co, agent=agent)
key = ca.group
if key:
if not key in groups:
groups[key] = AgentGroup(
ca.group,
{})
grp = groups[key]
afs = agent.functions.filter(function__cluster=self)
for af in afs:
if not af.function.id in grp.function_dict:
grp.function_dict[af.function.id] = AgentGroupFunction(
af.function, {})
fn = grp.function_dict[af.function.id]
for flow in af.outgoing_flows.all():
flowkey = "-".join([
str(flow.from_function.function.id),
str(flow.to_function.function.id),
str(flow.resource_type.id),
])
if not flowkey in fn.flow_dict:
fn.flow_dict[flowkey] = AgentGroupFlow(
flow.from_function.function,
flow.to_function.function,
flow.resource_type,
0.0, Decimal("0.00"), 0.0)
rt = fn.flow_dict[flowkey]
rt.quantity += flow.quantity
rt.price += flow.price
rt.value += flow.value
group_flows = []
for grp in groups.values():
for fn in grp.all_functions():
group_flows.extend(fn.flows())
return group_flows
def flow_bfs(self, start, resource_filter=None):
visited=set([start])
edges = []
order = [start]
# next(iter) lines commented out until locecon upgraded to python2.7
#stack=[(start, iter(start.outflow_functions(resource_filter)))]
stack=[(start, start.outflow_functions(resource_filter))]
while stack:
parent, children = stack[0]
try:
#child = next(children)
child = children.pop(0)
if child not in visited:
edges.append((parent, child))
visited.add(child)
order.append(child)
#stack.append((child, iter(child.outflow_functions(resource_filter))))
stack.append((child, child.outflow_functions(resource_filter)))
#except StopIteration:
except IndexError:
stack.pop(0)
return [edges, order]
def flows(self):
return FunctionResourceFlow.objects.filter(
from_function__cluster=self)
def toposort_flows(self):
flows = self.flows()
functions = [flow.from_function for flow in flows]
functions.extend([flow.to_function for flow in flows])
G = list(set(functions))
for u in G:
u.preds = [flow.from_function for flow in u.incoming_flows.all()]
u.next = [flow.to_function for flow in u.outgoing_flows.all()]
Q = [u for u in G if not u.preds]
return toposort(G, Q)
def toposort_frs(self):
G = graphify(self)
for u in G:
u.preds = u.from_nodes(cluster)
u.next = u.to_nodes(cluster)
Q = [u for u in G if not u.preds]
return toposort(G, Q)
def cycles(self):
frts = FunctionResourceType.objects.filter(
function__cluster=self)
graph = {}
if frts:
gc = self.fr_graph_nodes()
for node in gc:
graph[node] = node.to_nodes(self)
else:
flows = self.function_flows()
for flow in flows:
if not flow.from_function in graph:
graph[flow.from_function] = []
rtype = ResourceAtStage(";".join([flow.from_function.name,flow.resource_type.name]))
graph[flow.from_function].append(rtype)
if not rtype in graph:
graph[rtype] = []
graph[rtype].append(flow.to_function)
#import pdb; pdb.set_trace()
scc = strongly_connected_components(graph)
cycles = []
for sc in scc:
if len(sc) > 1:
cycles.append(sc)
return cycles
def has_cycles(self):
if self.cycles():
return True
else:
return False
def value_added_rows(self, start, resource_filter=None):
edges, order = self.flow_bfs(start, resource_filter)
rows = []
symbol = "$"
try:
symbol = cluster.community.unit_of_value.symbol
except:
pass
for fn in order:
rows.append(("Function:", fn.name, ""))
costs, income, margin, margin_percent = fn.value_summary()
rows.append(("", "Costs:", "".join([symbol, split_thousands(costs)])))
rows.append(("", "Income:", "".join([symbol, split_thousands(income)])))
rows.append(("", "Margin:", "".join([symbol, split_thousands(margin)])))
rows.append(("", "Margin percent:", "".join([str(margin_percent), "%"])))
return rows
def function_colors(self):
fns = self.functions.all()
colors = {}
#import pdb; pdb.set_trace()
for fn in fns:
if fn.agents.all():
if not fn.color in colors:
colors[fn.color] = ""
if colors[fn.color]:
colors[fn.color] = ", ".join([colors[fn.color], fn.name])
else:
colors[fn.color] = fn.name
return colors
class EconomicFunction(models.Model):
cluster = models.ForeignKey(Cluster, related_name="functions")
name = models.CharField(max_length=128)
aspect = models.CharField(max_length=128, blank=True)
color = models.CharField(max_length=12, choices=settings.COLOR_CHOICES,
default="green")
slug = models.SlugField("Page name", editable=False)
class Meta:
ordering = ('cluster', 'name',)
def __unicode__(self):
return " ".join([self.cluster.name, self.name])
def save(self, *args, **kwargs):
unique_slugify(self, self.name)
super(EconomicFunction, self).save(*args, **kwargs)
def value_summary(self):
costs = sum(input.get_value() for input in self.incoming_flows.all())
income = sum(input.get_value() for input in self.outgoing_flows.all())
margin = income - costs
if income:
margin_percent = round(((float(margin) / income) * 100), 2)
else:
margin_percent = 0
#print "margin:", margin, "income:", income, "margin_percent:", margin_percent
return costs, income, margin, margin_percent
def node_id(self):
return "".join([ type(self).__name__, "-", self.slug])
def inputs(self):
return self.resources.filter(role="consumes")
def outputs(self):
return self.resources.filter(role="produces")
def out_edges(self, cluster):
return self.outputs()
def out_edge_with(self, resource_type):
try:
return self.resources.get(
role="produces",
resource_type=resource_type)
except FunctionResourceType.DoesNotExist:
return None
def outflow_functions(self, resource_filter=None):
if resource_filter:
flows = self.outgoing_flows.filter(
resource_type__name__icontains=resource_filter)
else:
flows = self.outgoing_flows.all()
fns = []
for flow in flows:
if flow.to_function not in fns:
fns.append(flow.to_function)
return fns
def from_nodes(self, cluster):
# cluster is here only for duck typing
# with EconomicResourceTypes
return [v.resource_type for v in self.inputs()]
def to_nodes(self, cluster):
return [v.resource_type for v in self.outputs()]
def flows(self):
flows = list(self.incoming_flows.all())
flows.extend(list(self.outgoing_flows.all()))
return flows
# based on dfs from threaded_comments
def nested_objects(node, all_nodes):
to_return = [node,]
for subnode in all_nodes:
if subnode.parent and subnode.parent.id == node.id:
to_return.extend([nested_objects(subnode, all_nodes),])
return to_return
def flattened_children(node, all_nodes, to_return):
to_return.append(node)
for subnode in all_nodes:
if subnode.parent and subnode.parent.id == node.id:
flattened_children(subnode, all_nodes, to_return)
return to_return
class EconomicResourceType(models.Model):
name = models.CharField(max_length=128)
parent = models.ForeignKey('self', blank=True, null=True,
related_name='children')
slug = models.SlugField("Page name", editable=False)
unit_of_quantity = models.ForeignKey(Unit, blank=True, null=True,
limit_choices_to={"type": "quantity"},
related_name="resource_units")