Skip to content

Commit

Permalink
JBRULES-3261 testing fluent api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Proctor committed Dec 15, 2011
1 parent 270c30b commit 0fa06fa
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 deletions.
Expand Up @@ -16,6 +16,10 @@

package org.drools.command;

import java.util.HashMap;
import java.util.Map;

import org.drools.FactHandle;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderConfiguration;
import org.drools.builder.KnowledgeBuilderFactory;
Expand All @@ -24,23 +28,34 @@
import org.drools.command.Command;
import org.drools.command.Context;
import org.drools.command.impl.GenericCommand;
import org.drools.common.InternalFactHandle;
import org.drools.io.Resource;

public class GetVariableCommand
implements
GenericCommand<Object> {
private String identifier;
private String contextName;

public GetVariableCommand(String identifier) {
this.identifier = identifier;
}

public GetVariableCommand(String identifier,
String contextName) {
this.identifier = identifier;
this.contextName = contextName;
}

public Object execute(Context ctx) {
ctx = ctx.getContextManager().getContext( this.contextName );
return ctx.get( this.identifier );
public Object execute(Context ctx) {
Context targetCtx;
if ( this.contextName == null ) {
targetCtx = ctx;
} else {
targetCtx = ctx.getContextManager().getContext( this.contextName );
}

return targetCtx.get( identifier);
}

}
Expand Up @@ -45,8 +45,7 @@ public NewStatefulKnowledgeSessionCommand(KnowledgeSessionConfiguration ksession

public StatefulKnowledgeSession execute(Context context) {
KnowledgeBase kbase = ((KnowledgeCommandContext) context).getKnowledgeBase();
StatefulKnowledgeSession ksession = kbase
.newStatefulKnowledgeSession( this.ksessionConf, environment );
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession( this.ksessionConf, environment );

return ksession;
}
Expand Down
Expand Up @@ -58,7 +58,7 @@ public Object execute(Context context) {
Map<String, FactHandle> handles = ( Map<String, FactHandle> ) targetCtx.get( "h" );
if ( handles == null ) {
handles = new HashMap<String, FactHandle>();
context.set( "h", handles );
targetCtx.set( "h", handles );
}
handles.put( identifier, ( FactHandle ) o );

Expand Down
Expand Up @@ -62,44 +62,26 @@ public FixedKnowledgeCommandContext(Context context,
this.workingMemoryEntryPoint = workingMemoryEntryPoint;
}

/* (non-Javadoc)
* @see org.drools.command.impl.KnowledgeCommandContext#getKnowledgeBuilder()
*/
public KnowledgeBuilder getKnowledgeBuilder() {
return kbuilder;
}

/* (non-Javadoc)
* @see org.drools.command.impl.KnowledgeCommandContext#getKnowledgeBase()
*/
public KnowledgeBase getKnowledgeBase() {
return this.kbase;
}

/* (non-Javadoc)
* @see org.drools.command.impl.KnowledgeCommandContext#getStatefulKnowledgesession()
*/
public StatefulKnowledgeSession getStatefulKnowledgesession() {
return statefulKsession;
}

/* (non-Javadoc)
* @see org.drools.command.impl.KnowledgeCommandContext#getWorkItemManager()
*/
public WorkItemManager getWorkItemManager() {
return statefulKsession.getWorkItemManager();
}

/* (non-Javadoc)
* @see org.drools.command.impl.KnowledgeCommandContext#getExecutionResults()
*/
public ExecutionResults getExecutionResults() {
return this.kresults;
}

/* (non-Javadoc)
* @see org.drools.command.impl.KnowledgeCommandContext#getWorkingMemoryEntryPoint()
*/
public WorkingMemoryEntryPoint getWorkingMemoryEntryPoint() {
return workingMemoryEntryPoint;
}
Expand All @@ -108,6 +90,30 @@ public void setWorkingMemoryEntryPoint(WorkingMemoryEntryPoint workingMemoryEntr
this.workingMemoryEntryPoint = workingMemoryEntryPoint;
}

public KnowledgeBuilder getKbuilder() {
return kbuilder;
}

public void setKbuilder(KnowledgeBuilder kbuilder) {
this.kbuilder = kbuilder;
}

public KnowledgeBase getKbase() {
return kbase;
}

public void setKbase(KnowledgeBase kbase) {
this.kbase = kbase;
}

public StatefulKnowledgeSession getStatefulKsession() {
return statefulKsession;
}

public void setStatefulKsession(StatefulKnowledgeSession statefulKsession) {
this.statefulKsession = statefulKsession;
}

public ContextManager getContextManager() {
return context.getContextManager();
}
Expand Down
Expand Up @@ -10,16 +10,16 @@

public interface KnowledgeCommandContext extends Context{

public abstract KnowledgeBuilder getKnowledgeBuilder();
public KnowledgeBuilder getKnowledgeBuilder();

public abstract KnowledgeBase getKnowledgeBase();
public KnowledgeBase getKnowledgeBase();

public abstract StatefulKnowledgeSession getStatefulKnowledgesession();
public StatefulKnowledgeSession getStatefulKnowledgesession();

public abstract WorkItemManager getWorkItemManager();
public WorkItemManager getWorkItemManager();

public abstract ExecutionResults getExecutionResults();
public ExecutionResults getExecutionResults();

public abstract WorkingMemoryEntryPoint getWorkingMemoryEntryPoint();
public WorkingMemoryEntryPoint getWorkingMemoryEntryPoint();

}
Expand Up @@ -34,7 +34,7 @@
@XmlAccessorType(XmlAccessType.NONE)
public class SetGlobalCommand
implements
GenericCommand<Void> {
GenericCommand<Object> {

@XmlAttribute(required=true)
private String identifier;
Expand All @@ -58,7 +58,7 @@ public SetGlobalCommand(String identifier,
this.object = object;
}

public Void execute(Context context) {
public Object execute(Context context) {
StatefulKnowledgeSession ksession = ((KnowledgeCommandContext) context).getStatefulKnowledgesession();

if ( this.out ) {
Expand All @@ -68,7 +68,7 @@ public Void execute(Context context) {

ksession.setGlobal( this.identifier,
this.object );
return null;
return object;
}

public String getIdentifier() {
Expand Down

0 comments on commit 0fa06fa

Please sign in to comment.