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
|
begintemplate NetCell
public type, x, y // NetReadyCell or ArtCell, location
//implementation
public create1, real, replace
objref real
public name, pl, index
objref type
proc init() {
type = $o1
x = $2
y = $3
index = -1
}
proc name() {local i
sprint($s1, "%s%d", type.name, index)
}
proc pl() {local i
name($s2)
$o1.label(x, y, $s2, 1, 1, .5, .5, $3)
}
proc create1() {
type.create1(real)
real.position(x, y, 0)
}
proc replace() { //NetReadyCell or ArtCell
type = $o1
if (object_id(real)) {
create1()
}
}
endtemplate NetCell
begintemplate NetSource
public cell // NetCell
//implementation
public edges // list of NetEdge
objref cell, edges
proc init() {
cell = $o1
edges = new List()
}
endtemplate NetSource
begintemplate NetTarget
public cell, syn //NetCell, SynTypeInstance (or nil if NetCell is an ArtCell)
// implementation
public edges, synindex //list of NetEdge
objref cell, syn, edges
proc init() {
cell = $o1
synindex = $2
if (cell.type.is_art == 0) {
syn = cell.type.synlist.object(synindex)
}
edges = new List()
}
endtemplate NetTarget
begintemplate NetEdge
public source, target, w, d //NetCell, NetTarget, weight, delay
//implementation
public create1, netcon, w, d, name, change, namebar
objref netcon
objref source, target
proc init() {
source = $o1
target = $o2
w = $3
d = $4
}
proc create1() {
if (target.cell.type.is_art) {
netcon = source.cell.real.connect2target(target.cell.real.pp)
}else{
netcon = source.cell.real.connect2target( \
target.cell.real.synlist.object(target.synindex))
}
netcon.weight = w
netcon.delay = d
}
proc change() {
if (object_id(netcon) != 0) {
netcon.weight = w
netcon.delay = d
}
}
proc name() {
source.cell.name($s1)
if (target.cell.type.is_art) {
sprint($s1, "%s->%s%d", $s1, target.cell.type.name, \
target.cell.index)
}else{
sprint($s1, "%s->%s%d.%s%d", $s1, target.cell.type.name, \
target.cell.index, target.syn.stype.name,target.syn.index)
}
}
proc namebar() {
source.cell.name($s1)
if (target.cell.type.is_art) {
sprint($s1, "%s__%s%d", $s1, target.cell.type.name, \
target.cell.index)
}else{
sprint($s1, "%s__%s%d_%s%d", $s1, target.cell.type.name, \
target.cell.index, target.syn.stype.name,target.syn.index)
}
}
endtemplate NetEdge
begintemplate NetData
public sources, targets, edges // lists of NetCell, NetTarget, NetEdge
public add_edge, remove_edge, edge_index
public add_cell, remove_cell, replace_cell
public cel2tar
//implementation
public create1, created_, abstract
objref sources, targets, edges, cel2tar_
objref tobj, nil, oldtype
proc init() {
created_ = 0
sources = new List()
targets = new List()
edges = new List()
cel2tar_ = new Vector(0)
}
func add_edge() {
edges.append($o1)
$o1.source.edges.append($o1)
$o1.target.edges.append($o1)
if (created_) {$o1.create1()}
return edges.count-1
}
// room for elimination of one of the index searches in the following if
// pass index to remove_edge or return object in edge_index
func remove_edge() {local i
i = edges.index($o1)
edges.remove(i)
$o1.source.edges.remove($o1.source.edges.index($o1))
$o1.target.edges.remove($o1.target.edges.index($o1))
return i
}
func edge_index() {local i //NetSource, NetTarget -- return edges index
// in case of multiples it is the first one in source list
for i=0, $o1.edges.count-1 {
if ($o1.edges.object(i).target == $o2) {
return edges.index($o1.edges.object(i))
}
}
return -1
}
proc add_cell() {local i // NetCell
cel2tar_update_ = 1
sources.append(new NetSource($o1))
if ($o1.type.is_art) {
targets.append(new NetTarget($o1, 0))
}else{
for i=0, $o1.type.synlist.count-1 {
targets.append(new NetTarget($o1, i))
}
}
$o1.index = sources.count - 1
if (created_) {
$o1.create1()
}
}
proc remove_cell() { local i // NetCell
cel2tar_update_ = 1
sources.remove($o1.index)
for i=$o1.index, sources.count-1 {
sources.object(i).cell.index = i
}
for (i=targets.count-1; i >= 0; i -= 1) {
if (targets.object(i).cell == $o1) {
targets.remove(i)
}
}
for (i=edges.count-1; i >= 0; i -= 1) {
if (edges.object(i).source.cell == $o1 || edges.object(i).target.cell == $o1) {
edges.object(i).netcon = nil
edges.remove(i)
}
}
$o1.real = nil
}
proc replace_cell() { local i, j, r //NetCell, NetReadyCell or ArtCell
if ($o1.type == $o2) {
return
}
cel2tar_update_ = 1
// get rid of NetCon first to avoid bug when real cell deleted
if (created_) for i=0, edges.count - 1 {
tobj = edges.object(i)
if (tobj.source.cell == $o1 || tobj.target.cell == $o1) {
tobj.netcon = nil
}
}
oldtype = $o1.type
$o1.replace($o2)
r = 1
if (oldtype.is_art == 0 && $o2.is_art == 0) {
if (oldtype.synlist.count != $o1.synlist.count) {
// incompatible target replacement
r = 0
}
}
if (r == 0) { // target list is must be made consistent
// must remove target edges since no unambiguous map from old to new
for (i=targets.count-1; i >= 0; i -= 1) {
if (targets.object(i).cell == $o1) {
targets.remove(i)
}
}
for (i=edges.count-1; i >= 0; i -= 1) {
if (edges.object(i).target.cell == $o1) {
edges.object(i).netcon = nil
edges.remove(i)
}
}
if ($o1.type.is_art) {
targets.append(new NetTarget($o1, 0))
}else{
for i=0, $o1.type.synlist.count-1 {
targets.append(new NetTarget($o1, i))
}
}
}else{ // target list is consistent but synapses and netcon must be updated
for i=0, targets.count-1 {
tobj = targets.object(i)
if (tobj.cell == $o1) {
if ($o2.is_art == 0) {
tobj.syn = $o1.synlist.object(tobj.synindex)
}
if (created_) {
for j=0, tobj.edges.count-1 {
tobj.edges.object(j).create1()
}
}
}
}
}
// in any case we can keep the source edges
if (created_) for i=0, edges.count - 1 {
if (edges.object(i).source.cell == $o1) {
edges.object(i).create1()
}
}
}
func cel2tar() {
if (cel2tar_update_) { cel2tar_update() }
return cel2tar_.x[$1] + $2
}
proc cel2tar_update() { local n
cel2tar_.resize(sources.count)
if (sources.count > 0) {
cel2tar_.x[0] = 0
}
for i=0, sources.count-2 {
if (sources.object(i).cell.type.is_art) {
n = 1
}else{
n = sources.object(i).cell.type.synlist.count
}
cel2tar_.x[i+1] = cel2tar_.x[i] + n
}
cel2tar_update_ = 0
}
proc create1() {local i
created_ = 1
for i=0, sources.count-1 {
sources.object(i).cell.create1()
}
for i=0, edges.count-1 {
edges.object(i).create1()
}
}
proc abstract() { local i
created_ = 0
for i=0, edges.count-1 {
edges.object(i).netcon = nil
}
for i=0, sources.count-1 {
sources.object(i).cell.real = nil
}
}
endtemplate NetData
|