Esteban's Blog

/dev/rnd

Guvnor: Embed Asset’s Editor in your application.

Until now, if you wanted to create or edit rule assets in Drools you needed to enter Guvnor and work inside it. When Drools 5.2 sees the light of the day, there will be a new way: to embed Guvnor’s editors in your applications.

Guvnor’s Editors

Guvnor provides a set of editors to author rules in different ways. According to rule’s format a specialized editor is used. Some of the supported formats are: brl (guided editor), drl (plain editor), dsl (dsl editor), template (guided editor) and decision table (decision table editor).

Guided Editor for Rule Assets

Guided Editor

Editor for DRL Assets

DRL Editor

Editor for DSL Assets

DSL Editor

Editor for Decision Table Assets

Decision Table Editor

Since now, the only way to use these editor was using guvnor. But with Drools 5.2 you could embed any of these editors in your own application just like if they were standalone applications.

Standalone Editors and Guvnor

The standalone version of Guvnor’s Editors lets you to use just the editor window inside your applications. In order to be able to invoke a Editor instance from an external application, Guvnor must be running: Standalone Editor is just a part of Guvnor and not a different application.
So the first step is to have Guvnor deployed and running in a web/application server.

Using the standalone version of Guvnor’s Editor you can create or edit assets only inside existing categories and packages. You must configure at least one category and package with a valid model inside Guvnor to start working with this feature.

So, How do I embed these editors in my Application?

Guvnor defines a single entry-point to embed any of its editors in a web application: StandaloneEditorServlet. This servlet is found in /standaloneEditorServlet URL, and according t to the parameters you pass to it (parameters names and posible values are going to be explained later), you can open the editor in 3 different modes: BRL Edition Mode, Edition of Existing Assets Mode and Create New Asset Mode.
So, if you want to embed a Guvnor Editor in your application you will need to perform a request to /standaloneEditorServlet URL. Once open, you can interact with the editor using JavaScript.
Because you need to invoke JavaScript in order to interact with the editor, Guvnor and your application must be running in the same server. Otherwise you will find cross-domain JavaScript invocation problems.

Standalone Editor Modes

Depending on the parameters used to invoke the Editor, you can use it in 3 different ways: BRL Edition Mode, Edition of Existing Assets Mode and Create New Asset Mode.

1. BRL Edition Mode

BRL Edition Mode is used if you want to use BRL code in your application. You can provide multiple BRL sources to the Editor, each of them will be converted to a temporal RuleAsset (a Guvnor’s internal representation) and displayed in a separate editor.

The parameters involved in this mode are:

Parameter Name Explanation Allow multiple values Example
packageName The name of the package used to hold the created assets. The package must exist in Guvnor. false mortgages
categoryName Each rule must have at least one category. The created rules will belong to this category. The category must exist in Guvnor false Home Mortgage
brlSource The BRL source used by the editor. You can pass multiple brlSource parameters for multiple rules. true <rule>
<name>
Bankruptcy history
</name>
<modelVersion>
1.0
</modelVersion>
<attributes>

</attributes>
<lhs>

</lhs>
<rhs>

</rhs>
</rule>

All the assets created when using this mode are temporal. They are never going to be persisted in Guvnor. The purpose of this mode is to use just the Guided Editor and not Guvnor. You can provide one or more initial brls, work on them using the Guided Editor and then retrieve the modified source. Every time you want to edit a rule, you must provide its brl code.

Standalone Editor with 2 BRL Rules

2. Edition of Existing Assets Mode

You can use this mode if you want to use the Standalone Editor for edit assets that already exist inside Guvnor.
When editing existing assets, you will be able to save the changes in Guvnor as well as get the DRL and BRL code of them.

The parameters involved in this mode are:

Parameter Name Explanation Allow multiple values Example
assetsUUIDs The asset’s UUID. Use multiple parameters for specify multiple assets. true 968c9b3c-bc19-40ba-bb38-44435956ccee

The asset’s UUID could be found using Guvnor (we are working on a way to get this information using Guvnor’s REST API).
When using this mode, you can edit assets from different types: like rules and decision tables.

Standalone Editor with Multiple Assets

