Back to contents pageHow to get/set/remove an object's name using the API

How to get/set/remove an object's name using the API

Summary

You will learn how to set, get or remove an object's name and also, how to use the naming schema to get an object's ID.



Prerequisites

None. You can use this article on its own.



Guide

Before you start

Remember to:



Reading from a cached naming schema

For predictable scalability, it is preferable to create a schema cache client and query the local cache of schema data than to use remote procedure calls.

1) Getting all the names or objects

The Naming schema keeps the relation of an object with each of its names in the ObjectName relation. There are two available index methods for the ObjectName relation; name_ and object_name_. The index methods return a cursor to the elements of the relation that match the parameter given.

In this case we are not looking for a particular object or name, so there's no need to specify a parameter. The following code snippets can be used to get all the objects or names from the naming schema.


    using (Ubisense.UName.Naming.ReadTransaction xact = namingSchema.ReadTransaction())
    {
        foreach (Ubisense.UName.Naming.ObjectName.RowType row
            in Ubisense.UName.Naming.ObjectName.name_(xact))
        {
            // Type your code here. What do you want to do for each row?
            // row.name_;
            // row.object_;
        }
    }

OR

    using (Ubisense.UName.Naming.ReadTransaction xact = namingSchema.ReadTransaction())
    {
        foreach (Ubisense.UName.Naming.ObjectName.RowType row
            in Ubisense.UName.Naming.ObjectName.object_name_(xact))
        {
        }
    }



2) Getting objects by name

We can query the naming schema to get only the rows that have a specific objName and read the object_ value from each of them. Notice that more than one object can have the exact same name and if that were the case with objName, the code inside the foreach statement would be executed more than once.


    SortedSet<Ubisense.UBase.Object> namedObjs = new SortedSet<Ubisense.UBase.Object>();

    using (Ubisense.UName.Naming.ReadTransaction xact = namingSchema.ReadTransaction())
    {
        foreach (Ubisense.UName.Naming.ObjectName.RowType row
            in Ubisense.UName.Naming.ObjectName.name_(xact, objName))
        {
            namedObjs.Add(row.object_);
        }
    }



3) Getting all the names of an object

Same as before, but you need to know the object beforehand, retrieved from a different schema or maybe entered to the naming schema previously by your application.


    SortedSet<string> namesForObj = new SortedSet<string>();

    using (Ubisense.UName.Naming.ReadTransaction xact = namingSchema.ReadTransaction())
    {
        foreach (Ubisense.UName.Naming.ObjectName.RowType row
            in  Ubisense.UName.Naming.ObjectName.object_name_(xact, knownObj))
        {
            namesForObj.Add(row.name_);
        }
    }



4) Getting a specific entry, defining both object and name

In case you need to locate a specific name for an object, you can do so, like this:


    using (Naming.ReadTransaction xact = namingSchema.ReadTransaction())
    {
        foreach (Ubisense.UName.Naming.ObjectName.RowType row
            in  Ubisense.UName.Naming.ObjectName.object_name_(xact, knownObj, objName))
        {
        }
    }





Reading from the server using remote operations

All remote operations of any schema are executed at the server and might add latency if they communicate reliably (the client side waits for a response from the server). Remember to use a cached schema instead of using remote operations to query a schema whenever possible.

1) Getting objects by name (from server)

To get the objects that have a specific name from the server:


    SortedSet<Ubisense.UBase.Object> objects = namingSchema.get_objects(objName);



2) Getting all the names of an object (from server)

To get all the names for an object from the server:


    SortedSet<string> names = namingSchema.get_names(obj);



3) Getting a list of objects with the specified prefix (from server)

You can use the following code to retrieve groups of objects from the server that share the same prefix.


    SortedSet<UObject> ObjectsWhoseNameStartWithJ = namingSchema.match_objects(prefix);





Writing to the server using remote operations

The only way to modify the state of a schema in the server is using remote operations. Consider keeping a record of the changes within the scope of your program and only commit the necessary changes (e.g. the ones you want to remain persistent or need to communicate to other clients).

In Ubisense, an object exists only while it is in one of the relational schemas. The Naming schema provides the easiest way to identify an object and find out its ID to look for that object in other schemas. Removing an object from the Naming schema does not remove it from other schemas; modifying the Naming schema could make identifying a specific object more difficult.

1) Inserting an object-name entry (at server)

An object can have many names, appearing many times in the names table; each row would contain the same object but different names. The following code creates an entry in the naming schema server:


    namingSchema.insert_object_name(obj,name);



2) Changing an object's names (at server)

Deletes all the entries in the server for the specified object and creates a new one with the specified name. If there are no entries it will still create the latter.


    namingSchema.set_object_name(obj,name)



3) Removing all entries for an object (at server)

This will delete all the entries at the server that have the specified object in their object_ index


    namingSchema.remove_object(obj);



4) Removing all entries for a name (at server)

This will delete all the entries at the server that have the specified name in their name_ index


    namingSchema.remove_name(name);



5) Removing a specific entry (at server)

This will only remove the entry at the server that matches both object and name


    namingSchema.remove_object_name(obj,name)







Back to top