1 |
from blocktype import * |
2 |
|
3 |
blocknameindex = {} |
4 |
|
5 |
PORT_IN = 0 |
6 |
PORT_OUT = 1 |
7 |
|
8 |
class BlockInstance: |
9 |
""" |
10 |
Application-layer representation of an instance of a Block on the Canvas. |
11 |
Includes a reference to the BlockType as well as the block's name. |
12 |
Eventually, the instance will include a reference to the corresponding |
13 |
ASCEND object, allowing results from the ASCEND solver to be inspected via |
14 |
the Canvas GUI. |
15 |
""" |
16 |
# FIXME still need some way of setting *parameters* associated with a block. |
17 |
|
18 |
def __init__(self,blocktype,name=None): |
19 |
self.blocktype = blocktype; |
20 |
n = str(blocktype.type.getName()) |
21 |
if not blocknameindex.has_key(n): |
22 |
blocknameindex[n] = 0 |
23 |
blocknameindex[n] += 1 |
24 |
print "Block index for %s is %d" % (n, blocknameindex[n]) |
25 |
if name is None: |
26 |
name = self.get_default_name() |
27 |
self.name = name |
28 |
# ASCEND reference: |
29 |
self.instance = None |
30 |
|
31 |
self.ports = {} |
32 |
for n in self.blocktype.inputs: |
33 |
t = n.getId() |
34 |
# TODO record that it's an input port |
35 |
self.ports[t] = PortInstance(self,t, PORT_IN) |
36 |
for n in self.blocktype.outputs: |
37 |
t = n.getId() |
38 |
# TODO record that it's an output port |
39 |
self.ports[t] = PortInstance(self,t, PORT_OUT) |
40 |
|
41 |
def get_default_name(self): |
42 |
n = str(self.blocktype.type.getName()) |
43 |
print blocknameindex |
44 |
print "the name is:",n |
45 |
if not blocknameindex.has_key(n): |
46 |
print "the key '%s' is not in blocknameindex" % n |
47 |
|
48 |
return "%s%s" % (n, blocknameindex[n]) |
49 |
|
50 |
def __str__(self): |
51 |
return "\t%s IS_A %s;\n" % (self.name, self.blocktype.type.getName()) |
52 |
|
53 |
class PortInstance: |
54 |
""" |
55 |
Application-layer representation of a Port, which is a variable inside a |
56 |
MODEL which is connectable using the Canvas GUI. Class includes the name of |
57 |
the variable represented by the Port, but no type information, as that is |
58 |
currently difficult to extract from the ASCEND API. |
59 |
""" |
60 |
def __init__(self,blockinstance,name, type): |
61 |
self.blockinstance = blockinstance |
62 |
self.name = name |
63 |
self.type = type |
64 |
|
65 |
class LineInstance: |
66 |
def __init__(self,fromport=None,toport=None): |
67 |
self.fromport = fromport |
68 |
self.toport = toport |
69 |
|
70 |
def __str__(self): |
71 |
""" |
72 |
Create a string for use in MODEL export. |
73 |
""" |
74 |
fromname = "%s.%s" % (self.fromport.blockinstance.name, self.fromport.name) |
75 |
toname = "%s.%s" % (self.toport.blockinstance.name, self.toport.name) |
76 |
return "\t%s, %s ARE_THE_SAME;\n" % (fromname, toname) |
77 |
|
78 |
# TODO set up reversible properties...? |
79 |
|
80 |
|