How to Define and Use Luban Structure

 

1.      How to Define a Luban Structure

Luban Structure is property based dynamic object. A Luban Structure is consisted of two parts, one is interface definition, another is body definition. Interface definition is basically a list of all accessible properties of the structure. Body definition describes the relationship among the properties. Body definition can be compositional or procedural. In this tutorial we only use procedural body definition for simplicity.

A property of Luban Structure must have at least a name and a flow type. A flow type can be one of these four: input, output, store and static. For store and static properties user can also specify access permission that can be one of these three: readwrite, readonly and hidden. For any property, user can optionally specify the type of the data that can reside in that property. Below is an example of Luban Structure definition.

 

            namespace demo;

 

            struct Greeting

            (

                        input:

                                    saywhat;

                                    towhom;

                        output:

                                    greeting;

            )

            as process

            {

                        output.greeting = string(saywhat)+”, “+string(input.towhom);

            }

 

Above Luban code defines a Luban structure named “Greeting” that generated a greeting message string by combine its inputs plus a comma in between.

 

2.      Set and Get Luban Structure Property

You can construct one instance of demo::Greeting like below:

            greetingstruct = demo::Greeting();

 

You can set and get its properties:

greetingstruct.saywhat = “Hello”;

            greetingstruct.towhom = “World”;

            msg = greetingstruct.greeting; // msg will contain string “Hello, World”

 

Please note that every time a Luban structure input property is set, Luban will try to synchronize its input and output here by running its process body. Sometimes you may want to set multiple properties before the process runs. To do that use multiple property set statement like below:

 

            greetingstruct.( saywhat = “Hello”, towhom=”World”);

            msg = greetingstruct.greeting; //msg will contain string “Hello, World”

 

The difference between above example with previous one is while previous run process body twice for two set operations, the above one only runs the process body once.

 

3.      Automatic Dependency Tracking in Property Value Setting

For example there is a structure instance “x” that has a property “y” its value is also structure. And “y” has a property “z” that contains a vector. What will happen for below Luban code?

 

            x.y.z[0]=0;

 

When above Luban code is run, first the first element vector content of “z” property will be set to zero by the assignment. Then structure inside “y” property will be evaluated to reflect the change of its “z” property. Afterward structure “x” will be evaluated to reflect the change of state of its “y” property. All this dependencies are automatically taken care by Luban interpreter.

 

4.      Luban Structure Call

Another way to use a Luban structure is similar to a traditional function call. User pass in the arguments and the process body runs using those arguments. The big difference between structure and call and property set and get is that the structure call does NOT change the state of the structure object that is called. Actually a copy is of the structure is made first, then the property values are overwritten by call arguments, and the process body is run at last. Take a look at the below example.

 

            newgreetingobj = greetingstruct( towhom = “dude”);

            newmsg = newgreetingobj.greeting; // new msg is “Hello, dude”

            oldmsg = greetingstructg.greeting; // old msg is still “Hello, World”

 

Basically structure call is actually a copy of the structure object with properties overwritten by call arguments.

 

You can make a structure call not only with a structure instance but also a structure type name, like below:

 

            Msg = demo::Greeting(towhom=”me”, saywhat=”hi”).greeting;

 

If you wish to make a structure call without offering any overwriting arguments, you can do it as below:

 

            struct_instance(=);

 

Above call will generate a new copy of “struct_instance”, do the evaluation and then discard the new copy because there is no taker of the new copy.