Visual Studio DSL 入门 11---为状态机设计器添加规则

上一节我们在设计器的显示方面进行了完善,在这一节,我们将深入状态机设计器的一些逻辑细节,给我们的设计器添加逻辑规则.为生成代码做好准备.
   在开始之前,我们先看一下Transition的几个属性之间的关系: 
     1.编辑Event,Condition,Action属性时,Label属性能够自动计算显示,计算逻辑为Event [Condition] / Action 
     2.当修改属性Label时,Event,Condition,Action的值也能够对应自动更新.  
   我们使用Vs.net Dsl的规则来实现:
     1.在Dsl项目下新增CustomCode文件夹来存放我们的自定义的代码,这是Dsl开发中的通用做法,Dsl再强大也不可能不一点代码不用写就能够使满足需求,一般情况下,无论是在开发Dsl还是在使用Dsl时,都需要结合生成的代码和自定义代码.在CustomCode文件夹下面新建文件夹Validation,用于存放手写的自定义验证类.
     2.在Validation文件夹下面添加类TransitionLabelRule.
     3.修改TransitionLabelRule继承于ChangeRule,并使用RuleOn属性标签标识此规则应用到域关系Transition上面.

隐藏行号 复制代码
  1. using Microsoft.VisualStudio.Modeling;
    
  2. namespace Company.LanguageSm
    
  3. {
    
  4.     [RuleOn(typeof(Transition))]
    
  5.     public sealed class TransitionLabelRule : ChangeRule
    
  6.     {
    
  7.     }
    
  8. }
    

 

       4.在规则类里面,我们需要实现ChangeRule唯一的一个方法ElementPropertyChanged(ElementPropertyChangedEventArgs e),从这个方法我们可以看出,当Transition的一些属性发生变化时就会触发这个规则,参数类型ElementPropertyChangedEventArgs,这个参数包含当前的模型元素ModelElement,编辑的属性DomainProperty,原值OldValue,新值NewValue,我们只需要判断当前的属性,如果是以上的Event,Condition,Action,Lable时,修改后就计算其余的属性.
   

隐藏行号 复制代码
  1. public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
    
  2.         {
    
  3.             Transition t = e.ModelElement as Transition;
    
  4.             // Compute Label when Event changes
    
  5.             if (e.DomainProperty.Id == Transition.EventDomainPropertyId)
    
  6.                 t.Label = ComputeSummary(e.NewValue as string, t.Condition, t.Action);
    
  7.             // Compute Label when Condition changes
    
  8.             else if (e.DomainProperty.Id == Transition.ConditionDomainPropertyId)
    
  9.                 t.Label = ComputeSummary(t.Event, e.NewValue as string, t.Action);
    
  10.             // Compute Label when Action changes
    
  11.             else if (e.DomainProperty.Id == Transition.ActionDomainPropertyId)
    
  12.                 t.Label = ComputeSummary(t.Event, t.Condition, e.NewValue as string);
    
  13.             // Compute Event, Condition, Action when Label changes
    
  14.             else if (e.DomainProperty.Id == Transition.LabelDomainPropertyId)
    
  15.                 ComputeProperties(e.NewValue as string, t);
    
  16.         }
    

       6.生成转换所有模板,我们来测试一下我们的规则:
 ~7OF3)~R2I0MRM~5]Q1P_C2

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值