Sparx Systems Forum

Enterprise Architect => General Board => Topic started by: requirement4life on July 07, 2016, 08:34:16 am

Title: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: requirement4life on July 07, 2016, 08:34:16 am
Hi,

I'm a relatively new user to Sparx EA 12.1. I recently made about 100 requirements for a project before realizing that I had not used the <<extendedRequirement>> element type. I need to show compartmentalized verifyMethod for these requirements. Is there an easy to to convert the <<requirement>> elements into <<extendedRequirement>> elements? Or is there an easy way to add Sparx EA recognized verifyMethod to the <<requirement>> elements?

Thanks!
Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: Sunshine on July 07, 2016, 01:21:04 pm
Could try exporting to CSV file with the fields GUID, Name and stereotype.
Open in excel then search and replace <<requirement>> to <<extendedRequirement>> and save file.
Then import CSV back into Sparx EA model.
Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: PeterHeintz on July 07, 2016, 05:05:44 pm
Or you could write a small script doing that for you.
Such a script is also useful for other scenarios.
Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: requirement4life on July 08, 2016, 02:09:56 am
I'll try the CSV import again, but unfortunately I've also been having some trouble getting that to function properly.

I've been following the formatting instructions http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/projects_and_teams/csvimport.html (http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/projects_and_teams/csvimport.html) to no avail. Should I have columns named exactly those values and not follow those instructions?

Thanks again for the help!

Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: requirement4life on July 08, 2016, 03:01:58 am
Ah, I just realized that you meant to export and then import an altered file. Would I just be altering the stereotype and/or Fully Qualified stereotype or would I have to alter the GUID?

Thanks!  :)
Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: qwerty on July 08, 2016, 04:39:23 am
I don't use the csv, but changing the GUID will create new elements. Try changing the stereo and re-importing (I'd test that on a copy of the repository first). I'd prefer to use a script anyway ;-)

q.
Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: requirement4life on July 08, 2016, 06:52:04 am
Ah, ok thanks! I wish I was more fluent in scripting a solution to this problem. In time hopefully I'll be at that point in my understanding of the software. Thanks for the help!
Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: Sunshine on July 08, 2016, 01:19:40 pm
Here is some jscript that could be used.

Code: [Select]
!INC Local Scripts.EAConstants-JScript

function TypeConversion(sourceObject,sourceStereotype,targetObject, targetStereotype)
{
    this.sourceObject = sourceObject;
    this.sourceStereotype = sourceStereotype;
    this.targetObject = targetObject;
    this.targetStereotype = targetStereotype;
}

var Conversions = new Array();
// sourceObject,sourceStereotype,targetObject, targetStereotype

//Add one or more Conversion mappings here
Conversions[0] = new TypeConversion("Requirement", "", "Requirement", "extendedRequirement");


// =================================================================================
// Converts elements type and stereotype as defined in the element conversation table.
// Navigates from selected package or element and recursively modifies each element from source to target
// NOTE: Requires a package to be selected in the Project Browser
//
// Related APIs
// =================================================================================
// Element API - http://www.sparxsystems.com/enterprise_architect_user_guide/10/automation_and_scripting/element2.html
// Repository API http://www.sparxsystems.com/enterprise_architect_user_guide/10/automation_and_scripting/repository3.html

/*
 * Project Browser Script main function
 */
function OnProjectBrowserScript()
{
// Get the type of element selected in the Project Browser
var treeSelectedType = Repository.GetTreeSelectedItemType();

// Handling Code: Uncomment any types you wish this script to support
// NOTE: You can toggle comments on multiple lines that are currently
// selected with [CTRL]+[SHIFT]+[C].
switch ( treeSelectedType )
{
case otElement :
{
// Code for when an element is selected
var theElement as EA.Element;
theElement = Repository.GetTreeSelectedObject();
ConvertElements( "", theElement );
break;
}
case otPackage :
{
// Code for when a package is selected
var thePackage as EA.Package;
thePackage = Repository.GetTreeSelectedObject();
NavigatePackage( "", thePackage );

break;
}
// case otDiagram :
// {
// // Code for when a diagram is selected
// var theDiagram as EA.Diagram;
// theDiagram = Repository.GetTreeSelectedObject();
//
// break;
// }
// case otAttribute :
// {
// // Code for when an attribute is selected
// var theAttribute as EA.Attribute;
// theAttribute = Repository.GetTreeSelectedObject();
//
// break;
// }
// case otMethod :
// {
// // Code for when a method is selected
// var theMethod as EA.Method;
// theMethod = Repository.GetTreeSelectedObject();
//
// break;
// }
default:
{
// Error message
        Session.Prompt( "This script requires a package to be selected in the Project Browser.\n" +
            "Please select a package or element in the Project Browser and try again.", promptOK );
}
}
}

