[jboss-svn-commits] JBL Code SVN: r12588 - in labs/jbossrules/trunk/experimental: drools-update and 22 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jun 14 16:41:14 EDT 2007
Author: tirelli
Date: 2007-06-14 16:41:14 -0400 (Thu, 14 Jun 2007)
New Revision: 12588
Added:
labs/jbossrules/trunk/experimental/drools-update/
labs/jbossrules/trunk/experimental/drools-update/.classpath
labs/jbossrules/trunk/experimental/drools-update/.project
labs/jbossrules/trunk/experimental/drools-update/pom.xml
labs/jbossrules/trunk/experimental/drools-update/src/
labs/jbossrules/trunk/experimental/drools-update/src/main/
labs/jbossrules/trunk/experimental/drools-update/src/main/java/
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/UpdateTool.java
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/ActionsRegistry.java
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/DRLUpdate.java
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/DescriptorsVisitor.java
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/UpdateAction.java
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/actions/
labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/actions/MemoryActionsFix.java
labs/jbossrules/trunk/experimental/drools-update/src/main/resources/
labs/jbossrules/trunk/experimental/drools-update/src/test/
labs/jbossrules/trunk/experimental/drools-update/src/test/java/
labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/
labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/drools/
labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/drools/tools/
labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/drools/tools/update/
labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/drools/tools/update/drl/
labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/drools/tools/update/drl/DRLUpdateTest.java
labs/jbossrules/trunk/experimental/drools-update/src/test/resources/
labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/
labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/
labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/
labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/
labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/
labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v3.drl
labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v4.drl
Log:
JBRULES-929: adding migration tool for 3.0.x DRLs to 4.0 DRLs
Added: labs/jbossrules/trunk/experimental/drools-update/.classpath
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/.classpath (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/.classpath 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,27 @@
+<classpath>
+ <classpathentry kind="src" path="src/main/java"/>
+ <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
+ <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
+ <classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
+ <classpathentry kind="output" path="target/classes"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="var" path="M2_REPO/antlr/antlr/2.7.7/antlr-2.7.7.jar"/>
+ <classpathentry kind="var" path="M2_REPO/xpp3/xpp3/1.1.3.4.O/xpp3-1.1.3.4.O.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/drools/drools-compiler/4.0.0.SNAPSHOT/drools-compiler-4.0.0.SNAPSHOT.jar"/>
+ <classpathentry kind="var" path="M2_REPO/xstream/xstream/1.1.3/xstream-1.1.3.jar"/>
+ <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/drools/drools-core/4.0.0.SNAPSHOT/drools-core-4.0.0.SNAPSHOT.jar"/>
+ <classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/1.0/commons-lang-1.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/commons-cli/commons-cli/1.0/commons-cli-1.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.0/commons-logging-1.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/eclipse/jdt/core/3.2.1.v_677_R32x/core-3.2.1.v_677_R32x.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/apache/ant/ant-launcher/1.7.0/ant-launcher-1.7.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/commons-io/commons-io/1.3.1/commons-io-1.3.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.4.0/xercesImpl-2.4.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/antlr/stringtemplate/3.0/stringtemplate-3.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/antlr/antlr/3.0/antlr-3.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/janino/janino/2.5.7/janino-2.5.7.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/apache/ant/ant/1.7.0/ant-1.7.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/mvel/mvel14/1.2beta25/mvel14-1.2beta25.jar"/>
+</classpath>
\ No newline at end of file
Added: labs/jbossrules/trunk/experimental/drools-update/.project
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/.project (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/.project 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,13 @@
+<projectDescription>
+ <name>drools-update</name>
+ <comment>A rule production system</comment>
+ <projects/>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
\ No newline at end of file
Added: labs/jbossrules/trunk/experimental/drools-update/pom.xml
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/pom.xml (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/pom.xml 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>drools</artifactId>
+ <groupId>org.drools</groupId>
+ <version>4.0.0.SNAPSHOT</version>
+ </parent>
+
+ <artifactId>drools-update</artifactId>
+ <packaging>jar</packaging>
+ <name>Drools :: Update Tool</name>
+
+ <repositories>
+ <repository>
+ <id>basedir</id>
+ <url>file://${basedir}/../../m2_repo</url>
+ </repository>
+ </repositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-core</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-compiler</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.3.1</version>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-cli</groupId>
+ <artifactId>commons-cli</artifactId>
+ <version>1.0</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.ant</groupId>
+ <artifactId>ant</artifactId>
+ <version>1.7.0</version>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <includes>
+ <include>**/*Test.java</include>
+ </includes>
+ <excludes>
+ </excludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+
+</project>
Added: labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/UpdateTool.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/UpdateTool.java (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/UpdateTool.java 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2007 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jun 13, 2007
+ */
+package org.drools.tools.update;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.util.Iterator;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.FileSet;
+import org.apache.tools.ant.types.resources.FileResource;
+import org.drools.compiler.DroolsParserException;
+import org.drools.tools.update.drl.DRLUpdate;
+
+/**
+ * An update tool to migrate source code from Drools 3.0.x to
+ * Drools 4.0.x
+ *
+ * @author etirelli
+ */
+public class UpdateTool {
+
+ public static void main( String args[] ) {
+ // create the command line parser
+ CommandLineParser parser = new PosixParser();
+ Options options = createOptions();
+
+ try {
+ // parse the command line arguments
+ CommandLine line = parser.parse( options, args, true );
+
+ if( line.hasOption( "f" ) ) {
+ String fileMask = line.getOptionValue( "f" );
+ String sufix = ".updated";
+ String dir = ".";
+ if( line.hasOption( "s" ) ) {
+ sufix = line.getOptionValue( "s" );
+ }
+ if( line.hasOption( "d" ) ) {
+ dir = line.getOptionValue( "d" );
+ }
+ printHeader( dir, fileMask, sufix );
+ processFiles( dir, fileMask, sufix );
+ } else {
+ // print the help
+ printHelp( options);
+ }
+
+ }
+ catch( ParseException exp ) {
+ System.err.println("Error parsing command line arguments: "+exp.getMessage());
+ // print the help
+ printHelp( options);
+ }
+
+ }
+
+ private static Options createOptions() {
+ // create the Options
+ Options options = new Options();
+ options.addOption( "d", "dir", true, "source directory" );
+ options.addOption( "f", "files", true, "pattern for the files to be updated" );
+ options.addOption( "s", "sufix", true, "the sufix to be added to all updated files" );
+ options.addOption( "h", "help", false, "list the usage help" );
+
+ return options;
+ }
+
+ private static void printHelp( Options options ) {
+ HelpFormatter formatter = new HelpFormatter();
+ String commandLine = "org.drools.tools.update.UpdateTool -f <filemask> [-d <basedir>] [-s <sufix>]";
+ formatter.printHelp( commandLine, options );
+ }
+
+ private static void printHeader( String dir, String fileMask, String sufix ) {
+ System.out.println("***************************************");
+ System.out.println("* Drools Update Tool *");
+ System.out.println("***************************************\n");
+ System.out.println("Basedir : "+dir);
+ System.out.println("File Mask : "+fileMask);
+ System.out.println("Sufix : "+sufix+"\n");
+ System.out.println("***************************************\n");
+ }
+
+ private static void processFiles( String dir, String fileMask, String sufix ) {
+ DRLUpdate updater = new DRLUpdate();
+ File curdir = new File( dir );
+ Project project = new Project();
+ FileSet fileset = new FileSet();
+ fileset.setDir( curdir );
+ fileset.setIncludes( fileMask );
+ fileset.setProject( project );
+
+ for( Iterator it = fileset.iterator(); it.hasNext(); ) {
+ Reader reader = null;
+ Writer writer = null;
+ try {
+ FileResource inputFile = (FileResource) it.next();
+ reader = new InputStreamReader( inputFile.getInputStream() );
+ String outFileName = inputFile.getName()+sufix;
+ writer = new PrintWriter( new File( outFileName ) );
+
+ System.out.println("Updating "+inputFile.getName()+" to "+outFileName);
+ updater.updateDrl( reader, writer );
+
+ } catch ( IOException e ) {
+ e.printStackTrace();
+ } catch ( DroolsParserException e ) {
+ e.printStackTrace();
+ } finally {
+ try { if( reader != null ) reader.close(); } catch(Exception e) {}
+ try { if( writer != null ) writer.close(); } catch(Exception e) {}
+ }
+ }
+
+ }
+
+}
Added: labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/ActionsRegistry.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/ActionsRegistry.java (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/ActionsRegistry.java 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2006 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jun 13, 2007
+ */
+package org.drools.tools.update.drl;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A registry class for all actions
+ *
+ * @author etirelli
+ */
+public class ActionsRegistry {
+
+ private Map<Class, List> actionMap;
+
+ public ActionsRegistry() {
+ this.actionMap = new HashMap<Class, List>();
+ }
+
+ public void addAction( Class target, UpdateAction action ) {
+ List actions = this.actionMap.get( target );
+ if( actions == null ) {
+ actions = new LinkedList<UpdateAction>();
+ this.actionMap.put( target, actions );
+ }
+ actions.add( action );
+ }
+
+ public List<UpdateAction> getActionsForClass( Class target ) {
+ List actions = this.actionMap.get( target );
+ if( actions == null ) {
+ actions = Collections.EMPTY_LIST;
+ }
+ return actions;
+ }
+
+}
Added: labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/DRLUpdate.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/DRLUpdate.java (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/DRLUpdate.java 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2006 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jun 13, 2007
+ */
+package org.drools.tools.update.drl;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
+
+import org.drools.compiler.DrlParser;
+import org.drools.compiler.DroolsParserException;
+import org.drools.lang.DrlDumper;
+import org.drools.lang.descr.PackageDescr;
+import org.drools.lang.descr.RuleDescr;
+import org.drools.tools.update.drl.actions.MemoryActionsFix;
+
+/**
+ * A class to update DRL source code from version
+ * 3.0 to version 4.0
+ *
+ * @author etirelli
+ */
+public class DRLUpdate {
+
+ ActionsRegistry actionsRegistry = new ActionsRegistry();
+
+ public DRLUpdate() {
+ this.actionsRegistry.addAction( RuleDescr.class, new MemoryActionsFix() );
+ }
+
+ public void updateDrl( Reader source, Writer target ) throws DroolsParserException, IOException {
+ DrlParser parser = new DrlParser();
+ PackageDescr pkg = parser.parse( source );
+ DescriptorsVisitor visitor = new DescriptorsVisitor( this.actionsRegistry );
+
+ visitor.visit( pkg );
+
+ DrlDumper dumper = new DrlDumper();
+ String drl = dumper.dump( pkg );
+ target.write( drl );
+
+ }
+
+}
Added: labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/DescriptorsVisitor.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/DescriptorsVisitor.java (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/DescriptorsVisitor.java 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2006 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jun 13, 2007
+ */
+package org.drools.tools.update.drl;
+
+import java.util.Iterator;
+
+import org.drools.lang.descr.BaseDescr;
+import org.drools.lang.descr.PackageDescr;
+import org.drools.lang.descr.RuleDescr;
+import org.drools.util.ReflectiveVisitor;
+
+/**
+ * A visitor for a package descriptor structure
+ *
+ * @author etirelli
+ */
+public class DescriptorsVisitor extends ReflectiveVisitor {
+
+ ActionsRegistry actionsRegistry = null;
+
+ public DescriptorsVisitor( ActionsRegistry actionsRegistry ) {
+ this.actionsRegistry = actionsRegistry;
+ }
+
+ public void visitPackageDescr(final PackageDescr packageDescr) {
+ this.executeActions( packageDescr );
+ for ( final Iterator iterator = packageDescr.getRules().iterator(); iterator.hasNext(); ) {
+ this.visit( iterator.next() );
+ }
+ }
+
+ public void visitRuleDescr(final RuleDescr descr) {
+ this.executeActions( descr );
+ }
+
+ private void executeActions( final BaseDescr descr ) {
+ for( UpdateAction action : this.actionsRegistry.getActionsForClass( descr.getClass() ) ) {
+ action.update( descr );
+ }
+ }
+
+}
Added: labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/UpdateAction.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/UpdateAction.java (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/UpdateAction.java 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2006 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jun 13, 2007
+ */
+package org.drools.tools.update.drl;
+
+import org.drools.lang.descr.BaseDescr;
+
+/**
+ * An interface for all update actions
+ *
+ * @author etirelli
+ */
+public interface UpdateAction {
+
+ public void update( BaseDescr descr );
+
+}
Added: labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/actions/MemoryActionsFix.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/actions/MemoryActionsFix.java (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/main/java/org/drools/tools/update/drl/actions/MemoryActionsFix.java 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2006 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jun 13, 2007
+ */
+package org.drools.tools.update.drl.actions;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.drools.lang.descr.BaseDescr;
+import org.drools.lang.descr.RuleDescr;
+import org.drools.tools.update.drl.UpdateAction;
+
+/**
+ * @author etirelli
+ *
+ */
+public class MemoryActionsFix
+ implements
+ UpdateAction {
+
+ static Pattern MODIFY = Pattern.compile( "(.*\\b)modify(\\s*\\(([^)]+)\\)(\\s*;.*))",
+ Pattern.DOTALL );
+ static Pattern ASSERT = Pattern.compile( "(.*\\b)assert(\\s*\\((.*)\\)(\\s*;.*))",
+ Pattern.DOTALL );
+ static Pattern ASSERT_LOGICAL = Pattern.compile( "(.*\\b)assertLogical(\\s*\\((.*)\\)(\\s*;.*))",
+ Pattern.DOTALL );
+
+
+ public void update(BaseDescr descr) {
+ RuleDescr rule = (RuleDescr) descr;
+ String consequence = (String) rule.getConsequence();
+
+ Matcher m = MODIFY.matcher( consequence );
+ consequence = m.replaceAll( "$1update$2" );
+
+ m = ASSERT.matcher( consequence );
+ consequence = m.replaceAll( "$1insert$2" );
+
+ m = ASSERT_LOGICAL.matcher( consequence );
+ consequence = m.replaceAll( "$1insertLogical$2" );
+
+ rule.setConsequence( consequence );
+ }
+
+}
Added: labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/drools/tools/update/drl/DRLUpdateTest.java
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/drools/tools/update/drl/DRLUpdateTest.java (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/test/java/org/drools/tools/update/drl/DRLUpdateTest.java 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2006 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jun 13, 2007
+ */
+package org.drools.tools.update.drl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.Writer;
+
+import junit.framework.TestCase;
+
+import org.drools.compiler.DroolsParserException;
+
+/**
+ * @author etirelli
+ *
+ */
+public class DRLUpdateTest extends TestCase {
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Test method for {@link org.drools.tools.update.drl.DRLUpdate#updateDrl(java.io.Reader, java.io.Writer)}.
+ * @throws DroolsParserException
+ * @throws IOException
+ */
+ public void testUpdateDrl() throws DroolsParserException, IOException {
+ Reader reader = new InputStreamReader( this.getClass().getResourceAsStream( "test_assert_modify_replacement_v3.drl" ));
+ ByteArrayOutputStream result = new ByteArrayOutputStream();
+ Writer writer = new PrintWriter( result );
+
+ DRLUpdate updater = new DRLUpdate();
+
+ updater.updateDrl( reader, writer );
+ reader.close();
+ writer.close();
+
+ reader = new InputStreamReader( this.getClass().getResourceAsStream( "test_assert_modify_replacement_v4.drl" ));
+ ByteArrayOutputStream expected = new ByteArrayOutputStream();
+ writer = new PrintWriter( expected );
+ char[] bytebuf = new char[1024];
+ int r = 0;
+ while( ( r = reader.read( bytebuf )) > 0 ) {
+ writer.write( bytebuf, 0, r );
+ }
+ writer.close();
+ reader.close();
+
+ assertEqualsIgnoreWhiteSpaces( expected.toString(), result.toString() );
+ }
+
+ public void assertEqualsIgnoreWhiteSpaces( String expected, String result ) {
+ expected = expected.replaceAll( "\\s+", "" );
+ result = result.replaceAll( "\\s+", "" );
+
+// System.out.println("["+expected+"]");
+// System.out.println("["+result+"]");
+ assertEquals( expected, result );
+ }
+
+}
Added: labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v3.drl
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v3.drl (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v3.drl 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,26 @@
+package org.drools.test;
+
+import org.drools.Cheese;
+
+rule "test1"
+ salience 10
+ no-loop true
+ when
+ cheese : Cheese();
+ then
+ retract(cheese);
+ assert(cheese);
+end;
+
+
+rule "test2"
+ salience 0
+ no-loop true
+ when
+ cheese : Cheese();
+ then
+ modify(cheese);
+ retract(cheese);
+end;
+
+
Property changes on: labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v3.drl
___________________________________________________________________
Name: svn:executable
+ *
Added: labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v4.drl
===================================================================
--- labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v4.drl (rev 0)
+++ labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v4.drl 2007-06-14 20:41:14 UTC (rev 12588)
@@ -0,0 +1,26 @@
+package org.drools.test;
+
+import org.drools.Cheese;
+
+rule "test1"
+ salience 10
+ no-loop true
+ when
+ cheese : Cheese()
+ then
+ retract(cheese);
+ insert(cheese);
+end
+
+
+rule "test2"
+ salience 0
+ no-loop true
+ when
+ cheese : Cheese()
+ then
+ update(cheese);
+ retract(cheese);
+end
+
+
Property changes on: labs/jbossrules/trunk/experimental/drools-update/src/test/resources/org/drools/tools/update/drl/test_assert_modify_replacement_v4.drl
___________________________________________________________________
Name: svn:executable
+ *
More information about the jboss-svn-commits
mailing list