Namespaces

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Namespaces

kitfox-3
I'm pulling my hair out over namespaces.  I'm trying to programatically
add methods to objects at different namespaces, but nearly anything I
try fails.  I'm trying to create a structure four or five levels deep.  
In each namespace will be one object passed in from my Java program via
the set() method.  I want to  interactively operate on my Java objects
by using syntax like

Root.menu.grid.cell.color = Color.red;

I'm bulding my code similarly to:

Root = object();
saveNS = this.namespace;
setNameSpace(Root.namespace);

menu = object();
saveNS = this.namespace;
setNameSpace(Root.namespace);

...

MyProxyObject proxy;

public Color getColor() { return proxy.getColor(); }
public void setColor(Color c) { proxy.setColor(c); }

...

setNameSpace(saveNS);

setNameSpace(saveNS);


However, nested objects keep ending up in the global namespace.  I'm not
sure why.

What would be really useful is a command that lists all objects in the
current namespace (and only the objects in the current namespace).  Is
there such a command?

'super' also doesn't always access the parent namespace.

Please let me know if I've got this namespace thing figured wrong.  Any
example that is similar to what I'm doing would be very much appreciated.

Mark McKay


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Namespaces

patniemeyer

On Aug 7, 2005, at 6:15 PM, Mark McKay wrote:

> I'm pulling my hair out over namespaces.  I'm trying to  
> programatically add methods to objects at different namespaces, but  
> nearly anything I try fails.  I'm trying to create a structure four  
> or five levels deep.  In each namespace will be one object passed  
> in from my Java program via the set() method.  I want to  
> interactively operate on my Java objects by using syntax like
>
> Root.menu.grid.cell.color = Color.red;

Ok, just to confirm a few things first -  You can get the above style  
of access at couple of ways without building any namespaces explicitly.

First, you can simply do the assignment above and BeanShell will  
construct the intermediate objects for you... building a tree of  
values for properties style access.

Second, you could create a Java object tree with getters and setters  
corresponding to the tree above and BeanShell will call the  
appropriate getters and setters to make the above call into a  
property access.

> MyProxyObject proxy;
>
> public Color getColor() { return proxy.getColor(); }
> public void setColor(Color c) { proxy.setColor(c); }

One thing I'm afraid you're going to run into right away is that  
BeanShell currently doesn't obey the properties style access for  
scripted objects... This is just a bug and should be fixed for the  
next release.  So, in other words, if you script an object with  
getters bsh won't find them with a simple dot access.

> What would be really useful is a command that lists all objects in  
> the current namespace (and only the objects in the current  
> namespace).  Is there such a command?

You can print the "magic" variable this.methods or this.variables to  
see what's in a scripted object's scope.  e.g.

   print( this.variables );

or use the getVariables() and getMethods() methods of the NameSpace.

  print( this.namespace.getVariables() );

...

So, thinking now about what you're trying to do... It sounds like you  
want to sort of map a bunch of property accessors in different  
objects in your application to one tree of properties in the script,  
without actually constructing a tree of the Java objects?   If it  
weren't for the access bug this should be pretty straightforward to  
do...  you might even use importObject() to pull the methods of your  
Java object into the correct scope.

Let me know if this is what you're trying to do.


thanks,
Pat



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Namespaces

kitfox-3
Hi Patrick,

I'm actually trying to create my own Javascript-like interpreter.  That
is, the user will have an XML file and will attach 'scriptlets' to
various nodes of the tree.  Currently, I'm building a proxy tree out of
my XML to represent the data, with a Java object reprenting each node of
the tree.  I would like to reveal this proxy tree to the BeanShell
engine so that it can call methods of the tree as well as manipulate
it's structure.

The tricky part is the dynamic code.  Following the Javascript
tradition, I'm attaching code to various nodes in the XML to provide
scope specific scripts.  Much as in Javascript one may write:

    <body onLoad="myFunc()">
        <a href="javascript:alert('eieio')">Old MacDonald had a farm</a>
        <form>
           <input type="button" name="Hello" onClick="alert('hello')"/>
           <input type="button" name="Goodbye" onClick="alert('goodbye')"/>
        </form>
    </body>

I have similarly scoped dynamic code in my tree.  I can't construct my
proxy objects with the dynamic code without compiling them on the fly,
which would be both hard to maintain and a security risk.

After reading about namespaces in the BeanShell docs, they seemed to be
the ideal solution.  I tried to construct a tree of empty object(), give
each one the proxy object they 'wrap', and then write the dynamic
scripts into the correct BeanShell object (along with some scope
specific helper functions to allow easy manipulation of the proxy objects).

