DataObject
¶
DataObject is a node in a tree of data.
All data within the data context is described as a tree of data objects. The class provides access and look-up functionality of the contained data objects.
The interface for navigating and modifying the tree of data objects mirrors the same interface as in Node2D and Node3D.
To create a data object call a create function that returns one of the data objects.
Those functions are DataObject:create, DataObjectInt:create, DataObjectBool:create, etc.
-- Create a DataObjectString. Note that an owning reference is created.
local dataObjectStringRef = DataObjectString:create("Name", "Value")
-- It can be used in the same way non-owning references are used.
assertEq(dataObjectStringRef:getName(), "Name")
Kanzi arranges data objects in a tree that you can think of as a directed acyclic graph with no loops. Each data object can have multiple children but only one parent.
DataObject:create:local root = DataObject:create("root", {
gauges = {
speed = 91.45,
unit = "km/h",
fuel = 75,
battery = 20,
},
turnindicator = {
left = false,
right = true,
},
navigation = {
visualdirections = 1,
distance = "5 mi",
instructions = 1,
},
contacts = {
{ firstname = "Rob", lastname = "Krar", phone = "+1 (503) 123 4567", },
{ firstname = "Anna", lastname = "Frost", phone = "+64 3-483 2081", },
{ firstname = "Kwamie", lastname = "Liv", phone = "+45 20 32 73 29", },
{ firstname = "Ricky", lastname = "Gates", phone = "+1 (303) 786 9255", },
},
})
The types of data objects will be inferred based on the values provided. If an unknown type is provided, the function will raise an error.
Consider the following important details:
dataObject:getChild(0) can return any of the three subobjects.Object data type.-- For the definition schema above.
root:findChild("contacts"):addItem(DataObject:create("ListItem", { firstname = "Mike", lastname = "Bron", phone = "+375 22 444 43 44" }))
root:findChild("contacts"):removeItem(1) -- removes `Anna`
assertEq(root:findChild("contacts"):getItem(2):findChild("firstname"):getValue(), "Ricky")
getItem function, instead of using the generic acquireItem function, even though both share the same implementation for the in-memory list. This is because acquireItem has additional semantics and requires managing resources, associated with getting said item. When writing generic code over lists, use the acquireItem and releaseItem functions.-- Add the `dataObjectSpeed` data object to the `root` data object, which is a root of some data source:
local dataObjectSpeed = DataObjectInt:create("Speed", 80)
root:addChild(dataObjectSpeed)
-- Remove the previously added child data objects from the `root` data object.
root:removeChild(dataObjectSpeed)
-- Release a Lua reference. Note that data objects take ownership of their children.
-- But since previously `removeChild` was executed, this will release the reference.
dataObjectSpeed = nil
-- The reference is kept alive by Lua Runtime until garbage collected. You can optionally force Lua garbage collection.
collectgarbage() -- Marks `dataObjectSpeed` for GC.
collectgarbage() -- Deletes `dataObjectSpeed`.
You can iterate over the data object children by using iterators.
for child in root:iterateChildren() do
print(child:getName())
end
While you are iterating over a collection, modifying it is not safe.
for child in root:iterateChildren() do
root:removeAllChildren() -- Bad. Will crash Kanzi Engine.
end
Usually you should search for data objects from DataSource through DataContext interface:
local speedObject = dataSource:lookupDataContext("vehicle.cluster.speed")
If lookup fails, Kanzi returns nil.
Inherits properties and message types from DataObjectMetadata.
| create() | Creates an instance of DataObject |
| getName() | Retrieves the name of the object |
| setName() | Sets the name of the data object |
| getType() | Allows to retrieve the type of the object |
| setParent() | Assigns a parent data object to this object |
| getParent() | Retrieves the parent data object of this object |
| getChildCount() | Retrieves the count of child objects in this data object |
| getChild() | Retrieves a child object at the given index |
| getChildIndex() | Retrieves the index of a child data object |
| findChild() | Tries to find a child with the given name |
| hasChild() | Checks whether or not the child data object exists |
| addChild() | Adds the data object as a child of this object |
| insertChild() | Inserts the data object as a child of this object, allows to specify index of the object |
| removeChildAtIndex() | Removes a child object at the given index |
| removeChild() | Removes a child object from the list of children |
| removeAllChildren() | Removes all children |
| iterateChildren() | Returns an iterator used to iterate over children of the data object |
Creates an instance of DataObject.
| objectName | (string) | The name of the object. |
| schema | (table or nil) | Optional table definining data object tree. |
Retrieves the name of the object.
| (string) | The name of the object. |
Sets the name of the data object.
| name | (string) | The name. |
Allows to retrieve the type of the object.
| (DataObjectType) | Returns the type of the object. |
Assigns a parent data object to this object.
| parent | (DataObject) | Parent DataObject to assign. |
Retrieves the parent data object of this object.
| (DataObject) | The parent DataObject of this DataObject. |
Retrieves the count of child objects in this data object.
| (number) | Child object count. |
Retrieves a child object at the given index.
| index | (number) | Index of the child. |
| (DataObject) | Child data object at the given index. |
Retrieves the index of a child data object.
| child | (DataObject) | Child DataObject which index to get. |
| (number) | Index of the item in question. |
Tries to find a child with the given name.
| (DataObject or nil) | Child object that was found, or nil if there is no such child. |
Checks whether or not the child data object exists.
| child | (DataObject) | Child to check. |
| (boolean) | true if there is such child, otherwise false. |
Adds the data object as a child of this object.
| child | (DataObject) | Child object to add. |
Inserts the data object as a child of this object, allows to specify index of the object.
| index | (number) | Index where to add the given object. |
| child | (DataObject) | Child object to add. |
Removes a child object at the given index.
| index | (number) | Index of the child object to remove. |
Removes a child object from the list of children.
| child | (DataObject) | Child object to remove. |
Removes all children.
Returns an iterator used to iterate over children of the data object.