package seg.jUCMNav.importexport; import grl.Actor; import grl.ActorRef; import grl.Belief; import grl.BeliefLink; import grl.Contribution; import grl.Decomposition; import grl.DecompositionType; import grl.Dependency; import grl.ElementLink; import grl.Evaluation; import grl.EvaluationStrategy; import grl.GRLGraph; import grl.IntentionalElement; import grl.IntentionalElementRef; import grl.IntentionalElementType; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Iterator; import org.eclipse.swt.widgets.Display; import seg.jUCMNav.Messages; import seg.jUCMNav.extensionpoints.IURNExport; import seg.jUCMNav.strategies.EvaluationStrategyManager; import seg.jUCMNav.views.wizards.importexport.ExportWizard; import ucm.UCMspec; import ucm.map.ComponentRef; import ucm.map.PluginBinding; import ucm.map.RespRef; import ucm.map.Stub; import ucm.map.UCMmap; import urn.URNlink; import urn.URNspec; import urncore.Component; import urncore.ComponentKind; import urncore.IURNConnection; import urncore.IURNDiagram; import urncore.IURNNode; import urncore.Responsibility; /** * Export DXL Script to be run in Telelogic DOORS * * @see DXL Reference http://publib.boulder.ibm.com/infocenter/rsdp/v1r0m0/topic/com.ibm.help.download.doors.doc/pdf/dxl_reference_manual.pdf * * @author Yongdae Kim, jkealey, jfroy * */ public class ExportAsUSE implements IURNExport { private FileOutputStream fos = null; /* public static final String QUOTES = "\""; //$NON-NLS-1$ public static final String QUOTES_COMMA = "\", "; //$NON-NLS-1$ public static final String COMMA = ","; //$NON-NLS-1$ public static final String QUOTES_DOUBLE = QUOTES + QUOTES; public static final String END_ELEM = " )\n"; //$NON-NLS-1$ public static final String QUOTES_END_ELEM = "\" )\n"; //$NON-NLS-1$*/ public static final String CREATE = "!create"; public static final String Set = "!set"; public static final String Insert = "!insert"; public static final String DoubleDots = ":"; public static final String END_ELEM = " )"; public static final String START_ELEM = "("; public static final String INTO = " into"; public static final String DoubleDotsEq = ":="; private String filename; /** * Not used. */ public void export(URNspec urn, HashMap mapDiagrams, FileOutputStream fos) throws InvocationTargetException { // TODO Auto-generated method stub } /** * Export the URNspec to the given filename in DXL format. */ public void export(URNspec urn, HashMap mapDiagrams, String filename) throws InvocationTargetException { this.filename = filename; try { fos = new FileOutputStream(filename); // writeHeader(urn); // writeDevices(urn); writeActors(urn); // writeComponents(urn); // writeResponsibilities(urn); writeIntentionalElements(urn); writeElementLinks(urn); // writeGrlDiagrams(urn, filename); writeMaps(urn, filename); // writeScenarios(urn); // writeStrategies(urn); writeUrnLinks(urn); // writeFooter(); } catch (Exception e) { throw new InvocationTargetException(e); } finally { // close the stream if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } /** * Write the string to the file output stream. * * @param s * the string to write * @throws IOException */ public void write(String s) throws IOException { if (s != null && s.length() > 0) { fos.write(s.getBytes()); } } /** * Write the escaped string to the file output stream. * * @param s * the string to write * @throws IOException */ public void escapeAndWrite(String s) throws IOException { write(escape(s)); } /** * Replaces invalid characters in the string. * @param s */ public String escape(String s) { if (s!=null) { // s = s.replace("\\", "\\\\").replace("\"", "\\\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ } return s; } /** * Writes the information about actors such as id, name, type, and description * * @param urn * URNspec * @throws IOException * */ protected void writeActors(URNspec urn) throws IOException { for (Iterator iter = urn.getGrlspec().getActors().iterator(); iter.hasNext();) { Actor actor = (Actor) iter.next(); write(CREATE+" "); //$NON-NLS-1$ // ID String name = actor.getName(); name = name.replaceAll(" ",""); write(name); write (":"); write ("Stakeholder"); // write(actor.getName()); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"name := '"); write(actor.getName()+"' "); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"id :="); write(actor.getId()); // String line = System.getProperty("line.separator"); write (System.getProperty("line.separator")); // write ("!insert ( " + name+ ", IntegaredGoalModel)" + "into Goal__Stakeholder" ); // write(System.getProperty("line.separator")); //write(System.getProperty("line.separator")); // Name // write(END_ELEM); } write("\n"); //$NON-NLS-1$ } /** * Writes the information about actor references, such as id, fx, fy, width, height, definition id, name, and parent actor of stubs. * * @param graph * GRLGraph * @throws IOException */ /** * Writes the information about intentional elements * * @param urn * urn * @throws IOException */ protected void writeIntentionalElements(URNspec urn) throws IOException { for (Iterator iter = urn.getGrlspec().getIntElements().iterator(); iter.hasNext();) { IntentionalElement element = (IntentionalElement) iter.next(); // write("intentionalelement( "); //$NON-NLS-1$ // ID String nameGM = "IntegratedGoalModel"; // nameGM = nameGM.replaceAll(" ",""); write(CREATE+" "); write(nameGM); write (":"); write ("GoalModel"); write (System.getProperty("line.separator")); String type = element.getType().getName(); if (type.contains("indicator")) { write(CREATE+" "); //$NON-NLS-1$ // ID String name = element.getName(); name = name.replaceAll(" ",""); write(name); write (":"); write ("Indicator"); write (System.getProperty("line.separator")); } else { write(CREATE+" "); //$NON-NLS-1$ // ID String name = element.getName(); name = name.replaceAll(" ",""); name = name.replaceAll("/",""); write(name); write (":"); write ("Goal"); write (System.getProperty("line.separator")); } String name = element.getName(); name = name.replaceAll(" ",""); name = name.replaceAll("/",""); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"name := '"); write(name+"' "); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"id :="); write(element.getId()); write (System.getProperty("line.separator")); // String act= write ("!insert (" +name+ "," + nameGM+ ")" + "into Goal__GoalModel" ); write(System.getProperty("line.separator")); write(System.getProperty("line.separator")); } // write("\n"); //$NON-NLS-1$ } protected void writeElementLinks(URNspec urn) throws IOException { for (Iterator iter = urn.getGrlspec().getLinks().iterator(); iter.hasNext();) { // Create different type of dxl based on the type of link ElementLink link = (ElementLink) iter.next(); String type = null; if (link instanceof Contribution) { // write("contribution("); //$NON-NLS-1$ write("!create "); type = "GContribution"; } else { write("!create "); //$NON-NLS-1$ type = "Decomposition"; } String name = link.getName(); name = name.replaceAll(" ",""); name = name.replaceAll("/",""); write(name +":"+type); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // name write(name+"."+"name := '"); write(name+"' "); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"id :="); write(link.getId()); write (System.getProperty("line.separator")); // write(link.); // ID // write(QUOTES); // write(link.getId()); // write(QUOTES_COMMA); // Name // write(QUOTES); // escapeAndWrite(link.getName()); // write(QUOTES_COMMA); /* if (link instanceof Decomposition) { write(QUOTES); write("decomposition"); //$NON-NLS-1$ write(QUOTES_COMMA); } else if (link instanceof Dependency) { write(QUOTES); write("dependency"); //$NON-NLS-1$ write(QUOTES_COMMA); } else if (link instanceof Contribution) { write(QUOTES); write("contribution"); //$NON-NLS-1$ write(QUOTES_COMMA); // Contribution Type write(QUOTES); escapeAndWrite(((Contribution) link).getContribution().getName()); write(QUOTES_COMMA); // Correlation write(QUOTES); if (((Contribution) link).isCorrelation()) { write("1"); //$NON-NLS-1$ } else { write("0"); //$NON-NLS-1$ } write(QUOTES_COMMA); } else { throw new IOException(Messages.getString("ExportDXL.InvalidElementLinkType")); //$NON-NLS-1$ } */ // Description // write(QUOTES); // escapeAndWrite(link.getDescription()); //write(QUOTES_COMMA); // Source //write(QUOTES); // write(link.getSrc().getId()); // write(QUOTES_COMMA); String source = link.getSrc().getName(); source = source.replaceAll(" ",""); source = source.replaceAll("/",""); // Destination // write(QUOTES); // write(link.getDest().getId()); // write(QUOTES_END_ELEM); String destination = link.getDest().getName(); destination = destination.replaceAll(" ",""); destination = destination.replaceAll("/",""); write("!insert (" +source+ "," +name+") into ___"); write (System.getProperty("line.separator")); write("!insert ("+name+ "," +destination+") into __"); write (System.getProperty("line.separator")); } write("\n"); //$NON-NLS-1$ } protected void writeMaps(URNspec urn, String filename) throws IOException { for (Iterator iter = urn.getUrndef().getSpecDiagrams().iterator(); iter.hasNext();) { IURNDiagram element = (IURNDiagram) iter.next(); if (element instanceof UCMmap) { UCMmap ucmmap = (UCMmap) element; // map // write("map( "); //$NON-NLS-1$ write(CREATE+" "); //$NON-NLS-1$ // id String name = ucmmap.getName(); name = name.replaceAll(" ",""); write(name); write (":"); write ("Process"); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"name := '"); write(ucmmap.getName()+"' "); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"id :="); write(ucmmap.getId()); write (System.getProperty("line.separator")); write(System.getProperty("line.separator")); // MapFileName /* int firstIndex = filename.lastIndexOf("\\") + 1; //$NON-NLS-1$ int lastIndex = filename.lastIndexOf("."); //$NON-NLS-1$ String bitmapFilename = filename.substring(firstIndex, lastIndex); bitmapFilename = bitmapFilename.concat("-Map"); //$NON-NLS-1$ bitmapFilename = bitmapFilename.concat(mapID); bitmapFilename = bitmapFilename.concat("-"); //$NON-NLS-1$ bitmapFilename = bitmapFilename.concat(ExportWizard.cleanFileName(mapName)); bitmapFilename = bitmapFilename.concat(".bmp"); //$NON-NLS-1$ */ // MapTitle // write("--"); // escapeAndWrite(mapName); // Description // write(QUOTES); // escapeAndWrite(ucmmap.getDescription()); //(QUOTES_END_ELEM); // Nodes (respRef, stub) // respRef writeRespRef(ucmmap); // stub writeStub(ucmmap); // compRef // writeCompRef(ucmmap); //writeResponsibilities(ucmmap); } } write("\n\n"); //$NON-NLS-1$ } /** * Writes the information about responsibilities, such as id, name, description, and processor demand of the responsibilities * * @param urn * urn * @throws IOException */ // protected void writeResponsibilities(URNspec urn) throws IOException { // for (Iterator iter = urn.getUrndef().getResponsibilities().iterator(); iter.hasNext();) { // IURNDiagram element = (IURNDiagram) iter.next(); // UCMmap ucmmap = (UCMmap) element; // Responsibility resp = (Responsibility) iter.next(); // write("responsibility( "); //$NON-NLS-1$ //write(CREATE+" "); //$NON-NLS-1$ //String name = resp.getName(); //name = name.replaceAll(" ",""); //write(name); //write (":"); //write ("Activity"); // write (System.getProperty("line.separator")); //write(Set+" "); //$NON-NLS-1$ // name //write(name+"."+"name := '"); //write(resp.getName()+"' "); //write (System.getProperty("line.separator")); //write(Set+" "); //$NON-NLS-1$ // ID // write(name+"."+"id :="); // write(resp.getId()); // write (System.getProperty("line.separator")); //write ("!insert ( " + name+ "," + ucmmap.getName()+")" + "into hasOrIntegrate__newOrcurrent" ); // } // write("\n"); //$NON-NLS-1$ // } /** * Writes the information about grl nodes (intentional element references or belief), such as id, fx, fy, enclosing actor, definition id, name, description, * priority and criticality * * @param graph * GRLGraph * @throws IOException */ protected void writeRespRef(UCMmap ucmmap) throws IOException { for (Iterator iter1 = ucmmap.getNodes().iterator(); iter1.hasNext();) { IURNNode specNode = (IURNNode) iter1.next(); if (specNode instanceof RespRef) { RespRef respRef = (RespRef) specNode; // write(" respRef( "); //$NON-NLS-1$ // ID // Fx // String respX = "" + respRef.getX(); //$NON-NLS-1$ // write(respX); // write(System.getProperty("line.separator")); // Fy // String respY = "" + respRef.getY(); //$NON-NLS-1$ // write(respY); // write(","); // EnclosingComponent // ComponentRef compRef = (ComponentRef) respRef.getContRef(); // write("Component"); // if (compRef != null) { // write(compRef.getId()); //} //write(","); // DefinitionID Responsibility res = respRef.getRespDef(); // write("RefDef"); // write(res.getId()); // write(System.getProperty("line.separator")); // Name // write("Name"); // escapeAndWrite(res.getName()); // write(System.getProperty("line.separator")); // write ("!insert ( " + res.getName()+ "," + ucmmap.getName()+")" ); // Description // write("Description"); //escapeAndWrite(res.getDescription()); // write(System.getProperty("line.separator")); write(CREATE+" "); //$NON-NLS-1$ String name = res.getName(); name = name.replaceAll(" ",""); name = name.replaceAll("/",""); write(name); write (":"); write ("Activity"); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // name write(name+"."+"name := '"); write(name+"' "); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"id :="); write(respRef.getId()); write (System.getProperty("line.separator")); String ProcessName= ucmmap.getName(); ProcessName = ProcessName.replaceAll(" ",""); ProcessName = ProcessName.replaceAll("/",""); write ("!insert ( " +ProcessName+ "," + name+")" + "into hasOrIntegrate__newOrcurrent" ); write(System.getProperty("line.separator")); write(System.getProperty("line.separator")); } } } protected void writeUrnLinks(URNspec urnspec) throws IOException { for (Iterator iter = urnspec.getUrnLinks().iterator(); iter.hasNext();) { URNlink links = (URNlink) iter.next(); int i = iter.hashCode(); // write("urnlink("); //$NON-NLS-1$ String type = links.getType(); if (type.contains("change")) { write(CREATE+" "); //$NON-NLS-1$ String name = "Change" + i; // name = name.replaceAll(" ",""); write(name); write (":"); write ("Change"); write (System.getProperty("line.separator")); String sourceName = links.getFromElem().getName(); sourceName = sourceName.replaceAll(" ",""); String destiName = links.getToElem().getName(); destiName = destiName.replaceAll(" ",""); write ("!insert ( " + sourceName+ "," + name+")" + "into __brings" ); write(System.getProperty("line.separator")); write ("!insert ( " + destiName+ "," + name+")" + "into __changes" ); } // From // write(QUOTES); // write(links.getFromElem().getId()); // write(QUOTES_COMMA); // TO // write(QUOTES); // write(links.getToElem().getId()); } else { String name = "Contribute" + i; write(name); write (":"); write ("Contribution"); write (System.getProperty("line.separator")); String sourceName = links.getFromElem().getName(); sourceName = sourceName.replaceAll(" ",""); String destiName = links.getToElem().getName(); destiName = destiName.replaceAll(" ",""); write ("!insert ( " + sourceName+ "," + name+")" + "ToActivity__intiates" ); write(System.getProperty("line.separator")); write ("!insert ( " + destiName+ "," + name+")" + "ToGoal__intiates" ); write(System.getProperty("line.separator")); write(System.getProperty("line.separator")); } // write(QUOTES_END_ELEM); } // write("\n"); //$NON-NLS-1$ } protected void writeComponents(URNspec urn) throws IOException { for (Iterator iter = urn.getUrndef().getComponents().iterator(); iter.hasNext();) { Component element = (Component) iter.next(); UCMspec element1 = urn.getUcmspec(); // String ucmName = element1.ge // write("component( "); //$NON-NLS-1$ // ID String name = element.getName(); name = name.replaceAll(" ",""); write(name); write (":"); write ("Stakeholder"); // write(actor.getName()); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"name := '"); write(element.getName()+"' "); write (System.getProperty("line.separator")); write(Set+" "); //$NON-NLS-1$ // ID write(name+"."+"id :="); write(element.getId()); // String line = System.getProperty("line.separator"); write (System.getProperty("line.separator")); // ucmName = ucmName.replaceAll(" ",""); } write("\n"); //$NON-NLS-1$ } protected void writeStub(UCMmap ucmmap) throws IOException { for (Iterator iter1 = ucmmap.getNodes().iterator(); iter1.hasNext();) { IURNNode specNode = (IURNNode) iter1.next(); String orig = ucmmap.getName(); orig = orig.replaceAll(" ",""); // orign = local.replaceAll(" ",""); if (specNode instanceof Stub) { Stub stub = (Stub) specNode; // write(" stub( "); //$NON-NLS-1$ // ID // write(QUOTES); // write(stub.getId()); // write(QUOTES_COMMA); // Name // StubType // write(QUOTES); // if (stub.isDynamic()) { // write("dynamic"); //$NON-NLS-1$ // } else { // write("static"); //$NON-NLS-1$ // } // write(QUOTES_COMMA); // PluginIDs // write(QUOTES); for (Iterator iter2 = stub.getBindings().iterator(); iter2.hasNext();) { PluginBinding binding = (PluginBinding) iter2.next(); UCMmap map = binding.getPlugin(); String local = map.getName(); local = local.replaceAll(" ",""); local = local.replaceAll("/",""); write ("!insert ( " + orig+ "," + local+")" + " into parent__subprocess" ); write(System.getProperty("line.separator")); write(System.getProperty("line.separator")); // write(map.getId()); // write(";"); //$NON-NLS-1$ } // write(QUOTES_END_ELEM); } } } }