Wednesday, December 29, 2010

JBoss Rules (Drools) "Brush" for Syntax Highlighter

After completing my last two JBoss Rules (Drools) posts, I decided to create a Syntax Highlighter brush for the Drools language. The syntax was largely inspired by Alan Daniel's vim syntax for Drools: http://www.vim.org/scripts/script.php?script_id=1940.  If anyone with a more stable hosting solution wouldn't mind hosting the file, please leave me a comment on this post.

Creating a new brush for Syntax Highlighter is really quite simple.  Here's all it takes to get support for Drools:

SyntaxHighlighter.brushes.Drools = function()
{

  var keywords    = 'assert duration end eval function' 
                  + 'modify query salience '
                  + 'retract return rule then when ' 
                  + 'activation-group '
                  + 'agenda-group auto-focus no-loop';
  var external    = 'native package import static';
  var conditional = 'if else switch and excludes '
                  + 'exists or not matches';
  var operator    = 'new instanceof ->';
  var type        = 'boolean char byte short int '
                  + 'long float double void';
 
  this.regexList = [
    { regex: /\s*#.*/gm, 
       css: 'comments' }, 
    { regex: SyntaxHighlighter.regexLib.singleLineCComments, 
       css: 'comments' }, 
    { regex: /\/\*([^\*][\s\S]*)?\*\//gm, 
       css: 'comments' }, 
    { regex: /\/\*(?!\*\/)\*[\s\S]*?\*\//gm, 
       css: 'preprocessor' }, 
    { regex: SyntaxHighlighter.regexLib.doubleQuotedString, 
       css: 'string' }, 
    { regex: SyntaxHighlighter.regexLib.singleQuotedString, 
       css: 'string' }, 
    { regex: /\b([\d]+(\.[\d]+)?|0x[a-f0-9]+)\b/gi, 
       css: 'value' }, 
    { regex: new RegExp(this.getKeywords(keywords), 'gm'), 
       css: 'keyword' }, 
    { regex: new RegExp(this.getKeywords(external), 'gm'), 
       css: 'color3' },
    { regex: new RegExp(this.getKeywords(conditional), 'gm'), 
       css: 'constants' }, 
    { regex: new RegExp(this.getKeywords(operator), 'gm'), 
       css: 'color2' }, 
    { regex: new RegExp(this.getKeywords(type), 'gm'), 
       css: 'color2' } 
 ];
   
  this.forHtmlScript({
    left : /(&lt;|<)%[@!=]?/g, 
    right : /%(&gt;|>)/g 
  });
};
SyntaxHighlighter.brushes.Drools.prototype 
   = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Drools.aliases  = ['drools', 'drl'];

You will need to place this in a JavaScript file and link to it after you have imported the mandatory Syntax Highlighter JavaScript files.

<script type='text/javascript' 
  src='http://path/to/drools/brush/shBrushDrools.js' />

Now all you need to do is wrap your Drools Expert code in "pre" tags, referencing the appropriate brush class:

<pre class="brush: drools">
... include rules here ...
</pre>

Here's the "LocalAssertionsNotPingPong.drl" example from the source package of the Drools 5.1.1 framework:

/**
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, 
 * Version 2.0 (the "License");
 * you may not use this file except in 
 * compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or 
 * agreed to in writing, software
 * distributed under the License is distributed 
 * on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied.
 * See the License for the specific 
 * language governing permissions and
 * limitations under the License.
 */

package org.drools.test;

import java.lang.String;
import java.lang.Integer;
import java.util.List;

global java.lang.Integer i;
global java.lang.String s;
global java.util.List l;

rule "not s then i"
  when
    not String()
  then
    if (l.size() < 10) {
      l.add(new Integer(0));
      System.out.println("Logical assert i");
      insertLogical(i);
    }
end
rule "i then s"
  when
    Integer()
  then
    if (l.size() < 10) {
      l.add(new Integer(0));
      System.out.println("Logical assert s");      
      insertLogical(s);
    }
end

That's it.  Enjoy.

Richard

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.