# Robust-first Computing Wiki

### Site Tools

dev:questions_ulam

This is an old revision of the document!

## Week One

To be copied here

## Week Two

### Q: Dynamic atom coloring?

We can override some properties of an element, such as its color, when we were programming C++ to create elements on the MFM. How can we do the same thing in ULAM? I want to create two types of Res with different colors. (This is a simplification of the Aura and Medicine element. Later I will convert them to real Aura and Medicine)

A: 28-May-2015 01:04:05PM-0600 There was a getColor() special function to allow the ULAM programmer to determine atom colors at runtime, but that mechanism is currently broken by the in-progress Site redesign efforts. Hope for getColor or some replacement to reappear within a week!

### Q: Access other atom data members?

Sometimes we want to get other Atom's data member. In the 'Aura and Medicine' game, a Medicine atom will compare the density that it observed with the density stored in another Aura atom. How can we access the data member of other atoms? I tried to write like this but got compile errors:

Atom a = ew[slot];
Int yourDensity = a.density;

A: 29-May-2015 04:37:12AM-0600: Probably the most straightforward is to use an 'is' expression combined with a cast:

Atom a = ew[slot];
if (a is Foo) {
Foo f = (Foo) a;    // Note that f is a copy of a!
Int n = f.fooField;
// ..use n for something..
} // else a is not a Foo

If you want to modify fields of an atom this way, you need to write the changes back:

element Foo {
EventWindow ew;
Int(7) fooField;

Void behave() {
if (ew[1] is Foo) {
Foo f = (Foo) ew[1];  // Makes a copy
++f.fooField;         // Modifies the copy
ew[1] = f;            // Updates eventwindow
}
}
}

### Q: WindowServices next() returning -1?

I have a problem using WindowServices with a for loop. I printed out the value of ws.next(). It is '-1'. I think that's why the code inside the for loop is not performed. Please tell me again how to pick a nearby neighbor in my EventWindow so that I can read or modify its fields.


WindowServises ws;
ws.reset(0,4);
Signal s;
Int st=au.getType((Atom) s);
if(ws.scan(st)){
density=ws.getHits(0);
if(density>0){
du.print((Int) ws.next());
for (Int slot = ws.next(); slot >= 0; slot = ws.next()) {
Atom a = ew[slot];
if (a is Signal) {
}
}
}    

A: 29-May-2015 06:24:01PM-0600: The WindowServices next() method returns -1 when there are no more slots to scan in your chosen range – but the main issue here is: You don't want to use both a ws.scan() call and a for loop with ws.next, you want one or the other. If you use scan, then you use getHits to see if anything matched, and getPick to get a randomly-chosen match. That code has got other issues too (e.g., WindowServices).

### Q: How to access slot number when using ''WindowServices scan()''?

Thank you! I fixed the code. This time I use the for loop on ws.next(). I wanted to use ws.scan(someType) but then if I used ws.getPick(0) to find a match, how would I know its actual 'slot number'?

WindowServices ws;
Random rdm;
ws.reset(1,4);
Request r;
Int rt=au.getType((Atom) r);
for(Int slot=ws.next();slot>=0;slot=ws.next()){
Atom a= ew[slot];
if(a is Request){
Request you=(Request) a;
if(density>you.density){
if(rdm.oneIn(2)){
ew.swap(0,slot);
}
}
}
}

A: 29-May-2015 11:08:39PM-0600: As long as the corresponding ws.getHits returns greater than zero, then return value of ws.getPick is the slot number! You can do stuff like:

WindowServices ws;
ws.reset(1,4);
Request r;
Int rt=au.getType((Atom) r);
if (ws.scan(rt)) {            // Any matches for rt?
Atom a = ew[ws.getPick(0)]; // Yes! Access chosen slot
if(a is Request){           // This if will always succeed..
Request r = (Request) a;
.. use r.density etc ..
}
}

or even this:

WindowServices ws;
ws.reset(1,4);
Request r;
Int rt=au.getType((Atom) r);  // Get type of element Request
if (ws.scan(rt)) {            // Any matches for that?
Request r = (Request) ew[ws.getPick(0)]; // Yes! Access slot of chosen one
.. use r.density etc ..
}

because that particular ws.scan(rt) can only return true if ws.getPick(0) is going to return the slot number of a Request atom. (Though it wouldn't be that easy if you were scanning for multiple things simultaneously.)