Flash Integration

Introduction

What is a Flash-Cedar Application?


Flash- Cedar Application (FCA) is simply a SWF (*.swf) file that communicates with Cedar along its running. Building a FCA app is simple and fun. To learn more about using and developing FCA, read the following paragraphs.

Opening FCA through Cedar

Opening FCA is simple. There are three ways:
  1. Press Ctrl+G
  2. Go to Edit -> "Open FCA"
  3. Enter "openFlash" at the command line
After using one of the ways you will need to choose a SWF file. After Choosing file Cedar will open the FCA immediately.

Why FCA is good?

A lot of the usage in flash around the web is on stuff that based on deterministic instructions. A lot of the flash Games, for example, are based on specific rules which predict the next action of the program. We think that the option to integrate flash with Cedar fast proof engine can make the flash programming easier. We think that good usage of our program capabilities can only make the web richer.

Under the hood

How does Cedar works?

Cedar is compose of three parts: the parser, the data and the proof engine:

Every time a query is been asking, Cedar "compile" it in parser. The parser converts the query (text) into a "compiled data" (to some kind of data structure). The Proof Engine (PE) has always a connection to the data that been announced previously by the client as true and already been compiled. Every time a compiled query is calling to be prove, Cedar will call PE to run on it and get the answer (YES or NO). For the client there are only two main operations:
  1. load new data
  2. ask queries

FCA interface

FCA a SWF file that can be load into Cedar be allow to control and use Cedar interface. As you can imagine, Cedar offers to the flash the same interface it offers to the regular client:

Developing a FCA

FCA API

As the last chapter says, the FCA API will be divided into two categories:
  1. Controlling the data (load & delete data)
  2. Asking questions (sending queries) to be answer.
This the interface of Cedar for ActionScript:
 functiondescription 
Ask Asking CEDAR to answer a query (String) by calling to specific function (String). 
NextAnswer 
Asking CEDAR to answer the next answer to the last query calling to specific function (String).
loadSystemFilesLoad system (from the .EXE file directory) files (Strings Array) to CEDAR. 
loadFilesFromFolderLoad files (Strings Array) from the .SWF file directory to CEDAR.  
loadDataFromString 
Load specific data query (String) to CEDAR 
 deleteAllDataDelete all data that loaded by the FCA
trace  Sending text (String) to the Cedar trunk windows
Notes: 
  1. Every FCA has its own "sandbox". The data one FCA can load and delete in is a Library of data that build specifically for it. FCA cannot talk to any other part of the Cedar parts besides the part that was build only for it.
  2. Debugging (trace(...) function and some automatic tracing) for FCA is disabled in Cedar by default. To turn debugging on\off just enter "debug" in the command- line.

Communicate with Cedar on ActionScript

 The FCA technology is based on the ActionScript 2.0 class ExternalInterface. That's why every FCA ActionScript code begins with the line:

import flash.external.ExternalInterface;

Calling an external function from the FCA API is easy. Let's tell Cedar to load the system files "math.cdr" and "lists.cdr". The function loadSystemFiles is getting a Array with the names of the files:

ExternalInterface.call("loadSystemFiles",new Array("math", "lists"));

That's it! Simple but powerful. Now Let's say you ask Cedar the query "write('hello world')." . The Ask(...) function is a little bit more complicated. The first argument is the query we want to ask. The second argument is the function Cedar will call when he has the answer. Let's say the function to be called is getAnswer(...). In those terms, the calling will look like that:

ExternalInterface.call("Ask", "write('hello world!').", "getAnswer");

Again, simple. But that's not all of it. The function to be called (in this case, getAnswer(....)) must be a special kind of function. First, if you want Cedar to be able to call a function (for example, getAnswer(...) function) you must add to your code this line:

ExternalInterface.addCallback("getAnswer", FunctionMethod, getAnswer);

The first argument means the name of the function as it seen to Cedar, the second argument is the method that declared the function and the third argument is the function real name (without quotes).
The second condition to the function is that it must have specific number and types of argument. To be exact:

function anyGetAnswerFunction(Answer:Boolean, Assignments:Array, Board:String)

(The names of the arguments are meaningless). The first argument is the actual answer, yes or no, true or false. The second argument is Array that represents the variables assignments. The third argument is a String represent the board (for example, for the query "write('hello world!')." the board will be "hello world!").
Now let's get a more complicated program:

import flash.external.ExternalInterface;

\\adding getAnswer:
ExternalInterface.addCallback("getAnswer", this, getAnswer);
\\load data from string:
ExternalInterface.call("loadFromString","bestProgram(cedar).");
\\asking query:
ExternalInterface.call("Ask", "bestProgram(X).", "getAnswer");

function getAnswer(Answer:Boolean, Assignments:Array, Board:String)
{
    trace(Assignments[1][0] + " is the best program ever!!!");
}

The program loads the data "bestProgram(cedar)." and asks Cedar "bestProgram(X)." . Of course, the answer will be "Yes, X = cedar". The getAnswer argument "Assignments:Array" is an Array that composed with two Arrays in it, the first Array has the names of the variable to be assign, and the second Array has the assignments to those variable:
As you can see, the trace of the program above should be:

trace: cedar is the best program ever!!!

Notes:
  1. Pay attention! Be careful about the function you want Cedar to call for you. Remember to add them with addCallback and be careful with the function arguments!
  2. When the answer is false the assignments Array is null.
  3. Cedar is working for you, that is why when variable assign to a prolog list, Cedar will convert it automatically into ActionScript  Array. The same for numbers and strings (for atoms).

Support

We hope this page was helpful. If you need more resources you can see the attachments to this page right on the bottom of it. If you have any questions, please contact as from this mail: prolog.cedar@gmail.com or add a comment. At the future we may open a FCA free market. If you want your app with your name on it to be shown in our market, please contact us.

We hope you'll enjoy building your own FCA,
Cedar group
Comments