Cast class to subclass inside Drools

1.7k Views Asked by At

I am using Drools 6.6.0. These are the dependencies for drools support inside my pom.xml file.

<dependency>
       <groupId>org.mvel</groupId>
        <artifactId>mvel2</artifactId>
        <version>2.1.8.Final</version>
</dependency>
<dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr-runtime</artifactId>
        <version>3.3</version>
</dependency>
<dependency>
        <groupId>org.eclipse.jdt</groupId>
        <artifactId>core</artifactId>
        <version>3.4.2.v_883_R34x</version>
 </dependency>
 <dependency>
        <groupId>org.drools</groupId>
        <artifactId>knowledge-api</artifactId>
        <version>6.0.0.Final</version>
 </dependency>
 <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-jsr94</artifactId>
        <version>6.0.0.Final</version>
 </dependency>
 <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-decisiontables</artifactId>
        <version>6.0.0.Final</version>
 </dependency>
 <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>6.0.0.Final</version>
  </dependency>
  <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>6.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.janino</groupId>
        <artifactId>janino</artifactId>
        <version>2.5.16</version>
    </dependency>

I am passing to drools object, and to validate the same I have to access to subclass attributes. According to JBoss tutorials I should be able to cast base object into subtype by this way:

Person(  name=="mark", address#LongAddress.( country == "uk" ) )  
Person(  name=="mark", address#LongAddress.country == "uk"  )
Person(  name=="mark", address isa LongAddress, address.country == "uk" ) 

But my drl file does not recognize operator # as casting operator (actually drools thinks I want to write comment), and is is not recognized.

First I was using drools version 5, and PackageBuilder. I located drl file inside src/main/resources/META-INF folder and was able to read file. At the moment I am using drools version 6 - and still cannot cast object inside drools, and Knowledge builder does not see drl file on the same location.

This is my code for reading drl file:

  private static String DRL_FILE = "/META-INF/validationrules.drl";

  KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
  kbuilder.add(ResourceFactory.newFileResource(DRL_FILE), ResourceType.DRL);

  if( kbuilder.hasErrors() )
  {

       System.out.println( kbuilder.getErrors() );
       return;
  }

  Collection<KnowledgePackage> kpkgs = kbuilder.getKnowledgePackages();

  KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
  StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
  ksession.insert(message);

Am I using the correct drools dependencies and does actually drools support casting inside rule? Where should I place a drl file? As Java environment I am using IntelliJ.

0

There are 0 best solutions below