def test_type_based_retrieval(self): from libcellml import Annotator, Model, Parser annotator = Annotator() model = Model() parser = Parser() model_string = file_contents("annotator/unique_ids.cellml") model = parser.parseModel(model_string) annotator.setModel(model) v1v1 = (model.component("component2").variable("variable1"), model.component("component2").component("component3").variable( "variable1")) v2v2 = (model.component("component2").variable("variable2"), model.component("component2").component("component3").variable( "variable2")) self.assertEqual(model.name(), annotator.model("model_1").name()) self.assertEqual(model.name(), annotator.encapsulation("encapsulation_1").name()) self.assertEqual( model.component("component1").name(), annotator.component("component_1").name()) self.assertEqual( model.component("component2").name(), annotator.component("component_2").name()) self.assertEqual( model.component("component2").name(), annotator.component("component_ref_1").name()) self.assertEqual( model.component("component2").component("component3").name(), annotator.component("component_3").name()) self.assertEqual( model.component("component2").component("component3").name(), annotator.componentRef("component_ref_2").name()) self.assertEqual( model.component("component1").importSource().url(), annotator.importSource("import_1").url()) self.assertEqual( model.units("units1").name(), annotator.units("units_1").name()) self.assertEqual( model.units("units1").importSource().url(), annotator.importSource("import_2").url()) self.assertEqual( model.units("units2").name(), annotator.units("units_2").name()) self.assertEqual( model.units("units2").name(), annotator.unit("unit_1").units().name()) self.assertEqual(0, annotator.unit("unit_1").index()) self.assertEqual( model.component("component2").variable("variable1").name(), annotator.variable("variable_1").name()) self.assertEqual( model.component("component2").variable("variable2").name(), annotator.variable("variable_2").name()) self.assertEqual( model.component("component2").reset(0).variable().name(), annotator.reset("reset_1").variable().name()) self.assertEqual( model.component("component2").reset(0).testVariable().name(), annotator.reset("reset_1").testVariable().name()) self.assertEqual( model.component("component2").reset(0).testValue(), annotator.testValue("test_value_1").testValue()) self.assertEqual( model.component("component2").reset(0).resetValue(), annotator.resetValue("reset_value_1").resetValue()) self.assertEqual( model.component("component2").component("component3").variable( "variable1").name(), annotator.variable("variable_3").name()) self.assertEqual( model.component("component2").component("component3").variable( "variable2").name(), annotator.variable("variable_4").name()) self.assertEqual( v1v1[0].name(), annotator.connection("connection_1").variable1().name()) self.assertEqual( v1v1[1].name(), annotator.connection("connection_1").variable2().name()) self.assertEqual( v1v1[0].name(), annotator.mapVariables("map_variables_1").variable1().name()) self.assertEqual( v1v1[1].name(), annotator.mapVariables("map_variables_1").variable2().name()) self.assertEqual( v2v2[0].name(), annotator.mapVariables("map_variables_2").variable1().name()) self.assertEqual( v2v2[1].name(), annotator.mapVariables("map_variables_2").variable2().name()) self.assertIsNone(annotator.model("i_dont_exist")) self.assertIsNone(annotator.component("i_dont_exist")) self.assertIsNone(annotator.variable("i_dont_exist")) self.assertIsNone(annotator.units("i_dont_exist")) self.assertIsNone(annotator.unit("i_dont_exist")) self.assertIsNone(annotator.reset("i_dont_exist")) self.assertIsNone(annotator.resetValue("i_dont_exist")) self.assertIsNone(annotator.testValue("i_dont_exist")) self.assertIsNone(annotator.componentRef("i_dont_exist")) self.assertIsNone(annotator.connection("i_dont_exist")) self.assertIsNone(annotator.importSource("i_dont_exist"))
# 3.e # Check that the id of 'polo' is now unique in the model by calling the # isUnique function. assert (annotator.isUnique('polo')) # end 3.e # Now we know that there is only one item in the model with id 'polo', and we also know # that it has type UNIT. This means that we can retrieve a Unit item directly from the # annotator rather than needing to cast it using the std.any_cast. Instead of calling # the annotator's item function, call the Annotator.unit function with the id 'polo' to return the # unit item directly. # 3.f # Retrieve the Unit with id polo without casting. polo_unit = annotator.unit('polo') # end 3.f # The Unit is another std.pair with: **TODO** # - first attribute is the Units parent item and # - second attribute is the index of this Unit within the parent. print('----------------------------------------------------------') print(' STEP 4: See who else is lurking in this pool ') print('----------------------------------------------------------') # Now that we've found Marco and fixed the duplicates of Polo, we'd like to know # what other ids are being used in this model. # 4.a # Use the Annotator.ids function to return a vector of id strings used in the model, and # print them to the terminal.
print() # STEP 3 # Retrieve items by their id where the item type is known. # Retrieve a component with the id of 'yellow'. We can only do this because # we have prior knowledge that the item with id of 'yellow' is actually # a Component. component = annotator.component('yellow') # The same applies to the other item types below. variable = annotator.variable('indigo') reset = annotator.reset('violet') importSource = annotator.importSource('orange') units = annotator.units('green') unit = annotator.unit('blue') connection = annotator.connection('beige') mapVariables = annotator.mapVariables('puce') # Some kinds of items are returned by their parent item. These are: # - componentRef: returns the Component with this id on its encapsulation item. componentRef = annotator.componentRef('black') # - encapsulation: returns the Model with this id on its encapsulation item. encapsulation = annotator.encapsulation('brown') # - resetValue: returns the Reset with this id on its reset value. resetValue = annotator.resetValue('taupe') # - testValue: returns the Reset with this id on its test value. testValue = annotator.testValue('mauve')