One problem I keep running into is that super.namespace always refers to
the global namespace, no matter what namespace I am currently in.  I am
also getting the impression that all objects are immediate children of
the global namespace, even if they are parented to other objects.

Below is a script I just wrote as a sample of what I'm trying to do:

aaa = object();

savedNS = this.namespace;
setNameSpace(aaa.namespace);

    final HashMap stringToAaa = new HashMap();

    Object myAaaProxyObject;  //Will be filled in later via an
Interpreter.set() command
 
    public void setNode(String name, Object node)
    {
        stringToAaa.put(name, node);
    }

    //Dynamic methods to extend the abilities of object aaa will go here

    bbb = object();

    savedNS = this.namespace;
    setNameSpace(bbb.namespace);
   
        Object myBbbProxyObject;  //Will be filled in later via an
Interpreter.set() command

        setNode("myBbb", this);

        //Dynamic methods to extend the abilities of object aaa.bbb will
go here

    setNameSpace(savedNS);

setNameSpace(savedNS);


(For some reason, cutting and pasting this as a block into the BeanShell
desktop causes it to crash.  But if you enter it one line at a time, it
works).

Anyhow, when you get to setNode("myBbb", this);, I get the error message:

// Error: EvalError: Command not found: setNode( java.lang.String,
bsh.XThis ) : at Line: 1 : in file: <unknown file> : setNode ( "myBbb" ,
this )

Called from method: object : at Line: 1 : in file: <unknown file> :
object ( )

Using super.setNode("myBbb", this); doesn't help either:

// Error: EvalError: Method setNode( java.lang.String, bsh.XThis ) not
found in bsh scripted object: global : at Line: 1 : in file: <unknown
file> : super .setNode ( "myBbb" , this )

Called from method: object : at Line: 1 : in file: <unknown file> :
object ( )

One other oddity I've noticed is:

xxx = object();

print("Namespace of global: " + this.namespace);
   
bsh.NameSpace savedNS = this.namespace;
setNameSpace(xxx.namespace);

    print("Namespace of xxx: " + this.namespace);
       
    yyy = object();

    bsh.NameSpace savedNS = this.namespace;
    setNameSpace(yyy.namespace);
   
        print("Namespace of yyy: " + this.namespace);
       
        //Shouldn't this be xxx's namespace?
        print("Namespace of yyy's super: " + super.namespace);
       
        //Shouldn't this be xxx's namespace?
        print("Namespace of yyy's parent: " + this.namespace.getParent());
   
    setNameSpace(savedNS);
   
    //Shouldn't this be xxx's namespace?
    print("Namespace after returning to scope xxx: " + this.namespace);
   
setNameSpace(savedNS);

print("Namespace after returning to scope global: " + this.namespace);


Anyhow, I've tried several variations, and can't seem to figure out how
to build my tree properly.  If you could suggest anything I could do, it
would be a big help.

PS: what's the appropriate data type for these dynamic objects created
by object()?

Thanks,

Mark McKay




Patrick Niemeyer wrote:

>
> On Aug 7, 2005, at 6:15 PM, Mark McKay wrote:
>
>> I'm pulling my hair out over namespaces.  I'm trying to  
>> programatically add methods to objects at different namespaces, but  
>> nearly anything I try fails.  I'm trying to create a structure four  
>> or five levels deep.  In each namespace will be one object passed  in
>> from my Java program via the set() method.  I want to   interactively
>> operate on my Java objects by using syntax like
>>
>> Root.menu.grid.cell.color = Color.red;
>
>
> Ok, just to confirm a few things first -  You can get the above style  
> of access at couple of ways without building any namespaces explicitly.
>
> First, you can simply do the assignment above and BeanShell will  
> construct the intermediate objects for you... building a tree of  
> values for properties style access.
>
> Second, you could create a Java object tree with getters and setters  
> corresponding to the tree above and BeanShell will call the  
> appropriate getters and setters to make the above call into a  
> property access.
>
>> MyProxyObject proxy;
>>
>> public Color getColor() { return proxy.getColor(); }
>> public void setColor(Color c) { proxy.setColor(c); }
>
>
> One thing I'm afraid you're going to run into right away is that  
> BeanShell currently doesn't obey the properties style access for  
> scripted objects... This is just a bug and should be fixed for the  
> next release.  So, in other words, if you script an object with  
> getters bsh won't find them with a simple dot access.
>
>> What would be really useful is a command that lists all objects in  
>> the current namespace (and only the objects in the current  
>> namespace).  Is there such a command?
>
>
> You can print the "magic" variable this.methods or this.variables to  
> see what's in a scripted object's scope.  e.g.
>
>   print( this.variables );
>
> or use the getVariables() and getMethods() methods of the NameSpace.
>
>  print( this.namespace.getVariables() );
>
> ...
>
> So, thinking now about what you're trying to do... It sounds like you  
> want to sort of map a bunch of property accessors in different  
> objects in your application to one tree of properties in the script,  
> without actually constructing a tree of the Java objects?   If it  
> weren't for the access bug this should be pretty straightforward to  
> do...  you might even use importObject() to pull the methods of your  
> Java object into the correct scope.
>
> Let me know if this is what you're trying to do.
>
>
> thanks,
> Pat
>
>



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Namespaces

