Example of Creating an irAuthor Extension

  • Updated

The following example walks through the process of creating an extension for irAuthor that will demonstrate:

  • Adding a button to the Ribbon and using a Command to run user defined code
  • Enabling and disabling of a button using authoring framework events
  • Working with the Selection Manager Service
  • Modifying the rule application through user defined code so irAuthor is aware of the changes using the Rule Application Service
  • Setting up a new Project
  1. Create a new WpfCustomControlLibrary
    mceclip0.png
  2. Select the .NET Framework 4.7.2 option for the Target Profile on the Application property page.
    mceclip1.png
  3. Delete the default CustomControl1.cs page
  4. Delete the Themes folder
  5. Add a new class called Extension.cs
  6. Make the class Public
  7. Add the following references (assemblies available from the InRule installation folder\irSDK\bin)

Inrule.authoring.dll
Inrule.authoring.windows.dll
Inrule.common.dll
Inrule.respository.dll

Creating the extension class

  1. Set the Extension class to inherit from the InRule.Authoring.Windows.ExtensionBase class
    public class Extension:ExtensionBase
     
  2. Create a default Constructor that will call the base class constructor with the name, description and Guid for the extension.  The built in Guid generator in Visual Studio can be used to create your Guid.
    public Extension()
           : base("EntityViewer",
              "Generates a notification that dumps all fields and their values into a notification",
              new Guid("{CD02ADCB-BC8A-4393-A8EA-2903D5A2AD11}"))
           {
           }
     
  3. Create a method in the Extension class called Enable which is an abstract inherited member and must be implemented.  This is the method that will be called when the extension is loaded and where the code will go that adds the button to the Ribbon and wires up events that will enable and disable our button when not applicable.

    public override void Enable()
          {         
          }
     

  4. Add a private class variable of type VisualDelegateCommand which will be the command that executes when the button is clicked

    private VisualDelegateCommand _notificationCommand;
     

  5. In the Enable method, add a new group to the Ribbon passing in the text for the group and the image that will be displayed if there is not enough room in the Ribbon.  The image below is retrieved using the ImageFactory, which contains all of the images that ship with InRule, each of which is available in this manner.

    var group = IrAuthorShell.HomeTab.AddGroup("Notifications",
    ImageFactory.GetImageAuthoringAssembly("/Images/FireNotificationInfo16.png"));
     

  6. Instantiate the notification command.  The delegate command is passed the method (which will run when the button is clicked), button text, small button image, large button image and whether the button is enabled by default.
     
    Note:
    The AddEntityNotification method creation will be shown below.

    _notificationCommand = new VisualDelegateCommand(AddEntityViewerNotification, "Add entity viewer",
      ImageFactory.GetImageAuthoringAssembly("/Images/FireNotificationInfo16.png"),
      ImageFactory.GetImageAuthoringAssembly("/Images/FireNotificationInfo32.png"),

      false);
     

  7. Add the notification command to the group.
     
    Note:
    Several buttons could be added to a single group.  Buttons can be added or removed  to existing groups as well.

    group.AddButton(_notificationCommand);

     

  8. The button is now added, next wire up the events that will enable/disable it.  In this case, the button should only be enabled when the selected item is a rule set and when a rule application is opened.  To wire up the events, we use the RuleApplicationService and SelectionManager as shown below.

    RuleApplicationService.Opened += SetEnabled;
    RuleApplicationService.Closed += SetEnabled;
    SelectionManager.SelectedItemChanged += SetEnabled;

  9. The SetEnabled method will set the IsEnabled property on the command which will enable/disable the button.

    private void SetEnabled(object sender, EventArgs eventArgs)
    {
      // make sure we are on a rule element
      var selectedDef = SelectionManager.SelectedItem as RuleElementDef;
      if (selectedDef != null)
       {
          // if the selected def has an entity and rule set, this is a entity based rule or ruleset
           _notificationCommand.IsEnabled = ((selectedDef.ThisEntity != null) && (selectedDef.ThisRuleSet != null));
       }
      else
       {
          // if this is not a rule element, always disable it
           _notificationCommand.IsEnabled = false;
       }
    }

  10. Finally, create the method that will use the InRule SDK to create the notification.  Most of the method below is standard SDK code, which has not changed in the new release of InRule.  The actual adding of the notification to the RuleSet does use the new Rule Application service so the rest of the application knows that it was added, such as the tree.

    private void AddEntityViewerNotification(object obj)
    {
      // get selected rule set
      var ruleElementDef = SelectionManager.SelectedItem as RuleElementDef;
             
      if (ruleElementDef != null)
       {
          // get the ruleSet, should not be null or button would be disabled
          var ruleSetDef = ruleElementDef.ThisRuleSet;
          // get the entity that the ruleset is authored in (assumes entity based rule set)
          var entityDef = ruleSetDef.ThisEntity;
          var s = new StringBuilder();
           s.AppendFormat("{0} field values:{1}", entityDef.Name, Environment.NewLine);
          // spin through entity fields and generate the notification
          foreach (FieldDef fieldDef in entityDef.Fields)
           {
               s.AppendFormat("{0}: <% {0} %> {1}", fieldDef.Name, Environment.NewLine);
           }
          // create notification and set message text
          var notificationDef = new FireNotificationActionDef();
           notificationDef.NotificationMessageText = s.ToString();
          // add notification to the rule set using the Controller
           RuleApplicationService.Controller.AddDef(notificationDef, ruleSetDef);
       }
    }

Deploying the Extension

After compiling the project, to deploy it, simply copy the DLL to the Extensions folder underneath the location where the irAuthor.exe is running.  This is typically located at <InRule installation directory>\irAuthor\Extensions.  irAuthor must be closed to copy the DLL if it already existed in the Extensions folder.  After copying, launch irAuthor and go to File -->Extensions to enable the extension.

Note: You will need to close and re-open irAuthor in order to see a newly added extension.

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.