序列图的脚本

接下来为序列图写了一个脚本,可以显示序列图图像,然后在下面列出调用动作的内容,

 

/* Add attribute get and set accessor to selected classes */

//Global varieties
var docFileName = "c://temp.doc";

var styleCaption1 = "标题 1";
var styleCaption2 = "标题 2";
var styleCaption3 = "标题 3";
var styleCaption4 = "标题 4";
var styleCaption5 = "标题 5";
var styleCaption6 = "标题 6";

var styleTableName = "表名";
var styleChartName = "图名";

var styleText = "正文";
var styleTable = "彩色型 2";

//StarUML objects
var app = new ActiveXObject("StarUML.StarUMLApplication");
var prj = app.GetProject();
var facto = app.UMLFactory;
var selmgr = app.SelectionManager;
       
//Just select one model
var m = selmgr.GetSelectedModelAt(0);

//Only operate for UMLSequenceDiagram
if( m.GetClassName()  == "UMLSequenceDiagram" )
{
 //File system object
 var fso = new ActiveXObject("Scripting.FileSystemObject");

 //Word objects
 var word = new ActiveXObject("Word.Application");
 word.Visible = false;
 var doc = word.Document;
 doc = word.Documents.Open( docFileName );
 
 //Create word document
 app.log(m.name);
 CreateDorForSequence(m);
   
 //Save and close word
 doc.save();
 word.quit();
}

else
{
 app.log("No sequence diagram found.");
}


app.log("Document create finished");


/***********/
/*functions*/
/***********/

function CreateDorForSequence( model )
{
 var stimulusCount = 0;
 var view;
 var m;
 
 var dv = model.DiagramView;
 var nameStr = model.name;
 
 if(dv == null )
 {
  app.log("Get model diagram view failed!");
  return;
 }
  
 SetTextStyle(nameStr, styleCaption3);
     SetTextStyle(nameStr, styleTableName);
   
   var table = doc.Tables.Add(doc.Paragraphs(doc.Paragraphs.Count).Range, 3, 1);
 SetTableStyle(table, true); 
 
 //Export diagram view picture
 var jpgName = "c://" + nameStr + ".jpg";
        dv.ExportDiagramAsJPEG(jpgName);
   
   //Write sequence chart information
  table.Cell(1,1).Range.InsertAfter( nameStr );
  table.Cell(2,1).Range.InlineShapes.AddPicture(jpgName);
 table.Cell(3,1).Range.InsertAfter( dv.documentation );
 
  //Delete temporary picture file
  var picFile = fso.GetFile(jpgName);
 picFile.Delete();
 
 SetTextStyle(nameStr+"调用序列", styleTableName);
 app.log("View number in diagram: " + dv.GetOwnedViewCount());
  
 //Get stimulus number for table
 for (var i = 0; i < dv.GetOwnedViewCount(); i++)
 {
  view = dv.GetOwnedViewAt(i);
  if(view == null)
   app.log("null");
  else
  {
   m = view.Model;
   
   //Get UMLStimulus only
   if (m.IsKindOf("UMLStimulus"))
   {
    stimulusCount = stimulusCount + 1;
   }
  }
 }
 
 app.log("Stimulus number: " + stimulusCount);
 
 var table1 = doc.Tables.Add(doc.Paragraphs(doc.Paragraphs.Count).Range, stimulusCount + 1, 6);
 SetTableStyle(table1, true);
 
 table1.Cell(1,1).Range.InsertAfter("序号");
 table1.Cell(1,2).Range.InsertAfter("名称");
 table1.Cell(1,3).Range.InsertAfter("可见性");  
 table1.Cell(1,4).Range.InsertAfter("调用");
 table1.Cell(1,5).Range.InsertAfter("实现方法");  
 table1.Cell(1,6).Range.InsertAfter("描述");
 
 var row = 2;
 for (var i = 0; i < dv.GetOwnedViewCount(); i++)
 {
  view = dv.GetOwnedViewAt(i);
  if(view == null)
   app.log("null");
  else
  {
   m = view.Model;
   app.log( m.name );
   
   if (m.IsKindOf("UMLStimulus"))
   {
    //Get action, it must keep unnamed
    var action =  m.FindByName("");
        
    table1.Cell(row,1).Range.InsertAfter( row - 1 );
    table1.Cell(row,2).Range.InsertAfter( ShowNull(m.name) );
    table1.Cell(row,3).Range.InsertAfter( GetVisibilityExpression(m.visibility) );
    table1.Cell(row,4).Range.InsertAfter( GetActionInfo(action) );
    
    if (action != null)
    {
     table1.Cell(row,5).Range.InsertAfter( ShowNull(action.Script) );
    }
       
    table1.Cell(row,6).Range.InsertAfter( ShowNull(m.Documentation) );
    
    row = row + 1;
   }
  }
 } 
}