kitfox-3
In reply to this post by patniemeyer

Hi,

I've cleaned up the first example of the last email.  The last one was
correct, but a little confusing:

aaa = object();

savedNS = this.namespace;
setNameSpace(aaa.namespace);

    //A string based index of our member objects
    final HashMap stringToBbb = new HashMap();
   
    public void indexNode(String name, Object node)
    {
        stringToBbb.put(name, node);
    }


    bbb = object();

    savedNS = this.namespace;
    setNameSpace(bbb.namespace);
   
        indexNode("myBbb", this);

    setNameSpace(savedNS);

setNameSpace(savedNS);

Also, I saw that BeanShell has passed the JSR process.  
Congratulations!  Does this mean BeanShell will be shipped with Mustang?

Mark McKay




Patrick Niemeyer wrote:

>
> On Aug 7, 2005, at 6:15 PM, Mark McKay wrote:
>
>> I'm pulling my hair out over namespaces.  I'm trying to  
>> programatically add methods to objects at different namespaces, but  
>> nearly anything I try fails.  I'm trying to create a structure four  
>> or five levels deep.  In each namespace will be one object passed  in
>> from my Java program via the set() method.  I want to   interactively
>> operate on my Java objects by using syntax like
>>
>> Root.menu.grid.cell.color = Color.red;
>
>
> Ok, just to confirm a few things first -  You can get the above style  
> of access at couple of ways without building any namespaces explicitly.
>
> First, you can simply do the assignment above and BeanShell will  
> construct the intermediate objects for you... building a tree of  
> values for properties style access.
>
> Second, you could create a Java object tree with getters and setters  
> corresponding to the tree above and BeanShell will call the  
> appropriate getters and setters to make the above call into a  
> property access.
>
>> MyProxyObject proxy;
>>
>> public Color getColor() { return proxy.getColor(); }
>> public void setColor(Color c) { proxy.setColor(c); }
>
>
> One thing I'm afraid you're going to run into right away is that  
> BeanShell currently doesn't obey the properties style access for  
> scripted objects... This is just a bug and should be fixed for the  
> next release.  So, in other words, if you script an object with  
> getters bsh won't find them with a simple dot access.
>
>> What would be really useful is a command that lists all objects in  
>> the current namespace (and only the objects in the current  
>> namespace).  Is there such a command?
>
>
> You can print the "magic" variable this.methods or this.variables to  
> see what's in a scripted object's scope.  e.g.
>
>   print( this.variables );
>
> or use the getVariables() and getMethods() methods of the NameSpace.
>
>  print( this.namespace.getVariables() );
>
> ...
>
> So, thinking now about what you're trying to do... It sounds like you  
> want to sort of map a bunch of property accessors in different  
> objects in your application to one tree of properties in the script,  
> without actually constructing a tree of the Java objects?   If it  
> weren't for the access bug this should be pretty straightforward to  
> do...  you might even use importObject() to pull the methods of your  
> Java object into the correct scope.
>
> Let me know if this is what you're trying to do.
>
>
> thanks,
> Pat
>
>



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Namespaces

kitfox-3
In reply to this post by patniemeyer
Hi Patrick,

It occurred to me that I was using namespaces as scope modifiers, rather
than true namespaces, so I came up with the below program that ought to
have worked with the more rigid namespace definition.  However, this one
seems to not be able to set the namespace at all.  Could you suggest
what's wrong?

Thanks,

Mark McKay





Object nsStack = new Object()
{
    private LinkedList stack = new LinkedList();
   
    //Push
    public void switchTo(bsh.NameSpace ns)
    {
        stack.addLast(this.namespace);
        setNameSpace(ns);
    }
   
    //Pop
    public void restore()
    {
        bsh.NameSpace ns = stack.removeLast();
        setNameSpace(ns);
    }
};


Object aaa = object();

print(this.namespace);

nsStack.switchTo(aaa.namespace);

    print(this.namespace);
   
    Object bbb = object();
    nsStack.switchTo(bbb.namespace);

        print(this.namespace);
   
    nsStack.restore();
   
    print(this.namespace);

nsStack.restore();
print(this.namespace);

unset("nsStack");


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users