[jboss-svn-commits] JBL Code SVN: r21353 - in labs/jbossrules/trunk/drools-compiler/src: test/java/org/drools/compiler and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Aug 5 01:54:30 EDT 2008
Author: michael.neale at jboss.com
Date: 2008-08-05 01:54:30 -0400 (Tue, 05 Aug 2008)
New Revision: 21353
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderTest.java
Log:
JBRULES-1709 allow packages to be single namespace
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2008-08-05 04:07:53 UTC (rev 21352)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2008-08-05 05:54:30 UTC (rev 21353)
@@ -73,7 +73,7 @@
* This is the main compiler class for parsing and compiling rules and
* assembling or merging them into a binary Package instance. This can be done
* by merging into existing binary packages, or totally from source.
- *
+ *
* If you are using the Java dialect the JavaDialectConfiguration will attempt
* to validate that the specified compiler is in the classpath, using
* ClassLoader.loasClass(String). If you intented to just Janino sa the compiler
@@ -87,7 +87,7 @@
private Map<String, PackageRegistry> pkgRegistryMap;
- private List results;
+ private List<DroolsError> results;
private PackageBuilderConfiguration configuration;
@@ -134,12 +134,12 @@
/**
* Pass a specific configuration for the PackageBuilder
- *
+ *
* PackageBuilderConfiguration is not thread safe and it also contains
* state. Once it is created and used in one or more PackageBuilders it
* should be considered immutable. Do not modify its properties while it is
* being used by a PackageBuilder.
- *
+ *
* @param configuration
*/
public PackageBuilder(final PackageBuilderConfiguration configuration) {
@@ -150,7 +150,7 @@
// /**
// * This allows you to pass in a pre existing package, and a configuration
// * (for instance to set the classloader).
- // *
+ // *
// * @param pkg
// * A pre existing package (can be null if none exists)
// * @param configuration
@@ -217,7 +217,7 @@
this.configuration = configuration;
// FIXME, we need to get drools to support "default" namespace.
- //this.defaultNamespace = pkg.getName();
+ //this.defaultNamespace = pkg.getName();
this.defaultDialect = this.configuration.getDefaultDialect();
this.pkgRegistryMap = new HashMap<String, PackageRegistry>();
@@ -229,7 +229,7 @@
/**
* Load a rule package from DRL source.
- *
+ *
* @param reader
* @throws DroolsParserException
* @throws IOException
@@ -246,7 +246,7 @@
/**
* Load a rule package from XML source.
- *
+ *
* @param reader
* @throws DroolsParserException
* @throws IOException
@@ -267,7 +267,7 @@
/**
* Load a rule package from DRL source using the supplied DSL configuration.
- *
+ *
* @param source
* The source of the rules.
* @param dsl
@@ -349,7 +349,12 @@
if ( !isEmpty( packageDescr.getNamespace() ) ) {
// use the default namespace
- this.defaultNamespace = packageDescr.getNamespace();
+ if (checkNamespace(packageDescr.getNamespace())) {
+ this.defaultNamespace = packageDescr.getNamespace();
+ } else {
+ //force the default.
+ packageDescr.setNamespace(this.defaultNamespace);
+ }
} else {
// packagedescr defines a new default namespace
packageDescr.setNamespace( this.defaultNamespace );
@@ -434,13 +439,23 @@
}
}
- public boolean isEmpty(String string) {
+ /**
+ * This checks to see if it should all be in the one namespace.
+ */
+ private boolean checkNamespace(String newName) {
+ if (this.configuration == null) return true;
+ if (this.defaultNamespace == null) return true;
+ if (this.defaultNamespace.equals(newName)) return true;
+ return this.configuration.isAllowMultipleNamespaces();
+ }
+
+ public boolean isEmpty(String string) {
return (string == null || string.trim().length() == 0);
}
-
+
public void updateResults() {
- // some of the rules and functions may have been redefined
- this.results = getResults( this.results );
+ // some of the rules and functions may have been redefined
+ this.results = getResults( this.results );
}
public void compileAll() {
@@ -507,7 +522,7 @@
PackageRegistry pkgRegistry = new PackageRegistry( this,
pkg );
-
+
// add default import for this namespace
pkgRegistry.addImport( packageDescr.getNamespace() + ".*" );
@@ -575,7 +590,7 @@
for ( TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations() ) {
// make sure namespace is set on components
if ( isEmpty( typeDescr.getNamespace() ) ) {
- // use the default namespace
+ // use the default namespace
typeDescr.setNamespace( defaultRegistry.getPackage().getName() );
pkgRegistry = defaultRegistry;
} else {
@@ -655,8 +670,8 @@
}
/**
- *
- * @param registry
+ *
+ * @param registry
* @throws SecurityException
* @throws IllegalArgumentException
* @throws InstantiationException
@@ -698,7 +713,7 @@
/**
* Generates a bean, and adds it to the composite class loader that
* everything is using.
- *
+ *
*/
private void generateDeclaredBean(TypeDeclarationDescr typeDescr,
TypeDeclaration type,
@@ -808,7 +823,7 @@
* can report on by calling getErrors or printErrors. If you try to
* add an invalid package (or rule) to a RuleBase, you will get a
* runtime exception.
- *
+ *
* Compiled packages are serializable.
*/
public Package getPackage() {
@@ -845,7 +860,7 @@
/**
* Return the PackageBuilderConfiguration for this PackageBuilder session
- *
+ *
* @return The PackageBuilderConfiguration
*/
public PackageBuilderConfiguration getPackageBuilderConfiguration() {
@@ -863,7 +878,7 @@
/**
* Return the ClassFieldExtractorCache, this should only be used internally,
* and is subject to change
- *
+ *
* @return the ClsasFieldExtractorCache
*/
public ClassFieldAccessorCache getClassFieldExtractorCache() {
@@ -899,7 +914,7 @@
public String getDefaultNamespace() {
return this.defaultNamespace;
}
-
+
public String getDefaultDialect() {
return this.defaultDialect;
}
@@ -926,7 +941,7 @@
* report a compile error of its type, should it happen. This is needed, as
* the compiling is done as one hit at the end, and we need to be able to
* work out what rule/ast element caused the error.
- *
+ *
* An error handler it created for each class task that is queued to be
* compiled. This doesn't mean an error has occurred, it just means it *may*
* occur in the future and we need to be able to map it back to the AST
@@ -950,7 +965,7 @@
}
/**
- *
+ *
* @return A DroolsError object populated as appropriate, should the
* unthinkable happen and this need to be reported.
*/
@@ -1061,10 +1076,10 @@
}
}
-
- public static class SrcErrorHandler extends ErrorHandler {
+ public static class SrcErrorHandler extends ErrorHandler {
+
public SrcErrorHandler(final String message) {
this.message = message;
}
@@ -1085,10 +1100,10 @@
this.object = object;
this.message = message;
}
-
+
public Object getObject() {
return this.object;
- }
+ }
public int[] getErrorLines() {
return this.errorLines;
@@ -1097,7 +1112,7 @@
public String getMessage() {
return this.message;
}
-
+
public String toString() {
final StringBuffer buf = new StringBuffer();
buf.append( this.message );
@@ -1116,7 +1131,7 @@
return buf.toString();
}
- }
+ }
private String ucFirst(final String name) {
return name.toUpperCase().charAt( 0 ) + name.substring( 1 );
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java 2008-08-05 04:07:53 UTC (rev 21352)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java 2008-08-05 05:54:30 UTC (rev 21353)
@@ -2,13 +2,13 @@
/*
* Copyright 2005 JBoss Inc
- *
+ *
* 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.
@@ -46,26 +46,26 @@
import org.mvel.MVEL;
/**
- * This class configures the package compiler.
+ * This class configures the package compiler.
* Dialects and their DialectConfigurations are handled by the DialectRegistry
* Normally you will not need to look at this class, unless you want to override the defaults.
- *
- * This class is not thread safe and it also contains state. Once it is created and used
- * in one or more PackageBuilders it should be considered immutable. Do not modify its
+ *
+ * This class is not thread safe and it also contains state. Once it is created and used
+ * in one or more PackageBuilders it should be considered immutable. Do not modify its
* properties while it is being used by a PackageBuilder.
- *
+ *
* drools.dialect.default = <String>
* drools.accumulate.function.<function name> = <qualified class>
* drools.evaluator.<ident> = <qualified class>
* drools.dump.dir = <String>
- *
+ *
* default dialect is java.
* Available preconfigured Accumulate functions are:
* drools.accumulate.function.average = org.drools.base.accumulators.AverageAccumulateFunction
* drools.accumulate.function.max = org.drools.base.accumulators.MaxAccumulateFunction
* drools.accumulate.function.min = org.drools.base.accumulators.MinAccumulateFunction
* drools.accumulate.function.count = org.drools.base.accumulators.CountAccumulateFunction
- * drools.accumulate.function.sum = org.drools.base.accumulators.SumAccumulateFunction
+ * drools.accumulate.function.sum = org.drools.base.accumulators.SumAccumulateFunction
*/
public class PackageBuilderConfiguration {
@@ -91,7 +91,22 @@
private File dumpDirectory;
+ private boolean allowMultipleNamespaces = true;
+
+ public boolean isAllowMultipleNamespaces() {
+ return allowMultipleNamespaces;
+ }
+
+
/**
+ * By default multiple namespaces are allowed. If you set this to "false" it will
+ * make it all happen in the "default" namespace (the first namespace you define).
+ */
+ public void setAllowMultipleNamespaces(boolean allowMultipleNamespaces) {
+ this.allowMultipleNamespaces = allowMultipleNamespaces;
+ }
+
+ /**
* Constructor that sets the parent class loader for the package being built/compiled
* @param classLoader
*/
@@ -458,10 +473,10 @@
* evaluator ID will be added to the registry. In case there exists
* an implementation for that ID already, the new implementation will
* replace the previous one.
- *
+ *
* @param className the name of the class for the implementation definition.
* The class must implement the EvaluatorDefinition interface.
- *
+ *
*/
public void addEvaluatorDefinition(String className) {
this.evaluatorRegistry.addEvaluatorDefinition( className );
@@ -471,9 +486,9 @@
* Adds an evaluator definition class to the registry. In case there exists
* an implementation for that evaluator ID already, the new implementation will
* replace the previous one.
- *
+ *
* @param def the evaluator definition to be added.
- *
+ *
*/
public void addEvaluatorDefinition(EvaluatorDefinition def) {
this.evaluatorRegistry.addEvaluatorDefinition( def );
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderTest.java 2008-08-05 04:07:53 UTC (rev 21352)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderTest.java 2008-08-05 05:54:30 UTC (rev 21353)
@@ -984,8 +984,8 @@
PackageDescr pkgDescr = new PackageDescr( "org.test" );
builder.addPackage( pkgDescr );
DialectCompiletimeRegistry reg = builder.getPackageRegistry( pkgDescr.getName() ).getDialectCompiletimeRegistry();
-
-
+
+
final Field dialectField = builder.getClass().getDeclaredField( "defaultDialect" );
dialectField.setAccessible( true );
String dialectName = (String) dialectField.get( builder );
@@ -1008,7 +1008,7 @@
dialectName = (String) dialectField.get( builder );
reg = builder.getPackageRegistry( pkgDescr.getName() ).getDialectCompiletimeRegistry();
- dialect = reg.getDialect( dialectName );
+ dialect = reg.getDialect( dialectName );
compiler = (JavaCompiler) compilerField.get( dialect );
assertSame( JaninoJavaCompiler.class,
compiler.getClass() );
@@ -1022,7 +1022,7 @@
dialectName = (String) dialectField.get( builder );
reg = builder.getPackageRegistry( pkgDescr.getName() ).getDialectCompiletimeRegistry();
- dialect = reg.getDialect( dialectName );
+ dialect = reg.getDialect( dialectName );
compiler = (JavaCompiler) compilerField.get( dialect );
assertSame( EclipseJavaCompiler.class,
compiler.getClass() );
@@ -1306,6 +1306,32 @@
assertFalse( bldr.hasErrors() );
}
+ public void testSinglePackage() throws Exception {
+ PackageBuilderConfiguration cfg = new PackageBuilderConfiguration();
+ cfg.setAllowMultipleNamespaces(false);
+ PackageBuilder bldr = new PackageBuilder( cfg );
+ bldr.addPackageFromDrl(new StringReader("package whee\n import org.drools.Cheese"));
+ assertFalse(bldr.hasErrors());
+ bldr.addPackageFromDrl(new StringReader("package whee\n import org.drools.Person"));
+ assertFalse(bldr.hasErrors());
+ bldr.addPackageFromDrl(new StringReader("package whee2\n import org.drools.Person"));
+ assertFalse(bldr.hasErrors());
+
+ assertEquals(1, bldr.getPackages().length);
+
+ cfg = new PackageBuilderConfiguration();
+ assertEquals(true, cfg.isAllowMultipleNamespaces());
+ bldr = new PackageBuilder( cfg );
+ bldr.addPackageFromDrl(new StringReader("package whee\n import org.drools.Cheese"));
+ assertFalse(bldr.hasErrors());
+ bldr.addPackageFromDrl(new StringReader("import org.drools.Person"));
+ assertFalse(bldr.hasErrors());
+ bldr.addPackageFromDrl(new StringReader("package whee2\n import org.drools.Person"));
+ assertFalse(bldr.hasErrors());
+
+ assertEquals(2, bldr.getPackages().length);
+ }
+
public void testTimeWindowBehavior() throws Exception {
final PackageBuilder builder = new PackageBuilder();
More information about the jboss-svn-commits
mailing list