Quick JBoss Rules DRL Compiler - via Junit

From Kb

Jump to: navigation, search

Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author

JBoss Rules

To (quickly) compile JBoss rules , run the following as part of a (JUnit) test.

	public void testCompile() throws Exception{
	
        WorkingMemory localWorkingMemory =  loadDrlRulesFromFile("myDrlFile.drl").newWorkingMemory();
               
        // Place the Java Bean in the working memory , keep a handle to it
        FactHandle currentHandle = localWorkingMemory.assertObject(new SearchModel());

        // use Drools *without* any Java intervention
        localWorkingMemory.fireAllRules();

        // Remove the reference to this as part of the cleanup for later
        localWorkingMemory.retractObject(currentHandle);

        log.debug("SUCCESS");
		
	}
    

    private RuleBase loadDrlRulesFromFile(String drlFileName,String dslFileName) {

        // Local Variables
        RuleBase returnRuleBase = null;

        // get the file exists (via file or classpath)
        InputStream iStreamRules = getFileIfExists(drlFileName);
        
        // Convert the Stream into a reader that Drools uses to parse files
        InputStreamReader drl = new InputStreamReader(iStreamRules);

       
            log.info("Reading Rules from drl:" + drlFileName);

            PackageBuilder builder = new PackageBuilder();
            builder.addPackageFromDrl(drl);

            Package pkg = builder.getPackage();

            if (pkg != null) {
                returnRuleBase = RuleBaseFactory.newRuleBase();
                returnRuleBase.addPackage(pkg);
            }
       
        return returnRuleBase;

    }
    
    /**
     * Searches for a file via the local filesystem , then via the Classpath
     *
     * @param fileName to search for
     * @return InputStream containing the opened file
     */
    private InputStream getFileIfExists(String fileName) throws Exception {

        // Local Variables
        InputStream rIStream = null;

        log.debug("Searching for File name:" + fileName);

        // The next line creates one execution of the process definition.
        // After construction the process execution has one main path
        // of execution (the root token) that is positioned in the
        // startstate.
        try {
            rIStream = new FileInputStream(fileName);

        } catch (FileNotFoundException fnfe1) {
            log
                    .debug("Could not find resource as file - attempting the classpath");

            try {
                ClassPathResource myClassPathReader = new ClassPathResource(
                        fileName);
                rIStream = myClassPathReader.getInputStream();
                log.debug("Found File via Classpath at path:" + myClassPathReader.getPath() + " file:" + myClassPathReader.getFile());

            } catch (Exception fnfe2) {
                log.debug("Returning Null as could not find file" + fileName + "locally or on classpath");
                //log.info("Could not find resource as file", fnfe1);
                //log.info("Could not find resource on classpath", fnfe2);

                // we will return null
            }
        }

        return rIStream;

    }

Personal tools