function ShowNull(str)
{
 if (str == "")
  return "N/A";
 else
  return str;
}

function blankArray(n)
{
 for (var i = 0; i < n; i++)
  this[i] = null;
 this.length = n;
}

function GetVisibilityExpression(type)
{
 switch(type)
 {
  case 0:
   return "公有";
   
  case 1:
   return "保护";
   
  case 2:
   return "私有";
   
  case 3:
   return "包";
   
  default:
   return "未知"
   
 }
}
function GetActionInfo(action)
{
 var str;
 if (action != null)
 {
  var className = action.GetClassName();
 
  if( className == "UMLCallAction" )
  {
   str = "调用" ;
   
   //Get call operation, which is a reference
   var op = action.MOF_GetReference("Operation");

   if( op != null)
   {
    app.log( op.name );
    str = str + op.name;
   }
   return str;
  }
  
  else if( className == "UMLSendAction" )
   return "发送";
   
  else if( className == "UMLReturnAction" )
   return "返回";
   
  else if( className == "UMLCreateAction" )
   return "创建";
   
  else if( className == "UMLDestroyAction" )
   return "销毁";
   
  else
   return "未知"
 }
 else
  return "没有动作";
}
  
function GetParaDirection(type)
{
 switch(type)
 {
  case 0:
   return "输入";
   
  case 1:
   return "双向";
   
  case 2:
   return "输出";
   
  case 3:
   return "返回";
   
  default:
   return "未知"
   
 }
}

function SetTableStyle(table, flag)
{
        table.Style = styleTable;
        table.ApplyStyleLastRow = false;
        table.ApplyStyleLastColumn = false;
   
 if( flag == true)
 {
         table.ApplyStyleHeadingRows = true;
         table.ApplyStyleFirstColumn = false;
 }
 else
 {
  table.ApplyStyleHeadingRows = false;
  table.ApplyStyleFirstColumn = true;
 }
}

function SetTextStyle(text, style)
{
 //Insert text using specific style
 doc.Paragraphs(doc.Paragraphs.Count).Range.InsertAfter("/r/n");
  doc.Paragraphs(doc.Paragraphs.Count).Range.ParagraphFormat.Style = doc.Styles(style);
  doc.Paragraphs(doc.Paragraphs.Count).Range.InsertAfter(text);
  doc.Paragraphs(doc.Paragraphs.Count).Range.InsertAfter("/r/n");
  
  //Set back
  doc.Paragraphs(doc.Paragraphs.Count).Range.ParagraphFormat.Style = doc.Styles(styleText);
}

效果如下:

 
9.1.1          上传状态
 
表 1      上传状态

上传状态
 
 

 
表 2      上传状态调用序列

序号
名称
可见性
调用
实现方法
描述
1
getCommand
公有
调用
N/A
N/A
2
N/A
公有
调用sendTicketBoxStatus
N/A
要添加回复的命令ID,所以与状态变化发送的命令有所不同(加入回复ID)
3
sendMessage
公有
调用
N/A
N/A
4
N/A
公有
返回
N/A
N/A

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值