Sunday, April 13, 2008

ActionScript Parsing

I've been helping Mark Olah out on some molecular computer visualizations in ActionScript and we are moving towards getting an actual front end for his simulator. In order to do that, we need to have some mechanism of communicating between his software and the ActionScript I have been working on to draw the pictures.

Mark suggested we have a fairly simple format that looks something like this:

//Hello World! This is Brian J. Stinar reading in a file with Adobe AIR.


Begin Surface
-1, -1, 2, 2
1.0, 1.0, 0, A
1.0, 2.0, 1, A
1.0, 3.0, 2, A
1.0, 4.0, 3, A
2.0, 1.0, 4, B
3.0, 1.0, 5, B
4.0, 1.0, 6, B
5.0, 1.0, 7, B
End Surface

Begin Spider
brian_spider_1, 4, 1
brian_spider_2, 4, 3
brian_spider_3, 4, 1,
End Spider
// Here is another comment

Begin Trace
Body Move brian_spider_1
LegAttach brian_spider_1, 1, 0
Spider Detach brian_spider_1

End Trace


If you would like to test out my parser with this file, the file I am parsing is available here.

Then, I will read in this file and generate graphics accordingly. After working today for a few hours, I think I have parsing down in ActionScript (Adobe AIR, actually?). The regular expression syntax is just like Perl, and well documented by Adobe.

Here is the code I came up with :


package com.identityMine.filereader
{

import flash.filesystem.*;
import flash.display.MovieClip;

// The extention here is to make it so that I can independently test these routines
public class FILEREADER extends MovieClip {


// Constructor - loads in the input file we are reading
public function FILEREADER(){
// Create a file object and let our app know where the file
// exists

// have we seen the "Begin Surface" command yet?
var inSurface : Boolean = false;
var inTrace : Boolean = false;
var inSpider : Boolean = false;
var inMove : Boolean = false;

// This is the file we are reading
var myFile:File = File.applicationDirectory;
myFile = myFile.resolvePath("mySampleFile.txt");

var fileStream : FileStream = new FileStream();
fileStream.open(myFile, FileMode.READ);

// Everything inside our file
var fileContents : String = fileStream.readUTFBytes(fileStream.bytesAvailable);

// Split our entire file based on newlines
var splitString = fileContents.split("\n");

// this prints out everything in my fileContents string
// trace (splitString);

// iterate through each element in the array
// each represents a piece of information necessary to construct our
// visualization

// looping variable
var i : int;
// splitting variable
var split_val : int;

// Regular Expression for matching a string of 1 or more digits
var re1 = new RegExp("[0-9]+");

// Regular Expression for matching a sequence of alph characters
var re2 = new RegExp("[a-z]*[A-Z]*");
var re3 = new RegExp("[//s]*LegAttach");

var SubVals;

for (i = 0; i < split_val = "Begin Surface" insurface =" true;" i =" i" insurface ="="" subvals =" splitString[i].split(',');" split_val = "End Surface" insurface =" false;" split_val = "Begin Trace" intrace =" true;" split_val = "Begin Spider" inspider =" true;" split_val = "End Spider" inspider ="="" inspider =" false;" inspider ="="" subvals =" splitString[i].split(',');" split_val = "Body Move" inmove =" true;" split_val = "Spider Detach" inmove ="="" intrace ="="" inmove =" false;" split_val = "End Trace" intrace ="="" intrace =" false;" split_val = "LegAttach" intrace ="="" inmove ="="" subvals =" splitString[i].split(',');">













Code Available Here



This is a TERRIBLE solution to my problem of not being able to display code well on my blog. As an actual solution, I should make my blog wider (and yes, adjust the images that came with the template.)


As of yet, I still need to check to see how error resilient my code is. I think my regular expressions might accept too much stuff. For the next small increment, I am going to actually print out the corresponding function call I plan on making. For testing purposes, I added a bit of junk into my trace file and my program spit it out.



No comments: