Showing posts with label Cue. Show all posts
Showing posts with label Cue. Show all posts

Friday, May 17, 2013

Pythagorean theorem in Cue Editor

Last month I presented Cue language for scripting events in QetriX. I mentioned I created an online editor, but I didn't show it here. So here is a screenshot: (it's shrinked together as I wanted to save pixels here)

Pythagorean theorem in Cue Editor
On the very left there is a block schema, where you can see a flow of all commands, because Cue utilizes a lot of "goto" commands and it may be easy to get lost in the code.

In the gray box, titled "Events", you can see the source code on the left an and debug output and trace on the right. Output is like a console, but here is nothing to print out. In Trace you can see what commands in what order (and with what result) has been executed.

"RESULT" is the value, what is returned into code after processing the Cue code.

In the code I defined two constants, but you can get those input values from QetriX, or e.g. GET, POST or COOKIE variables. So first two lines contains numbers 3 and 4. Pythagorean theorem for calculation the hypotenuse (variable "c") is: "c = sqrt(a² + b²)", so first of all we have to calculate square power for "a" and "b" (lines 3 and 4, showing two different ways how to achieve that, using "mul" and "pow" commands). Then we add those two results together and finally we calculate a square root of the sum.

There are no gotos or decisions, so the code is nicely straight - from Start ("S" circle) to End ("E" circle). Final result of the code is the same, as the result of the last line.

Tuesday, April 23, 2013

Cool Events

I tried to came up with a cool title for this hot spring day, so I hope this is cool enough ;-)

Event is triggered on a specific occasion. It can be specific timestamp (wildcards are possible) or any kind of manipulation with entity/attrib/relation type or even specific entity/attrib/relation.

Main purpose of an event is to launch associated script (set of commands) in a Cue scripting language, described earlier in this blog.

My first hunger for events was in my early project (2007), where everything was hardcoded. And except nobody has an idea how it actually works, when something was wrong, it was pain to correct it. For the next version I designed (in my head) a way, how to make it editable. Unfortunately there was nobody willing to pay the development, so the idea stayed just in my head.

I don't need any bells or whistles, I just need a tool for designing a list of consecutive commands and some minor evaluating on the way. It's like "when this, do that".

For timing events there will be a cron or a job (mostly every day or every hour), for data manipulation there will be some hooks hardcoded into CRUD methods. Nevertheless, the system has to be detachable, because I want to have system of independent layers (as I hate to execute unnecessary code over and over again).

And what is so cool about those events? I won't have to hardcode specific behavior any more, I'll just set a trigger (event) and write some commands to be executed. And that pays off.

Thursday, April 18, 2013

Cue for events

Everything in QetriX is somehow unique or innovative. From my previous projects I knew I would need a simple scripting. I tried LUA, but it was too complex. I tried JavaScript, but it was even more complex :)

In my mind I already had a concept of simple scripting language. Every line will contain one command and, like in Assembler, there will be a keyword first, then arguments separated by space. I didn't care about spaces in text, because only language keywords would be allowed and they won't contain spaces anyway.

I call this language "Cue", because in English it has the same pronunciation as "Q" and it's meaning is similar to purpose of the language - to trigger an action.

From general perspective it's a bad language, because it has wildly disputed "goto" command. And even worse - "goto" here is the only command for structure, as there are no blocks (like curly braces in C) and all functions has to be coded in advance in platform's source code. From Cue you can only call them with arguments and retrieve their return value.

There are no variables in Cue, the result of each command is stored under a number of the line, where the command is. When you need the value, you can simply recall it by using hash and number of the line, e.g. #5 to get a value from the fifth line.

There are a major catch for editing the code, when number of lines changes due to add/remove lines in the middle of the code. For this reason there is a smart code editor "out of the box", which also draws a diagram of the source code (little help to manage all those gotos) and provides inline help for each command and it's arguments. It also allows to run the code and display a step trace.

Now let me show you how to code (gray comments are not part of the code):
  1. add 5 4 // calculates 5 + 4 and stores the result (9) into line number
  2. sqrt #1 // calculates square root of the result from line 1 (=9)
The output of the script above is 3. You can get the output by either using a "return" call and specify the value (constant or line value) as its parameter, or by finishing a last line of script, where output will be a value of the last line's value. Second choice is our case, final output will be a value of the 2nd line.

The only thing I don't like (but I don't see any other option, so I dealt with it) are conditions; IF statements, so to speak. Because it technically consists of two parts - condition and then/else branches, I had to split it onto two lines. In the first one I eval the condition (and store true/false result), the second one is if-goto statement. There is no else branch, because if the condition is false, the parser goes to the next line (which is, in fact, the else branch).

I thought about more complex "if" statement, but only this way you can use multiple conditions clearly. The only hack is, that "if" is always if-goto, it's NOT an if-command, because it would violate the single-line-single-command concept.

QetriX Cue Code Editor

The best usage though is to define actions between two states in a work flow. In QetriX you can define events, as a starting point for launching Cue script. Events are fired in specific occasion, but I'll write about it next time. For more info about Cue please visit our wiki.