In this post I will show you how to easily work with Sparx Enterprise Architect Baseline information in C#. Baseline processing in Enterprise Architect is exposed via the ProjectClass (EA.Repository.GetProjectInterface()). […]
In this post I will show you how to easily work with Sparx Enterprise Architect Baseline information in C#.
Baseline processing in Enterprise Architect is exposed via the ProjectClass (EA.Repository.GetProjectInterface()). From there we have to use a two-step approach to get a baseline and its stored information:
Invoke project.GetBaselines() with a PackageGuid to get the stored baselines for a sopecific package.
Invoke project.DoBaselineCompare() with the previous PackageGuid and a BaselineGuid returned in the previous step.
Project.GetBaselines
Suppose we retrieve the baselines of a package (with Guid {50168942-9238-44a6-89F3-B624125BFBAE}) with the baselines as outlined in the figure below. After our call to project.GetBaselines("{50168942-9238-44a6-89F3-B624125BFBAE}", "") we get an XML similar to this:
As we can see every baselines has its own unique guid and a version attribute.
Project.DoBaselineCompare
If we now compare the model against the first baseline (with Guid {93F584F1-8B22-4eca-AB96-5FA1011975D4}) and call project.DoBaselineCompare("{50168942-9238-44a6-89F3-B624125BFBAE}", "{93F584F1-8B22-4eca-AB96-5FA1011975D4}", "") EA will return the follwing XML:
Side note: The guid attribute in CompareItem is not always a real GUID, but can also contain trailing textual information (for examples with connectors). So we have to check the type first before trying to convert it to a real guid.
Suppose I only want to retrieve changed element and property count, I create a derived visitor class and only override the following methods:
public class MyBaselineVisitor : BaselineComparisonVisitor
{
public int ElementCount;
public int PropertyCount;
public override void VisitProperty(BaselineComparison.PropertyElement element)
{
PropertyCount++;
}
public override void VisitElement(BaselineComparison.CompareItemElement element)
{
ElementCount++;
}
}
The code to invoke the comparison in a unit test would then look like this:
[TestMethod]
public void Test()
{
var baseline = XmlSerialiserBase.Deserialise<BaselineComparison>(BaselineComparisonTest.XML);
var sut = new MyBaselineVisitor();
sut.Visit(baseline, BaselineComparisonStatus.Changed);
Assert.AreEqual(1, sut.ElementCount);
Assert.AreEqual(2, sut.PropertyCount);
}
The Assert statements verify the correct behaviour (based on the example XML above).
This is actually it. With a small utility classes we no longer need to use MSXML2 and iterating over xml elements but persue a clean approach working with native C# objects.
Complete Example
In the Gist below you find the following files:
DTO for the baseline summary information that you can use with the XmlSerialiser to convert the returned XML into a C# object.
DTO for the baseline comparison result that you can use with the XmlSerialiser to convert the baseline comparison XML into a C# object.
A simple visitor class that helps you iterate over the BaselineComparison object.
Deserialising the XML into the C# classes can be performed like this:
public static T Deserialise<T>(string xml)
where T : XmlSerialiserBase
{
using (var stream = new StringReader(xml))
{
return (T) new XmlSerializer(typeof(T)).Deserialize(stream);
}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters