MoMa Properties and Values

For this example we show how to manipulate MoMa Properties and Values.

Let’s assume our current MoMa tree looks like the following.

../_images/example_moma_tree.png

Getting some prerequisite information first

First we need the owner (=current user), the levels and a list of models and units.

owner = ni.get_current_gst_user()
levels = ni.list_levels()
models = ni.list_elements(owner, levels[0])
units = ni.list_elements(owner, levels[1])

MoMa Property Values are defined on a specific MoMa Node (also called link). We want to set MoMa Property Values for the Augsburg and InnerCity MoMa Nodes in the above tree.

We first need to get the Augsburg MoMa model and the InnerCity MoMa unit (the MoMa elements). With their help, we can get the MoMa links that reference those MoMa elements.

augsburg_model = next((model for model in models if model.label == "Augsburg"))
inner_city_unit = next((unit for unit in units if unit.label == "InnerCity"))

model_links = ni.list_children()
augsburg_link = next(
    (link for link in model_links
     if link.target_id == augsburg_model.id
     and link.target_type == GSTPy.LinkAdjacencyTargetType.Element))

augsburg_child_links = ni.list_children(augsburg_link)
inner_city_link = next(
    (link for link in augsburg_child_links
     if link.target_id == inner_city_unit.id
     and link.target_type == GSTPy.LinkAdjacencyTargetType.Element))

Creating MoMa Properties

Each MoMa Property has a name, a type and an owner. During creation, the owner is optional and defaults to the current GST user.

Note: MoMa Properties only support TypeBool, TypeInt, TypeDouble and TypeText. Using TypeLong will be mapped to TypeInt. TypeFloat to TypeDouble.

We create a MoMa Property named “description” with type text and one named “revision” of type int.

moma_prop_description = ni.create_moma_property(
    "description", GSTPy.PropertyTypes.TypeText, owner)
moma_prop_revision = ni.create_moma_property(
    "revision", GSTPy.PropertyTypes.TypeInt)

Creating MoMa Property Values

Each MoMa Property Value has a MoMa link, a MoMa Property and a Value. The type of the Value depends on the type set during the creation of the MoMa Property.

We create values for each of the MoMa properties created above for the Augsburg and InnerCity MoMa nodes.

ni.create_moma_property_value(augsburg_link, moma_prop_description,
                              "3D model of Augsburg.")
ni.create_moma_property_value(augsburg_link, moma_prop_revision, 1)

ni.create_moma_property_value(inner_city_link, moma_prop_description,
                              "The inner city area.")
ni.create_moma_property_value(inner_city_link, moma_prop_revision, 1)

Updating MoMa Property Values

To update a MoMa Property Value, we first need the handle to the Value. We can either use the one returned by the create function or get it from the database.

There are two methods for this task. Listing alle Values belonging to a MoMa Property (Method 1) or all Values belonging to a MoMa Node (Method 2).

Method 1:

revision_values = ni.list_moma_property_values(moma_prop_revision)
revision_inner_city_value = next(
    (val for val in revision_values
     if val.moma_property.id == moma_prop_revision.id))

Method 2:

inner_city_link_values = ni.list_moma_property_values_of_element(
    inner_city_link)
revision_inner_city_value = next(
    (val for val in inner_city_link_values
     if val.moma_property.id == moma_prop_revision.id))

Now with our MoMa Property Value handle at hand, we can update the revision.

ni.update_moma_property_value(revision_inner_city_value, 2)

Deleting MoMa Properties and values

Deleting MoMa Properties is quite simple.

To delete a MoMa Property Value, pass the handle to deletion function.

ni.delete_moma_property_value(revision_inner_city_value)

To delete a MoMa Property, with all associated values, pass the MoMa Property handle to deletion function.

ni.delete_moma_property(moma_prop_description)

Full code sample

import GSTPy

owner = ni.get_current_gst_user()
levels = ni.list_levels()
models = ni.list_elements(owner, levels[0])
units = ni.list_elements(owner, levels[1])

augsburg_model = next((model for model in models if model.label == "Augsburg"))
inner_city_unit = next((unit for unit in units if unit.label == "InnerCity"))

model_links = ni.list_children()
augsburg_link = next(
    (link for link in model_links
     if link.target_id == augsburg_model.id
     and link.target_type == GSTPy.LinkAdjacencyTargetType.Element))

augsburg_child_links = ni.list_children(augsburg_link)
inner_city_link = next(
    (link for link in augsburg_child_links
     if link.target_id == inner_city_unit.id
     and link.target_type == GSTPy.LinkAdjacencyTargetType.Element))

moma_prop_description = ni.create_moma_property(
    "description", GSTPy.PropertyTypes.TypeText, owner)
moma_prop_revision = ni.create_moma_property(
    "revision", GSTPy.PropertyTypes.TypeInt)

ni.create_moma_property_value(augsburg_link, moma_prop_description,
                              "3D model of Augsburg.")
ni.create_moma_property_value(augsburg_link, moma_prop_revision, 1)

ni.create_moma_property_value(inner_city_link, moma_prop_description,
                              "The inner city area.")
ni.create_moma_property_value(inner_city_link, moma_prop_revision, 1)

# get revision value for inner city (option 1)
revision_values = ni.list_moma_property_values(moma_prop_revision)
revision_inner_city_value = next(
    (val for val in revision_values
     if val.moma_property.id == moma_prop_revision.id))

# get revision value for inner city (option 2)
inner_city_link_values = ni.list_moma_property_values_of_element(
    inner_city_link)
revision_inner_city_value = next(
    (val for val in inner_city_link_values
     if val.moma_property.id == moma_prop_revision.id))

ni.update_moma_property_value(revision_inner_city_value, 2)

ni.delete_moma_property_value(revision_inner_city_value)

ni.delete_moma_property(moma_prop_description)