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:
typeid : product
// instructions to define the type
end type
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 attributeidentifier type:positive32
end end
| The definition of an attribute starts with the keyword |
| The type of attribute |
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_XMLout result type:string end
end
| The command's identifier is |
| The command's result is stored in output argument |
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
endThat's all for the type. As objects are created by factories, let's start defining a factory that will be used to create products:
factoryproduct_factory
type: product
end factory
| A factory starts with the keyword |
| The factory's identifier is |
| The factory implements type |
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 creatorcreate
kind:in_all
end end factory
| A creator starts with the keyword |
| The creator's identifier is |
| By specifying |
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
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 | start of the script that implements command |
| the result is stored in output argument |
| start of a triple quoted string (for more information see the section called “Triple quoted string literal”) |
| start of an expression embedded in the string |
|
|
| end of an expression embedded in the string |
| another embedded expression |
| 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 factoryNow an object of type product can be created as follows:
var product banana=
product_factory.create
( &
identifier = 123
& name = "banana"
& price_in_cents = 20
)
| declare variable |
| assignment operator |
| use creator |
| & is the 'line continuation character' in Obix. (see the section called “Line continuation”) |
| assign the value |
| assign |
| assign |
To display an XML representation of the banana on the system console, we can write:
console.message( banana.convert_to_XML
)
| use command |
| execute command |
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 )
console.message ( banana.convert_to_XML )
end script
end command
end serviceExecuting 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 |
|---|---|
| The above example is written without using prefixed identifiers. Those are optional and they will be explained in the section called “Prefixed identifier”. |