Question & Answer
Question
How do you Reverse Engineer Simulink (.m) files using JAVA API in IBM Rational Rhapsody?
Cause
You would like to use Java Application Programming Interfaces [API] code to automate the manual task to be performed with in Rational Rhapsody or to achieve something that is not possible through the Rhapsody GUI.
Answer
In order to Reverse Engineer Simulink (.m) files using JAVA API in IBM Rational Rhapsody, you can use the below code as reference:
All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
---|
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import com.telelogic.rhapsody.core.IRPApplication;
import com.telelogic.rhapsody.core.IRPAttribute;
import com.telelogic.rhapsody.core.IRPClass;
import com.telelogic.rhapsody.core.IRPClassifier;
import com.telelogic.rhapsody.core.IRPDependency;
import com.telelogic.rhapsody.core.IRPModelElement;
import com.telelogic.rhapsody.core.IRPPackage;
import com.telelogic.rhapsody.core.IRPProject;
import com.telelogic.rhapsody.core.IRPStereotype;
import com.telelogic.rhapsody.core.IRPType;
import com.telelogic.rhapsody.core.RhapsodyAppServer;
public class RE_Simulink_Files {
public static void main(String[] args) {
IRPApplication myApp = RhapsodyAppServer.getActiveRhapsodyApplication();
IRPProject myPrj = myApp.activeProject();
System.out.println("Enter the path to the simulink files (use '\\\\' insetead of single '\\' for directory delimiters). Ex: \"C:\\\\Work\\\\mFiles\\\\DefaultConfig\".");
Scanner scan = new Scanner(System.in);
String path = scan.nextLine();
String pkgName = path.substring(path.lastIndexOf("\\\\")+2, path.length());
IRPPackage myPkg = myPrj.addPackage(pkgName);
IRPPackage myPkg1 = null;
IRPClass myCls = null;
IRPStereotype myStr1 = (IRPStereotype) myPrj.findNestedElementRecursive("ICD_Base", "Stereotype");
IRPStereotype myStr2 = (IRPStereotype) myPrj.findNestedElementRecursive("ICD_Base_Type", "Stereotype");
IRPStereotype myStr3 = (IRPStereotype) myPrj.findNestedElementRecursive("ICD_Message", "Stereotype");
IRPStereotype myStr4 = (IRPStereotype) myPrj.findNestedElementRecursive("ICD_Signal", "Stereotype");
IRPStereotype myStr5 = (IRPStereotype) myPrj.findNestedElementRecursive("ICD_Information", "Stereotype");
File directory = new File(path);
File files[] = directory.listFiles();
for (File f : files) {
if(f.toString().contains(".m")) {
myPkg1 = myPkg.addNestedPackage(f.toString().substring(f.toString().lastIndexOf('\\')+1, f.toString().length()-2) + "_pkg");
myCls = myPkg1.addClass(f.toString().substring(f.toString().lastIndexOf('\\')+1, f.toString().length()-2));
myCls.setStereotype(myStr1);
}
try {
FileInputStream fstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
IRPType myType = null;
if(strLine.contains("ICD_Base_Type:-") || strLine.contains("ICD_Message:-")) {
if(strLine.contains("ICD_Message:-")) {
myType = myPkg1.addType(strLine.substring(17, strLine.length()));
myType.setStereotype(myStr3);
IRPAttribute myAtt = myCls.addAttribute(strLine.substring(17, strLine.length()));
IRPDependency myDep = myAtt.addDependencyTo((IRPModelElement)myType);
myDep.setStereotype(myStr5);
}
else {
myType = myPkg1.addType(strLine.substring(19, strLine.length()));
myType.setStereotype(myStr2);
}
myType.setKind("Structure");
while(!strLine.contains(" }} ...")) {
strLine = br.readLine();
if(strLine.contains("}; ...")) {
IRPAttribute myAtt = null;
try {
myAtt = myType.addAttribute(strLine.substring(7, strLine.indexOf("\',")));
} catch (Exception e) {
myAtt = myType.addAttribute(strLine.substring(7, strLine.indexOf("\' ")));
}
myAtt.setStereotype(myStr4);
strLine = strLine.substring(strLine.indexOf(", "));
int index1 = strLine.indexOf(", ");
strLine = strLine.substring(index1+2);
int index2 = strLine.indexOf(", ");
myAtt.setMultiplicity(strLine.substring(index1, index2));
strLine = strLine.substring(index1 = strLine.indexOf(", "));
strLine = strLine.substring(strLine.indexOf('\'')+1);
String typeStr = strLine.substring(0, strLine.indexOf('\''));
if(typeStr.equals("uint8"))
typeStr = "Unsigned_8";
if(typeStr.equals("uint16"))
typeStr = "Unsigned_16";
if(typeStr.equals("uint32"))
typeStr = "Unsigned_32";
if(typeStr.equals("int8"))
typeStr = "Integer_8";
if(typeStr.equals("int16"))
typeStr = "Integer_16";
if(typeStr.equals("int32"))
typeStr = "Integer_32";
if(typeStr.equals("fixdt(1, 16, 0)"))
typeStr = "Fixed_16";
if(typeStr.equals("fixdt(1, 32, 0)"))
typeStr = "Fixed_32";
if(typeStr.equals("fixdt(1, 64, 0)"))
typeStr = "Fixed_64";
if(typeStr.equals("double"))
typeStr = "Double";
if(typeStr.equals("boolean"))
typeStr = "RhpBoolean";
IRPType myType12;
try {
myType12 = (IRPType) myPkg1.findNestedElementRecursive(typeStr, "Type");
myAtt.setType((IRPClassifier)myType12);
} catch (Exception e) {
myType12 = (IRPType) myPrj.findNestedElementRecursive(typeStr, "Type");
myAtt.setType((IRPClassifier)myType12);
}
}
}
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Product Synonym
Rational Rhapsody
Was this topic helpful?
Document Information
Modified date:
27 May 2022
UID
swg21588899