WiiLi Wiki frontpage Include your post in the News Get links Hoteles Quito
WiiLi.org Forum Index WiiLi.org
a new revolution
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

OpenPIE Engine design discussion
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    WiiLi.org Forum Index -> OpenPIE
View previous topic :: View next topic  
Author Message
bellwethr



Joined: 05 Nov 2007
Posts: 25

Digg It
PostPosted: Fri Nov 09, 2007 7:49 am    Post subject:

Short: yes.

Longer:

Yes, but I'd make everything an expression, including loops, interpreter thread management, etc. That way, you just walk your way through the script, execute every expression. Strive for elegance. That will help keep your code sane, and maintainable.

For example:

Your LoopExpression will contain it's own set of expressions which it will loop through as appropriate. Even your termination condition is an Expression which is evaluated after each run!

Your ParallelExpression will run multiple expressions in parallel by giving instantiating each inside their own ThreadExpression, etc.
Back to top
View user's profile Send private message
Cha0s
Site Admin


Joined: 17 Jan 2007
Posts: 493

Digg It
PostPosted: Fri Nov 09, 2007 11:01 pm    Post subject:

I think bellwethr is right on here. Basically, the parser is creating a tree of code to execute. If you keep everything in the tree, your life will be easier. I have some ideas about concurrent threads, though. I would definitely use Java's built-in system. I've done a decent bit of messing around with Threads. I'm assuming you want concurrent loops of code to execute all run at the same rate (i.e. once a block is created, every time it finishes a loop, the other blocks must finish their loops before it can loop again). In this case, I would use Java Threads and object synchronization. The advantage of this method is that Java will do most of the work for you. Sample code follows. Note that I name ParallelExpressions "threads" since that is what they are in concept.

Random side-note: are we using "Expression" or "Statement" for naming? We should standardize that. It's kind of weird having a Statement that only has Expressions as children. :\ Also is Statement going to be an abstract class (does it have any methods that are useful for all Statements) or an interface?

Code:

public class ThreadControlCenter
{
    private Vector<ParallelExpression> threads = new Vector<ParallelExpression();
    private int completedThreads = 0;
   
    public void registerThread(ParallelExpression thread)
    {
        if (!threads.contains(thread))threads.add(thread);
    }
   
    public void removeThread(ParallelExpression thread)
    {
        threads.remove(thread);
    }

    public synchronized void completeLoop(ParallelExpression thread)
    {
        //if all threads have completed a loop
        if (++completedThreads >= threads.size())
        {
            completedThreads = 0;
            for (ParallelExpression exp : threads)exp.resetLoop();
            notifyAll(); //wake up sleeping threads. They will begin executing in an arbitrary order.
        }
        else
        {
            //for as long as the current loop is considered to be finished...
            while(thread.isLoopDone())
            {
                //...make the current thread (whichever ParallelExpression just finished) sleep
                try{wait();}catch(InterruptedException e){e.printStackTrace();}
            }
        }
    }
}

public class ParallelExpression extends Statement
{
    private static ThreadControlCenter threadControlCenter = new ThreadControlCenter();
   
    private Thread parallelExpThread;
    private boolean loopDone = false;
   
    //I'm guessing the constructor for ParallelExpression will contain a list of Statements to execute.
    //It doesn't effect my implementation, though, so I'll omit it for now
   
    public void execute()
    {
        threadControlCenter.registerThread(this); //registers this thread with the thread control center
        final ParallelExpression thisExp = this;
        parallelExpThread = new Thread(new Runnable()
        {
            public void run()
            {
                while(true) //there'll probably be a condition here.
                {
                    //do the same stuff as running a normal iterated expression. Then, at the end of a loop:
                    loopDone = true;
                    threadControlCenter.completeLoop(thisExp);
                }
                threadControlCenter.removeThread(this);
            }
        });
        parallelExpThread.start();
       
    }
   
    public boolean isLoopDone(){return loopDone;}
    public boolean resetLoop(){loopDone = false;}
}


This is just a quick sketch of what such a system might look like. One caveat: we need to handle all exceptions to be absolutely sure that every thread calls completedLoop() at the end of its execution. If a thread gets stuck and never gets to that method, all the other threads will hang indefinitely.

Note also that with this method, the primary thread would also have to be a ParallelExpression. This makes sense logically, though, so it shouldn't be an issue.

Anyways, just wanted to put that on the table.
P.S. I haven't tested this code. I don't even know if it will compile. Just a disclaimer. Wink
_________________
Cha0s
Back to top
View user's profile Send private message
bellwethr



Joined: 05 Nov 2007
Posts: 25

Digg It
PostPosted: Sat Nov 10, 2007 12:24 am    Post subject:

Yeah, I basically agree with you, Cha0s, regarding theading--with one exception.

