Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.drools.simulation.impl;
 
 import java.util.Map;
 import java.util.Set;
 
 
 public class Simulator
         implements WorldGetDefaultValue {
 
     private PriorityQueue<SimulationStep>           queue;
     private SimulationImpl                simulation;
     //    private SessionPseudoClock  clock;
     private long                          startTime;
 
     private Context                       root;
     private Map<StringContext>          contexts;
 
     private static String                 ROOT             = "ROOT";
 
 
 
     private Object                        lastReturnValue;
 
     public SimulatorSimulation simulation,
                       //SessionPseudoClock clock,
                       long startTime ) {
         //  this.clock = clock;
         this. = new HashSet<StatefulKnowledgeSession>();
 
         this. = startTime;
         this. = (SimulationImplsimulation;
         this. = new ContextImpl,
                                      this );
         
         this..set"simulator"
                        this );
 
         this. = new HashMap<StringContext>();
         this..put,
                            this. );
 
         Map<StringSimulationPathpaths = this..getPaths();
 
         // calculate capacity
         int capacity = 0;
         for ( SimulationPath path : paths.values() ) {
             this..putpath.getName(),
                                new ContextImplpath.getName(),
                                                 this,
                                                  ) );
 
             capacity += path.getSteps().size();
         }
 
         if ( capacity == 0 ) {
             return;
         }
 
         this. = new PriorityQueuecapacity,
                                         new Comparator<SimulationStep>() {
                                             public int compare(SimulationStep s1,
                                                                SimulationStep s2) {
                                                 return (int) (s1.getDistanceMillis() - s2.getDistanceMillis());
                                            }
                                        } );
        for ( SimulationPath path : paths.values() ) {
            for ( SimulationStep step : path.getSteps() )
                this..addstep );
        }
    }
    public void run() {
        SimulationStep step;
        while ( (step = executeNextStep()) != null ) {
        }
    }
    public SimulationStep executeNextStep() {
        if ( this..isEmpty() ) {
            return null;
        }
        SimulationStepImpl step = (SimulationStepImplthis..remove();
        SimulationPathImpl path = (SimulationPathImplstep.getPath();
        Context pathContext = new ResolvingKnowledgeCommandContextthis..getpath.getName() ) );
        // increment the clock for all the registered ksessions
        for ( StatefulKnowledgeSession ksession : this. ) {
            SessionPseudoClock clock = (SessionPseudoClockksession.getSessionClock();
            long newTime =  + step.getDistanceMillis();
            long currentTime = clock.getCurrentTime();
            clock.advanceTimenewTime - currentTime,
                               . );
        }
        for ( Command cmd : step.getCommands() ) {
            if ( cmd instanceof NewKieSessionCommand ) {
                // instantiate the ksession, set it's clock and register it
                StatefulKnowledgeSession ksession = (StatefulKnowledgeSession.execute( (GenericCommandcmd,
                                                                                                         pathContext );
                if ( ksession != null ) {
                    SessionPseudoClock clock = (SessionPseudoClockksession.getSessionClock();
                    long newTime =  + step.getDistanceMillis();
                    long currentTime = clock.getCurrentTime();
                    clock.advanceTimenewTime - currentTime,
                                       . );
                    this..addksession );
                    this. = ksession;
                }
            } else if ( cmd instanceof GenericCommand ) {
                this. = .execute( (GenericCommandcmd,
                                                                 pathContext );
            }
        }
        return step;
    }
    public void setCommandExecutionHandler(CommandExecutionHandler executionHandler) {
        this. = executionHandler;
    }
    public Context getContext(String identifier) {
        return this..getidentifier );
    }
    public Context getRootContext() {
        return this.;
    }
    public Simulation getSimulation() {
        return this.;
    }
    
    public Object getLastReturnValue() {
        return this.;
    }
    public static interface CommandExecutionHandler {
        public Object execute(GenericCommand command,
                              Context context);
    }
    public static class DefaultCommandExecutionHandler
        implements
        CommandExecutionHandler {
        public Object execute(GenericCommand command,
                              Context context) {
            return command.executecontext );
        }
    }
    public Object getObject() {
        return ;
    }
		return this;
	}
	public String getName() {
		return .getName();
	}
	public Object get(String identifier) {
		return .getidentifier );
	}
	public void set(String identifierObject value) {
		.setidentifiervalue );
	}
	public void remove(String identifier) {
		.removeidentifier );
	}
    //    public static interface CommandExecutorService<T> {
    //        T execute(Command command);
    //    }
    //    
    //    public static class SimulatorCommandExecutorService<T> implements CommandExecutorService {
    //        Map map = new HashMap() {
    //            {
    //               put( KnowledgeBuilderAddCommand.class, null);
    //            }
    //        };
    //        
    //        public  T execute(Command command) {
    //            return null;
    //        }
    //    }
    //    
    //    public static interface CommandContextAdapter {
    //        Context getContext();
    //    }
    //    
    //    public static class KnowledgeBuilderCommandContextAdapter implements CommandContextAdapter {
    //
    //        public Context getContext() {
    //            return new KnowledgeBuilderCommandContext();
    //        }
    //        
    //    }
    //    public void runUntil(SimulationStep step) {
    //        
    //    }
    //    
    //    public void runForTemporalDistance(long distance) {
    //        
    //    }
New to GrepCode? Check out our FAQ X