-
Notifications
You must be signed in to change notification settings - Fork 0
/
cg_generator.py
65 lines (48 loc) · 2.2 KB
/
cg_generator.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
import random
from sim import *
class CGGenerator():
# trying to channel both Ash*
def __init__(self, maxwidth, seed = None):
random.seed(seed)
self.maxwidth = maxwidth
self.cnt = 0
def skolem(self):
self.cnt += 1
return "N" + str(self.cnt)
def callgraph(self, parent, maxdepth, maxalternatives, mandatory = False):
nchildren = random.randint(1, self.maxwidth)
if mandatory:
optional = False
else:
optional = (random.randint(0,1) == 0)
name = self.skolem()
#print "CREATE NODE " + str(name) + " optional " + str(optional)
alt = None
a = CallTree(name, parent, optional, alt)
#print "I just created " + str(a) + " parent " + str(a.parent)
if parent is not None and not optional and random.randint(0, maxalternatives) > 0:
#print "I have an alter ego."
# I shall have an alter ego
alt = self.callgraph( a, maxdepth, maxalternatives-1, True)
#print "NODE " + name + "has alternative " + str(alt)
a.add_alternative(alt)
# now decide if we have children, and if so how many.
#print "flip a coin from 0 - " + str(maxdepth) + " to decide if " + str(a) + "spawns"
if random.randint(0, maxdepth) > 0:
#print "NODE " + name + " shall have " + str(nchildren)+ " children"
# I shall reproduce
#for i in range(nchildren):
#print "lalala " + str(i)
for i in range(nchildren):
#print "OK OK " + str(a) + " at " + str(i)
chld = self.callgraph(a, maxdepth-1, maxalternatives)
#print "NODE " + name + " has child " + str(chld)
# something funny is going to happen with the children of alternatives
a.add_child(chld)
#else:
# print "NODE " + name + " is childless"
return a
def new_graph(self, maxdepth, maxalternatives):
# this is hard to get right, bear with me
# first, decide if I am a leaf. pick a number between 0 and maxdepth; if it's zero, I am a leaf.
return self.callgraph(None, maxdepth, maxalternatives, True)