Now we want to import an xml file into ARIS and generate new models. Therefore we presume the same notation of the xml file as generated above.

First we need a browse dialog so that the user can select the xml file he wants to import:

function getImportFile() {
    var sdefname = "";
    var sdefext = "*.xml!!xml file|*.xml||";
    var sdefdir = "";
    var stitle = "Import xml file";

    var aFiles = Dialogs.getFilePath(sdefname, sdefext, sdefdir, stitle, 0);    // Displays a file selection dialog on the client and makes the content of the selected files available in the report
    if (aFiles != null && aFiles.length > 0) {
        return aFiles[0];
    }
    return null;
}

The return value is an object of type “SelectedFile”. So we use method “getData” to get the content of the file and put it as input for the xml parsing method:

var importFile = getImportFile();
if (importFile != null) {
    var xmlData = importFile.getData();             // Content of the file
    var xmlReader = Context.getXMLParser(xmlData);  // Parses the specified XML file and returns an object that provides access to the XML document.
    if (xmlReader.isValid()) {
        var xmlRoot = xmlReader.getRootElement();   // Root element of the XML document

Next step is to read the model elements from the root object and create models in ARIS:

 var xmlModels = xmlRoot.getChildren("Model");
     var iterModel = xmlModels.iterator()
     while(iterModel.hasNext()) {
        var xmlModel = iterModel.next();
        // Create model
        var oModel = createModel(xmlModel, oGroup);
        if (oModel != null) {

Based on these models we read the objects and the connections and create the corresponding objects/connections in ARIS:

 var xmlObjs = xmlModel.getChildren("ObjDef");
        var iterObj = xmlObjs.iterator()
        while(iterObj.hasNext()) {
            var xmlObj = iterObj.next();
            // Create object definition and occurence
            createObject(xmlObj, oGroup, oModel);
        }

        var xmlCxns = xmlModel.getChildren("CxnDef");
        var iterCxn = xmlCxns.iterator()
        while(iterCxn.hasNext()) {
            var xmlCxn = iterCxn.next();
            // Create cxn definition and occurence
            createCxn(xmlCxn, oModel);
        }

During generation of the model we place all objects an connections with default values in the model so we had to layout the model after generating. We can do this by using the method “doLayout”.

At least, we open the model in the in the modeling window after running the report.

oModel.doLayout();                  // Executes a layout of the model
       oModel.openModel()                  // Opens the model in the modeling window after running the report

Function to create models:

function createModel(xmlModel, oGroup) {
    var sName = xmlModel.getAttribute("Name").getValue();
    var nTypeNum = parseInt(xmlModel.getAttribute("TypeNum").getValue());
    var sID = xmlModel.getAttribute("ID").getValue();

    var oModel = oGroup.CreateModel(nTypeNum, sName, g_nLoc);   // Creates a new model in the current group
    if (oModel.IsValid()) {                                     // Tests the validity of the object
        setAttrID(oModel, sID);                                 // Write attribute 'Identifier'
        return oModel;
    }
    return null;
}

To write the attribute “Identifier” we use this function:

function setAttrID(oItem, sID) {
    return oItem.Attribute(Constants.AT_ID, g_nLoc).setValue(sID);
}

Function to create object definitions and occurrences:

function createObject(xmlObj, oGroup, oModel) {
    var sName = xmlObj.getAttribute("Name").getValue();
    var nTypeNum = parseInt(xmlObj.getAttribute("TypeNum").getValue());
    var sID = xmlObj.getAttribute("ID").getValue();

    var oObjDef = oGroup.CreateObjDef(nTypeNum, sName, g_nLoc); // Creates a new m object definition in the current group
    if (oObjDef.IsValid()) {                                    // Tests the validity of the object
        setAttrID(oObjDef, sID);                                // Write attribute 'Identifier'

        var nSymbol = getSymbol(oModel, oObjDef);
        if (nSymbol > 0) {
            var oObjOcc = oModel.createObjOcc(nSymbol, oObjDef, 0, 0, true);    // Creates a new object occurrence of the specified object definition in the model
            if (oObjOcc.IsValid()) {                            // Tests the validity of the object
                return oObjOcc;
            }
            return null;
        }
    }
    return null;

    
    function getSymbol(oModel, oObjDef) {
        var aSymbols = ArisData.ActiveFilter().Symbols(oModel.TypeNum(), oObjDef.TypeNum());    // Returns a list of permitted symbols of the specified object type in the specified model type
        if (aSymbols.length > 0) {
            return aSymbols[0];
        }
        return 0;
    }
}

Function to create connection definitions and occurrences:

function createCxn(xmlCxn, oModel) {
    var sSourceName = xmlCxn.getAttribute("SourceName").getValue();
    var sTargetName = xmlCxn.getAttribute("TargetName").getValue();    
    var nTypeNum = parseInt(xmlCxn.getAttribute("TypeNum").getValue());
    var sID = xmlCxn.getAttribute("ID").getValue();

    var oSourceObjOcc = getObjectInModel(oModel, sSourceName);
    var oTargetObjOcc = getObjectInModel(oModel, sTargetName);
    
    if (oSourceObjOcc != null && oTargetObjOcc != null) {
        var oCxnOcc = oModel.CreateCxnOcc(oSourceObjOcc, oTargetObjOcc, nTypeNum, getDummyPointList()); // Creates a new relationship (definition and occurrence) between the specified object occurrences in the model
        if (oCxnOcc != null) {
            var oCxnDef = oCxnOcc.Cxn();
            setAttrID(oCxnDef, sID);                                // Write attribute 'Identifier'
        }
        return oCxnOcc;
    }
    return null;

    
    function getObjectInModel(oModel, sName) {
        var oObjOccsInModel = oModel.ObjOccList();                  // Returns all object occurrences in the model
        for (var i = 0; i < oObjOccsInModel.length; i++) {
            var oObjOcc = oObjOccsInModel[i];
            var oObjDef = oObjOcc.ObjDef();
            if (StrComp(oObjDef.Name(g_nLoc), sName) == 0) {        // Compare names
                return oObjOcc;
            }
        }
        return null;
    }    
    
    function getDummyPointList() {
        var pointList = new Array();
        pointList.push(new java.awt.Point(0, 0));
        pointList.push(new java.awt.Point(1, 1));        
        return pointList;
    }    
}

Here you can download the entire source code from the "XML Import" report.

Note: This article describe how to develop a report in ARIS. See this post for links to similar articles.

 or register to reply.

Notify Moderator