title: How do I add facts to working memory at runtime in the Drools DRL and retrieve them in the execution results of a stateless session?
body: <p><strong>Background:</strong></p>
  
  <p>I'm working on an application that transforms an input object into one of two output objects based upon a set of drools rules.  The output object is not known until runtime and it is created in the first rule to execute.</p>
  
  <p>Here is the rule that creates the output object and an example transformation rule:</p>
  
  <pre><code>rule "Initialization"
      dialect "java"
      salience 1000
      no-loop true
      when
          t : Trade()
      then 
          if(t.getTran().getInsType().equalsIgnoreCase("EEO") ||
             t.getTran().getInsType().equalsIgnoreCase("EEF"))
          {
              insert(new Option());
          }
          else
          {
              insert(new Swap());
          }
  end
  
  rule "Example Rule"
      dialect "java"
      when
          t : Trade()
          opt : Option()
      then
          opt.setCounterpartyName(t.getTran().getCParty());
  end
  </code></pre>
  
  <p>Here is the code that is calling the rules:</p>
  
  <pre><code>private void test(){
      for(File xmlFile : getXmlFilesFromDirectory(XML_DIRECTORY))
      {
          Trade trade = (Trade)unmarshall(xmlFile, Trade.class);
  
          KnowledgeBase kbase = readKnowledgeBase();
  
          StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
          KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
  
          List&lt;Command&gt; commands = new ArrayList&lt;Command&gt;();
          commands.add(CommandFactory.newInsert(trade, "trade"));
          commands.add(CommandFactory.newFireAllRules());
  
          ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
          logger.close();
      }
   }
  
  private static KnowledgeBase readKnowledgeBase() throws Exception 
  {
      KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
      kbuilder.add(ResourceFactory.newClassPathResource("security-transformation.drl"), ResourceType.DRL);
      KnowledgeBuilderErrors errors = kbuilder.getErrors();
      if (errors.size() &gt; 0) 
      {
          for (KnowledgeBuilderError error: errors) 
          {
              System.err.println(error);
          }
  
          throw new IllegalArgumentException("Could not parse knowledge.");
      }
  
      KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
      kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
      return kbase;
  }
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>When I execute the rules I am not receiving the output object in my returned facts.  I receive the trade object back but I do not get back the Option or the Swap object depending on which was added to working memory by the first rule.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>How do I add facts to working memory at runtime in the drl and retrieve them in the execution results of a stateless session?</p>
  
  <p>EDIT:
  Do I need to use a drools query?</p>