title: Drools API confusion
body: <p>I am trying to evaluate JBoss Drools for a validation service that we are building from scratch (via Spring Boot) but I am having some trouble understanding and differentiating between the multiple APIs that exist. </p>
  
  <p>My first try was a rather simple approach, taken more or less directly from a web tutorial but it worked. With the single dependency of drools-core 6.0.1 I could get a RuleBase with the following code:</p>
  
  <pre><code>public static RuleBase readRule(String ruleFile) throws Exception {
      // read in the source
      Reader source = new InputStreamReader(RuleUtils.class.getClassLoader().getResourceAsStream("rules/" + ruleFile));
  
      PackageBuilder builder = new PackageBuilder();
  
      // this will parse and compile in one step
      builder.addPackageFromDrl(source);
  
      //check for errors
      if (builder.hasErrors()) {
          System.out.println(builder.getErrors().toString());
      }
      // get the compiled package (which is serializable)
      Package pkg = builder.getPackage();
  
      // add the package to a rulebase (deploy the rule package).
      RuleBase ruleBase = RuleBaseFactory.newRuleBase();
      ruleBase.addPackage(pkg);
  
      return ruleBase;
  }
  </code></pre>
  
  <p>With that RuleBase, I could create a new stateful session (WorkingMemory) and then insert objects and fire my rules. 
  However, I have read <a href="http://drools-moved.46999.n3.nabble.com/Difference-between-Rule-Base-and-Knowledge-Base-td1263675.html" rel="nofollow noreferrer">here</a> that this approach seems to be deprecated and also I would not want to use an obsolete version of Drools for a shiny new project (6.0.1 as opposed to the current version 7.5.0).</p>
  
  <p>Another API that was very common in a lot of tutorials was this:</p>
  
  <pre><code>public static RuleBase readRule(String ruleFile) throws Exception {
      // read in the source
      Reader source = new InputStreamReader(RuleUtils.class.getClassLoader().getResourceAsStream("rules/" + ruleFile));
  
      KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
  
      // this will parse and compile in one step
      kBuilder.add(ResourceFactory.newReaderResource(source), ResourceType.DRL);
  
      //check for errors
      if (kBuilder.hasErrors()) {
          System.out.println(kBuilder.getErrors().toString());
      }
      // get the compiled package (which is serializable)
      Collection&lt;KnowledgePackage&gt; pkgs = kBuilder.getKnowledgePackages();
  
      // add the package to a rulebase (deploy the rule package).
      KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
      kbase.addKnowledgePackages(pkgs);
  
      return kbase;
  }
  </code></pre>
  
  <p>But that is where the confusion starts. All of these classes seem to be available in two separate libraries (org.drools and org.kie.internal). With org.drools I could not get the above code to work - I always got a <code>java.lang.ClassNotFoundException: 
   org.drools.builder.impl.KnowledgeBuilderFactoryServiceImpl</code> - I have no idea which library I might have missed. This is an excerpt of my pom.xml which already is much larger than with the first approach, even though I am not sure which libraries I actually need (the official documentation is not extremely helpful here):</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.drools&lt;/groupId&gt;
          &lt;artifactId&gt;drools-compiler&lt;/artifactId&gt;
          &lt;version&gt;7.5.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.drools&lt;/groupId&gt;
          &lt;artifactId&gt;drools-core&lt;/artifactId&gt;
          &lt;version&gt;7.5.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.drools&lt;/groupId&gt;
          &lt;artifactId&gt;drools-api&lt;/artifactId&gt;
          &lt;version&gt;5.1.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.kie&lt;/groupId&gt;
          &lt;artifactId&gt;kie-api&lt;/artifactId&gt;
          &lt;version&gt;7.5.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.kie&lt;/groupId&gt;
          &lt;artifactId&gt;kie-internal&lt;/artifactId&gt;
          &lt;version&gt;7.5.0.Final&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>When I use the other implementation, my KnowledgeBase gets marked as deprecated, which seems to be an obvious hint not to use that anymore. 
  I have then found yet another suggestion on how to actually implement this with Drools 6.x and tried that as well:</p>
  
  <pre><code>public static KieBase readRuleKie(String ruleFile) throws Exception {
      // read in the source
      Reader source = new InputStreamReader(RuleUtils.class.getClassLoader().getResourceAsStream("rules/" + ruleFile));
  
      // Get access to Drools services
      KieServices services = KieServices.Factory.get();
  
      // Obtain a new empty virtual file system
      KieFileSystem fileSystem = services.newKieFileSystem();
  
      // Load a DRL resource from src/main/resources into the virtual file system
      String location = "/rules/" + ruleFile;
      InputStream stream = RuleUtils.class.getClassLoader().getResourceAsStream("rules/" + ruleFile);
      Resource resource = ResourceFactory.newInputStreamResource(stream);
      fileSystem.write("src/main/resources" + location, resource);
  
      // Convert the files in the virtual file system into a builder
      KieBuilder builder = services.newKieBuilder(fileSystem).buildAll();
  
      // Check for errors, print them and stop if any
      Results results = builder.getResults();
      if (results.hasMessages(Message.Level.ERROR)) {
          System.out.println(results.getMessages());
      }
  
      // Create a new kie base out of a repository and a container
      KieRepository repository = services.getRepository();
      KieContainer container = services.newKieContainer(repository.getDefaultReleaseId());
      KieBase base = container.getKieBase();
  
      return base;
  }
  </code></pre>
  
  <p>Now, this actually worked, but not before also adding a dependency to XStream in Version 1.4.10 to my project. But still, I seem to be using deprecated libraries for building my KnowledgeBase/KieBase/RuleBase. Furthermore, the API seems to get more and more verbose with every iteration.</p>
  
  <p>The question is, what would I need to do or change in the above code so that I can get it to work with Drools 7? The API seems to have changed completely and I'd rather not use a kmodule.xml file in my META-INF which seems to be the favored approach by JBoss. </p>
  
  <ul>
  <li>Is there a way to programmatically build my KnowledgeBase similar to my examples above with Drools 7.x?</li>
  <li>Which API should I use? Is there any non-deprecated library?</li>
  <li>What dependencies do I actually need in order to get the project to run?</li>
  </ul>
  
  <p>I could always just go with the first example, but that is no longer possible with the newer Drools APIs, and I want to avoid being dependent on old libraries.</p>