How can I add a custom condition to an existing RUTA project? Started, but am stuck

StackOverflow https://stackoverflow.com//questions/22056617

  •  22-12-2019
  •  | 
  •  

Question

I want to add a custom UIMA RUTA rule condition. I have an existing UIMA Ruta project in Eclipse. So far I created a source file in the same project with a basic annotator stub:

package mynamespace.extensions;

[imports]

public class MyNewCondition extends AbstractRutaCondition {

private final String para1;

public MyNewCondition(String para1) {
    super();
    this.para1 = para1;
}

@Override
public EvaluatedCondition eval(AnnotationFS annotation,
        RuleElement element, RutaStream stream, InferenceCrowd crowd) {
    // TODO Auto-generated method stub
    if (para1 == "hfoo")
        return new EvaluatedCondition(this, true);
    else 
        return new EvaluatedCondition(this, false);
}

public String getPara() {
    return para1;
}
}

The file compiles to the target/classes/... folder, but when I create a RUTA script:

DECLARE Test;
SW{MyNewCondition("foo") -> MARK(Test)};

... Eclipse tells me that "MyNewCondition" is not defined and when I run it I get: "Error in line 40, "(": found no viable alternative" on the console. I presume I need to do some further import, but do not know how. I tried to work from the Extension example project in the Github repository, but I do not know where to start there as the script file does not contain any further imports, but the associated xml descriptor files do. But as these get automatically generated I do not know whether this is what I should change or it is something else.

I also tried importing the same new condition type from a second project via Eclipse's build path options, but no luck there either.

Can someone help? Thanks.

Was it helpful?

Solution

You need at least three classes for adding a new condition that also is resolved in the UIMA Ruta Workbench:

  1. An implementation of the condition as you did in your question
  2. An implementation of IRutaConditionExtension, which provides the condition implementation to the engine
  3. An implementation of IIDEConditionExtension, which provides the condition for the UIMA Ruta Workench

The condition itself contains only the functionality that should be added to the language. The analysis engine knows of course nothing about any external implementations resulting in a strange parse exception like "(" not found. That should be improved sometimes. The analysis engine provides a configuration parameter additionalExtensions that lists all known extensions to the language. If you are not using the UIMA Ruta Workbench, you need to add your implementation of IRutaConditionExtension to this parameter. The implemenation of IIDEConditionExtension provides the necessary functionality for the UIMA Ruta Workbench that is the syntax check, syntax highlighting and so on. Additionally, it enables the Workbench to generate correct descriptors. It adds your implementation of IRutaConditionExtension to the respective parameter. This extension of the Workbench needs of course to be implemented in an Eclipse plugin that is installed in your UIMA Ruta Workbench Eclipse instance, in order to be available in the Workbench. There is an extension point, which you need to extend that knows both your implementation of IRutaConditionExtension and IIDEConditionExtension.

There is an exemplary project that provides implementation of all possible language elements. This project contains the implementations for the analysis engine and also the implementation for the UIMA Ruta Workbench, and is therefore an Eclipse plugin (mind the pom file).

Concerning the ExampleCondition condition extension, there are three important spots/classes:

  1. ExampleCondition.java provides the implementation of the new condition, which evaluates dates
  2. ExampleConditionExtension.java provides the extension for the analysis engine. It knows the name of the condition, its implementation, can create new instances of that condition, and is able to verbalize the condition for the explanation components.
  3. ExampleConditionIDEExtension provides the syntax check for the editor and the keyword for syntax coloring.
  4. plugin.xml defines the extension for the Workbench:

    <extension point="org.apache.uima.ruta.ide.conditionExtension">
      <condition
        class="org.apache.uima.ruta.example.extensions.ExampleConditionIDEExtension"
        engine="org.apache.uima.ruta.example.extensions.ExampleConditionExtension">
      </condition>
    </extension>
    

If you do not use the UIMA Ruta Workbench or only want to apply the rules in UIMA pipelines, you only need ExampleCondition and ExampleConditionExtension, and you need to add org.apache.uima.ruta.example.extensions.ExampleConditionExtension to the additionalExtensions parameter of your UIMA Ruta analysis engine (descriptor).

Adding new conditions using Java projects in the same workspace has not been tested yet, but at least the Workbench support will be missing due to the inclusion of extensions using the extension point mechanism of Eclipse.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top