Skip to content

Commit

Permalink
[DROOLS-726] rewire mvel named consequences
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Feb 25, 2015
1 parent 117f78c commit 6604b35
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 24 deletions.
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
Expand All @@ -17,6 +18,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static java.util.Arrays.asList;

Expand Down Expand Up @@ -815,4 +817,49 @@ public void testNoLoop() {
assertEquals("t1", list.get(0));
assertEquals("t0", list.get(1));
}

@Test
public void testMvelInsertWithNamedConsequence() {
// DROOLS-726
String drl2 =
"package org.drools.compiler\n" +
"global java.util.concurrent.atomic.AtomicInteger counter\n" +
"declare Output\n" +
" feedback: String\n" +
"end\n" +
"rule \"Move to next\" dialect \"mvel\"\n" +
" when\n" +
" $i: Integer()\n" +
" if ($i == 1) break[nextStep1]\n" +
" then\n" +
" insert(new Output(\"defualt\"));\n" +
" then[nextStep1]\n" +
" insert(new Output(\"step 1\"));\n" +
"end\n" +
"\n" +
"rule \"Produce output\"\n" +
" when\n" +
" $output: Output()\n" +
" then\n" +
" System.out.println($output);\n" +
" retract($output);" +
" counter.incrementAndGet();\n" +
"end\n";

KieSession kSession = new KieHelper().addContent(drl2, ResourceType.DRL)
.build()
.newKieSession();

AtomicInteger counter = new AtomicInteger(0);
kSession.setGlobal("counter", counter);

FactHandle messageHandle = kSession.insert(1);
kSession.fireAllRules();

kSession.delete(messageHandle);
kSession.insert(2);
kSession.fireAllRules();

assertEquals(2, counter.get());
}
}
Expand Up @@ -16,6 +16,14 @@

package org.drools.core.rule;

import org.drools.core.base.mvel.MVELCompileable;
import org.drools.core.definitions.impl.KnowledgePackageImpl;
import org.drools.core.definitions.rule.impl.RuleImpl;
import org.drools.core.spi.Wireable;
import org.mvel2.ParserConfiguration;
import org.mvel2.integration.VariableResolver;
import org.mvel2.integration.impl.MapVariableResolverFactory;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
Expand All @@ -29,16 +37,8 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.drools.core.base.mvel.MVELCompileable;
import org.drools.core.definitions.impl.KnowledgePackageImpl;
import org.drools.core.definitions.rule.impl.RuleImpl;
import org.drools.core.spi.Wireable;
import org.mvel2.ParserConfiguration;
import org.mvel2.integration.VariableResolver;
import org.mvel2.integration.impl.MapVariableResolverFactory;
import java.util.Set;

public class MVELDialectRuntimeData
implements
Expand All @@ -49,7 +49,7 @@ public class MVELDialectRuntimeData

private final MapFunctionResolverFactory functionFactory;

private Map<Wireable, MVELCompileable> invokerLookups;
private Map<Wireable, List<MVELCompileable>> invokerLookups;
private Set<MVELCompileable> mvelReaders;

private ClassLoader rootClassLoader;
Expand All @@ -65,7 +65,7 @@ public class MVELDialectRuntimeData

public MVELDialectRuntimeData() {
this.functionFactory = new MapFunctionResolverFactory();
this.invokerLookups = new IdentityHashMap<Wireable, MVELCompileable>();
this.invokerLookups = new IdentityHashMap<Wireable, List<MVELCompileable>>();
this.mvelReaders = new HashSet<MVELCompileable> ();
this.imports = new HashMap<String, Object>();
this.packageImports = new HashSet<String>();
Expand All @@ -92,7 +92,7 @@ public void readExternal(ObjectInput in) throws IOException,
imports = (Map) in.readObject();
packageImports = (HashSet<String>) in.readObject();

invokerLookups = (Map<Wireable, MVELCompileable>) in.readObject();
invokerLookups = (Map<Wireable, List<MVELCompileable>>) in.readObject();
if ( !invokerLookups.isEmpty() ) {
// we need a wireList for serialisation
wireList = new ArrayList<Wireable>( invokerLookups.keySet() );
Expand Down Expand Up @@ -126,7 +126,7 @@ public void merge( DialectRuntimeRegistry registry,
}

this.packageImports.addAll( other.packageImports );
for ( Entry<Wireable, MVELCompileable> entry : other.invokerLookups.entrySet() ) {
for ( Entry<Wireable, List<MVELCompileable>> entry : other.invokerLookups.entrySet() ) {
invokerLookups.put( entry.getKey(),
entry.getValue() );
if ( this.wireList == Collections.<Wireable> emptyList() ) {
Expand Down Expand Up @@ -169,11 +169,12 @@ public void onRemove() {

public void onBeforeExecute() {
for ( Wireable target : wireList ) {
MVELCompileable compileable = invokerLookups.get( target );
compileable.compile( this );
for (MVELCompileable compileable : invokerLookups.get( target )) {
compileable.compile(this);

// now wire up the target
target.wire( compileable );
// now wire up the target
target.wire(compileable);
}
}
wireList.clear();

Expand Down Expand Up @@ -351,17 +352,17 @@ public void addPackageImport(String str) {
}

public void addCompileable(MVELCompileable compilable) {
this.mvelReaders.add( compilable );
this.mvelReaders.add(compilable);
}

public void addCompileable(Wireable wireable,
MVELCompileable compilable) {
invokerLookups.put( wireable,
compilable );
}

public Map<Wireable, MVELCompileable> getLookup() {
return this.invokerLookups;
List<MVELCompileable> compilables = invokerLookups.get(wireable);
if (compilables == null) {
compilables = new ArrayList<MVELCompileable>();
invokerLookups.put(wireable, compilables);
}
compilables.add( compilable );
}

public ClassLoader getRootClassLoader() {
Expand Down

0 comments on commit 6604b35

Please sign in to comment.