![]() | Note |
|---|---|
| This chapter only describes basic concepts of types. Please have a look at the links at the end of this section to get information about more advanced features (e.g. type inheritance and generic types). |
Types are used to describe features of objects.
There are 3 different kinds of features:
attributes
commands
events
Each type can contain 0, 1 or or more features of each kind.
Each type is identified by a unique type identifier. A type identifier is a prefixed identifier starting with ty_ as prefix. For more information about prefixed identifiers see the section called “Prefixed identifier”.
Types cannot be used to create objects. Objects can only be created by factories (see the section called “Factory”). 0, 1 or or more objects of a given type can exist at a given point of time.
Each type implicitly inherits from type any_type which is the parent type of each type in the type system. Type any_type is defined as follows:
///
Copyright (C) 2009-2012 Christian Neumanns (www.rps-obix.com)
This code can be used under the terms of the 'GNU Afero General Public License version 3'
The full text of this license can be found at http://www.gnu.org/licenses/agpl.html
THIS CODE IS DISTRIBUTED WITHOUT ANY WARRANTY. See the license for details.
end ///
type any_type
command to_string
out result type:string end
end
end type
Table 17.1. Type
| Production | Syntax | Links |
|---|---|---|
type |
| the section called “Type” |
A type is quite often used to simply define a structure of data, that is to say objects without any behavior. Such types contain only attributes, no commands and no events. The implementation of such types often consists of a factory with all attributes implemented as objects in memory, and a creator with one input argument for each attribute. As it would be tedious to code such factories again and again, the clause default_factory:yes tells the compiler to automatically create such a factory. In this case, the factory's id suffix is the same as the type's id suffix. See example below.
Example 17.1. Simple dog
First, a very simple example of a type with one attribute and one command
type dog attribute name type:string end command bark end end type
Example 17.2. Type customer
The following is a simple type with 4 attributes. It also declares a default factory.
type customer default_factory:yes attribute identifier type:positive32 end attribute name type:string kind:variable end // value can be changed after an object's creation attribute city type:string voidable:yes kind:variable end // value can be void attribute is_company type:yes_no default:no end // default value is no end type
default_factory:yes in the above example tells the compiler to automatically create the following factory, so we don't have to create it manually if the default implementation suits our needs:
factory customer type: customer
attribute identifier end
attribute name end
attribute city end
attribute is_company end
creator create
in identifier type:positive32 end
in name type:string end
in city type:string voidable:yes end
in is_company type:yes_no default:no end
out result type:customer end
script
o_result.a_identifier = i_identifier
o_result.a_name = i_name
o_result.a_city = i_city
o_result.a_is_company = i_is_company
end script
end creator
end factory
A customer can be created as follows:
var customer my_customer = fa_customer.co_create ( & identifier = 123 & name = "Albert Newton" & city = void )
As we didn't specify a value for attribute is_company in the above instruction, the value of that attribute will be no, which is the default value defined in type customer.
Example 17.3. Type coffee machine
This example shows a type coffee_machine that uses other types (cup_of_coffee, coffee_machine_switched_on_event, and coffee_machine_switched_off_event) which are not part of Obix's standard library.
type coffee_machine
attribute switched_on type:yes_no kind:readonly_variable end // is machine on or off?
command power_on end // turn machine on
command power_off end // turn machine off
command make_coffee
in with_milk type:yes_no default:yes end
in with_sugar type:yes_no default:no end
in_check check: object_.switched_on end // machine must be switched on before making coffee
out result type:cup_of_coffee end
end command
event switched_on type: coffee_machine_switched_on_event end // event fired whenever coffee machine is switched on
event switched_off type: coffee_machine_switched_off_event end // event fired whenever coffee machine is switched off
end type
type cup_of_coffee attribute is_empty type:yes_no kind:readonly_variable end command drink end // a command with no input and no output arguments end type
type coffee_machine_state_changed_event inherit object_event end attribute date_time type:local_date_time end end type
type coffee_machine_switched_on_event default_factory:yes inherit coffee_machine_state_changed_event end end type
type coffee_machine_switched_off_event default_factory:yes inherit coffee_machine_state_changed_event end end type
For more examples see Chapter 3, Simple example or have a look at the examples found in the links below