Skip to content
Josef edited this page Aug 12, 2021 · 21 revisions

Basic code examples

These code examples show basic principles for AutomationML application development using the Aml.Engine.

Basic Examples

1. Loading a Document

using Aml.Engine.CAEX;

var document = CAEXDocument.LoadFromFile ("myFile.aml");

2. Saving a Document

using Aml.Engine.CAEX;

CAEXDocument.SaveToFile ("myFile.aml", true);

3. Creation of a new CAEX document containing Internal Elements

using Aml.Engine.CAEX;

var document = CAEXDocument.New_Document ();
var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH");
var myIE = myIH.InternalElement.Append("myIE");

4. Accessing properties using Indexers

using Aml.Engine.CAEX;

var document = CAEXDocument.New_Document ();
var myIH = document.CAEXFile.InstanceHierarchy.Append("myIH");
myIH.Version = "1.0";

// Get the first CAEXElement from the sequence of elements
myIH = document.CAEXFile.InstanceHierarchy[0];

// Get the first CAEXElement from the sequence of elements with the name "myIH"
myIH = document.CAEXFile.InstanceHierarchy["myIH"];

// Get the first CAEXElement with a specific attribute AND value from the sequence of elements
myIH = document.CAEXFile.InstanceHierarchy[(Name:"Version", Value:"1.0")];

5. 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
    }
}

6. InternalElement creation by class instantiation.

using Aml.Engine.CAEX;

var document = CAEXDocument.New_Document ();
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 myIE = myIH.Insert (mySuc.CreateClassInstance());

7. Creation of instance to instance relations with InternalLinks

using Aml.Engine.CAEX;

var document = CAEXDocument.New_Document ();
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"];

8. Creation of class to class relations using AutomationML Base Classes

See Also

using Aml.Engine.CAEX;
using Aml.Engine.AmlObjects;

var document = CAEXDocument.New_Document ();

// the first solution shows an implementation, which uses existing libraries and classes
void methodWithClasses ()
{
    // add the AutomationMLInterfaceClassLib
    var amlBaseICLib = AutomationMLInterfaceClassLibType.InterfaceClassLib(document);
    var myIClib = document.CAEXFile.InterfaceClassLib.Append("myIClib");

    var myIC = myIClib.InterfaceClass.Append("myICClass");
    var 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");
    
    // creates a class relation to the AutomationML Base Interface Class
    myIC.MakeAutomationMLBaseInterface();
}

See Also

Other Resources

Clone this wiki locally