Skip to content

Object Accessors

Every NBO class is generated from the schema, so its accessors follow from the way the element is declared. Four patterns cover any document: primitives, optional values, complex children, and repeated elements.

Overview

Recognize the XSD construct, and the method names follow without consulting the reference.

XSD construct Generated accessors Value passed
Attribute or element of a primitive type set<Name>(), get<Name>() A native Python value
Optional attribute or element has<Name>(), set<Name>(), get<Name>(), reset<Name>() As above, with an explicit unset state
Element of a complex or restricted type set<Name>(), get<Name>() An NBO object of the declared type
Repeated element (maxOccurs="unbounded") set<Name>_List(), get<Name>_List(), addTo<Name>_List(), clear<Name>_List() A vector object, or one element at a time

Two behaviors deserve attention.

  • An unset optional value raises. get<Name>() raises InscoperNBOException instead of returning a default, so absent metadata is never reported as zero or as an empty string. Test with has<Name>(), or catch the exception.
  • Vectors are typed. Each repeated element has its own vector class, named after the element type: AnnotationRefVector for AnnotationRef. Vectors accept append() and len(), and the lists returned by get<Name>_List() are iterable.

Note

This example uses CMOS and Reagent because they expose all four patterns. The names differ per class; the patterns do not.

Read and write attributes and children

import inscoper_nbo

# ── Step 1: XSD primitives map to native Python types
camera = inscoper_nbo.CMOS()
camera.setPixelWidth(6.5)                   # xsd:float  -> float
camera.setPixelWellCapacity(30000)          # xsd:long   -> int
camera.setManufacturer("Hamamatsu")         # xsd:string -> str

print(camera.getPixelWidth(), camera.getPixelWellCapacity(), camera.getManufacturer())

# ── Step 2: Optional values expose has / get / set / reset
# Reading an unset optional value raises InscoperNBOException rather than
# returning a placeholder, so the absence of a value is never silently ignored.
print(camera.hasArrayWidth())               # False

try:
    camera.getArrayWidth()
except inscoper_nbo.InscoperNBOException as error:
    print(f"ArrayWidth is not set: {error}")

camera.setArrayWidth(2048)
print(camera.hasArrayWidth(), camera.getArrayWidth())

camera.resetArrayWidth()                    # back to the unset state
print(camera.hasArrayWidth())

# ── Step 3: Complex children are objects, assigned through their own accessor
name = inscoper_nbo.Denomination_Type()
name.set("ORCA-Fusion BT")
camera.setName(name)
print(camera.getName().get())

# ── Step 4: Repeated elements are lists, assigned in bulk or one by one
reagent = inscoper_nbo.Reagent()

annotation_refs = inscoper_nbo.AnnotationRefVector()
annotation_refs.append(inscoper_nbo.AnnotationRef())
annotation_refs.append(inscoper_nbo.AnnotationRef())
reagent.setAnnotationRef_List(annotation_refs)
print(len(reagent.getAnnotationRef_List()))

reagent.addToAnnotationRef_List(inscoper_nbo.AnnotationRef())
print(len(reagent.getAnnotationRef_List()))

reagent.clearAnnotationRef_List()
print(len(reagent.getAnnotationRef_List()))

#include <NBO/NBO/AnnotationRef.h>
#include <NBO/NBO/CMOS.h>
#include <NBO/NBO/Denomination_Type.h>
#include <NBO/NBO/Reagent.h>
#include <NBO/Shared/Exception/InscoperNBOException.h>

#include <iostream>
#include <memory>
#include <vector>

using namespace Inscoper;