The threading semantic you just described assumes a specific synchronization model--namely, that each thread progresses one loop at a time. That certainly is one type of valid behavior, but that does limit you to that mode.... You can actually keep it simpler and just spawn each ThreadExpression inside it's own thread, but provide synchronized access to the parent ParallelExpression's state. If necessary they can then synchronize using variables or semaphores inside the ParallelExpression.

This raises one issue: variable scoping. Does GlovePIE offer any scoping support? I can't seem to find a full tutorial or grammar for GlovePIE. Does such a thing exist?
Back to top
View user's profile Send private message
Cha0s
Site Admin


Joined: 17 Jan 2007
Posts: 493

Digg It
PostPosted: Sat Nov 10, 2007 6:59 am    Post subject:

Yes, I did say I was assuming that. I got that from the comment SwedishFrog made about "scheduling" the tasks. If they don't need to line up, it's rather simple: just start the threads separately and cut out the thread control center completely. Piece of cake. Now if you need some of both (synchronization and unrestricted execution), that's slightly more complicated (but not much Smile ).

I agree with your model for the most part, but I would modify it slightly. If I understand correctly, ParallelExpressions are mostly static regarding the threads they contain (this information is determined by the parser and fixed as a Statement). I would define a class ThreadExpressionGroup that would be a variable class (a variable definable by the user) containing ThreadVariables which could be obtained by assigning them ThreadExpressions. A ThreadExpressionGroup could have various methods of synchronization (sequential, simultaneous/synchronized, unrestricted, etc. And yes, my terminology may be a bit fuzzy. It's late). In any case, a user could add/remove ThreadVariables (which really just contain ThreadExpressions) to/from a ThreadExpressionGroup at will. Also, ThreadExpressions would not be required to be in a ThreadExpressionGroup to run (ie. they would not have to be assigned to ThreadVariables), so they could be created independently (in which case they would simply begin to execute immediately). I think this dynamic setup would create the most flexibility, as you could have groups of threads that change based on conditions in the script.

Anyways, threading is probably pretty low on the list of priorities. GlovePIE only has one thread, if I'm not mistaken (though I've never used it, so I don't really know), so I think we should focus on getting its features up and running first.
_________________
Cha0s
Back to top
View user's profile Send private message
bellwethr



Joined: 05 Nov 2007
Posts: 25

Digg It
PostPosted: Sat Nov 10, 2007 7:17 am    Post subject:

*lol*

I assumed GlovePie already had a threading model that we were trying to emulate.

I agree whole heartedly--base functionality first. Smile
Back to top
View user's profile Send private message
SwedishFrog
Site Admin


Joined: 25 Jan 2007
Posts: 273
Location: New York

Digg It
PostPosted: Sun Nov 11, 2007 6:02 am    Post subject:

On controlling everything through the tree's behaviour: I'm all for it

On general design of the Code tree: Although we could build this ourselves, couldn't we find some library that allows us to generate the code structures? A suitable one probably already exists. It'd save us alot of writing\debugging time so we could get more features "out the door"

I know .Net has a feature called CodeDOM that does simulates running code, but I think the last thing we want is to get tied into a Microsoft product. There must be another Code Generating library out there for Java -- I'll do some more googling on this, it'd be nice if others did too.

Chaos: What do you mean when you say "Expression" or "Statement" for naming? I don't understand, I always thought of these two things as separate: A statement can contain an expression but an expression can't be a statement on it's own because it doesn't seem to make sense.

On representing generic statements: I feel like the generic statement should be defined by an interface, unless you can thing of a reason it should be a generic class.

GlovePIE's Variable Scoping: I don't thing it supports any sort of scoping, and the docs don't seem to mention it. Skim through Documentation.rtf in the GlovePIE package for a basic rundown of its features.

GlovePIE's Threading: I'm pretty sure GlovePIE does have some sort of threading model, though the details are a bit fuzzy because we don't have the source. Carl does allude to it a bit about it in Documentation.rtf

Quote:
WHILE loops
...
A PIE script continuously loops through the entire script, even while IF statements are running in the background.
Back to top
View user's profile Send private message Visit poster's website AIM Address
bellwethr



Joined: 05 Nov 2007
Posts: 25

Digg It
PostPosted: Sun Nov 11, 2007 6:49 am    Post subject:

SwedishFrog: So, the partner of the lexer is the parser. And like there are automatic lexer generators (JavaCC, JFlex), there are also automatic parser generators, such as CUP.

JFlex/CUP are designed to work together--while JFlex does the low level tokenization, CUP builds up your expression trees... You still have to specify rules for how expressions are built, however.

<i>Expression vs Statement</i> I've always thought of these as interchangeable. *shrug*
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    WiiLi.org Forum Index -> OpenPIE All times are GMT
Goto page Previous  1, 2, 3, 4  Next
Page 2 of 4

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group