onMouseDown pointers inside a loop in rascal - rascal

In the following code I want each box (Figure) to have his own mousedown.
But after rendering all the boxes use the same file, the last read file.
for(loc file <- lsFile){
lsBox += box(
onMouseDown(bool (int butnr, map[KeyModifier,bool] modifiers) {
renderFile(file);
return true;
})
);
}
Anyone knows how to fix this?

for(loc file <- lsFile){
loc tmp = file;
lsBox += box(
onMouseDown(bool (int butnr, map[KeyModifier,bool] modifiers) {
renderFile(tmp);
return true;
})
);
}
The closure captures the file variable, not its value. So you slways have a reference to its last value. The tmp variable is newly allocated for each iteration, so it doesnot have this problem.

Related

How to remove previous printed line from Console in Dart

How I can remove previously printed line in console with print() in Dart?
I am willing to display a progress indicator such as:
int n = 0;
for (var item in list) {
n++;
print('${(n / list.length * 100).toStringAsFixed(0)}%');
// do something
}
Currently it prints:
1%
2%
3%
...
But I would like to delete the previous line and replace it with the newly calculated percent.
print always adds a newline, but you can write directly to stdout to avoid that.
To overwrite the previous line, you can place a carriage return ('\r') character at the start of your string, which tells the cursor to return to the beginning of the line. You may also want to pad the end of your string with some spaces to overwrite any text which might still be remaining from the previous write, although that's not necessary in this case.
It may look something like this in the end:
// At top of file
import 'dart:io';
int n = 0;
for (var item in list) {
n++;
stdout.write('\r${(n / list.length * 100).toStringAsFixed(0)}%');
// do something
}
You can use dart:io and a carriage return (\r):
import "dart:io";
void main() {
for (int i = 0; i < 5; i++) {
stdout.write("\r $i");
}
}

dxl script to paste the clipboard contents in selected objects

I want to add clipboard contents in existing Object Text using dxl script.
I searched around, including dxl_reference_manual but nothing helped.
The object in selection has some text for example "Already existing text in this object" and clipboard contents for example "My Clipboard text" should add at the beginning and form as a single object.
(Output should be something like below in a single object.)
My Clipboard text
Already existing text in this object
My code:
Skip fGetSelectedObjects(Module in_mod)
{
Skip skpObjects = create() // Return KEY and DATA both 'Object'
if (null in_mod) return(skpObjects)
Object oCurr = current,
o
for o in entire (in_mod) do
{ if (isSelected(o) or
o == oCurr) put(skpObjects, o, o)
}
return(skpObjects)
} // end fGetSelectedObjects()
Skip skpObjects = fGetSelectedObjects(current Module)
Object o
for o in skpObjects do
{ // deal with the selected o
string s = o."Object text"
// I don't know the way to activate the object text attribute instead of manual click. Thus it loops through selection and pastes the clipboard contents.
pasteToEditbox
//For Single Indentation use 360 points, double indentation 720 points and so on...
o."Object text" = richText (applyTextFormattingToParagraph(richText s,false,360,0))
}
delete(skpObjects)
Not sure why you would be using a Skip for this. I would look to do the following:
// Create Variables
Module mod = current
Object obj = null
Buffer buf = create
string str = stringOf ( richClip )
// Loop through Module
for obj in entire ( mod ) do {
// Grab the rich text from the clip and reset the buffer
buf = str
// Check if it's selected and object heading is empty
if ( ( isSelected ( obj ) ) && ( obj."Object Heading" "" == "" ) ) {
// If it is, add the text to the buffer
buf += " " richText ( obj."Object Text" )
// Set the object text with the clip stuff in front
obj."Object Text" = richText ( buf )
}
}
delete buf
Of note, this only works on items that have been specifically selected.
Edit- added exclusion for objects with an Object Heading. Unfortunately, DOORS does not (as far as I know) allow for non-contiguous object selection (the equivalent of ctrl-left click in Windows) which can be very frustrating.

Figure doesn't show correct string on event

In the following code I create 3 boxes with the text 1 to 3, in a fourth box I'd like to show the text of the box my mouse is hovering over. So i set an onMouseEnter FProperty for each of the boxes where I change the string of the fourth box and tell it to redraw.
bool redraw = false;
str s = "0";
Figure getTextbox() {
return computeFigure(bool() {bool temp = redraw; redraw = false; return temp; },
Figure() {
return text(str() {return s; });
});
}
list[Figure] boxes = [];
for (i <- [1..4]) {
boxes += box(text(toString(i)), onMouseEnter(void () {s = toString(i); redraw = true; }));
}
Figure changer = box(getTextbox());
render(vcat(boxes + changer));
However, for some odd reason all three boxes will tell the onMouseEnter method to change the text of the fourth box into "3" (the value of the last box) instead of their individual value.
Any clue why? Thanks!
Ah yes, this is the variable capturing closure problem with for loops, also known from other languages which have this particular feature like Javascript. This is the code with the issue:
for (i <- [1..4]) {
boxes += box(text(toString(i)), onMouseEnter(void () {s = toString(i); redraw = true; }));
}
The variable i is bound by the void closure and not its value. So every time the function which is created and passed to onMouseEnter it will read the latest value of the i variable. Since the callback is called after the loop terminates, all calls to the mouse enter function will have the value 3.
To fix this and "do what you want", the following code would work I believe:
for (i <- [1..4]) {
newS = toString(i);
boxes += box(text(toString(i)), onMouseEnter(void () {s = newS; redraw = true; }));
}
This works because for every pass of the for loop a new environment is created which binds the newS variable. So you'll get a fresh newS for every loop instead of the reused i.

