[jboss-user] [JBoss Tools (users)] - DROOLS: Parsing input files

amit_tomar do-not-reply at jboss.com
Sat May 17 03:58:47 EDT 2008


Hi All , 
I am new to this drools technology and want to do a prototype for checking its feasibility for our use. 
Most of the places I believe it will work fine.. but I have a use case of parsing input files recd in different formats. 

Scenario: 
I have 2 input files 
1. 
############################## 
Test config Start 
   100 bytes available  250 bytes total 
   1000 bytes is my life goal 
############################## 
2: 
############################## 
Test config Start 
   200 bytes available  300 bytes used  500 bytes total 
   2000 bytes is my life goal 
############################## 

I have to extract the numbers from here and populate an output objects.. containing whatever value is possible.. so for the first instance I will be able to populate 3 values.. but for the second one I will be able to extract 4 values 
I wrote a file to work on the first case but I am unable to get it working. 
Can somebody help me to complete this prototype.. (I am very new to this so please pardon my ignorance.. I just looked at help file and am trying to build this :) ) 

Thanks for looking..
- Amit

########################################################### 
ParsingExample.java 
########################################################### 
package org.drools.examples; 

import java.io.InputStreamReader; 

import org.drools.FactHandle; 
import org.drools.RuleBase; 
import org.drools.RuleBaseFactory; 
import org.drools.StatefulSession; 
import org.drools.audit.WorkingMemoryFileLogger; 
import org.drools.compiler.PackageBuilder; 
import java.util.*; 

public class ParsingExample { 

    /** 
     * @param args 
     */ 
    public static void main(final String[] args) throws Exception { 

        final PackageBuilder builder = new PackageBuilder(); 
        builder.addPackageFromDrl( new InputStreamReader( ParsingExample.class.getResourceAsStream( "Parsing.drl" ) ) ); 

        final RuleBase ruleBase = RuleBaseFactory.newRuleBase(); 
        ruleBase.addPackage( builder.getPackage() ); 

        final StatefulSession session = ruleBase.newStatefulSession(); 

        final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger( session ); 
        logger.setFileName( "log/OutputObj" ); 
        
        String config = ""; 
        config += "Test config Start\n"; 
        config += "   100 bytes available  250 bytes total\n"; 
        config += "   1000 bytes is my life goal"; 
        String[] configA = config.split("\n"); 
        List configL = new ArrayList(); 
        for (int i=0; i < configA.length; i++) { 
        configL.add(new Row(configA)); 
        } 
        final Lines l1 = new Lines( configL ); 
        final InputConfig a = new InputConfig( l1 ); 

        final OutputObj t1 = new OutputObj(); 

        session.insert( a ); 

        session.insert( t1 ); 


        config = ""; 
        config += "Test config Start\n"; 
        config += "   200 bytes available  300 bytes used  500 bytes total\n"; 
        config += "   2000 bytes is my life goal"; 
        configA = config.split("\n"); 
        configL = new ArrayList(); 
        for (int i=0; i < configA.length; i++) { 
        configL.add(new Row(configA)); 
        } 
        final Lines l2 = new Lines( configL ); 
        final InputConfig b = new InputConfig( l2 ); 

        final OutputObj t2 = new OutputObj(); 

        session.insert( b ); 

        session.insert( t2 ); 
        
        
        session.fireAllRules(); 

        session.dispose(); 

        logger.writeToDisk(); 
    } 
    

    public static class Row { 
        private String val; 

        public Row() { 

        } 

        public Row(final String val) { 
            this.val = val; 
        } 

        public String getVal() { 
            return this.val; 
        } 

        public String toString() { 
            return "[Row " + " : " + this.val + "]"; 
        } 

    } 
    
    public static class Lines { 
        private List rows; 

        public Lines() { 

        } 

        public Lines(final List rows) { 
            this.rows = rows; 
        } 

        public List getRows() { 
            return this.rows; 
        } 

        public String toString() { 
            return "[Lines " + " : " + this.rows.toString() + "]"; 
        } 

    } 

    public static class InputConfig { 
        private Lines config; 

        public InputConfig() { 

        } 

        public InputConfig(final Lines config) { 
            this.config = config; 
        } 

        public Lines getConfig() { 
            return this.config; 
        } 

        public String toString() { 
            return "[InputConfig " + " : " + this.config.toString() + "]"; 
        } 

    } 

    public static class OutputObj { 
        private String bu = ""; 
        private String ba = ""; 
        private String bt = ""; 
        private String blg = ""; 

        public OutputObj() { 
        } 

        public String getBu() { 
            return this.bu; 
        } 

        public void setBu(final String bu) { 
            this.bu = bu; 
        } 

        public String getBa() { 
            return this.ba; 
        } 

        public void setBa(final String ba) { 
            this.ba = ba; 
        } 

        public String getBt() { 
            return this.bt; 
        } 

        public void setBt(final String bt) { 
            this.bt = bt; 
        } 

        public String getBlg() { 
            return this.blg; 
        } 

        public void setBlg(final String blg) { 
            this.blg = blg; 
        } 

        public String toString() { 
            return "[OutputObj " +  "  used: " + this.bu +"  avail: " + this.ba + "  total: " + this.bt + "  Goal: " + this.blg +  "]"; 
        } 

    } 

} 

########################################################### 
Parsing.drl 
########################################################### 


package org.drools.examples 
  
import org.drools.examples.ParsingExample.InputConfig; 
import org.drools.examples.ParsingExample.OutputObj; 
import org.drools.examples.ParsingExample.Lines; 
import org.drools.examples.ParsingExample.Row; 
  
rule "get Bytes v10" 
    salience 10 
    when 
                $o : OutputObj()     
                $i : InputConfig() 
    InputConfig( $conf : config) 
    $row : rows( val matches "^\\s*\\d+\s+bytes available\s+\d+\s+bytes total\s*$") from $i.config 
    then 
                eval($o.bu = /$row/^\s*(\d+)\s+bytes available\s+\d+\s+bytes total\s*$/) 
                eval($o.bt = /$row/^\s*\d+\s+bytes available\s+(\d+)\s+bytes total\s*$/) 
                System.out.println("BU:" + $o.bu); 
                System.out.println("BT:" + $o.bt); 
end 



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4151581#4151581

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4151581



More information about the jboss-user mailing list