package com.sodius.doors.WordTableImporter; import com.sodius.mdw.core.model.RichText; import com.sodius.mdw.metamodel.doors.BaseTypeKind; public ruleset WordTableImporter(in word : word, out doors : doors) { public rule importTablesFromWord() { // setup DOORS // create a “Imports” folder in DOORS var folder : doors.Folder = doors.create("Folder"); folder.name = "Imports"; // create the formal module // use a subrule “createFormalModule” to encapsulate that function var fm : doors.FormalModule = @createFormalModule(folder, "Imported from Word"); // loop through each table in Word foreach (table : word.Table in word.getInstances("Table")) { // create DOORS object var object : doors.Object = doors.create("Object"); fm.objects.add(object); // get the user id from first row, first cell, 3 characters between [] @setAttributeValue(object, "User_ID", table.rows.get(0).cells.get(0).value.substring(1,4)); // get the short text after the user id object.objectShortText = RichText.valueOf(table.rows.get(0).cells.get(0).value.substring(6)); // get the category from the 4th row, second cell @setAttributeValue(object, "Category", table.rows.get(3).cells.get(1).value); // get the object text from 5th row, second cell object.objectText = RichText.valueOf(table.rows.get(4).cells.get(1).value); // user-defined attribute names are in row 2 var attNameRow : word.Row = table.rows.get(1); // loop through the user-defined attribute names in row 2 foreach (attNameCell : word.Cell in attNameRow.cells.subList(2, attNameRow.cells.size())) { // get the attribute values in the same cell in row 3 @setAttributeValue(object, attNameCell.value, table.rows.get(2).cells.get(attNameCell.y - 1).value); } } } /* Create a Formal Module with the given name */ private rule importTablesFromWord::createFormalModule(folder : doors.Folder, name : String) : doors.FormalModule { var fm : doors.FormalModule = doors.create("FormalModule"); // add the formal module to the given folder folder.ownedItems.add(fm); // set the name fm.name = name; // create base attribute type (only String for now) var type : doors.Type = doors.create("Type"); type.name = "String"; type.baseType = BaseTypeKind.STRING_LITERAL; fm.types.add(type); return fm; } /* Set the value of the attribute on the object */ private rule importTablesFromWord::setAttributeValue(object : doors.Object, attName : String, attValue : String) { // get the formal module's attribute that we want to set var attribute : doors.Attribute = @getOrCreateAttribute(object.module, attName); // create an instantiatedAttribute var instaniatedAttribute : doors.InstantiatedAttribute = doors.create("InstantiatedAttribute"); instaniatedAttribute.instantiates = attribute; // set the value instaniatedAttribute.value = RichText.valueOf(attValue); // add the attribute value to the Object object.instantiatedAttributes.add(instaniatedAttribute); } /* Find the attribute on the formal module or create a new one */ private rule setAttributeValue::getOrCreateAttribute(fm : doors.FormalModule, name : String) : doors.Attribute { // search for the attribute with the correct name var attribute : doors.Attribute = fm.attributes.detect("name", name); if (attribute == null) { // if the attribute is not found, create the attribute attribute = doors.create("Attribute"); // set the name attribute.name = name; // set the type attribute.type = fm.types.detect("name", "String"); // add the attribute to the formal module fm.attributes.add(attribute); } return attribute; } }