Chapter 3. Simple example

To see how Obix source code looks like, let's have a look at a very simple example. We didn't explain yet all elements used in the source code below, but the goal is to just get a first idea, without worrying about all the details.

Suppose we want to deal with products in an ERP application. Obviously, a first step is to define type product. This is how we do it:

type1 id : product2
   // instructions to define the type3
end type4
1 4

The definition of a type starts with the keyword type and ends with end type.

[Note]Note
Every software element is ended with the keyword end, optionally followed by it's name. So, instead of end type we can also simply write end

2

Our type's identifier is product.

id is a property of the type. Every property is defined by the syntax property_name : property_value. The spaces before and after the : are optional.

[Note]Note
Instead of writing type id : product we can also simply write type product

3

To increase readability, indentation (spaces or tabs) is used in front of instructions that are contained in an element.

One-line comments start with //.

By applying the simplifications explained in the above notes, we can write the code in a more concise way:

type product
   // instructions to define the type
end

The next step is to add attribute identifier that is used to uniquely identify the product:

type product
   attribute1 identifier type:positive322 end
end
1

The definition of an attribute starts with the keyword attribute.

2

The type of attribute identifier is positive32, which means that the identifier can be any positive number (greater than 0) stored in 32 bits

Let's add two more attributes:

type product
   attribute identifier type:positive32 end
   attribute name type:string end
   attribute price_in_cents type:positive32 end
end

If we want to convert the product to an XML string, we can add the following command:

command convert_to_XML1
   out result type:string end2
end
1

The command's identifier is convert_to_XML.

2

The command's result is stored in output argument result of type string

The final code for type product now becomes:

Example 3.1. Simple example of type product

type product

   attribute identifier type:positive32 end
   attribute name type:string end
   attribute price_in_cents type:positive32 end
   
   command convert_to_XML
      out result type:string end
   end

end

That's all for the type. As objects are created by factories, let's start defining a factory that will be used to create products:

factory1 product_factory2 type: product3
end factory
1

A factory starts with the keyword factory.

2

The factory's identifier is product_factory

3

The factory implements type product

Each factory must have at least one creator. We will use a standard creator that enables us to specify values for all attributes:

factory product_factory type: product
   creator1 create2 kind:in_all3 end
end factory
1

A creator starts with the keyword creator.

2

The creator's identifier is create

3

By specifying kind:in_all the compiler will automatically generate a creator with one input argument for each attribute defined in the type. See the section called “Creator” for more information.

A factory must define an implementation for each feature defined in the implemented type. However, the compiler generates a default implementation for each attribute if no explicit implementation is defined in the factory's source code. Hence, we don't have to code something for the attributes.

But a command's implementation must always be coded explicitly. In our case, command convert_to_XML could be implemented as follows:

   command convert_to_XML
      script1
         result2 = """3
           <product>
             <id>{{4identifier.to_string5}}6</id>
             <name>{{name}}7</name>
             <price_in_cents>{{price_in_cents.to_string}}</price_in_cents>
           </product>"""8
      end script
   end command
1

start of the script that implements command convert_to_XML

2

the result is stored in output argument result

3

start of a triple quoted string (for more information see the section called “Triple quoted string literal”)

4

start of an expression embedded in the string

5

identifier.to_string is an expression embedded in the string (attribute identifier of type positive32 is converted to type string)

6

end of an expression embedded in the string

7

another embedded expression

8

end of triple quoted string

The complete code for factory product_factory is:

Example 3.2. Simple example of factory product

factory product_factory type: product

   command convert_to_XML
      script
         result = """
           <product>
             <id>{{identifier.to_string}}</id>
             <name>{{name}}</name>
             <price_in_cents>{{price_in_cents.to_string}}</price_in_cents>
           </product>"""
      end script
   end command

   creator create kind:in_all end

end factory

Now an object of type product can be created as follows:

var product banana1 =2 product_factory.create3 ( &4
   identifier = 1235 &
   name = "banana"6 &
   price_in_cents = 207 )
1

declare variable banana of type product

2

assignment operator

3

use creator create of factory product_factory to create an object of type product

4

& is the 'line continuation character' in Obix. (see the section called “Line continuation”)

5

assign the value 123 to input argument identifier

6

assign "banana" to input argument name

7

assign 20 to input argument price_in_cents

To display an XML representation of the banana on the system console, we can write:

system.console.write_line1 ( banana.convert_to_XML2 )
1

use command write_line of the system's console (system.console) to display a message

2

execute command convert_to_XML on the object stored in variable banana

We can embed the above instructions in a service, as follows:

service product_tests

   command display_banana_XML
      script
         var product banana = product_factory.create ( &
            identifier = 123 &
            name = "banana" &
            price_in_cents = 20 )
         system.console.write_line ( banana.convert_to_XML )
      end script
   end command

end service

Executing product_tests.display_banana_XML will display the following:

           <product>
             <id>123</id>
             <name>banana</name>
             <price_in_cents>20</price_in_cents>
           </product>
[Note]Note
The above example is written without using prefixed identifiers. Those are optional and they will be explained in the section called “Prefixed identifier”.