[jboss-svn-commits] JBL Code SVN: r24233 - in labs/jbossrules/trunk: drools-api/src/main/java/org/drools/agent and 13 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Dec 4 05:40:13 EST 2008
Author: mark.proctor at jboss.com
Date: 2008-12-04 05:40:12 -0500 (Thu, 04 Dec 2008)
New Revision: 24233
Added:
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeBaseChangeSet.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeNotifier.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/InternalResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/BaseResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeBaseChangeSetImpl.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeComposition.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeResource.java
Removed:
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeManager.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeNotifier.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceModifiedEvent.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeComposition.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceModifiedEventImpl.java
Modified:
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/agent/KnowledgeAgentFactory.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/agent/KnowledgeAgentProvider.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeListener.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/Resource.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeMonitor.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceFactory.java
labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceProvider.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/XmlCompositionReader.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/CompositionHandler.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/DecisionTableConfigurationHandler.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/ResourceHandler.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/xml/composition/CompositionTest.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/impl/KnowledgeAgentImpl.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/impl/KnowledgeAgentProviderImpl.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ByteArrayResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ClassPathResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/EncodedResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/FileSystemResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/InputStreamResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ReaderResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceChangeNotifierImpl.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceChangeScannerImpl.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceProviderImpl.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/UrlResource.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/StringUtils.java
labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/CompositionTest.java
Log:
JBRULES-1885 Update KnowledgeAgent to use new Resource api
JBRULES-1874 Create Resource Framework
Added: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeBaseChangeSet.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeBaseChangeSet.java (rev 0)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/KnowledgeBaseChangeSet.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -0,0 +1,19 @@
+package org.drools;
+
+import java.util.Collection;
+
+import org.drools.definition.KnowledgeDefinition;
+import org.drools.io.Resource;
+
+public interface KnowledgeBaseChangeSet {
+ public Collection<Resource> getResourcesRemoved();
+
+
+ public Collection<Resource> getResourcesAdded();
+
+ public Collection<Resource> getResourcesModified();
+
+
+ public Collection<KnowledgeDefinition> getKnowledgeDefinitionsRemoved();
+
+}
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/agent/KnowledgeAgentFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/agent/KnowledgeAgentFactory.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/agent/KnowledgeAgentFactory.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -9,6 +9,14 @@
public class KnowledgeAgentFactory {
private static KnowledgeAgentProvider provider;
+
+ public static KnowledgeAgentConfiguration newKnowledgeAgentConfiguration() {
+ return getKnowledgeAgentProvider().newKnowledgeAgentConfiguration();
+ }
+
+ public static KnowledgeAgentConfiguration newKnowledgeAgentConfiguration(Properties properties) {
+ return getKnowledgeAgentProvider().newKnowledgeAgentConfiguration( properties );
+ }
public static KnowledgeAgent newKnowledgeAgent(String name,
KnowledgeBase kbase) {
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/agent/KnowledgeAgentProvider.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/agent/KnowledgeAgentProvider.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/agent/KnowledgeAgentProvider.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -5,6 +5,10 @@
import org.drools.KnowledgeBase;
public interface KnowledgeAgentProvider {
+ KnowledgeAgentConfiguration newKnowledgeAgentConfiguration();
+
+ KnowledgeAgentConfiguration newKnowledgeAgentConfiguration(Properties properties);
+
KnowledgeAgent newKnowledgeAgent(String name,
KnowledgeBase kbase);
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeListener.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeListener.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeListener.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -3,10 +3,8 @@
*/
package org.drools.event.io;
+import org.drools.KnowledgeBaseChangeSet;
-
public interface ResourceChangeListener {
- public void resourceAdded(ResourceModifiedEvent event);
- public void resourceModified(ResourceModifiedEvent event);
- public void resourceRemoved(ResourceModifiedEvent event);
+ void resourceChanged(KnowledgeBaseChangeSet changeSet);
}
\ No newline at end of file
Deleted: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeManager.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeManager.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeManager.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,21 +0,0 @@
-package org.drools.event.io;
-
-import java.util.Collection;
-
-import org.drools.definition.KnowledgeDefinition;
-import org.drools.io.Resource;
-import org.drools.io.ResourceChangeMonitor;
-
-public interface ResourceChangeManager {
- void subscribeResourceChangeListener(ResourceChangeListener listener,
- Resource resource);
-
- void unsubscribeResourceChangeListener(ResourceChangeListener listener,
- Resource resource);
-
- void addResourceChangeMonitor(ResourceChangeMonitor monitor);
-
- void removeResourceChangeMonitor(ResourceChangeMonitor monitor);
-
- Collection<ResourceChangeMonitor> getResourceChangeMonitor();
-}
Deleted: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeNotifier.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeNotifier.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeNotifier.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,9 +0,0 @@
-package org.drools.event.io;
-
-import org.drools.io.Resource;
-
-public interface ResourceChangeNotifier extends ResourceChangeManager {
- public void resourceAdded(Resource resource);
- public void resourceModified(Resource resource);
- public void resourceRemoved(Resource resource);
-}
Deleted: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceModifiedEvent.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceModifiedEvent.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceModifiedEvent.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,12 +0,0 @@
-package org.drools.event.io;
-
-import org.drools.io.Resource;
-
-
-public interface ResourceModifiedEvent {
-
- public abstract Resource getResource();
-
- public abstract long getTime();
-
-}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/Resource.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/Resource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/Resource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -4,12 +4,23 @@
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
+import java.util.Collection;
public interface Resource {
InputStream getInputStream() throws IOException;
+
public Reader getReader() throws IOException;
+
URL getURL() throws IOException;
+
boolean hasURL();
+
+ boolean isDirectory();
+
+ Collection<Resource> listResources();
+
long getLastModified();
+
public long getLastRead();
+
}
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeMonitor.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeMonitor.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeMonitor.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,6 +1,5 @@
package org.drools.io;
-import org.drools.event.io.ResourceChangeNotifier;
public interface ResourceChangeMonitor {
void subscribeNotifier(ResourceChangeNotifier notifier, Resource resource);
Copied: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeNotifier.java (from rev 24229, labs/jbossrules/trunk/drools-api/src/main/java/org/drools/event/io/ResourceChangeNotifier.java)
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeNotifier.java (rev 0)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeNotifier.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -0,0 +1,24 @@
+package org.drools.io;
+
+import java.util.Collection;
+
+import org.drools.KnowledgeBaseChangeSet;
+import org.drools.event.io.ResourceChangeListener;
+
+public interface ResourceChangeNotifier {
+ void subscribeResourceChangeListener(ResourceChangeListener listener,
+ Resource resource);
+
+ void unsubscribeResourceChangeListener(ResourceChangeListener listener,
+ Resource resource);
+
+ void subscribeChildResource(Resource directory, Resource child);
+
+ void addResourceChangeMonitor(ResourceChangeMonitor monitor);
+
+ void removeResourceChangeMonitor(ResourceChangeMonitor monitor);
+
+ Collection<ResourceChangeMonitor> getResourceChangeMonitor();
+
+ public void publishKnowledgeBaseChangeSet(KnowledgeBaseChangeSet changeSet);
+}
Property changes on: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceChangeNotifier.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceFactory.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceFactory.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -7,7 +7,6 @@
import java.util.Properties;
import org.drools.ProviderInitializationException;
-import org.drools.event.io.ResourceChangeNotifier;
public class ResourceFactory {
private static ResourceProvider resourceProvider;
Modified: labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceProvider.java
===================================================================
--- labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceProvider.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-api/src/main/java/org/drools/io/ResourceProvider.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -5,7 +5,6 @@
import java.io.Reader;
import java.net.URL;
-import org.drools.event.io.ResourceChangeNotifier;
public interface ResourceProvider {
ResourceChangeNotifier getResourceChangeNotifierService();
Deleted: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeComposition.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeComposition.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeComposition.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,21 +0,0 @@
-package org.drools.compiler;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class KnowledgeComposition {
- private List<KnowledgeResource> resources;
-
- public KnowledgeComposition() {
- this.resources = new ArrayList<KnowledgeResource>();
- }
-
- public List<KnowledgeResource> getResources() {
- return resources;
- }
-
- public void setResources(List<KnowledgeResource> parts) {
- this.resources = parts;
- }
-
-}
Deleted: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeResource.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeResource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,50 +0,0 @@
-package org.drools.compiler;
-
-import org.drools.builder.KnowledgeType;
-import org.drools.builder.ResourceConfiguration;
-import org.drools.io.Resource;
-
-public class KnowledgeResource {
- private String source;
- private KnowledgeType type;
- private ResourceConfiguration configuration;
-
- public KnowledgeResource(String src,
- KnowledgeType type) {
- this.source = src;
- this.type = type;
- }
-
- public KnowledgeResource(String src,
- KnowledgeType type,
- ResourceConfiguration configuration) {
- this.source = src;
- this.type = type;
- this.configuration = configuration;
- }
-
- public String getSource() {
- return this.source;
- }
-
- public void setSource(String src) {
- this.source = src;
- }
-
- public KnowledgeType getType() {
- return type;
- }
-
- public void setType(KnowledgeType type) {
- this.type = type;
- }
-
- public ResourceConfiguration getConfiguration() {
- return configuration;
- }
-
- public void setConfiguration(ResourceConfiguration configuration) {
- this.configuration = configuration;
- }
-
-}
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-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -48,8 +48,11 @@
import org.drools.facttemplates.FactTemplateImpl;
import org.drools.facttemplates.FieldTemplate;
import org.drools.facttemplates.FieldTemplateImpl;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
import org.drools.io.impl.ClassPathResource;
+import org.drools.io.impl.KnowledgeComposition;
+import org.drools.io.impl.KnowledgeResource;
import org.drools.io.impl.ReaderResource;
import org.drools.io.impl.UrlResource;
import org.drools.lang.descr.AttributeDescr;
@@ -441,27 +444,33 @@
try {
switch ( type ) {
case DRL : {
+ ((InternalResource)resource).setKnowledgeType( type );
addPackageFromDrl( resource );
break;
}
case DSLR : {
+ ((InternalResource)resource).setKnowledgeType( type );
addPackageFromDslr( resource );
break;
}
case DSL : {
+ ((InternalResource)resource).setKnowledgeType( type );
addDsl( resource );
break;
}
case XDRL : {
+ ((InternalResource)resource).setKnowledgeType( type );
addPackageFromXml( resource );
break;
}
case DRF : {
+ ((InternalResource)resource).setKnowledgeType( type );
addProcessFromXml( resource );
break;
}
case DTABLE : {
+ ((InternalResource)resource).setKnowledgeType( type );
DecisionTableConfiguration dtableConfiguration = (DecisionTableConfiguration) configuration;
String string = DecisionTableFactory.loadFromInputStream( resource.getInputStream(),
@@ -470,6 +479,7 @@
break;
}
case COMPOSITION : {
+ ((InternalResource)resource).setKnowledgeType( type );
XmlCompositionReader reader = new XmlCompositionReader( this.configuration.getSemanticModules() );
KnowledgeComposition composition = reader.read( resource.getReader() );
for ( KnowledgeResource kresource : composition.getResources() ) {
@@ -480,7 +490,17 @@
} else {
ioresource = new UrlResource( src );
}
- addKnowledgeResource( ioresource, kresource.getType(), kresource.getConfiguration() );
+ ((InternalResource)ioresource).setKnowledgeType( kresource.getType() );
+ if ( ioresource.isDirectory() ) {
+ this.resourceDirectories.add( ioresource );
+ for ( Resource childResource : ioresource.listResources() ) {
+ ((InternalResource)childResource).setKnowledgeType( kresource.getType() );
+ ((InternalResource)childResource).setFromDirectory( true );
+ addKnowledgeResource( childResource, kresource.getType(), kresource.getConfiguration() );
+ }
+ } else {
+ addKnowledgeResource( ioresource, kresource.getType(), kresource.getConfiguration() );
+ }
}
}
}
@@ -491,6 +511,8 @@
}
}
+ private Set<Resource> resourceDirectories = new HashSet<Resource>();
+
/**
* This adds a package from a Descr/AST This will also trigger a compile, if
* there are any generated classes to compile of course.
@@ -1016,6 +1038,7 @@
if ( hasErrors() && pkg != null ) {
pkg.setError( getErrors().toString() );
}
+ pkg.setResourceDirectories( this.resourceDirectories );
return pkg;
}
@@ -1033,6 +1056,7 @@
pkg.setError( errors );
}
pkgs[i++] = pkg;
+ pkg.setResourceDirectories( this.resourceDirectories );
}
return pkgs;
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/XmlCompositionReader.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/XmlCompositionReader.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/XmlCompositionReader.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -6,7 +6,7 @@
import javax.xml.parsers.SAXParser;
-import org.drools.compiler.KnowledgeComposition;
+import org.drools.io.impl.KnowledgeComposition;
import org.drools.lang.descr.PackageDescr;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/CompositionHandler.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/CompositionHandler.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/CompositionHandler.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -2,7 +2,7 @@
import java.util.HashSet;
-import org.drools.compiler.KnowledgeComposition;
+import org.drools.io.impl.KnowledgeComposition;
import org.drools.xml.BaseAbstractHandler;
import org.drools.xml.ExtensibleXmlParser;
import org.drools.xml.Handler;
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/DecisionTableConfigurationHandler.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/DecisionTableConfigurationHandler.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/DecisionTableConfigurationHandler.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -6,8 +6,8 @@
import org.drools.builder.DecisionTableInputType;
import org.drools.builder.ResourceConfiguration;
import org.drools.builder.impl.DecisionTableConfigurationImpl;
-import org.drools.compiler.KnowledgeComposition;
-import org.drools.compiler.KnowledgeResource;
+import org.drools.io.impl.KnowledgeComposition;
+import org.drools.io.impl.KnowledgeResource;
import org.drools.util.StringUtils;
import org.drools.xml.BaseAbstractHandler;
import org.drools.xml.ExtensibleXmlParser;
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/ResourceHandler.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/ResourceHandler.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/xml/composition/ResourceHandler.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -4,10 +4,10 @@
import java.util.HashSet;
import org.drools.builder.KnowledgeType;
-import org.drools.compiler.KnowledgeComposition;
-import org.drools.compiler.KnowledgeResource;
import org.drools.io.Resource;
import org.drools.io.impl.ClassPathResource;
+import org.drools.io.impl.KnowledgeComposition;
+import org.drools.io.impl.KnowledgeResource;
import org.drools.io.impl.UrlResource;
import org.drools.util.StringUtils;
import org.drools.xml.BaseAbstractHandler;
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/xml/composition/CompositionTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/xml/composition/CompositionTest.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/xml/composition/CompositionTest.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.UUID;
import junit.framework.TestCase;
@@ -24,12 +25,15 @@
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.KnowledgeType;
-import org.drools.compiler.KnowledgeComposition;
-import org.drools.compiler.KnowledgeResource;
import org.drools.compiler.PackageBuilderConfiguration;
+import org.drools.io.Resource;
import org.drools.io.ResourceChangeScannerConfiguration;
import org.drools.io.ResourceFactory;
+import org.drools.io.impl.FileSystemResource;
+import org.drools.io.impl.KnowledgeComposition;
+import org.drools.io.impl.KnowledgeResource;
import org.drools.io.impl.ResourceChangeNotifierImpl;
+import org.drools.io.impl.UrlResource;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.xml.XmlCompositionReader;
import org.xml.sax.SAXException;
@@ -73,24 +77,29 @@
assertEquals( DecisionTableInputType.XLS,
dtConf.getInputType() );
}
-
+
public void testIntegregation() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
- kbuilder.add( ResourceFactory.newClassPathResource( "composition1Test.xml", getClass()), KnowledgeType.COMPOSITION );
+ kbuilder.add( ResourceFactory.newClassPathResource( "composition1Test.xml",
+ getClass() ),
+ KnowledgeType.COMPOSITION );
assertFalse( kbuilder.hasErrors() );
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
List list = new ArrayList();
- ksession.setGlobal( "list", list );
+ ksession.setGlobal( "list",
+ list );
ksession.fireAllRules();
ksession.dispose();
-
- assertEquals( 2, list.size() );
- assertTrue ( list.containsAll( Arrays.asList( new String[] { "rule1", "rule2" } ) ) );
+
+ assertEquals( 2,
+ list.size() );
+ assertTrue( list.containsAll( Arrays.asList( new String[]{"rule1", "rule2"} ) ) );
}
-
- public void testIntegregation2() throws IOException, InterruptedException {
+
+ public void testModifyFile() throws IOException,
+ InterruptedException {
String rule1 = "";
rule1 += "package org.drools.test\n";
rule1 += "global java.util.List list\n";
@@ -99,12 +108,13 @@
rule1 += "then\n";
rule1 += "list.add( drools.getRule().getName() );\n";
rule1 += "end\n";
- File f1 = File.createTempFile( "rule1", ".drl" );
+ File f1 = File.createTempFile( "rule1",
+ ".drl" );
f1.deleteOnExit();
- Writer output = new BufferedWriter(new FileWriter(f1));
+ Writer output = new BufferedWriter( new FileWriter( f1 ) );
output.write( rule1 );
output.close();
-
+
String rule2 = "";
rule2 += "package org.drools.test\n";
rule2 += "global java.util.List list\n";
@@ -112,52 +122,62 @@
rule2 += "when\n";
rule2 += "then\n";
rule2 += "list.add( drools.getRule().getName() );\n";
- rule2 += "end\n";
- File f2 = File.createTempFile( "rule2", ".drl" );
+ rule2 += "end\n";
+ File f2 = File.createTempFile( "rule2",
+ ".drl" );
f2.deleteOnExit();
- output = new BufferedWriter(new FileWriter(f2));
- output.write( rule2 );
+ output = new BufferedWriter( new FileWriter( f2 ) );
+ output.write( rule2 );
output.close();
-
+
String xml = "";
xml += "<composition xmlns='http://drools.org/drools-4.0/composition'";
xml += " xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";
xml += " xs:schemaLocation='http://drools.org/drools-4.0/composition drools-composition-4.0.xsd' >";
- xml += " <resource source='" + f1.toURI().toURL() + "' type='DRL' />";
- xml += " <resource source='" + f2.toURI().toURL() + "' type='DRL' />";
+ xml += " <resource source='" + f1.toURI().toURL() + "' type='DRL' />";
+ xml += " <resource source='" + f2.toURI().toURL() + "' type='DRL' />";
xml += "</composition>";
- File fxml = File.createTempFile( "composition", ".xml" );
+ File fxml = File.createTempFile( "composition",
+ ".xml" );
fxml.deleteOnExit();
- output = new BufferedWriter(new FileWriter(fxml));
- output.write( xml );
- output.close();
-
+ output = new BufferedWriter( new FileWriter( fxml ) );
+ output.write( xml );
+ output.close();
+
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
- kbuilder.add( ResourceFactory.newUrlResource( fxml.toURI().toURL() ), KnowledgeType.COMPOSITION );
+ kbuilder.add( ResourceFactory.newUrlResource( fxml.toURI().toURL() ),
+ KnowledgeType.COMPOSITION );
assertFalse( kbuilder.hasErrors() );
-
+
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
-
+
ResourceChangeScannerConfiguration sconf = ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();
- sconf.setProperty( "drools.resource.scanner.interval", "2" );
+ sconf.setProperty( "drools.resource.scanner.interval",
+ "2" );
ResourceFactory.getResourceChangeScannerService().configure( sconf );
-
- KnowledgeAgentConfiguration aconf = new KnowledgeAgentConfigurationImpl();
- aconf.setProperty( "drools.agent.scanResources", "true" );
- aconf.setProperty( "drools.agent.newInstance", "true" );
- KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("test agent", kbase, aconf);
-
+
+ KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
+ aconf.setProperty( "drools.agent.scanResources",
+ "true" );
+ aconf.setProperty( "drools.agent.newInstance",
+ "true" );
+ KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "test agent",
+ kbase,
+ aconf );
+
StatefulKnowledgeSession ksession = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
List list = new ArrayList();
- ksession.setGlobal( "list", list );
+ ksession.setGlobal( "list",
+ list );
ksession.fireAllRules();
ksession.dispose();
-
- assertEquals( 2, list.size() );
- assertTrue( list.contains( "rule1" ));
- assertTrue( list.contains( "rule2" ));
-
+
+ assertEquals( 2,
+ list.size() );
+ assertTrue( list.contains( "rule1" ) );
+ assertTrue( list.contains( "rule2" ) );
+
list.clear();
rule1 = "";
rule1 += "package org.drools.test\n";
@@ -167,21 +187,147 @@
rule1 += "then\n";
rule1 += "list.add( drools.getRule().getName() );\n";
rule1 += "end\n";
- output = new BufferedWriter(new FileWriter(f1));
+ output = new BufferedWriter( new FileWriter( f1 ) );
output.write( rule1 );
- output.close();
+ output.close();
Thread.sleep( 3000 );
+
+ ksession = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
+ list = new ArrayList();
+ ksession.setGlobal( "list",
+ list );
+ ksession.fireAllRules();
+ ksession.dispose();
+
+ assertEquals( 2,
+ list.size() );
+ assertTrue( list.contains( "rule3" ) );
+ assertTrue( list.contains( "rule2" ) );
+ }
+
+ public void testModifyDirectory() throws IOException,
+ InterruptedException {
+ File dir = File.createTempFile( UUID.randomUUID().toString(),
+ "" );
+ dir = dir.getParentFile();
+ dir.deleteOnExit();
+ dir = new File( dir, UUID.randomUUID().toString() );
+ dir.mkdir();
+ dir.deleteOnExit();
+
+ String rule1 = "";
+ rule1 += "package org.drools.test\n";
+ rule1 += "global java.util.List list\n";
+ rule1 += "rule rule1\n";
+ rule1 += "when\n";
+ rule1 += "then\n";
+ rule1 += "list.add( drools.getRule().getName() );\n";
+ rule1 += "end\n";
+ File f1 = File.createTempFile( "rule1",
+ ".drl",
+ dir );
+ f1.deleteOnExit();
+ Writer output = new BufferedWriter( new FileWriter( f1 ) );
+ output.write( rule1 );
+ output.close();
+
+ String rule2 = "";
+ rule2 += "package org.drools.test\n";
+ rule2 += "global java.util.List list\n";
+ rule2 += "rule rule2\n";
+ rule2 += "when\n";
+ rule2 += "then\n";
+ rule2 += "list.add( drools.getRule().getName() );\n";
+ rule2 += "end\n";
+ File f2 = File.createTempFile( "rule2",
+ ".drl",
+ dir );
+ f2.deleteOnExit();
+ output = new BufferedWriter( new FileWriter( f2 ) );
+ output.write( rule2 );
+ output.close();
+
+ String xml = "";
+ xml += "<composition xmlns='http://drools.org/drools-4.0/composition'";
+ xml += " xmlns:xs='http://www.w3.org/2001/XMLSchema-instance'";
+ xml += " xs:schemaLocation='http://drools.org/drools-4.0/composition drools-composition-4.0.xsd' >";
+ xml += " <resource source='" + f1.getParentFile().toURI().toURL() + "' type='DRL' />";
+ xml += "</composition>";
+ File fxml = File.createTempFile( "composition",
+ ".xml" );
+ fxml.deleteOnExit();
+ output = new BufferedWriter( new FileWriter( fxml ) );
+ output.write( xml );
+ output.close();
+
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ kbuilder.add( ResourceFactory.newUrlResource( fxml.toURI().toURL() ),
+ KnowledgeType.COMPOSITION );
+ assertFalse( kbuilder.hasErrors() );
+
+ KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+ kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
+
+ ResourceChangeScannerConfiguration sconf = ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();
+ sconf.setProperty( "drools.resource.scanner.interval",
+ "2" );
+ ResourceFactory.getResourceChangeScannerService().configure( sconf );
+
+ KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
+ aconf.setProperty( "drools.agent.scanResources",
+ "true" );
+ aconf.setProperty( "drools.agent.newInstance",
+ "true" );
+ KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "test agent",
+ kbase,
+ aconf );
+
+ StatefulKnowledgeSession ksession = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
+ List list = new ArrayList();
+ ksession.setGlobal( "list",
+ list );
+ ksession.fireAllRules();
+ ksession.dispose();
+
+ assertEquals( 2,
+ list.size() );
+ assertTrue( list.contains( "rule1" ) );
+ assertTrue( list.contains( "rule2" ) );
+
+ list.clear();
+ String rule3 = "";
+ rule3 += "package org.drools.test\n";
+ rule3 += "global java.util.List list\n";
+ rule3 += "rule rule3\n";
+ rule3 += "when\n";
+ rule3 += "then\n";
+ rule3 += "list.add( drools.getRule().getName() );\n";
+ rule3 += "end\n";
+ File f3 = File.createTempFile( "rule3",
+ ".drl",
+ dir );
+ f3.deleteOnExit();
+ output = new BufferedWriter( new FileWriter( f3 ) );
+ output.write( rule3 );
+ output.close();
+
+ assertTrue( f1.delete() );
+
+
+ Thread.sleep( 3000 );
+
ksession = kagent.getKnowledgeBase().newStatefulKnowledgeSession();
list = new ArrayList();
- ksession.setGlobal( "list", list );
+ ksession.setGlobal( "list",
+ list );
ksession.fireAllRules();
ksession.dispose();
-
- assertEquals( 2, list.size() );
- assertTrue( list.contains( "rule3" ));
- assertTrue( list.contains( "rule2" ));
- }
-
-
+
+ assertEquals( 2,
+ list.size() );
+ assertTrue( list.contains( "rule2" ) );
+ assertTrue( list.contains( "rule3" ) );
+ }
+
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/impl/KnowledgeAgentImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/impl/KnowledgeAgentImpl.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/impl/KnowledgeAgentImpl.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -6,6 +6,7 @@
import java.util.Set;
import org.drools.KnowledgeBase;
+import org.drools.KnowledgeBaseChangeSet;
import org.drools.KnowledgeBaseFactory;
import org.drools.RuleBase;
import org.drools.agent.KnowledgeAgent;
@@ -14,11 +15,12 @@
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.KnowledgeType;
+import org.drools.definition.KnowledgeDefinition;
import org.drools.definition.process.Process;
import org.drools.event.io.ResourceChangeListener;
-import org.drools.event.io.ResourceModifiedEvent;
import org.drools.impl.KnowledgeBaseImpl;
import org.drools.io.Resource;
+import org.drools.io.InternalResource;
import org.drools.io.ResourceFactory;
import org.drools.io.impl.ResourceChangeNotifierImpl;
import org.drools.rule.Function;
@@ -30,8 +32,9 @@
implements
KnowledgeAgent,
ResourceChangeListener {
- private String name;
+ private String name;
private Map<Resource, ResourceMapping> resources;
+ private Set<Resource> resourceDirectories;
private KnowledgeBase kbase;
private ResourceChangeNotifierImpl notifier;
private boolean newInstance;
@@ -44,8 +47,9 @@
this.kbase = kbase;
this.resources = new HashMap<Resource, ResourceMapping>();
this.listener = listener;
+ this.newInstance = true; // we hard code this for now as incremental kbase changes don't work.
if ( configuration != null ) {
- this.newInstance = ((KnowledgeAgentConfigurationImpl) configuration).isNewInstance();
+ //this.newInstance = ((KnowledgeAgentConfigurationImpl) configuration).isNewInstance();
this.notifier = (ResourceChangeNotifierImpl) ResourceFactory.getResourceChangeNotifierService();
if ( ((KnowledgeAgentConfigurationImpl) configuration).isScanResources() ) {
this.notifier.addResourceChangeMonitor( ResourceFactory.getResourceChangeScannerService() );
@@ -60,6 +64,12 @@
synchronized ( this.resources ) {
for ( Package pkg : rbase.getPackages() ) {
+
+ for ( Resource resource : pkg.getResourceDirectories() ) {
+ this.notifier.subscribeResourceChangeListener( this,
+ resource );
+ }
+
for ( Rule rule : pkg.getRules() ) {
Resource resource = rule.getResource();
if ( resource == null || !resource.hasURL() ) {
@@ -73,7 +83,7 @@
this.resources.put( resource,
mapping );
}
- mapping.getObjects().add( rule );
+ mapping.getKnowledgeDefinitions().add( rule );
System.out.println( "agent : " + resource );
}
@@ -90,7 +100,7 @@
this.resources.put( resource,
mapping );
}
- mapping.getObjects().add( process );
+ mapping.getKnowledgeDefinitions().add( process );
System.out.println( "agent : " + resource );
}
@@ -107,7 +117,7 @@
this.resources.put( resource,
mapping );
}
- mapping.getObjects().add( typeDeclaration );
+ mapping.getKnowledgeDefinitions().add( typeDeclaration );
System.out.println( "agent : " + resource );
}
@@ -124,7 +134,7 @@
this.resources.put( resource,
mapping );
}
- mapping.getObjects().add( function );
+ mapping.getKnowledgeDefinitions().add( function );
System.out.println( "agent : " + resource );
}
}
@@ -137,46 +147,101 @@
}
}
- public void resourceModified(ResourceModifiedEvent event) {
- ResourceMapping mapping = this.resources.get( event.getResource() );
- System.out.println( "modified : " + event.getResource() );
- KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ // public void resourceModified(ResourceModifiedEvent event) {
+ // ResourceMapping mapping = this.resources.get( event.getResource() );
+ // System.out.println( "modified : " + event.getResource() );
+ // KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ // synchronized ( this.resources ) {
+ // for ( Resource resource : this.resources.keySet() ) {
+ // System.out.println( "building : " + resource );
+ // kbuilder.add( resource,
+ // KnowledgeType.DRL );
+ // }
+ //
+ // this.kbase = KnowledgeBaseFactory.newKnowledgeBase();
+ // this.kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
+ // }
+ // }
+
+ public void resourceChanged(KnowledgeBaseChangeSet changeSet) {
+ // for now we assume newIntance only, so just blow away the mappings and knowledgedefinition sets.
synchronized ( this.resources ) {
- for ( Resource resource : this.resources.keySet() ) {
+ // first remove the unneeded resources
+ for ( Resource resource : changeSet.getResourcesRemoved() ) {
+ this.resources.remove( resource );
+ }
+
+ // now add the new ones
+ for ( Resource resource : changeSet.getResourcesAdded() ) {
+ this.resources.put( resource,
+ null );
+ }
+
+ // modified we already know is in the map, so no need to process those
+
+ // now make a copy of the resource keys, as we are about to reset it, but need the keys to rebuild the kbase
+ Resource[] resourcesClone = this.resources.keySet().toArray( new Resource[this.resources.size()] );
+
+ // reset the resources map, so it can now be rebuilt
+ this.resources.clear();
+
+ // rebuild the kbase
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+ for ( Resource resource : resourcesClone ) {
System.out.println( "building : " + resource );
kbuilder.add( resource,
- KnowledgeType.DRL );
+ ((InternalResource)resource).getKnowledgeType() );
}
this.kbase = KnowledgeBaseFactory.newKnowledgeBase();
this.kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
}
- }
- public void resourceAdded(ResourceModifiedEvent event) {
- // TODO Auto-generated method stub
-
+ // code commented out to try and do incremental kbase changes
+ // @TODO get this working for incremental changes
+ // synchronized ( this.resources ) {
+ // // first deal with removals
+ // for ( Resource resource : changeSet.getResourcesRemoved() ) {
+ // ResourceMapping mapping = this.resources.remove(resource );
+ // if ( !this.newInstance ) {
+ // // we are keeping the current instance, so we need remove the individual knowledge definitions
+ // for ( KnowledgeDefinition kd : mapping.getKnowledgeDefinitions() ) {
+ // if ( kd instanceof Rule ) {
+ // Rule rule = ( Rule ) kd;
+ // this.kbase.removeRule( rule.getPackageName(), rule.getName() );
+ // } else if ( kd instanceof Process ) {
+ // Process process = ( Process ) kd;
+ // this.kbase.removeProcess( process.getId() );
+ // }
+ // // @TODO functions and type declarations
+ // }
+ // }
+ // }
+ //
+ // // now deal with additions
+ // for ( Resource resource : changeSet.getResourcesAdded() ) {
+ //
+ // }
+ //
+ // // final deal with modifies
+ // }
}
- public void resourceRemoved(ResourceModifiedEvent event) {
- // TODO Auto-generated method stub
-
- }
-
public static class ResourceMapping {
- private Resource resource;
- private Set objects;
+ private Resource resource;
+ private Set<KnowledgeDefinition> knowledgeDefinitions;
public ResourceMapping(Resource resource) {
- this.objects = new HashSet<Object>();
+ this.knowledgeDefinitions = new HashSet<KnowledgeDefinition>();
}
public Resource getResource() {
return resource;
}
- public Set getObjects() {
- return objects;
+ public Set<KnowledgeDefinition> getKnowledgeDefinitions() {
+ return knowledgeDefinitions;
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/impl/KnowledgeAgentProviderImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/impl/KnowledgeAgentProviderImpl.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/agent/impl/KnowledgeAgentProviderImpl.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -9,6 +9,14 @@
import org.drools.agent.KnowledgeAgentEventListener;
public class KnowledgeAgentProviderImpl implements KnowledgeAgentProvider {
+
+ public KnowledgeAgentConfiguration newKnowledgeAgentConfiguration() {
+ return new KnowledgeAgentConfigurationImpl();
+ }
+
+ public KnowledgeAgentConfiguration newKnowledgeAgentConfiguration(Properties properties) {
+ return new KnowledgeAgentConfigurationImpl(properties);
+ }
public KnowledgeAgent newKnowledgeAgent(String name,
KnowledgeBase kbase) {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -524,6 +524,7 @@
// Merge imports
final Map<String, ImportDeclaration> imports = pkg.getImports();
imports.putAll( newPkg.getImports() );
+ pkg.setResourceDirectories( newPkg.getResourceDirectories() );
String lastType = null;
try {
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/InternalResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/InternalResource.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/InternalResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -0,0 +1,13 @@
+package org.drools.io;
+
+import org.drools.builder.KnowledgeType;
+
+public interface InternalResource extends Resource {
+ boolean isFromDirectory();
+
+ void setFromDirectory(boolean fromDirectory);
+
+ void setKnowledgeType(KnowledgeType knowledgeType);
+ KnowledgeType getKnowledgeType();
+
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/BaseResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/BaseResource.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/BaseResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -0,0 +1,28 @@
+package org.drools.io.impl;
+
+import org.drools.builder.KnowledgeType;
+
+public abstract class BaseResource {
+ private boolean fromDirectory;
+ private KnowledgeType KnowledgeType;
+
+ public boolean isFromDirectory() {
+ return this.fromDirectory;
+ }
+
+ public void setFromDirectory(boolean fromDirectory) {
+ this.fromDirectory = fromDirectory;
+ }
+
+
+ public void setKnowledgeType(KnowledgeType knowledgeType) {
+ this.KnowledgeType = knowledgeType;
+ }
+
+
+ public KnowledgeType getKnowledgeType() {
+ return this.KnowledgeType;
+ }
+
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ByteArrayResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ByteArrayResource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ByteArrayResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,20 +1,27 @@
package org.drools.io.impl;
import java.io.ByteArrayInputStream;
+import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Date;
+import java.util.List;
+import org.drools.builder.KnowledgeType;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
+import org.drools.util.StringUtils;
-public class ByteArrayResource
+public class ByteArrayResource extends BaseResource
implements
- Resource {
+ InternalResource {
private byte[] bytes;
@@ -48,7 +55,15 @@
public long getLastRead() {
throw new IllegalStateException( "reader does have a modified date" );
}
+
+ public boolean isDirectory() {
+ return false;
+ }
+ public Collection<Resource> listResources() {
+ throw new RuntimeException( "This Resource cannot be listed, or is not a directory" );
+ }
+
public boolean equals(Object object) {
return (object == this || (object instanceof ByteArrayResource && Arrays.equals( ((ByteArrayResource) object).bytes,
this.bytes )));
@@ -57,5 +72,12 @@
public int hashCode() {
return (byte[].class.hashCode() * 29 * this.bytes.length);
}
+
+ public String toString() {
+ return "[ByteArrayResource resource=" + this.bytes + "]";
+ }
+
+
+
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ClassPathResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ClassPathResource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ClassPathResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,14 +1,22 @@
package org.drools.io.impl;
+import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
+import org.drools.util.StringUtils;
/**
* Borrowed gratuitously from Spring under ASL2.0.
@@ -16,9 +24,9 @@
*+
*/
-public class ClassPathResource
+public class ClassPathResource extends BaseResource
implements
- Resource {
+ InternalResource {
private String path;
private ClassLoader classLoader;
private Class clazz;
@@ -79,7 +87,7 @@
if ( is == null ) {
throw new FileNotFoundException( "'" + this.path + "' cannot be opened because it does not exist" );
}
- this.lastRead = getLastModified();
+ this.lastRead = getLastModified();
return is;
}
@@ -113,7 +121,7 @@
public boolean hasURL() {
return true;
}
-
+
public long getLastModified() {
try {
URLConnection conn = getURL().openConnection();
@@ -123,8 +131,8 @@
throw new RuntimeException( "Unable to get LastMofified for ClasspathResource",
e );
}
- }
-
+ }
+
public long getLastRead() {
return this.lastRead;
}
@@ -132,7 +140,45 @@
public Reader getReader() throws IOException {
return new InputStreamReader( getInputStream() );
}
+
+ public boolean isDirectory() {
+ try {
+ URL url = getURL();
+ if ( !"file".equals( url.getProtocol() ) ) {
+ return false;
+ }
+
+ File file = new File( StringUtils.toURI( url.toString() ).getSchemeSpecificPart() );
+
+ return file.isDirectory();
+ } catch ( Exception e ) {
+ return false;
+ }
+ }
+
+ public Collection<Resource> listResources() {
+ try {
+ URL url = getURL();
+
+ if ( "file".equals( url.getProtocol() ) ) {
+ File dir = new File( StringUtils.toURI( url.toString() ).getSchemeSpecificPart() );
+
+ List<Resource> resources = new ArrayList<Resource>();
+
+ for ( File file : dir.listFiles() ) {
+ resources.add( new FileSystemResource( file ) );
+ }
+
+ return resources;
+ }
+ } catch ( Exception e ) {
+ // swollow as we'll throw an exception anyway
+ }
+
+ throw new RuntimeException( "This Resource cannot be listed, or is not a directory" );
+ }
+
public boolean equals(Object object) {
if ( object == null || !(object instanceof ClassPathResource) ) {
return false;
@@ -153,4 +199,6 @@
public String toString() {
return "[ClassPathResource path='" + this.path + "']";
}
+
+
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/EncodedResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/EncodedResource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/EncodedResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -5,10 +5,12 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
+import java.util.Collection;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
-public class EncodedResource implements Resource {
+public class EncodedResource extends BaseResource implements InternalResource {
private final Resource resource;
private final String encoding;
@@ -86,8 +88,16 @@
return this.resource.getLastRead();
}
+ public boolean isDirectory() {
+ return this.resource.isDirectory();
+ }
+
+ public Collection<Resource> listResources() {
+ return this.resource.listResources();
+ }
+
public String toString() {
return "[EncodedResource resource=" + this.resource + " encoding='" + this.encoding + "']";
- }
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/FileSystemResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/FileSystemResource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/FileSystemResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -7,8 +7,12 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.Date;
+import java.util.List;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
import org.drools.util.StringUtils;
@@ -16,9 +20,10 @@
* Borrowed gratuitously from Spring under ASL2.0.
*
*/
-public class FileSystemResource implements Resource {
+public class FileSystemResource extends BaseResource implements InternalResource {
private File file;
private long lastRead = -1;
+ private boolean FromDirectory;
/**
* Create a new FileSystemResource from a File handle.
@@ -71,7 +76,22 @@
public File getFile() {
return this.file;
}
-
+
+ public boolean isDirectory() {
+ return this.file.isDirectory();
+ }
+
+ public Collection<Resource> listResources() {
+ File[] files = this.file.listFiles();
+ List<Resource> resources = new ArrayList<Resource>();
+
+ for ( File file : files ) {
+ resources.add( new FileSystemResource( file ) );
+ }
+
+ return resources;
+ }
+
/**
* This implementation returns a URL for the underlying file.
* @see java.io.File#toURI()
@@ -93,8 +113,19 @@
return this.lastRead;
}
-
public String toString() {
return "[FileResource file='" + this.file.toString() + "']";
}
+
+ public boolean equals(Object object) {
+ if ( object == null ) {
+ return false;
+ }
+
+ return ( object == this || ( object instanceof FileSystemResource && file.equals( ((FileSystemResource)object).file ) ) );
+ }
+
+ public int hashCode() {
+ return this.file.hashCode();
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/InputStreamResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/InputStreamResource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/InputStreamResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -6,10 +6,12 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
+import java.util.Collection;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
-public class InputStreamResource implements Resource {
+public class InputStreamResource extends BaseResource implements InternalResource {
private InputStream stream;
public InputStreamResource(InputStream stream) {
@@ -42,5 +44,13 @@
public long getLastRead() {
throw new IllegalStateException( "InputStream does have a modified date" );
}
+
+ public boolean isDirectory() {
+ return false;
+ }
+ public Collection<Resource> listResources() {
+ throw new RuntimeException( "This Resource cannot be listed, or is not a directory" );
+ }
+
}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeBaseChangeSetImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeBaseChangeSetImpl.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeBaseChangeSetImpl.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -0,0 +1,52 @@
+package org.drools.io.impl;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import org.drools.KnowledgeBaseChangeSet;
+import org.drools.definition.KnowledgeDefinition;
+import org.drools.io.Resource;
+
+public class KnowledgeBaseChangeSetImpl implements KnowledgeBaseChangeSet {
+ private Collection<Resource> resourcesRemoved = Collections.<Resource>emptyList();
+ private Collection<Resource> resourcesAdded = Collections.<Resource>emptyList();
+ private Collection<Resource> resourcesModified = Collections.<Resource>emptyList();
+ private Collection<KnowledgeDefinition> knowledgeDefinitionsRemoved = Collections.<KnowledgeDefinition>emptyList();
+
+ public KnowledgeBaseChangeSetImpl() {
+
+ }
+
+ public void setResourcesRemoved(Collection<Resource> resourcesRemoved) {
+ this.resourcesRemoved = resourcesRemoved;
+ }
+
+ public Collection<Resource> getResourcesRemoved() {
+ return resourcesRemoved;
+ }
+
+ public void setResourcesAdded(Collection<Resource> resourcesAdded) {
+ this.resourcesAdded = resourcesAdded;
+ }
+
+ public Collection<Resource> getResourcesAdded() {
+ return resourcesAdded;
+ }
+
+ public Collection<Resource> getResourcesModified() {
+ return resourcesModified;
+ }
+
+ public void setResourcesModified(Collection<Resource> resourcesModified) {
+ this.resourcesModified = resourcesModified;
+ }
+
+ public void setKnowledgeDefinitionsRemoved(Collection<KnowledgeDefinition> knowledgeDefinitionsRemoved) {
+ this.knowledgeDefinitionsRemoved = knowledgeDefinitionsRemoved;
+ }
+
+ public Collection<KnowledgeDefinition> getKnowledgeDefinitionsRemoved() {
+ return knowledgeDefinitionsRemoved;
+ }
+
+}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeComposition.java (from rev 24227, labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeComposition.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeComposition.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeComposition.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -0,0 +1,21 @@
+package org.drools.io.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class KnowledgeComposition {
+ private List<KnowledgeResource> resources;
+
+ public KnowledgeComposition() {
+ this.resources = new ArrayList<KnowledgeResource>();
+ }
+
+ public List<KnowledgeResource> getResources() {
+ return resources;
+ }
+
+ public void setResources(List<KnowledgeResource> parts) {
+ this.resources = parts;
+ }
+
+}
Property changes on: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeComposition.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeResource.java (from rev 24227, labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/KnowledgeResource.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeResource.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -0,0 +1,50 @@
+package org.drools.io.impl;
+
+import org.drools.builder.KnowledgeType;
+import org.drools.builder.ResourceConfiguration;
+import org.drools.io.Resource;
+
+public class KnowledgeResource {
+ private String source;
+ private KnowledgeType type;
+ private ResourceConfiguration configuration;
+
+ public KnowledgeResource(String src,
+ KnowledgeType type) {
+ this.source = src;
+ this.type = type;
+ }
+
+ public KnowledgeResource(String src,
+ KnowledgeType type,
+ ResourceConfiguration configuration) {
+ this.source = src;
+ this.type = type;
+ this.configuration = configuration;
+ }
+
+ public String getSource() {
+ return this.source;
+ }
+
+ public void setSource(String src) {
+ this.source = src;
+ }
+
+ public KnowledgeType getType() {
+ return type;
+ }
+
+ public void setType(KnowledgeType type) {
+ this.type = type;
+ }
+
+ public ResourceConfiguration getConfiguration() {
+ return configuration;
+ }
+
+ public void setConfiguration(ResourceConfiguration configuration) {
+ this.configuration = configuration;
+ }
+
+}
Property changes on: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/KnowledgeResource.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ReaderResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ReaderResource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ReaderResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -6,11 +6,13 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
+import java.util.Collection;
import java.util.Date;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
-public class ReaderResource implements Resource {
+public class ReaderResource extends BaseResource implements InternalResource {
private Reader reader;
private String encoding;
@@ -58,8 +60,19 @@
return this.encoding;
}
+ public boolean isDirectory() {
+ return false;
+ }
+
+ public Collection<Resource> listResources() {
+ throw new RuntimeException( "This Resource cannot be listed, or is not a directory" );
+ }
+
public boolean hasURL() {
return false;
}
+ public String toString() {
+ return "[ReaderResource resource=" + this.reader + " encoding='" + this.encoding + "']";
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceChangeNotifierImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceChangeNotifierImpl.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceChangeNotifierImpl.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,5 +1,6 @@
package org.drools.io.impl;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -7,13 +8,14 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.Map.Entry;
import java.util.concurrent.CopyOnWriteArrayList;
+import org.drools.KnowledgeBaseChangeSet;
import org.drools.event.io.ResourceChangeListener;
-import org.drools.event.io.ResourceChangeNotifier;
-import org.drools.event.io.ResourceModifiedEvent;
import org.drools.io.Resource;
import org.drools.io.ResourceChangeMonitor;
+import org.drools.io.ResourceChangeNotifier;
public class ResourceChangeNotifierImpl
implements
@@ -72,27 +74,88 @@
}
}
}
-
- public void resourceAdded(Resource resource) {
-
+
+ public void subscribeChildResource(Resource directory, Resource child) {
+ for ( ResourceChangeListener listener : this.subscriptions.get( directory ) ) {
+ subscribeResourceChangeListener( listener, child );
+ }
}
- public void resourceModified(Resource resource) {
- ResourceModifiedEvent event = new ResourceModifiedEventImpl( resource,
- resource.getLastModified() );
- Set<ResourceChangeListener> listeners = this.subscriptions.get( resource );
+ public void publishKnowledgeBaseChangeSet(KnowledgeBaseChangeSet changeSet) {
+ // this provides the complete published change set for this notifier.
+ // however different listeners might be listening to different resources, so provide
+ // listener change specified change sets.
- if ( listeners != null ) {
- for ( ResourceChangeListener listener : listeners ) {
- listener.resourceModified( event );
+ Map<ResourceChangeListener, KnowledgeBaseChangeSetImpl> localChangeSets = new HashMap<ResourceChangeListener, KnowledgeBaseChangeSetImpl>();
+
+ for ( Resource resource : changeSet.getResourcesAdded() ) {
+ Set<ResourceChangeListener> listeners = this.subscriptions.get( resource );
+ for ( ResourceChangeListener listener : listeners ) {
+ KnowledgeBaseChangeSetImpl localChangeSet = localChangeSets.get( listener );
+
+ if ( localChangeSet == null ) {
+ // lazy initialise changeSet
+ localChangeSet = new KnowledgeBaseChangeSetImpl();
+ localChangeSets.put( listener, localChangeSet );
+ }
+ if ( localChangeSet.getResourcesAdded().isEmpty() ) {
+ localChangeSet.setResourcesAdded( new ArrayList<Resource>() );
+ }
+ localChangeSet.getResourcesAdded().add( resource );
+
}
}
+ for ( Resource resource : changeSet.getResourcesRemoved() ) {
+ Set<ResourceChangeListener> listeners = this.subscriptions.remove( resource );
+ for ( ResourceChangeListener listener : listeners ) {
+ KnowledgeBaseChangeSetImpl localChangeSet = localChangeSets.get( listener );
+ if ( localChangeSet == null ) {
+ // lazy initialise changeSet
+ localChangeSet = new KnowledgeBaseChangeSetImpl();
+ localChangeSets.put( listener, localChangeSet );
+ }
+ if ( localChangeSet.getResourcesRemoved().isEmpty() ) {
+ localChangeSet.setResourcesRemoved( new ArrayList<Resource>() );
+ }
+ localChangeSet.getResourcesRemoved().add( resource );
+ }
+ }
+
+
+ for ( Resource resource : changeSet.getResourcesModified() ) {
+ Set<ResourceChangeListener> listeners = this.subscriptions.get( resource );
+ for ( ResourceChangeListener listener : listeners ) {
+ KnowledgeBaseChangeSetImpl localChangeSet = localChangeSets.get( listener );
+ if ( localChangeSet == null ) {
+ // lazy initialise changeSet
+ localChangeSet = new KnowledgeBaseChangeSetImpl();
+ localChangeSets.put( listener, localChangeSet );
+ }
+ if ( localChangeSet.getResourcesModified().isEmpty() ) {
+ localChangeSet.setResourcesModified( new ArrayList<Resource>() );
+ }
+ localChangeSet.getResourcesModified().add( resource );
+ }
+ }
+
+ for ( Entry<ResourceChangeListener, KnowledgeBaseChangeSetImpl> entry : localChangeSets.entrySet() ) {
+ ResourceChangeListener listener = entry.getKey();
+ KnowledgeBaseChangeSetImpl localChangeSet = entry.getValue();
+ listener.resourceChanged( localChangeSet );
+ }
+
+
+// ResourceModifiedEvent event = new ResourceModifiedEventImpl( resource,
+// resource.getLastModified() );
+// Set<ResourceChangeListener> listeners = this.subscriptions.get( resource );
+//
+// if ( listeners != null ) {
+// for ( ResourceChangeListener listener : listeners ) {
+// listener.resourceModified( event );
+// }
+// }
}
- public void resourceRemoved(Resource resource) {
- }
-
-
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceChangeScannerImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceChangeScannerImpl.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceChangeScannerImpl.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,16 +1,25 @@
package org.drools.io.impl;
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;
-import org.drools.event.io.ResourceChangeNotifier;
+import org.drools.KnowledgeBaseChangeSet;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
+import org.drools.io.ResourceChangeNotifier;
import org.drools.io.ResourceChangeScanner;
import org.drools.io.ResourceChangeScannerConfiguration;
+import org.drools.util.StringUtils;
public class ResourceChangeScannerImpl
implements
@@ -18,6 +27,7 @@
Runnable {
private Map<Resource, Set<ResourceChangeNotifier>> resources;
+ private Set<Resource> directories;
private volatile boolean scan;
@@ -25,6 +35,7 @@
public ResourceChangeScannerImpl() {
this.resources = new HashMap<Resource, Set<ResourceChangeNotifier>>();
+ this.directories = new HashSet<Resource>();
setInterval( 60 );
this.scan = true;
}
@@ -32,7 +43,7 @@
public void configure(ResourceChangeScannerConfiguration configuration) {
this.interval = ((ResourceChangeScannerConfigurationImpl) configuration).getInterval();
System.out.println( this.interval );
- synchronized ( this.resources ) {
+ synchronized ( this.resources ) {
this.resources.notify(); // notify wait, so that it will wait again
}
}
@@ -49,6 +60,9 @@
Resource resource) {
System.out.println( "scanner : " + resource );
synchronized ( this.resources ) {
+ if ( resource.isDirectory() ) {
+ this.directories.add( resource );
+ }
Set<ResourceChangeNotifier> notifiers = this.resources.get( resource );
if ( notifiers == null ) {
notifiers = new HashSet<ResourceChangeNotifier>();
@@ -69,23 +83,105 @@
notifiers.remove( notifier );
if ( notifiers.isEmpty() ) {
this.resources.remove( resource );
+ this.directories.remove( resource ); // don't bother with isDirectory check, as doing a remove is harmless if it doesn't exist
}
}
}
public void scan() {
System.out.println( "attempt scan : " + this.resources.size() );
+
+ if ( this.resources.size() > 0 ) {
+ System.out.println( "x" );
+ }
+ Map<ResourceChangeNotifier, KnowledgeBaseChangeSet> notifications = new HashMap<ResourceChangeNotifier, KnowledgeBaseChangeSet>();
+
+
+ List<Resource> removed = new ArrayList<Resource>();
+
+ // detect modified and added
+ for ( Resource resource : this.directories ) {
+ for ( Resource child : resource.listResources() ) {
+ if ( !this.resources.containsKey( child ) ) {
+ System.out.println( "found new file : " + child );
+ // child is new
+ ((InternalResource)child).setKnowledgeType( ((InternalResource)resource).getKnowledgeType() );
+ Set<ResourceChangeNotifier> notifiers = this.resources.get( resource ); // get notifiers for this directory
+ for ( ResourceChangeNotifier notifier : notifiers ) {
+ KnowledgeBaseChangeSetImpl changeSet = (KnowledgeBaseChangeSetImpl) notifications.get( notifier );
+ if ( changeSet == null ) {
+ // lazy initialise changeSet
+ changeSet = new KnowledgeBaseChangeSetImpl();
+ notifications.put( notifier,
+ changeSet );
+ }
+ if ( changeSet.getResourcesAdded().isEmpty() ) {
+ changeSet.setResourcesAdded( new ArrayList<Resource>() );
+ }
+ changeSet.getResourcesAdded().add( child );
+ notifier.subscribeChildResource( resource, child );
+ }
+ }
+ }
+ }
+
for ( Entry<Resource, Set<ResourceChangeNotifier>> entry : this.resources.entrySet() ) {
Resource resource = entry.getKey();
- for ( ResourceChangeNotifier notifier : entry.getValue() ) {
+ Set<ResourceChangeNotifier> notifiers = entry.getValue();
+
+ if ( !resource.isDirectory() ) {
+ // detect if Resource has been modified
System.out.println( "scan " + resource + ": " + resource.getLastModified() + " : " + resource.getLastRead() );
- if ( resource.getLastRead() < resource.getLastModified() ) {
- notifier.resourceModified( resource );
+ long lastModified = resource.getLastModified();
+ if ( lastModified == 0 ) {
+ removed.add( resource );
+ // resource is no longer present
+ // iterate notifiers for this resource and add to each removed
+ for ( ResourceChangeNotifier notifier : notifiers ) {
+ KnowledgeBaseChangeSetImpl changeSet = (KnowledgeBaseChangeSetImpl) notifications.get( notifier );
+ if ( changeSet == null ) {
+ // lazy initialise changeSet
+ changeSet = new KnowledgeBaseChangeSetImpl();
+ notifications.put( notifier,
+ changeSet );
+ }
+ if ( changeSet.getResourcesRemoved().isEmpty() ) {
+ changeSet.setResourcesRemoved( new ArrayList<Resource>() );
+ }
+ changeSet.getResourcesRemoved().add( resource );
+ }
+ } else if ( resource.getLastRead() < lastModified ) {
+ // it's modified
+ // iterate notifiers for this resource and add to each modified
+ for ( ResourceChangeNotifier notifier : notifiers ) {
+ KnowledgeBaseChangeSetImpl changeSet = (KnowledgeBaseChangeSetImpl) notifications.get( notifier );
+ if ( changeSet == null ) {
+ // lazy initialise changeSet
+ changeSet = new KnowledgeBaseChangeSetImpl();
+ notifications.put( notifier,
+ changeSet );
+ }
+ if ( changeSet.getResourcesModified().isEmpty() ) {
+ changeSet.setResourcesModified( new ArrayList<Resource>() );
+ }
+ changeSet.getResourcesModified().add( resource );
+ }
}
- }
+ }
}
+
+ // now iterate and removed the removed resources, we do this so as not to mutate the foreach loop while iterating
+ for ( Resource resource : removed ) {
+ this.resources.remove( resource );
+ }
+
+ for ( Entry<ResourceChangeNotifier, KnowledgeBaseChangeSet> entry : notifications.entrySet() ) {
+ ResourceChangeNotifier notifier = entry.getKey();
+ KnowledgeBaseChangeSet changeSet = entry.getValue();
+ notifier.publishKnowledgeBaseChangeSet( changeSet );
+ }
}
-
+
public void start() {
this.scan = true;
}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceModifiedEventImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceModifiedEventImpl.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceModifiedEventImpl.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,34 +0,0 @@
-/**
- *
- */
-package org.drools.io.impl;
-
-import org.drools.event.io.ResourceModifiedEvent;
-import org.drools.io.Resource;
-
-public class ResourceModifiedEventImpl implements ResourceModifiedEvent {
- private Resource resource;
- private long time;
-
- public ResourceModifiedEventImpl(Resource resource,
- long time) {
- super();
- this.resource = resource;
- this.time = time;
- }
-
- /* (non-Javadoc)
- * @see org.drools.io.impl.ResourceModifiedEvent#getResource()
- */
- public Resource getResource() {
- return resource;
- }
-
- /* (non-Javadoc)
- * @see org.drools.io.impl.ResourceModifiedEvent#getTime()
- */
- public long getTime() {
- return time;
- }
-
-}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceProviderImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceProviderImpl.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/ResourceProviderImpl.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -5,8 +5,8 @@
import java.io.Reader;
import java.net.URL;
-import org.drools.event.io.ResourceChangeNotifier;
import org.drools.io.Resource;
+import org.drools.io.ResourceChangeNotifier;
import org.drools.io.ResourceChangeScanner;
import org.drools.io.ResourceProvider;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/UrlResource.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/UrlResource.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/io/impl/UrlResource.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -1,5 +1,6 @@
package org.drools.io.impl;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -7,8 +8,12 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.Date;
+import java.util.List;
+import org.drools.io.InternalResource;
import org.drools.io.Resource;
import org.drools.util.StringUtils;
@@ -16,10 +21,13 @@
* Borrowed gratuitously from Spring under ASL2.0.
*
*/
-public class UrlResource implements Resource {
+public class UrlResource extends BaseResource implements InternalResource {
private URL url;
private long lastRead = -1;
+ private boolean FromDirectory;
+
+
public UrlResource(URL url) {
this.url = getCleanedUrl( url,
url.toString() );
@@ -94,11 +102,52 @@
public long getLastRead() {
return this.lastRead;
}
+
+ public boolean isDirectory() {
+ try {
+ URL url = getURL();
+ if ( "file".equals( url.getProtocol() ) ) {
+
+ File file = new File( StringUtils.toURI( url.toString() ).getSchemeSpecificPart() );
+
+ return file.isDirectory();
+ }
+ } catch ( Exception e ) {
+ // swallow as returned false
+ }
+
+ return false;
+ }
+
+ public Collection<Resource> listResources() {
+ try {
+ URL url = getURL();
+
+ if ( "file".equals( url.getProtocol() ) ) {
+ File dir = new File( StringUtils.toURI( url.toString() ).getSchemeSpecificPart() );
+
+ List<Resource> resources = new ArrayList<Resource>();
+
+ for ( File file : dir.listFiles() ) {
+ resources.add( new FileSystemResource( file ) );
+ }
+
+ return resources;
+ }
+ } catch ( Exception e ) {
+ // swallow as we'll throw an exception anyway
+ }
+ throw new RuntimeException( "This Resource cannot be listed, or is not a directory" );
+ }
+
/**
* This implementation compares the underlying URL references.
*/
public boolean equals(Object obj) {
+ if ( obj == null ) {
+ return false;
+ }
return (obj == this || (obj instanceof UrlResource && this.url.equals( ((UrlResource) obj).url )));
}
@@ -110,7 +159,7 @@
}
public String toString() {
- return "[UrlResource path='" + this.url.toExternalForm() + "']";
+ return "[UrlResource path='" + this.url.toString() + "']";
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -35,6 +35,7 @@
import org.drools.common.DroolsObjectOutputStream;
import org.drools.definition.process.Process;
import org.drools.facttemplates.FactTemplate;
+import org.drools.io.Resource;
/**
* Collection of related <code>Rule</code>s.
@@ -97,6 +98,8 @@
* valid
*/
private String errorSummary;
+
+ private Set<Resource> resourceDirectories = Collections.emptySet();
// ------------------------------------------------------------
// Constructors
@@ -165,6 +168,7 @@
out.writeBoolean( this.valid );
out.writeObject( this.rules );
out.writeObject( this.classFieldAccessorStore );
+ out.writeObject( this.resourceDirectories );
// writing the whole stream as a byte array
if ( !isDroolsStream ) {
bytes.flush();
@@ -206,6 +210,7 @@
this.valid = in.readBoolean();
this.rules = (Map) in.readObject();
this.classFieldAccessorStore = (ClassFieldAccessorStore) in.readObject();
+ this.resourceDirectories = (Set<Resource>) in.readObject();
if ( !isDroolsStream ) {
in.close();
}
@@ -489,7 +494,15 @@
public String getErrorSummary() {
return this.errorSummary;
}
+
+ public Set<Resource> getResourceDirectories() {
+ return resourceDirectories;
+ }
+ public void setResourceDirectories(Set<Resource> resourceDirectories) {
+ this.resourceDirectories = resourceDirectories;
+ }
+
public boolean equals(final Object object) {
if ( this == object ) {
return true;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -24,6 +24,7 @@
import java.io.ObjectOutput;
import java.io.Serializable;
+import org.drools.definition.KnowledgeDefinition;
import org.drools.factmodel.ClassDefinition;
import org.drools.facttemplates.FactTemplate;
import org.drools.io.Resource;
@@ -38,6 +39,7 @@
*/
public class TypeDeclaration
implements
+ KnowledgeDefinition,
Externalizable {
public static final String ATTR_CLASS = "class";
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/StringUtils.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/StringUtils.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/StringUtils.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -21,6 +21,8 @@
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
@@ -1078,8 +1080,12 @@
return sbuf.toString();
}
+ public static URI toURI(String location) throws URISyntaxException {
+ return new URI( StringUtils.replace( location,
+ " ",
+ "%20" ) );
+ }
-
/**
* Take a String which is a delimited list and convert it to a String array.
* <p>A single delimiter can consists of more than one character: It will still
Modified: labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/CompositionTest.java
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/CompositionTest.java 2008-12-04 09:32:32 UTC (rev 24232)
+++ labs/jbossrules/trunk/drools-decisiontables/src/test/java/org/drools/decisiontable/CompositionTest.java 2008-12-04 10:40:12 UTC (rev 24233)
@@ -15,11 +15,11 @@
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.KnowledgeType;
-import org.drools.compiler.KnowledgeComposition;
-import org.drools.compiler.KnowledgeResource;
import org.drools.compiler.PackageBuilderConfiguration;
import org.drools.io.ResourceFactory;
import org.drools.io.impl.ClassPathResource;
+import org.drools.io.impl.KnowledgeComposition;
+import org.drools.io.impl.KnowledgeResource;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.xml.XmlCompositionReader;
import org.xml.sax.SAXException;
More information about the jboss-svn-commits
mailing list