//
// Outputs the packages name and elements, and then recursively processes any child
// packages
//
// Parameters:
// - indent A string representing the current level of indentation
// - thePackage The package object to be processed
//
function NavigatePackage( indent, thePackage )
{
    // Cast thePackage to EA.Package so we get intellisense
    var currentPackage as EA.Package;
    currentPackage = thePackage;
   
    // Add the current package's name to the list
    Session.Output( indent + currentPackage.Name + " (PackageID=" +
        currentPackage.PackageID + ")" );
   
    // Convert the elements this package contains
    ConvertElementsInPackage( indent + "    ", currentPackage );
   
    // Recursively process any child packages
    var childPackageEnumerator = new Enumerator( currentPackage.Packages );
    while ( !childPackageEnumerator.atEnd() )
    {
        var childPackage as EA.Package;
        childPackage = childPackageEnumerator.item();
        NavigatePackage( indent + "    ", childPackage );
       
        childPackageEnumerator.moveNext();
    }
}

//
// Converts the elements of the provided package to the Script output window
//
// Parameters:
// - indent A string representing the current level of indentation
// - thePackage The package object to be processed
//
function ConvertElementsInPackage( indent, thePackage )
{
    // Cast thePackage to EA.Package so we get intellisense
    var currentPackage as EA.Package;
    currentPackage = thePackage;
   
    // Iterate through all elements and add them to the list
    var elementEnumerator = new Enumerator( currentPackage.Elements );
    while ( !elementEnumerator.atEnd() )
    {
        var currentElement as EA.Element;
        currentElement = elementEnumerator.item();
        ConvertElements(indent+"    ",currentElement );

        elementEnumerator.moveNext();
    }
}

function ConvertElements( indent, theElement )
{
    // Cast theElement to EA.Element so we get intellisense
    var currentElement as EA.Element;
    currentElement = theElement;
    currentElement.ObjectType
    ConvertElement(indent+"    ",currentElement );
    // Iterate through all embedded elements and add them to the list
    var elementEnumerator = new Enumerator( currentElement.Elements );
    while ( !elementEnumerator.atEnd() )
    {
        var currentElement as EA.Element;
        currentElement = elementEnumerator.item();
        ConvertElements(indent+"    ",currentElement );
        elementEnumerator.moveNext();
    }
}

// Converts the element from source to target mappings from conversions array
//
// Parameters:
// - indent A string representing the current level of indentation
// - theElement The element object to be processed
function ConvertElement( indent, theElement )
{
// Debug Comment out when run for real
    //Session.Output( indent + "CALLED: ConvertElement with " + theElement.Name + " [" + theElement.Type + ", " + theElement.Stereotype + " )" );

    for ( var i = 0 ; i < Conversions.length ; i++ )
    {
        // If stereotype matches source list then convert
        if ( (theElement.Stereotype == Conversions[i].sourceStereotype) && (theElement.Type == Conversions[i].sourceObject ))
        {
            Session.Output( indent + "CONVERTED: " + theElement.Name + " (" + theElement.Type + ", " + theElement.Stereotype + ")" + "=>" +"("+Conversions[i].targetObject+","+Conversions[i].targetStereotype+")" );
            theElement.Type = Conversions[i].targetObject;
            //Over write the stereotype list to have only one stereotype
            theElement.StereotypeEx = Conversions[i].targetStereotype;
            theElement.Update();
            break; // once found cease iterating through for-loop.
        }
    }
}

OnProjectBrowserScript();

Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: requirement4life on July 09, 2016, 09:11:12 am
Wow, thank-you! I only had 65 or so requirements so I just redid them all as extendedRequirements, but that will help so much next time  (although I don't think I'm making that mistake again).
Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: GeordieKiwi on July 09, 2016, 09:26:16 am
Here is some jscript that could be used.

Code: [Select]
!INC Local Scripts.EAConstants-JScript

function TypeConversion(sourceObject,sourceStereotype,targetObject, targetStereotype)
{
    this.sourceObject = sourceObject;
    this.sourceStereotype = sourceStereotype;
    this.targetObject = targetObject;
    this.targetStereotype = targetStereotype;
}

var Conversions = new Array();
// sourceObject,sourceStereotype,targetObject, targetStereotype

//Add one or more Conversion mappings here
Conversions[0] = new TypeConversion("Requirement", "", "Requirement", "extendedRequirement");


// =================================================================================
// Converts elements type and stereotype as defined in the element conversation table.
// Navigates from selected package or element and recursively modifies each element from source to target
// NOTE: Requires a package to be selected in the Project Browser
//
// Related APIs
// =================================================================================
// Element API - http://www.sparxsystems.com/enterprise_architect_user_guide/10/automation_and_scripting/element2.html
// Repository API http://www.sparxsystems.com/enterprise_architect_user_guide/10/automation_and_scripting/repository3.html

/*
 * Project Browser Script main function
 */