3. New Asset Mode

You can use this mode if you want to start a new asset from scratch. Assets created using this mode could be persisted inside Guvnor using the “Save all Changes” button. At this moment, you can only create one asset at a time using this mode.

The parameters involved in this mode are:

Parameter Name Explanation Allow multiple values Example
packageName The name of the package used to hold the created asset. The package must exist in Guvnor. false mortgages
categoryName The created rule will belong to this category. The category must exist in Guvnor false Home Mortgage
createNewAsset Flag indicating that we want to start a new rule from the scratch false true
false
assetName The name for the asset to be created false New Rule
assetFormat The format of the asset to be created. The format identifies the editor to be used. false brl (default)
drl
dsl
gdst
template
And possibly any value defined in org.drools.guvnor.client.common.AssetFormats

Rule’s Sections Visibility Parameters

When you edit or create rules in BRL format (using the Guided Editor), you can choose which part of the rules would be visible (LHS, RHS and Attributes). You can specify this using 3 different HTTP parameters:

Parameter Name Explanation Allow multiple values Example
hideRuleLHS Should the LHS of the rules be hidden? false true
false
hideRuleRHS Should the RHS of the rules be hidden? false true
false
hideRuleAttributes Should the Attributes of the rules be hidden? false true
false

Contraint Fact Types

When you edit or create rules in BRL format (using the Guided Editor), you can define a subset of the Fact Types defined in the rule’s package. When authoring the rule, you will only see the Fact Types defined in that subset. This is the same concept as Working-Sets. In fact, a Working-Set will be created and applied on-the-fly using the provided subset.

If you want to define this set of Fact Types you could use this parameter:

Parameter Name Explanation Allow multiple values Example
validFactType The name of a valid Fact Type. This is just the class name and not the fqn. true Bankruptcy
ApplicationLoan

Interacting with the Editor

After the Editor is open, you can interact with it using JavaScript.
The Editor defines a JavaScript object in the Window where it is rendered. This object looks like this:

var guvnorEditorObject = {
    getDRL: function (callbackFunction),
    getBRL: function (callbackFunction),
    registerAfterSaveAndCloseButtonCallbackFunction: function (callbackFunction),
    registerAfterCancelButtonCallbackFunction: function (callbackFunction),
    getAssetsUUIDs: function()
}

The window.guvnorEditorObject defines 5 functions that you can use to interact with it. getDRL() and getBRL() receive a callback function as parameter. This function will receive a String containing the DRL or BRL of the rules you are editing.

The next 2 functions are for register callbacks for “Save”, “Done” and “Cancel” buttons. These callback functions don’t accept any parameter.
The last function is ised to retrieve the UUIDs of the assets you are editing. This is very useful when you are creating a new rule asset and you don’t know the UUID of it.

Conclusion

If you want to create rules in your applications using Guvnor’s Editors you can embed them in your applications now.

To invoke an Editor instance you need to perform a HTTP Request to /standaloneEditorServlet URL. According to the parameters passed, you could use Guvnor’s Editors in 3 different ways.When you need to interact with Guvnor’s Editor, you can use a JavaScript object defined in the window/frame where it is open.

Handy Resources

If you want to try this feature, you need to get the latest snapshot of Guvnor 5.2 version.

I also have created the following files to test all the functionalities while I was developing them. You can use them if you want to make a quick test, or to see how your applications should interact with Guvnor’s Editors.
You could download them and change the ACTION attribute of the form they have.

  • brlEditor.html: Shows how to open an Editor for 2 BRL sources.
  • newAssetEditor.html: Shows how to create a new Asset from the scratch. You can select from different Asset Formats.
  • assetEditor.html: Shows how to edit existing assets. You will need to change the harcoded Asset’s UUIDs.
  • GECallbackFunctions.txt: Shows the different function you can invoke on guvnorEditorObject. You can use frebug to execute these functions.

