Bundt Programmer's Guide
·
Bundt Programmer's Interface version 1.0.1.0
And and Edit Instance Model Elements
The following example shows how to add some model elements to an instance model, and then retrieve and modify them.
Model elements can be usually added by calling the appropriate method on the owner entity.
For example, call InstanceModel.AddNewObject to add an object to an instance model.
Once added to a model, elements can be directly manipulated and altered by using a number of collections and properties, as illustrated below.
To execute this code, you will need a reference to the ModellingEngine library.
using Incipit.Bundt.ModellingEngine;
//load a type model and create an instance model conforming to it.
var tmo = new FileModelPersister("C:\\Temp\\MyModel.bundt-tm").LoadTypeModel();
var imo = ModelManager.CreateInstanceModel(tmo, "InstanceTest", VersionInfo.OneZeroZeroZero);
//capture some type model elements.
var clPerson = tmo.FindClass("Person");
var clDog = tmo.FindClass("Dog");
var attPersonAge = clPerson.FindAttribute("Age");
var attDogBreed = clDog.FindAttribute("Breed");
var asoOwns = clPerson.FindSemiAssociation("Owns").Association;
//add a couple of objects.
var obAlice = imo.AddNewObject(clPerson, "alice");
var obFido = imo.AddNewObject(clDog);
//connect the two objects via a link.
imo.AddNewLink(asoOwns, obAlice, obFido);
//set some object values.
obAlice.ValueSets[attPersonAge].SetContents(42);
obFido.ValueSets[attDogBreed].SetContents("Border Collie");
//find an object in the model.
var obFound = imo.FindObject("alice");
//change the found object's identifier.
obFound.Identifier = "al";
//modify Alice's age.
obFound.ValueSets[attPersonAge].SingleValue.UpdateTo(43);
In lines 4-5, we laod a type model from disk and create a conforming instance model.
For the sake if this example, we assume that this type model contains some classes such as Person and Animal, together with some attributes and associations.
In lines 8-12, we retrieve some type model elements into variables, so that further references as more readable.
In lines 15-16, we add two objects to the instance model, the person Alice and the dog Fido, by calling AddNewObject and passing the class to instantiate as an argument.
Optionally, we can also pass an identifier, such as "alice".
If we don't, the system will generate a random object identifier for us.
In line 19, we add a new link to the model to express the fact that Alice owns Fido.
In lines 22-23, we set some object values.
To do it, we first retrieve the object's value set for the attribute to alter.
Then we call ValueSet.SetContents and pass in the contents to set.
In line 26, we retrieve an object from the instance model by its identifier.
In line 29, we change the found object's identifier to a different one.
Notice that object identifiers can only use alphanumeric characters plus underscores.
Changes like this are effective immediately, and any identifier clashes or other naming issues will be detected on assignment.
An exception is thrown if necessary.
Finally, in line 32 we modify Alice's age by first finding the right value set, and then obtaining its single value.
We can do this because we know that Person.Age is a single-valued attribute, so ValueSet.SingleValue may be used.
Once we have it, we call Value.UpdateTo and pass in the content to be assigned as an argument.
Contents distributed under a Creative Commons Attribution 4.0 International License
·
About
·
Terms of Use
·
Contact Us
·
last updated on 08 October 2020