![]() | Basic code examples |
These code examples show basic principles for AutomationML application development using the AMLEngine.
Loading a Document
using Aml.Engine.CAEX; // Load the document from a file var document = CAEXDocument.LoadFromFile ("myFile.aml"); // Load the document from a file with indices for fast object search, // use this for read-only documents only document = CAEXDocument.LoadFromFile ("myFile.aml", createIndices:true);
Saving a Document
using Aml.Engine.CAEX; // save the document to a file document.SaveToFile ("myFile.aml", prettyPrint:true);
Creation of a new CAEX document with InternaElements
using Aml.Engine.CAEX; // create a new empty CAEX document using var document = CAEXDocument.New_CAEXDocument (); // append an empty InstanceHierarchy element to the CAEXFile object of the document, named "IH" var myIH = document.CAEXFile.InstanceHierarchy.Append("IH"); // append an InternalElement element to the created InstanceHierarchy, named "IE" var myIE = myIH.InternalElement.Append("IE");
Indexers
using Aml.Engine.CAEX; using var document = CAEXDocument.New_CAEXDocument (); // create an empty InstanceHierarchy var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH"); // Get the first CAEXElement from the sequence of InstanceHierarchies myIH = document.CAEXFile.InstanceHierarchy[0]; // Get the first CAEXElement from the sequence of InstanceHierarchies with the name "myIH" myIH = document.CAEXFile.InstanceHierarchy["myIH"]; // Append an InternalElement named "IE1" with one child element "IE1_1" var ie1_1 = myIH.InternalElement.Append("IE1").Append("IE1_1"); // Access the child InternalElement from the InstanceHierarchy // by specifying the names of the element tree ie1_1 = myIH.InternalElement["IE1", "IE1_1"]; // Append an Attribute named "AT1" with one child attribute "AT1_1" var at1_1 = ie1_1.Attribute.Append("AT1").Attribute.Append("AT1_1"); // Access the child Attribute from the InternalElement // by specifying the names of the attribute element tree at1_1 = myIE.Attribute["AT1", "AT1_1"];
Attributes: Types and Values
using Aml.Engine.CAEX; using Aml.Engine.CAEX.Extensions; using var document = CAEXDocument.New_CAEXDocument (); // create an empty InstanceHierarchy var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH"); // Append an InternalElement named "IE1" var ie = myIH.InternalElement.Append("IE1"); // Define some Attributes with different types // creates a new attribute named "IsConnected" with AttributeDatatype "xs:boolean" and Value "false" ie.SetAttributeValue ("IsConnected", false); // creates a new structured attribute "Size" with properties "With" and "Height" with AttributeDatatype // xs:double and inital double values ie.Attribute.Append ("Size").SetAttributeValue ("With", 10.0); ie.Attribute["Size"].SetAttributeValue ("Height", 20.0); // alternatives to modify values of nested attributes ie.Attribute["Size/With"].AttributeValue = 15.0; // in CAEX 2.0 use "Size.With" as path ie.Attribute["Size","With"].AttributeValue = 15.0; // works in all CAEX versions ie.SetAttributeValue ("Size/With", 15.0);
Traversing a Document.
using Aml.Engine.CAEX; var document = CAEXDocument.LoadFromFile("myFile.aml"); // browse the Instance Hierarchies in the file foreach (var instanceHierary in document.CaexFile.InstanceHierarchy) { // browse all InternalElements deep and import the internal Elements to your system foreach (var internalElement in instanceHierarchy.Descendants<InternalElementType>()) { // ToDo: import internal Element } }
Application of instantiation of a SystemUnitClass to insert an InternalElement.
using Aml.Engine.CAEX; var document = CAEXDocument.New_CAEXDocument (); var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH"); var mySlib = document.CAEXFile.SystemUnitClassLib.Append("mySlib"); var mySuc = mySlib.SystemUnitClass.Append("suClass"); // insert a new class instance (InternalElement) to the InstanceHierarchy var firstIE = myIH.Insert (mySuc.CreateClassInstance()); // append a new class instance (InternalElement) to the InstanceHierarchy var secondIE = myIH.Insert (mySuc.CreateClassInstance(), asFirst:false);
Creation of instance to instance relations using InternalLinks
using Aml.Engine.CAEX; var document = CAEXDocument.New_CAEXDocument (); var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH"); var myIClib = document.CAEXFile.InterfaceClassLib.Append("myIClib"); var myIC = myIClib.InterfaceClass.Append("myIC"); // create an InternalElement which is a common parent to hold the InternalLink var linkParent = myIH.InternalElement.Append ("linkParent"); // create the instances var myIEA = linkParent.InternalElement.Append ("myIEA"); var myIEB = linkParent.InternalElement.Append ("myIEB"); // create the Interfaces for the InternalLink connection myIEA.ExternalInterface.Append ("a"); myIEB.ExternalInterface.Append ("b"); // create the instance to instance relation var relation = InternalLinkType.New_InternalLink (myIEA.ExternalInterface["a"], myIEB.ExternalInterface["b"], "rel1"); // an alternative way is, to use the InternalLink collection // var relation = linkParent.InternalLink.Append ("rel1"); // relation.AInterface = myIEA.ExternalInterface["a"]; // relation.BInterface = myIEB.ExternalInterface["b"];
Creation of class to class relations using AutomationML Base Classes.
see also: IClassWithBaseClassReference, BaseClass, InterfaceClassLib, MakeAutomationMLBaseInterface
using Aml.Engine.CAEX; using Aml.Engine.AmlObjects; var document = CAEXDocument.New_CAEXDocument (); // the first solution shows an implementation, which uses existing libraries and classes void methodWithClasses () { // Add the standard AutomationMLInterfaceClassLib var amlBaseICLib = AutomationMLInterfaceClassLibType.InterfaceClassLib(document); var myIClib = document.CAEXFile.InterfaceClassLib.Append("myIClib"); var myIC = myIClib.InterfaceClass.Append("myICClass"); // Create a baseclass relation to the AutomationMLBaseInterface class myIC.BaseClass = amlBaseICLib.AutomationMLClass(AutomationMLInterfaceClassLib.AutomationMLBaseInterface); } // the second solution shows an implementation, which uses the standardized class path void methodWithClassPath () { var myIClib = document.CAEXFile.InterfaceClassLib.Append("myIClib"); var myIC = myIClib.InterfaceClass.Append("myICClass"); // Create a baseclass reference using the path AutomationMLBaseInterface myIC.RefBaseClassPath = AutomationMLInterfaceClassLib.AutomationMLBaseInterface; }