19 comments on “Guvnor: Embed Asset’s Editor in your application.

  1. Abdel
    November 23, 2010

    Very nice…
    This is a great idea.
    Thanks Edson.

  2. Abdel
    November 23, 2010

    Sorry, I meant thank Estaban – nice work man.
    Thanks to Edson for fusion 🙂

  3. cooldude
    February 5, 2011

    Not Able to download Guvnor 5.2. please let me know location from where i can download.

  4. Pingback: Integrate Oryx Designer and Guvnor in your own applications « Tihomir Surdilovic

  5. christoph
    May 30, 2011

    hmm, sorry! I can’t get this working for me. I’m running Guvnor 5.2.0M2. On the same server I put a little hmtl-file (like assetEditor.html) defining nothing then a single key-value-pair (assetsUUIDs and uuidOfAnExistingAsset) inside the form-tag and sends it against the standaloneEditorServlet of Guvnor. After invoking the redirect to StandalonEditor.html is ok, but the url shows not the asset-uuid and it throws a 500-error 😦 any suggestions?

    • esteban
      May 31, 2011

      Are you getting any stacktrace in Server’s output console?

      • christoph
        June 1, 2011

        Hi,
        yeah, it gives me a Nullpointer-Exception in StandaloneEditorServiceImplementation where the server tries to handle the clientName-String from the request. In your example you do not provide the option “client” within the Post-Form. After adding this option, it works like a charme. You can find my solution in the trackback above.

  6. Pingback: Standalone-Editor für Drools-Guvnor - Christoph Burmeister

  7. dd
    January 1, 2012

    How about rule validation? That would be a nice menu option to have

    • esteban
      January 2, 2012

      Yes, I was thinking about adding rule validation and verification options to standalone BRL Editor.
      Not in my priority list yet 🙂 But you know, contributions are always welcomed!

  8. Veeras
    January 5, 2012

    Hi All,

    1)In the above file “brlEditor.html” the url at the end
    127.0.0.1:9997 what it (9997) represents exactly.
    and when i running this i am getting gwt plugin required..

    2) can anybody tell me is it possible or not : in guvnor console when i create a new package ( or) rule after immadeiatly when i click save button i want to call a java class is it possible……

    Thanks in Advance…

    • esteban
      January 5, 2012

      1) 9997 is the port. You have to adapt the url to match your guvnor instance.

      2) It is possible. What you have to do is to register a callback in the Save and Close button in js using “registerAfterSaveAndCloseButtonCallbackFunction()”. Using that function you can register your own function to do whatever you want after the user press the “Save and Close” button.

      • Veeras
        January 6, 2012

        Thanks for your valuable suggestion Esteban.But where to write js registerAfterSaveAndCloseButtonCallBackFunction()??In the guvnor itself?
        Please suggest me.

      • esteban
        January 6, 2012

        I have a question for you first: Are you using Guvnor’s UI or are you creating your own application that uses Guvnor’s editors? This post is about the latter. If you want to use Guvnor instead of your custom application, then you will need to modify Guvnor’s code in order to achieve what you need.

  9. Veeras
    January 5, 2012

    Hi Esteban,

    can u give an idea how to achieve my requirement:

    i have a java class when i compile that class the packages & rules which i was created in Guvnor console is storing in DB.
    when i add a new Package (or) rule in guvnor even i have to compile that java class then only newly added package & rules are storing in Db.
    in guvnor when i create a new package ( or) rule after immadeiatly when i click save button i want to call a java class is it possible……

    Thanks in Advance…

    • esteban
      January 5, 2012

      You can achieve part of what you are looking for (execute custom code after a rule is created/modified) only if you are using Rule Editor inside your own application. If you are using Guvnor (I mean, accessing Guvnor through your browser) to do this, then there is no way to do what you need. You will need to modify Guvnor’s source code.

      Best Regards,

  10. g3
    November 5, 2012

    Hi Estaban,
    I am looking for a way to call a method for Guvnor, Decision Table Guided Editors. Is it possible in the current version? All I see are setting values for fields.
    Thanks in Advance

    • esteban
      November 19, 2012

      Unfortunately, the current version doesn’t support the required functionality. The embedded editor is treated as a generic editor, so there is no way to invoke specific methods of a specific editor type.

      Best Regards,

Leave a comment

Information

This entry was posted on November 23, 2010 by in drools, java and tagged , , , .