[rules-users] NPE on fact insertion if rule has sliding window

John jfullard at progeny.net
Tue Jul 12 13:32:51 EDT 2011


Hello,

I've been messing with this for days now and can't get to the bottom of
this.  I'm using drools 5.2.0 Final and setting up my KnowledgeBase with a
KnowledgeAgent which is pulling a package from guvnor (also 5.2.0 Final).  I
do NOT get this error if I use a KnowledgeBuilder and add the exact same drl
file as a resource.

If the DRL in guvnor has a rule that uses a sliding window, i get an NPE
when trying to insert an object and the following stack trace:

java.lang.NullPointerException
	at
org.drools.reteoo.ReteooFactHandleFactory.newFactHandle(ReteooFactHandleFactory.java:56)
	at
org.drools.common.AbstractFactHandleFactory.newFactHandle(AbstractFactHandleFactory.java:80)
	at
org.drools.common.AbstractFactHandleFactory.newFactHandle(AbstractFactHandleFactory.java:65)
	at org.drools.common.NamedEntryPoint.createHandle(NamedEntryPoint.java:705)
	at org.drools.common.NamedEntryPoint.insert(NamedEntryPoint.java:282)
	at
org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:905)
	at
org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:864)
	at
org.drools.impl.StatefulKnowledgeSessionImpl.insert(StatefulKnowledgeSessionImpl.java:255)
	at john.DroolsError.runRules(DroolsError.java:48)
	at john.DroolsError.main(DroolsError.java:40)

Here is the DRL:

1.	|	package drools
2.	|	import simple.SimpleType
3.	|	
4.	|	
5.	|	
6.	|	
7.	|	
8.	|	
9.	|	import simple.SimpleType;
10.	|	
11.	|	declare SimpleType
12.	|	    @role(event)
13.	|	end
14.	|	
15.	|	rule "timer"
16.	|	timer (cron:5/5 * * * * ?)
17.	|	when
18.	|	    $newest : Number()
19.	|	        from accumulate( SimpleType ($num : number) over
window:time(5s),
20.	|	            max($num) )
21.	|	    $st : SimpleType( number == $newest.intValue )
22.	|	then
23.	|	    System.out.println("SimpleType: " + $st.number);
24.	|	end
(note: the first import of simple.SimpleType comes from the package
configuration in guvnor, the second is in the drl file itself. the file will
not validate without the import explicitly listed there, saying the type is
not declared as an event...)

Here is the Java code:

ResourceChangeScannerConfiguration sconf = ResourceFactory
				.getResourceChangeScannerService()
				.newResourceChangeScannerConfiguration();
		sconf.setProperty("drools.resource.scanner.interval", "20");
		ResourceFactory.getResourceChangeScannerService().configure(sconf);

		KnowledgeBaseConfiguration config = KnowledgeBaseFactory
				.newKnowledgeBaseConfiguration();
		config.setOption(EventProcessingOption.STREAM);

		KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory
				.newKnowledgeAgentConfiguration();
		aconf.setProperty("drools.agent.scanDirectories", "true");
		aconf.setProperty("drools.agent.scanResources", "true");
		aconf.setProperty("drools.agent.newInstance", "false");
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config);
		KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent(
				"drools agent", kbase, aconf);

		String urlString =
"http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/drools/LATEST/ChangeSet.xml";
		try {
			URL url = new URL(urlString);
			UrlResource urlResource = (UrlResource) ResourceFactory
					.newUrlResource(url);
			urlResource.setBasicAuthentication("enabled");
			urlResource.setUsername("John");
			urlResource.setPassword("");
			kagent.applyChangeSet(urlResource);
		} catch (Exception e) {
			System.out.println("Caught exception: " + e.getMessage());
			e.printStackTrace();
		}

		ResourceFactory.getResourceChangeNotifierService().start();
		ResourceFactory.getResourceChangeScannerService().start();

		StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
		System.out.println("Initialize drools complete");

		SimpleType simp = new SimpleType();
		simp.setNumber(5);

		ksession.insert(simp);

Thanks,
John

--
View this message in context: http://drools.46999.n3.nabble.com/NPE-on-fact-insertion-if-rule-has-sliding-window-tp3163261p3163261.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list