function OnProjectBrowserScript()
{
// Get the type of element selected in the Project Browser
var treeSelectedType = Repository.GetTreeSelectedItemType();

// Handling Code: Uncomment any types you wish this script to support
// NOTE: You can toggle comments on multiple lines that are currently
// selected with [CTRL]+[SHIFT]+[C].
switch ( treeSelectedType )
{
case otElement :
{
// Code for when an element is selected
var theElement as EA.Element;
theElement = Repository.GetTreeSelectedObject();
ConvertElements( "", theElement );
break;
}
case otPackage :
{
// Code for when a package is selected
var thePackage as EA.Package;
thePackage = Repository.GetTreeSelectedObject();
NavigatePackage( "", thePackage );

break;
}
// case otDiagram :
// {
// // Code for when a diagram is selected
// var theDiagram as EA.Diagram;
// theDiagram = Repository.GetTreeSelectedObject();
//
// break;
// }
// case otAttribute :
// {
// // Code for when an attribute is selected
// var theAttribute as EA.Attribute;
// theAttribute = Repository.GetTreeSelectedObject();
//
// break;
// }
// case otMethod :
// {
// // Code for when a method is selected
// var theMethod as EA.Method;
// theMethod = Repository.GetTreeSelectedObject();
//
// break;
// }
default:
{
// Error message
        Session.Prompt( "This script requires a package to be selected in the Project Browser.\n" +
            "Please select a package or element in the Project Browser and try again.", promptOK );
}
}
}

//
// Outputs the packages name and elements, and then recursively processes any child
// packages
//
// Parameters:
// - indent A string representing the current level of indentation
// - thePackage The package object to be processed
//
function NavigatePackage( indent, thePackage )
{
    // Cast thePackage to EA.Package so we get intellisense
    var currentPackage as EA.Package;
    currentPackage = thePackage;
   
    // Add the current package's name to the list
    Session.Output( indent + currentPackage.Name + " (PackageID=" +
        currentPackage.PackageID + ")" );
   
    // Convert the elements this package contains
    ConvertElementsInPackage( indent + "    ", currentPackage );
   
    // Recursively process any child packages
    var childPackageEnumerator = new Enumerator( currentPackage.Packages );
    while ( !childPackageEnumerator.atEnd() )
    {
        var childPackage as EA.Package;
        childPackage = childPackageEnumerator.item();
        NavigatePackage( indent + "    ", childPackage );
       
        childPackageEnumerator.moveNext();
    }
}

//
// Converts the elements of the provided package to the Script output window
//
// Parameters:
// - indent A string representing the current level of indentation
// - thePackage The package object to be processed
//
function ConvertElementsInPackage( indent, thePackage )
{
    // Cast thePackage to EA.Package so we get intellisense
    var currentPackage as EA.Package;
    currentPackage = thePackage;
   
    // Iterate through all elements and add them to the list
    var elementEnumerator = new Enumerator( currentPackage.Elements );
    while ( !elementEnumerator.atEnd() )
    {
        var currentElement as EA.Element;
        currentElement = elementEnumerator.item();
        ConvertElements(indent+"    ",currentElement );

        elementEnumerator.moveNext();
    }
}

function ConvertElements( indent, theElement )
{
    // Cast theElement to EA.Element so we get intellisense
    var currentElement as EA.Element;
    currentElement = theElement;
    currentElement.ObjectType
    ConvertElement(indent+"    ",currentElement );
    // Iterate through all embedded elements and add them to the list
    var elementEnumerator = new Enumerator( currentElement.Elements );
    while ( !elementEnumerator.atEnd() )
    {
        var currentElement as EA.Element;
        currentElement = elementEnumerator.item();
        ConvertElements(indent+"    ",currentElement );
        elementEnumerator.moveNext();
    }
}

// Converts the element from source to target mappings from conversions array
//
// Parameters:
// - indent A string representing the current level of indentation
// - theElement The element object to be processed
function ConvertElement( indent, theElement )
{
// Debug Comment out when run for real
    //Session.Output( indent + "CALLED: ConvertElement with " + theElement.Name + " [" + theElement.Type + ", " + theElement.Stereotype + " )" );

    for ( var i = 0 ; i < Conversions.length ; i++ )
    {
        // If stereotype matches source list then convert
        if ( (theElement.Stereotype == Conversions[i].sourceStereotype) && (theElement.Type == Conversions[i].sourceObject ))
        {
            Session.Output( indent + "CONVERTED: " + theElement.Name + " (" + theElement.Type + ", " + theElement.Stereotype + ")" + "=>" +"("+Conversions[i].targetObject+","+Conversions[i].targetStereotype+")" );
            theElement.Type = Conversions[i].targetObject;
            //Over write the stereotype list to have only one stereotype
            theElement.StereotypeEx = Conversions[i].targetStereotype;
            theElement.Update();
            break; // once found cease iterating through for-loop.
        }
    }
}

OnProjectBrowserScript();

Good timing Sunshine, was just about to do a similar exercise of changing stereotypes.
Title: Re: Converting Between <<requirement>> and <<extendedRequirement>>
Post by: Sunshine on July 09, 2016, 09:35:17 am
Wow, thank-you! I only had 65 or so requirements so I just redid them all as extendedRequirements, but that will help so much next time  (although I don't think I'm making that mistake again).

You are welcome. Suggest you keep that script handy you'll never know when you might need it. I've used it for things like changing UML activity diagrams to BPMN, ArchiMate 1.0 to 2.0. etc.

The export CSV, change stereotype and import CSV is a common technique used by folk with no programming skills so that works fine too.