/*
* JaamSim Discrete Event Simulation
* Copyright (C) 2013 Ausenco Engineering Canada Inc.
* Copyright (C) 2016 JaamSim Software 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 com.jaamsim.ProcessFlow;
import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.Samples.SampleConstant;
import com.jaamsim.Samples.SampleInput;
import com.jaamsim.input.Keyword;
import com.jaamsim.units.TimeUnit;
/**
* Server processes entities one by one from a queue. When finished with an entity, it passes it to the next
* LinkedComponent in the chain.
*/
public class Server extends LinkedService {
@Keyword(description = "The service time required to process an entity.\n" +
"A constant value, a distribution to be sampled, or a time series can be entered.",
exampleList = { "3.0 h", "NormalDistribution1", "'1[s] + 0.5*[TimeSeries1].PresentValue'" })
private final SampleInput serviceTime;
private DisplayEntity servedEntity; // the DisplayEntity being server
{
serviceTime = new SampleInput("ServiceTime", "Key Inputs", new SampleConstant(TimeUnit.class, 0.0));
serviceTime.setUnitType(TimeUnit.class);
serviceTime.setEntity(this);
serviceTime.setValidRange(0, Double.POSITIVE_INFINITY);
this.addInput(serviceTime);
}
public Server() {}
@Override
public void earlyInit() {
super.earlyInit();
servedEntity = null;
}
@Override
protected boolean startProcessing(double simTime) {
// Determine the match value
Integer m = this.getNextMatchValue(getSimTime());
this.setMatchValue(m);
// Stop if the queue is empty
if (waitQueue.getValue().getMatchCount(m) == 0) {
return false;
}
// Remove the first entity from the queue
servedEntity = this.getNextEntityForMatch(m);
this.moveToProcessPosition(servedEntity);
return true;
}
@Override
protected boolean processStep(double simTime) {
// Send the entity to the next component in the chain
this.sendToNextComponent(servedEntity);
servedEntity = null;
return true;
}
@Override
protected double getStepDuration(double simTime) {
return serviceTime.getValue().getNextSample(simTime);
}
}