What is the default return value of a Dart function?

The function below works even though I intentionally deleted 'return' command:
main() {
add(i) => i + 2; //I intentionally deleted 'return'
print(add(3)); //5
}
But, the function below doesn't work after I intentionally deleted the 'return' command.
main() {
makeAdder(num addBy) {
return (num i) {
addBy + i; //I intentionally deleted 'return'
};
}
var add2 = makeAdder(2);
print(add2(3) ); //expected 5, but null.
}
Edited to clarify my question.
The last sentence in the latter function above, add2(3) doesn't return a value(I expect 5) but just null returns.
My question is why 'addBy + i' of the latter function doesn't return contrary to the fact that 'add(i) => i + 2' of the first function returns 'i + 2'.
Edited again.
The answer is in the fact of '=>' being {return }, not just {}.
main() {
makeAdder(num addBy) => (num i) { return addBy + i; };
var add2 = makeAdder(2);
print(add2(3) ); // 5
}
Even the code below works as '=>' has 'return' command in it.
main() {
makeAdder(num addBy) => (num i) => addBy + i; ;
var add2 = makeAdder(2);
print(add2(3) ); //5
}
In Dart each function without an explicit return someValue; returns null;
The null object does not have a method 'call'.
makeAdder (add2) without return returns null and null(3) leads to the exception.
I would like to quote two important note here. It might help others:
Though Dart is Optionally typed ( meaning, specifying the return type of a function such as int or void is optional ), it always recommended to specify type wherever possible. In your code, as a sign of good programming practice, do mention the return type.
If your function does not return a value then specify void. If you omit the return type then it will by default return null.
All functions return a value. If no return value is specified, the statement return null; is implicitly appended to the function body.

c++ xml parser function not working

I am using xerces c++ to manipulate an xml file? but getNodeValue() and setNodeValue() are not working but getNodeName() is working. Do anyone has any suggestions?
if( currentNode->getNodeType() && currentNode->getNodeType() == DOMNode::ELEMENT_NODE )
{
// Found node which is an Element. Re-cast node as element
DOMElement* currentElement= dynamic_cast< xercesc::DOMElement* >( currentNode );
if( XMLString::equals(currentElement->getTagName(), TAG_ApplicationSettings))
{
// Already tested node as type element and of name "ApplicationSettings".
// Read attributes of element "ApplicationSettings".
const XMLCh* xmlch_OptionA = currentElement->getAttribute(ATTR_OptionA);
m_OptionA = XMLString::transcode(xmlch_OptionA);
XMLCh* t,*s;
//s= XMLString::transcode("manish");
//currentNode->setElementText(s);
t=(XMLCh*)currentNode->getNodeName();
s=(XMLCh*)currentNode->getNodeValue();
cout<getNodeValue()) << "\n";
A DOMElement may contain a collection of other DOMElements or a DOMText. To get the text value of an element you need to call the method getTextContent(), getNodeValue will always return NULL.
The is another better way conceptually, as the DOMText is a child of the DOMElement we can traverse through the child node and get the value.
Below is the logic in the form of a method:
string getElementValue(const DOMElement& parent)
{
DOMNode *child;
string strVal;
for (child = parent.getFirstChild();child != NULL ; child = child->getNextSibling())
{
if(DOMNode::TEXT_NODE == child->getNodeType())
{
DOMText* data = dynamic_cast<DOMText*>(child);
const XMLCh* val = data->getWholeText();
strVal += XMLString::transcode(val);
}
else
{
throw "ERROR : Non Text Node";
}
}
return strVal;
}
Hope this helps :)
getNodeValue() will always return an empty string, because the "value" of an element node is in its child. In our case it is text node child. Either way is to iterate through child nodes
or use getTextContent.
First check for child nodes in a node using hasChildNodes() then use methods like getFirstChild() etc. . Afterwards use getNodeValue().
DOMNode* ptrDomNode = SomeNode;
if(ptrDomNode->hasChildNodes())
{
DOMNode* dTextNode = ptrDomNode->getFirstChild();
char* string = XMLString::transcode(dTextNode->getNodeValue());
}

Resources