预订演示

请注意 : 本帮助页面不适用于最新版本的Enterprise Architect. 最新的帮助文档在这里.

前页 后页

验证模型示例

This example code is written in C# and provides a skeleton model validation implementation that you might want to use as a starting point in writing your own model validation rules.

Main.cs

using System;
namespace myAddin
{
     public class Main
     {
          public Rules theRules;
          public Main()
          {
               theRules = new Rules();
          }
          public string EA_Connect(EA.Repository Repository)
          {
               return "";
          }
          public void EA_Disconnect()
          {
               GC.Collect();
               GC.WaitForPendingFinalizers();
          }
          private bool IsProjectOpen(EA.Repository Repository)
          {
               try
               {
                    EA.Collection c = Repository.Models;
                    return true;
               }
               catch
               {
                    return false;
               }
          }
          public object EA_GetMenuItems(EA.Repository Repository, string MenuLocation, string MenuName)
          {
               switch (MenuName)
               {
                    case "":
                         return "-&myAddin";
                    case "-&myAddin":
                         string() ar = { "&Test" };
                         return ar;
               }
               return "";
          }
          public void EA_GetMenuState(EA.Repository Repository, string MenuLocation, string MenuName,
          string ItemName, ref bool IsEnabled, ref bool IsChecked)
          {
               // if no open project, disable all menu options
               if (IsProjectOpen(Repository))
                    IsEnabled = true;
               else
                    IsEnabled = false;
          }
          public void EA_MenuClick(EA.Repository Repository, string MenuLocation, string MenuName, string ItemName)
          {
               switch (ItemName)
               {
                    case "&Test";
                         DoTest(Repository);
                         break;
               }
          }
          public void EA_OnInitializeUserRules(EA.Repository Repository)
          {
               if (Repository != null)
               {
                    theRules.ConfigureCategories(Repository);
                    theRules.ConfigureRules(Repository);
               }
          }
          public void EA_OnRunElementRule(EA.Repository Repository, string RuleID, EA.Element element)
          {
               theRules.RunElementRule(Repository, RuleID, element);
          }
          public void EA_OnRunDiagramRule(EA.Repository Repository, string RuleID, long lDiagramID)
          {
               theRules.RunDiagramRule(Repository, RuleID, lDiagramID);
          }
          public void EA_OnRunConnectorRule(EA.Repository Repository, string RuleID, long lConnectorID)
          {
               theRules.RunConnectorRule(Repository, RuleID, lConnectorID);
          }
          public void EA_OnRunAttributeRule(EA.Repository Repository, string RuleID, string AttGUID, long lObjectID)
          {
               return;
          }
          public void EA_OnDeleteTechnology(EA.Repository Repository, EA.EventProperties Info)
          {
               return;
          }
          public void EA_OnImportTechnology(EA.Repository Repository, EA.EventProperties Info)
          {
               return;
          }
          private void DoTest(EA.Repository Rep)
          {
               // TODO: insert test code here
           }
      }
}

Rules.cs

using System;
using System.Collections;
namespace myAddin
{
     public class Rules
     {
          private string m_sCategoryID;
          private System.Collections.ArrayList m_RuleIDs;
          private System.Collections.ArrayList m_RuleIDEx;
          private const string cRule01 = "Rule01";
          private const string cRule02 = "Rule02";
          private const string cRule03 = "Rule03";
          // TODO: expand this list as much as necessary
          public Rules()
          {
               m_RuleIDs = new System.Collections.ArrayList();
               m_RuleIDEx = new System.Collections.ArrayList();
          }
          private string LookupMap(string sKey)
          {
               return DoLookupMap(sKey, m_RuleIDs, m_RuleIDEx);
          }
          private string LookupMapEx(string sRule)
          {
               return DoLookupMap(sRule, m_RuleIDEx, m_RuleIDs);
          }
          private string DoLookupMap(string sKey, ArrayList arrValues, ArrayList arrKeys)
          {
               if (arrKeys.Contains(sKey))
                    return arrValues(arrKeys.IndexOf(sKey)).ToString();
               else
                    return "";
          }
          private void AddToMap(string sRuleID, string sKey)
          {
               m_RuleIDs.Add(sRuleID);
               m_RuleIDEx.Add(sKey);
          }
          private string GetRuleStr(string sRuleID)
          {
               switch (sRuleID)
               {
                    case cRule01:
                         return "Error Message 01";
                    case cRule02:
                         return "Error Message 02";
                    case cRule03:
                         return "Error Message 03";
                    // TODO: add extra cases as much as necessary
               }
               return "";
          }
          public void ConfigureCategories(EA.Repository Repository)
          {
               EA.Project Project = Repository.GetProjectInterface();
               m_sCategoryID = Project.DefineRuleCategory("Enterprise Collaboration Architecture (ECA) Rules");
          }
          public void ConfigureRules(EA.Repository Repository)
          {
               EA.Project Project = Repository.GetProjectInterface();
               AddToMap(Project.DefineRule(m_sCategoryID, EA.EnumMVErrorType.mvError, GetRuleStr(cRule01)), cRule01);
               AddToMap(Project.DefineRule(m_sCategoryID, EA.EnumMVErrorType.mvError, GetRuleStr(cRule02)), cRule02);
               AddToMap(Project.DefineRule(m_sCategoryID, EA.EnumMVErrorType.mvError, GetRuleStr(cRule03)), cRule03);
               // TODO: expand this list
          }
          public void RunConnectorRule(EA.Repository Repository, string sRuleID, long lConnectorID)
          {
               EA.Connector Connector = Repository.GetConnectorByID((int)lConnectorID);
               if (Connector != null)
               {
                    switch (LookupMapEx(sRuleID))
                    {
                         case cRule02:
                              // TODO: perform rule 2 check
                              break;
                         // TODO: add more cases
                    }
               }
          }
          public void RunDiagramRule(EA.Repository Repository, string sRuleID, long lDiagramID)
          {
               EA.Diagram Diagram = Repository.GetDiagramByID((int)lDiagramID);
               if (Diagram != null)
               {
                    switch (LookupMapEx(sRuleID))
                    {
                         case cRule03:
                              // TODO: perform rule 3 check
                              break;
                         // TODO: add more cases
                    }
               }
          }
          public void RunElementRule(EA.Repository Repository, string sRuleID, EA.Element Element)
          {
               if (Element != null)
               {
                    switch (LookupMapEx(sRuleID))
                    {
                         case cRule01:
                              DoRule01(Repository, Element);
                              break;
                         // TODO: add more cases
                    }
               }
          }
          private void DoRule01(EA.Repository Repository, EA.Element Element)
          {
               if (Element.Stereotype != "myStereotype")
                    return;
               // TODO: validation logic here
               // report validation errors
               EA.Project Project = Repository.GetProjectInterface();
               Project.PublishResult(LookupMap(cRule01), EA.EnumMVErrorType.mvError, GetRuleStr(cRule01));
          }
     }
}