##Description
This sketch displays the first few terms in the Fibonacci series. Change the value in the first line to set the number of terms to display.
##EndDescription
##Code
nbrTerms = 12
n1 = 1; n2 = 1 ; count = 2;
print(n1 + ' ' + n2 + ' ')
WHILE(nbrTerms > 2)
next = n1 + n2
print(next + ' ')
count = count + 1
IF(count % 10 == 0)
println('')
ENDIF
n1 = n2; n2 = next
nbrTerms = nbrTerms - 1
WEND
println(''); END(next)
##EndCode
QScriptIDE uses G4P and there are 2 versions for PS2 and PS3 so that is the conflict. I suggest you have separate sketch folders for PS2 and PS3 then the right versions of the libraries will be installed.
I now try to run your QScript Fibonacci-Code (see above).
To do so I tried to make a basic small sketch to run the QScript Fibonacci-Code. I extracted the relevant lines from your exampel sketch QScript IDE. But it doesn't show results (see below).
I have to say that it seems to me that QScript promises more than it can keep:
It promises that you can run any QScript within a hosting processing sketch
but in reality you have to make a lot of adjustements in the hosting processing sketch to make the QScript code really run
How would a hosting processing sketch look like that could run any QScript code ?
Best, Chrisir ;-)
// test for Fibonacci
import org.qscript.eventsonfire.*;
import org.qscript.events.*;
import org.qscript.editor.*;
import org.qscript.*;
import org.qscript.operator.*;
import org.qscript.errors.*;
import java.util.*;
// ---------------------------------------------
boolean firstTime = true;
Script script;
String[] algor;
String algorText;
// boolean showAlgor = false;
final int OFFSET_TOP = 20;
final int OFFSET_LEFT = 50;
// Selection box data
boolean selecting = false;
int x0, y0, x1, y1;
boolean validSelection;
final int nbrColors = 32;
final int resolution = 1;
int[] colors = new int[nbrColors +1];
LinkedList<ScriptEvent> event_queue = new LinkedList<ScriptEvent>();
/// -------------------------------------------------------
public void setup() {
size(500, 550);
algor = loadStrings("fibonacci.txt");
algorText = PApplet.join(algor, "\n");
printArray(algor);
script = new Script(algor);
script.setCode(algor);
textAlign(LEFT, TOP);
// Scripting events are to be sent to this object
script.addListener(this);
println("end of setup() ----------------------------------------");
}
public void draw() {
background(48);
noStroke();
fill(255);
text("we are in draw()", 14, 14);
if (firstTime) {
// Solver$.evaluate(script);
firstTime=false;
} // if
Solver$.evaluate(script);
// script.parse();
} // func
// =============================================================
/**
* Since events are fired asynchronously this method might be called whilst G4P
* is drawing its controls causing the program to crash. To avoid this we will
* add the event to a queue and process it during Processing's event loop.
*/
@EventHandler
public void onScriptEvent(ScriptEvent event) {
if (event instanceof HaltExecutionEvent && event.etype == ErrorType.STOPPED_BY_USER)
event_queue.addFirst(event);
else
event_queue.addLast(event);
}
/**
* This method has been registered with Processing so will be called just
* before the draw method. It will process a maximum of 20 events in the FIFO
* queue, then allow the draw method to execute.
* Since the script can generate hundreds of events per frame we have to
* cap the number processed if we want the GUI to be responsive.
*/
public void pre() {
int count = 0;
while (!event_queue.isEmpty () && count++ < 20)
performEventAction(event_queue.removeFirst());
}
/**
* This method performs the actions needed by any particular type of script
* event.
*/
public void performEventAction(ScriptEvent event) {
if (event instanceof TraceEvent && false) {
//
} else if (event instanceof SyntaxErrorEvent || event instanceof EvaluationErrorEvent) {
//
} else if (event instanceof HaltExecutionEvent) {
//
} else if (event instanceof ScriptFinishedEvent) {
event_queue.clear();
} else if (event instanceof OutputEvent) {
println(event.extra[0]);
} else if (event instanceof StoreUpdateEvent) {
Variable var = (Variable) event.extra[0];
StringBuilder s = new StringBuilder();
} else if (event instanceof WaitEvent) {
Argument arg = (Argument) event.extra[0];
int time = arg.toInteger();
}
} // func
//
To do so I tried to make a basic small sketch to run the QScript Fibonacci-Code. I extracted the relevant lines from your example sketch QScript IDE.
and that was one of your mistakes because you hacked away code that was still needed. :(
The next mistake was assuming that the file "fibonacci.txt" only contained the code. In fact the file includes the code and a description When loaded by 'QScript IDE' they are separated, both the code and the description are displayed but ONLY the code is used in the evaluation. You are trying to evaluate the description :D
It promises that you can run any QScript within a hosting processing sketch
Despite what you think this is true, but I didn't say it is was easy. Using the Solver$.evaluate(script); means that it is evaluated in a separate thread so special care has to be taken to avoid concurrent access exceptions. This naturally leads to more complex code.
The library is large and complicated so I strongly recommend that you read the library guides on my website here
Try this code first.
// test for Fibonacci
import org.qscript.eventsonfire.*;
import org.qscript.events.*;
import org.qscript.editor.*;
import org.qscript.*;
import org.qscript.operator.*;
import org.qscript.errors.*;
import java.util.*;
// ---------------------------------------------
Script script;
String[] algor = {
"nbrTerms = 12",
"n1 = 1; n2 = 1 ; count = 2;",
"print(n1 + ' ' + n2 + ' ')",
"WHILE(nbrTerms > 2)",
" next = n1 + n2",
" print(next + ' ') ",
" count = count + 1",
" IF(count % 10 == 0) ",
" println('')",
" ENDIF",
" n1 = n2; n2 = next",
" nbrTerms = nbrTerms - 1",
"WEND",
"println(''); END(next)"
};
// Int list to store fibseries
IntList fibseries;
LinkedList<ScriptEvent> event_queue = new LinkedList<ScriptEvent>();
/// -------------------------------------------------------
public void setup() {
size(500, 550);
// Initialize the fib series list
fibseries = new IntList();
fibseries.append(1);
fibseries.append(1);
// Create the script to evaluate
script = new Script(algor);
// Allow a maximum 20 seconds to execute the script
script.setTimeLimit(20);
// Scripting events are to be sent to this object
script.addListener(this);
// Register the pre method for processing the events
registerMethod("pre", this);
// Evaluate the script
Solver$.evaluate(algor);
}
public void draw() {
background(48);
noStroke();
fill(255);
int c = 0, px = 20, py = 30;
for (Integer i : fibseries) {
text(i, px, py);
c++;
px += 40;
if (c % 10 == 0) {
px = 20;
py += 20;
}
}
} // func
// ======================================
/**
* Since events are fired asynchronously this method might be called whilst G4P
* is drawing its controls causing the program to crash. To avoid this we will
* add the event to a queue and process it during Processing's event loop.
*/
@EventHandler
public void onScriptEvent(ScriptEvent event) {
println("SCRIPT EVENT " + event);
if (event instanceof HaltExecutionEvent && event.etype == ErrorType.STOPPED_BY_USER)
event_queue.addFirst(event);
else
event_queue.addLast(event);
}
/**
* This method has been registered with Processing so will be called just
* before the draw method. It will process a maximum of 20 events in the FIFO
* queue, then allow the draw method to execute.
* Since the script can generate hundreds of events per frame we have to
* cap the number processed if we want the GUI to be responsive.
*/
public void pre() {
int count = 0;
while (!event_queue.isEmpty () && count++ < 20)
performEventAction(event_queue.removeFirst());
}
/**
* This method performs the actions needed by any particular type of script
* event.
*/
public void performEventAction(ScriptEvent event) {
println("EVENT " + event);
if (event instanceof TraceEvent) {
println("Trace event");
} else if (event instanceof HaltExecutionEvent) {
} else if (event instanceof ScriptFinishedEvent) {
} else if (event instanceof OutputEvent) {
} else if (event instanceof StoreUpdateEvent) {
println("Storage event");
Variable var = (Variable) event.extra[0];
if (var.getIdentifier().equals("next")) {
int n = var.toInteger();
println(n);
fibseries.append(n);
}
} else if (event instanceof WaitEvent) {
} else if (event instanceof SyntaxErrorEvent) {
//Hopefully this won't be needed
} else if (event instanceof EvaluationErrorEvent) {
//Hopefully this won't be needed
}
} // func
//
So the problem is not QScript but positioning and rotating the quadrants. I have been messing about with this for a couple of hours but have had no luck. Sorry :)
Answers
The QScriptIDE example that comes with the library will do that for you ;;)
damn...
I am overworked....
and underpayed
sorry....
No Problem.
I couldn't run the QScript IDE because processing 2.2.1 crashed with some awt error.
Maybe because I already installed processing 3.0 and they interfere?
Is QScript for processing 3.0 out already...?
;-)
QScriptIDE uses G4P and there are 2 versions for PS2 and PS3 so that is the conflict. I suggest you have separate sketch folders for PS2 and PS3 then the right versions of the libraries will be installed.
Dear @quark
I am still on it.
I now try to run your QScript Fibonacci-Code (see above).
To do so I tried to make a basic small sketch to run the QScript Fibonacci-Code. I extracted the relevant lines from your exampel sketch QScript IDE. But it doesn't show results (see below).
I have to say that it seems to me that QScript promises more than it can keep:
It promises that you can run any QScript within a hosting processing sketch
but in reality you have to make a lot of adjustements in the hosting processing sketch to make the QScript code really run
How would a hosting processing sketch look like that could run any QScript code ?
Best, Chrisir ;-)
and that was one of your mistakes because you hacked away code that was still needed. :(
The next mistake was assuming that the file "fibonacci.txt" only contained the code. In fact the file includes the code and a description When loaded by 'QScript IDE' they are separated, both the code and the description are displayed but ONLY the code is used in the evaluation. You are trying to evaluate the description :D
Despite what you think this is true, but I didn't say it is was easy. Using the
Solver$.evaluate(script);
means that it is evaluated in a separate thread so special care has to be taken to avoid concurrent access exceptions. This naturally leads to more complex code.Ok
Sorry, I didn't mean to be rude
Which parts are missing then?
I don't want to run it in a separate thread
No apology needed, no offense taken :)
The library is large and complicated so I strongly recommend that you read the library guides on my website here
Try this code first.
If you only want to display the output in the console then this would do it :)
@quark:
what is the right command instead of
Solver$.evaluate(script);
?I didn't find
script.run();
orscript.start();
To evaluate the script in the main thread (synchronous) then use
Solver.evaluate(script);
The only difference is removing the $.
The last example used synchronous evaluation and displays the first 12 terms of the series.
This example (also synchronous) shows how you can get a particular term in the series.
Note
ok, this is the minimalistic version
the QScript script is
result
ah, sorry, I haven't seen your posts since I was working...
;-)
No problem, I hope they helped :)
thanks a lot......
;-)
journey tomorrow for a few days...
but then....
it will be part of a much bigger project though..............
;-)
Dear @quark,
I now try to bring the Fibonacci QScript into the classical graphical representation of Fibonacci.
but something is terrible wrong....
Could you please take a look?
Thank you!
Best, Chrisir
;-)
So the problem is not QScript but positioning and rotating the quadrants. I have been messing about with this for a couple of hours but have had no luck. Sorry :)
weird....
did you check the 1st openprocessing sketch?
oh, I solved it
issue was that we must call
only once and not throughout (as I had it first).
It is called once in
setup()
and then x,y are changed throughout.I had the mistake that I had these 2 lines within the function
drawArc()
.Btw. the code stems of course from openprocessing, see below.
Thank you!
Chrisir ;-)
@quark : in this new version I replaced the arcs with mere rects (little simpler)
thanks again!
Chrisir ;-)
Dear @quark,
after I made the first of the 3 example QScript scripts that come with your nice QScript IDE example into a visual (Fibonacci):
Are there any special ideas how to show the other 2 examples graphical?
I was just thinking of a bar diagram...
Some visual way to convey there meaning as we did with the Fibonacci values?
Do you have any other ideas for visuals?
thank you!
Best, Chrisir ;-)
There is already a graphical example of the prime number sieve (comes with the library).
I can't see any interesting graphical representation of the 'Solve Quadratic Equation' example .