From: <jbo...@li...> - 2006-04-27 18:42:39
|
Author: mar...@jb... Date: 2006-04-27 14:42:31 -0400 (Thu, 27 Apr 2006) New Revision: 3996 Added: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/DrlDumper.java labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/PackageDescrDumper.java labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/XmlDumper.java Log: JBRULES-240 XMLDump and DRLDump creators -Add xml and drl dumpers -Added common interface Added: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/DrlDumper.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/DrlDumper.java 2006-04-27 12:05:12 UTC (rev 3995) +++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/DrlDumper.java 2006-04-27 18:42:31 UTC (rev 3996) @@ -0,0 +1,319 @@ +package org.drools.lang; + +/* + * Author Jayaram C S + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.drools.lang.descr.*; + +import org.drools.util.ReflectiveVisitor; + +/** + * + * @author <a href="mailto:jay...@gm...">Author Jayaram C S</a> + * + */ +public class DrlDumper extends ReflectiveVisitor + implements + PackageDescrDumper { + + private StringBuffer drlDump; + private static final String eol = System.getProperty( "line.separator" ); + + public synchronized String dump(final PackageDescr packageDescr) { + this.drlDump = new StringBuffer(); + visitPackageDescr( packageDescr ); + return this.drlDump.toString(); + } + + public void visitAndDescr(final AndDescr descr) { + this.template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + this.template = processDescrList( descr.getDescrs() ); + } else { + this.template = ""; + } + } + + public void visitAttributeDescr(final AttributeDescr attributeDescr) { + this.template = new String(); + this.template = "\t " + attributeDescr.getName() + " " + attributeDescr.getValue() + this.eol; + } + + public void visitBoundVariableDescr(final BoundVariableDescr descr) { + this.template = new String(); + this.template = descr.getFieldName() + " " + descr.getEvaluator() + " " + descr.getIdentifier(); + } + + public void visitColumnDescr(final ColumnDescr descr) { + this.template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + if ( descr.getIdentifier() != null ) { + this.template = "\t\t" + descr.getIdentifier() + " : " + descr.getObjectType() + "( " + processColoumnConstraintList( descr.getDescrs() ) + ")"; + } else { + this.template = "\t\t" + descr.getObjectType() + "( " + processColoumnConstraintList( descr.getDescrs() ) + ")"; + } + } else { + if ( descr.getIdentifier() != null ) { + this.template = "\t\t" + descr.getIdentifier() + " : " + descr.getObjectType() + "( )"; + } else { + this.template = "\t\t" + descr.getObjectType() + "( )"; + } + } + + } + + public void visitEvalDescr(final EvalDescr descr) { + this.template = new String(); + this.template = "\t\teval ( " + descr.getText() + " )" + this.eol; + } + + public void visitExistsDescr(final ExistsDescr descr) { + this.template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + this.template = "\t\texists " + processDescrList( descr.getDescrs() ); + } else { + this.template = ""; + } + } + + public void visitFieldBindingDescr(final FieldBindingDescr descr) { + this.template = new String(); + this.template = descr.getIdentifier() + " : "; + } + + public void visitFunctionDescr(final FunctionDescr functionDescr) { + this.template = new String(); + final String parameterTemplate = processParameters( functionDescr.getParameterNames(), + functionDescr.getParameterTypes() ); + + this.template = "function " + functionDescr.getReturnType() + " " + functionDescr.getName() + "(" + parameterTemplate + "){" + + + functionDescr.getText() + this.eol + "}" + this.eol; + + } + + public void visitLiteralDescr(final LiteralDescr descr) { + this.template = new String(); + String text = descr.getText(); + try { + Integer.parseInt( text ); + } catch ( final NumberFormatException e ) { + text = "\"" + text + "\""; + } + + this.template = descr.getFieldName() + " " + descr.getEvaluator() + " " + text; + } + + public void visitNotDescr(final NotDescr descr) { + this.template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + this.template = "\t not " + processDescrList( descr.getDescrs() ); + } else { + this.template = ""; + } + + } + + public void visitOrDescr(final OrDescr descr) { + this.template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + this.template = processOrDescrList( descr.getDescrs() ); + } else { + this.template = " "; + } + } + + public void visitPackageDescr(final PackageDescr packageDescr) { + final String packageName = packageDescr.getName(); + final String xmlString = "package " + packageName + ";" + this.eol + this.eol; + + appendDrlDump( xmlString ); + if ( packageDescr.getImports() != null ) { + appendDrlDump( processImportsList( packageDescr.getImports() ) ); + } + if ( packageDescr.getGlobals() != null ) { + appendDrlDump( processGlobalsMap( packageDescr.getGlobals() ) ); + } + if ( packageDescr.getFunctions() != null ) { + appendDrlDump( processFunctionsList( packageDescr.getFunctions() ) ); + } + if ( packageDescr.getRules() != null ) { + appendDrlDump( processRules( packageDescr.getRules() ) ); + } + + } + + public void visitPredicateDescr(final PredicateDescr descr) { + this.template = new String(); + this.template = descr.getDeclaration() + ":" + descr.getFieldName() + " -> ( " + descr.getText() + " )"; + + } + + public void visitReturnValueDescr(final ReturnValueDescr descr) { + this.template = new String(); + this.template = descr.getFieldName() + " " + descr.getEvaluator() + " ( " + descr.getText() + ")"; + } + + public void visitQueryDescr(final QueryDescr descr) { + this.template = new String(); + this.template = "<query name=\"" + descr.getName() + "\">" + "<lhs>" + processDescrList( descr.getLhs().getDescrs() ) + "</lhs>" + "</query>"; + } + + private String template; + + private String processRules(final List rules) { + String ruleList = ""; + for ( final Iterator iterator = rules.iterator(); iterator.hasNext(); ) { + final RuleDescr ruleDescr = (RuleDescr) iterator.next(); + String rule = "rule \"" + ruleDescr.getName() + "\" " + this.eol; + final String attribute = processAttribute( ruleDescr.getAttributes() ); + String lhs = ""; + if ( ruleDescr.getLhs().getDescrs() != Collections.EMPTY_LIST ) { + lhs = "\t when" + this.eol + processDescrList( ruleDescr.getLhs().getDescrs() ) + this.eol; + } else { + + lhs = "\t when"; + } + + String rhs = ruleDescr.getConsequence(); + if ( rhs == null ) { + rhs = "\t then" + this.eol + "\t"; + } else { + rhs = "\t then" + this.eol + "\t\t" + ruleDescr.getConsequence(); + } + + rule += attribute; + rule += lhs; + rule += rhs; + rule += "end" + this.eol; + ruleList += rule; + } + + return ruleList + this.eol; + } + + private String processOrDescrList(final List descr) { + String descrString = ""; + for ( final Iterator iterator = descr.iterator(); iterator.hasNext(); ) { + visit( iterator.next() ); + descrString += this.template; + if ( descrString.endsWith( this.eol ) ) { + descrString = descrString.substring( 0, + descrString.indexOf( this.eol ) ); + } + descrString += " || "; + } + return descrString.substring( 0, + descrString.length() - 4 ); + } + + private String processColoumnConstraintList(final List descr) { + String descrString = ""; + for ( final Iterator iterator = descr.iterator(); iterator.hasNext(); ) { + + final Object temp = iterator.next(); + visit( temp ); + descrString += this.template; + if ( !(temp instanceof FieldBindingDescr) ) { + descrString += " , "; + } + + } + return descrString.substring( 0, + descrString.length() - 2 ); + } + + private String processDescrList(final List descr) { + String descrString = ""; + for ( final Iterator iterator = descr.iterator(); iterator.hasNext(); ) { + visit( iterator.next() ); + descrString += this.template; + descrString += this.eol; + } + return descrString; + } + + private String processFunctionsList(final List functions) { + String functionList = ""; + + for ( final Iterator iterator = functions.iterator(); iterator.hasNext(); ) { + visit( iterator.next() ); + functionList += this.template; + } + + return functionList + this.eol; + } + + private String processAttribute(final List attributes) { + + String attributeList = ""; + for ( final Iterator iterator = attributes.iterator(); iterator.hasNext(); ) { + final AttributeDescr attributeDescr = (AttributeDescr) iterator.next(); + visit( attributeDescr ); + attributeList += this.template; + } + return attributeList; + } + + private String processParameters(final List parameterNames, + final List parameterTypes) { + String paramList = ""; + int i = 0; + for ( final Iterator iterator = parameterNames.iterator(); iterator.hasNext(); i++ ) { + final String paramName = (String) iterator.next(); + final String paramType = (String) parameterTypes.get( i ); + final String paramTemplate = paramType + " " + paramName + ","; + paramList += paramTemplate; + } + paramList = paramList.substring( 0, + paramList.length() - 1 ); + return paramList; + } + + private String processGlobalsMap(final Map globals) { + String globalList = ""; + + for ( final Iterator iterator = globals.keySet().iterator(); iterator.hasNext(); ) { + final String key = (String) iterator.next(); + final String value = (String) globals.get( key ); + final String globalTemplate = "global " + value + " " + key + ";" + this.eol; + globalList += globalTemplate; + } + + return globalList + this.eol; + } + + private String processImportsList(final List imports) { + String importList = ""; + + for ( final Iterator iterator = imports.iterator(); iterator.hasNext(); ) { + final String importString = (String) iterator.next(); + final String importTemplate = "import " + importString + ";" + this.eol; + importList += importTemplate; + } + return importList + this.eol; + } + + private void appendDrlDump(final String temp) { + this.drlDump.append( temp ); + } + +} \ No newline at end of file Added: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/PackageDescrDumper.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/PackageDescrDumper.java 2006-04-27 12:05:12 UTC (rev 3995) +++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/descr/PackageDescrDumper.java 2006-04-27 18:42:31 UTC (rev 3996) @@ -0,0 +1,5 @@ +package org.drools.lang.descr; + +public interface PackageDescrDumper { + public String dump(final PackageDescr packageDescr); +} Added: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/XmlDumper.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/XmlDumper.java 2006-04-27 12:05:12 UTC (rev 3995) +++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/XmlDumper.java 2006-04-27 18:42:31 UTC (rev 3996) @@ -0,0 +1,270 @@ +package org.drools.xml; + +/* + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.drools.lang.descr.*; + +import org.drools.util.ReflectiveVisitor; + +/** + * + * @author <a href="mailto:jay...@gm...">Author Jayaram C S</a> + * + */ +public class XmlDumper extends ReflectiveVisitor + implements + PackageDescrDumper { + + private StringBuffer xmlDump; + private final static String eol = System.getProperty( "line.separator" ); + + public synchronized String dump(final PackageDescr packageDescr) { + this.xmlDump = new StringBuffer(); + visitPackageDescr( packageDescr ); + return this.xmlDump.toString(); + } + + public void visitAndDescr(AndDescr descr) { + template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + template = "<and>" + processDescrList( descr.getDescrs() ) + "</and>"; + } else { + template = "<and> </and>"; + } + } + + public void visitAttributeDescr(AttributeDescr attributeDescr) { + template = new String(); + template = "<rule-attribute name=\"" + attributeDescr.getName() + "\" value=\"" + attributeDescr.getValue() + "\" />" + eol; + } + + public void visitBoundVariableDescr(BoundVariableDescr descr) { + template = new String(); + template = "<bound-variable field-name=\"" + descr.getFieldName() + "\" evaluator=\"" + getEvaluator( descr.getEvaluator() ) + "\" identifier=\"" + descr.getIdentifier() + "\" />" + eol; + } + + public void visitColumnDescr(ColumnDescr descr) { + template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + if ( descr.getIdentifier() != null ) { + template = "<column identifier=\"" + descr.getIdentifier() + "\" object-type=\"" + descr.getObjectType() + "\" >" + processDescrList( descr.getDescrs() ) + "</column>" + eol; + } else { + template = "<column object-type=\"" + descr.getObjectType() + "\" >" + processDescrList( descr.getDescrs() ) + "</column>" + eol; + } + } else { + if ( descr.getIdentifier() != null ) { + template = "<column identifier=\"" + descr.getIdentifier() + "\" object-type=\"" + descr.getObjectType() + "\" > </column>" + eol; + } else { + template = "<column object-type=\"" + descr.getObjectType() + "\" > </column>" + eol; + } + } + + } + + public void visitEvalDescr(EvalDescr descr) { + template = new String(); + template = "<eval>" + descr.getText() + "</eval>" + eol; + } + + public void visitExistsDescr(ExistsDescr descr) { + template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + template = "<exists>" + processDescrList( descr.getDescrs() ) + "</exists>"; + } else { + template = "<exists> </exists>"; + } + } + + public void visitFieldBindingDescr(FieldBindingDescr descr) { + template = new String(); + template = "<field-binding field-name=\"" + descr.getFieldName() + "\" identifier=\"" + descr.getIdentifier() + "\" />" + eol; + } + + public void visitFunctionDescr(FunctionDescr functionDescr) { + template = new String(); + String parameterTemplate = processParameters( functionDescr.getParameterNames(), + functionDescr.getParameterTypes() ); + + template = "<function return-type=\"" + functionDescr.getReturnType() + "\" name=\"" + functionDescr.getName() + "\">" + eol + parameterTemplate + "<body>" + eol + functionDescr.getText() + eol + "</body>" + eol + "</function>" + eol; + } + + public void visitLiteralDescr(LiteralDescr descr) { + template = new String(); + template = "<literal field-name=\"" + descr.getFieldName() + "\" evaluator=\"" + getEvaluator( descr.getEvaluator() ) + "\" value=\"" + descr.getText() + "\" />" + eol; + } + + public void visitNotDescr(NotDescr descr) { + template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + template = "<not>" + processDescrList( descr.getDescrs() ) + "</not>"; + } else { + template = "<not> </not>"; + } + + } + + public void visitOrDescr(OrDescr descr) { + template = new String(); + if ( descr.getDescrs() != Collections.EMPTY_LIST ) { + template = "<or>" + processDescrList( descr.getDescrs() ) + "</or>"; + } else { + template = "<or> </or>"; + } + } + + public void visitPackageDescr(PackageDescr packageDescr) { + String packageName = packageDescr.getName(); + String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + eol + " <package name=\"" + packageName + "\" " + eol + "\txmlns=\"http://drools.org/drools-3.0\" " + eol + "\txmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\" " + eol + + "\txs:schemaLocation=\"http://drools.org/drools-3.0 drools-3.0.xsd\"> " + eol; + appendXmlDump( xmlString ); + appendXmlDump( processImportsList( packageDescr.getImports() ) ); + appendXmlDump( processGlobalsMap( packageDescr.getGlobals() ) ); + appendXmlDump( processFunctionsList( packageDescr.getFunctions() ) ); + appendXmlDump( processRules( packageDescr.getRules() ) ); + appendXmlDump( "</package>" ); + } + + public void visitPredicateDescr(PredicateDescr descr) { + template = new String(); + template = "<predicate field-name=\"" + descr.getFieldName() + "\" identifier=\"" + descr.getDeclaration() + "\" >" + descr.getText() + "</predicate>" + eol; + + } + + public void visitReturnValueDescr(ReturnValueDescr descr) { + template = new String(); + template = "<return-value field-name=\"" + descr.getFieldName() + "\" evaluator=\"" + getEvaluator( descr.getEvaluator() ) + "\" >" + descr.getText() + "</return-value>" + eol; + } + + public void visitQueryDescr(QueryDescr descr) { + template = new String(); + template = "<query name=\"" + descr.getName() + "\">" + "<lhs>" + processDescrList( descr.getLhs().getDescrs() ) + "</lhs>" + "</query>"; + } + + private String template; + + private String processRules(List rules) { + String ruleList = ""; + for ( Iterator iterator = rules.iterator(); iterator.hasNext(); ) { + RuleDescr ruleDescr = (RuleDescr) iterator.next(); + String rule = "<rule name=\"" + ruleDescr.getName() + "\">" + eol; + String attribute = processAttribute( ruleDescr.getAttributes() ); + String lhs = ""; + if ( ruleDescr.getLhs().getDescrs() != Collections.EMPTY_LIST ) { + lhs = "<lhs>" + processDescrList( ruleDescr.getLhs().getDescrs() ) + "</lhs>"; + } else { + + lhs = "<lhs> </lhs>"; + } + + String rhs = "<rhs>" + ruleDescr.getConsequence() + "</rhs>" + eol; + rule += attribute; + rule += lhs; + rule += rhs; + rule += "</rule>"; + ruleList += rule; + } + + return ruleList + eol; + } + + private String processDescrList(List descr) { + String descrString = ""; + for ( Iterator iterator = descr.iterator(); iterator.hasNext(); ) { + visit( iterator.next() ); + descrString += template; + descrString += eol; + } + return descrString + eol; + } + + private String processFunctionsList(List functions) { + String functionList = ""; + + for ( Iterator iterator = functions.iterator(); iterator.hasNext(); ) { + visit( iterator.next() ); + functionList += template; + } + + return functionList + eol; + } + + private String processAttribute(List attributes) { + + String attributeList = ""; + for ( Iterator iterator = attributes.iterator(); iterator.hasNext(); ) { + AttributeDescr attributeDescr = (AttributeDescr) iterator.next(); + visit( attributeDescr ); + attributeList += template; + } + return attributeList + eol; + } + + private String processParameters(List parameterNames, + List parameterTypes) { + String paramList = ""; + int i = 0; + for ( Iterator iterator = parameterNames.iterator(); iterator.hasNext(); i++ ) { + String paramName = (String) iterator.next(); + String paramType = (String) parameterTypes.get( i ); + String paramTemplate = "<parameter identifier=\"" + paramName + "\" type=\"" + paramType + "\" />" + eol; + paramList += paramTemplate; + } + + return paramList + eol; + } + + private String processGlobalsMap(Map globals) { + String globalList = ""; + for ( Iterator iterator = globals.keySet().iterator(); iterator.hasNext(); ) { + String key = (String) iterator.next(); + String value = (String) globals.get( key ); + String globalTemplate = "<global identifier=\"" + key + "\" type=\"" + value + "\" />" + eol; + globalList += globalTemplate; + } + + return globalList + eol; + } + + private String processImportsList(List imports) { + String importList = ""; + + for ( Iterator iterator = imports.iterator(); iterator.hasNext(); ) { + String importString = (String) iterator.next(); + String importTemplate = "<import name=\"" + importString + "\" /> " + eol; + importList += importTemplate; + } + return importList + eol; + } + + private void appendXmlDump(String temp) { + xmlDump.append( temp ); + } + + private String getEvaluator(String eval) { + + eval = eval.replaceAll( "<", + "<" ); + eval = eval.replaceAll( ">", + ">" ); + return eval; + } +} \ No newline at end of file |