int main() {
    // Step 1: XSD primitives map to standard C++ types
    NBO::CMOS camera;
    camera.setPixelWidth(6.5f);            // xsd:float  -> float
    camera.setPixelWellCapacity(30000);    // xsd:long   -> long long
    camera.setManufacturer("Hamamatsu");   // xsd:string -> std::string

    std::cout << camera.getPixelWidth() << " " << camera.getPixelWellCapacity() << " "
              << camera.getManufacturer() << std::endl;

    // Step 2: Optional values expose has / get / set / reset
    // Reading an unset optional value throws InscoperNBOException rather than
    // returning a placeholder, so the absence of a value is never silently ignored.
    std::cout << std::boolalpha << camera.hasArrayWidth() << std::endl;   // false

    try {
        camera.getArrayWidth();
    } catch (const NBO::InscoperNBOException &error) {
        std::cout << "ArrayWidth is not set: " << error.what() << std::endl;
    }

    camera.setArrayWidth(2048);
    std::cout << camera.hasArrayWidth() << " " << camera.getArrayWidth() << std::endl;

    camera.resetArrayWidth();                                            // back to unset
    std::cout << camera.hasArrayWidth() << std::endl;

    // Step 3: Complex children are shared pointers, assigned through their own accessor
    NBO::Denomination_TypePtr name = std::make_shared<NBO::Denomination_Type>();
    name->set("ORCA-Fusion BT");
    camera.setName(name);
    std::cout << camera.getName()->get() << std::endl;

    // Step 4: Repeated elements are vectors, assigned in bulk or one by one
    NBO::Reagent reagent;

    std::vector<NBO::AnnotationRefPtr> annotationRefs;
    annotationRefs.push_back(std::make_shared<NBO::AnnotationRef>());
    annotationRefs.push_back(std::make_shared<NBO::AnnotationRef>());
    reagent.setAnnotationRef_List(annotationRefs);
    std::cout << reagent.getAnnotationRef_List().size() << std::endl;

    reagent.addToAnnotationRef_List(std::make_shared<NBO::AnnotationRef>());
    std::cout << reagent.getAnnotationRef_List().size() << std::endl;

    reagent.clearAnnotationRef_List();
    std::cout << reagent.getAnnotationRef_List().size() << std::endl;

    return 0;
}

import com.inscoper.nbo.AnnotationRef;
import com.inscoper.nbo.AnnotationRefVector;
import com.inscoper.nbo.CMOS;
import com.inscoper.nbo.Denomination_Type;
import com.inscoper.nbo.Reagent;

public class ObjectAccessors {
    public static void main(String[] args) {
        // Step 1: XSD primitives map to native Java types
        CMOS camera = new CMOS();
        camera.setPixelWidth(6.5f);            // xsd:float  -> float
        camera.setPixelWellCapacity(30000L);   // xsd:long   -> long
        camera.setManufacturer("Hamamatsu");   // xsd:string -> String

        System.out.println(camera.getPixelWidth() + " " + camera.getPixelWellCapacity() + " "
                + camera.getManufacturer());

        // Step 2: Optional values expose has / get / set / reset
        // Reading an unset optional value throws InscoperNBOException rather than
        // returning a placeholder, so the absence of a value is never silently ignored.
        System.out.println(camera.hasArrayWidth());   // false

        // The generated getters carry no throws clause, so the checked exception
        // cannot be named in a catch block: catch Exception and read its message.
        try {
            camera.getArrayWidth();
        } catch (Exception error) {
            System.out.println("ArrayWidth is not set: " + error.getMessage());
        }

        camera.setArrayWidth(2048L);
        System.out.println(camera.hasArrayWidth() + " " + camera.getArrayWidth());

        camera.resetArrayWidth();                     // back to the unset state
        System.out.println(camera.hasArrayWidth());

        // Step 3: Complex children are objects, assigned through their own accessor
        Denomination_Type name = new Denomination_Type();
        name.set("ORCA-Fusion BT");
        camera.setName(name);
        System.out.println(camera.getName().get());

        // Step 4: Repeated elements are typed vectors, assigned in bulk or one by one
        Reagent reagent = new Reagent();

        AnnotationRefVector annotationRefs = new AnnotationRefVector();
        annotationRefs.add(new AnnotationRef());
        annotationRefs.add(new AnnotationRef());
        reagent.setAnnotationRef_List(annotationRefs);
        System.out.println(reagent.getAnnotationRef_List().size());

        reagent.addToAnnotationRef_List(new AnnotationRef());
        System.out.println(reagent.getAnnotationRef_List().size());

        reagent.clearAnnotationRef_List();
        System.out.println(reagent.getAnnotationRef_List().size());
    }
}