Skip to content

Inheritance

Josef edited this page Aug 16, 2021 · 5 revisions

Working with Inheritance Relations in CAEX

Inheritance Relations exist between classes. A Subclass inherits properties from its base class.

The programming examples show how to create inheritance relations between classes and how to use inheritance relations to interpret the meaning of AutomationML objects.

  1. Inheritance of SystemUnitClasses

using Aml.Engine.CAEX;

void CreateSystemUnitClassInheritance (CAEXDocument document)
{
    // adding a Class library and some classes
    var systemUnitClassLib = document.CAEXFile.SystemUnitClassLib.Append("Slib");
    var suc1 = systemUnitClassLib.SystemUnitClass.Append("s1");
    var suc2 = systemUnitClassLib.SystemUnitClass.Append("s2");
    var suc3 = systemUnitClassLib.SystemUnitClass.Append("s3");
    
    // suc2 becomes a sub class from suc1
    suc2.BaseClass = suc1;

    // suc3 becomes a sub class from suc2
    suc3.BaseClass = suc2;

    // get the inheritance hierarchy from suc3 (the base classes)
    var inheritanceHierarchy = suc3.GetReferenceHierarchy().ToArray();

    Debug.Assert(inheritanceHierarchy[0] == suc3);
    Debug.Assert(inheritanceHierarchy[1] == suc2);
    Debug.Assert(inheritanceHierarchy[2] == suc1);

    // gets the sub classes from suc1 (this also includes the sub-sub classes and further)
    var s = ServiceLocator.QueryService.AllClassReferencesDeep (suc1);

    Debug.Assert(s.Count() == 2);    
}

  1. Overriding inherited elements

void CreateOverridenAttributes(CAEXDocument document)
{
    // adding a Class library and some classes
    var systemUnitClassLib = document.CAEXFile.SystemUnitClassLib.Append("Slib2");
    var suc1 = systemUnitClassLib.SystemUnitClass.Append("s1");
    var suc2 = systemUnitClassLib.SystemUnitClass.Append("s2");
    var suc3 = systemUnitClassLib.SystemUnitClass.Append("s3");
    var suc4 = systemUnitClassLib.SystemUnitClass.Append("s4");

    // suc1 is defined a base class of all other classes
    suc2.BaseClass = suc3.BaseClass =  suc4.BaseClass = suc1;

    // adds an attribute to the base class
    var baseAtt = suc1.Attribute.Append("att1");

    // the added attribute is visible in all sub classes
    Debug.Assert(suc2.GetInheritedAttributes().Count() == 1);
    Debug.Assert(suc2.GetInheritedAttributes().First() == baseAtt);

    // this overrides the base class attribute 'att1'
    var overridenAtt = suc3.Attribute.Append("att1");

    // there is still only one attribute in the inheritance tree
    Debug.Assert(suc3.GetInheritedAttributes().Count() == 1);
    Debug.Assert(suc3.GetInheritedAttributes().First() == overridenAtt);

    // this deletes the inherited attribute from s4
    suc4.DeleteInheritedElement("att1", baseAtt.GetType());
    Debug.Assert(suc4.GetInheritedAttributes().Count() == 0);
}

Clone this wiki locally