#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Common;
using HeuristicLab.PDPSimulation.DomainModel;
namespace HeuristicLab.PDPSimulation {
[StorableClass]
public abstract class PDAction: Item {
[Storable]
private Guid baseObjectId;
public Guid BaseObjectId {
get { return baseObjectId; }
}
[Storable]
protected PDAction successor;
public PDAction Successor {
get { return successor; }
}
[Storable]
protected bool started;
public void AppendAction(PDAction action, bool recursive) {
if (!action.BaseObjectId.Equals(this.BaseObjectId))
throw new Exception("Can only append actions of the same object");
PDAction predecessor = this;
while (predecessor.successor != null &&
(recursive || predecessor.ActionCompleted()))
predecessor = predecessor.successor;
predecessor.successor = action;
}
public bool IsCompleted {
get {
return ActionCompleted() &&
(successor == null || successor.IsCompleted);
}
}
protected abstract bool ActionInterruptable();
public bool Interruptable() {
PDAction action = this;
while (action != null && action.ActionCompleted())
action = action.successor;
return action == null || !action.started || action.ActionInterruptable();
}
public abstract bool ActionCompleted();
protected abstract double Perform(BaseObject baseObject, double simulationTime, double tickTime, List changes);
public List Perform(BaseObject baseObject, double simulationTime, double timeStep) {
List changes = new List();
lock (this) {
double startTime = simulationTime;
double tickTime = timeStep;
bool complete = ActionCompleted();
if (!complete && tickTime > 0) {
started = true;
simulationTime = Perform(baseObject, simulationTime, tickTime, changes);
complete = ActionCompleted();
}
PDAction next = successor;
while (complete && next != null) {
tickTime = timeStep - (simulationTime - startTime);
complete = next.ActionCompleted();
if (!complete && tickTime > 0) {
next.started = true;
simulationTime = next.Perform(baseObject, simulationTime, tickTime, changes);
complete = next.ActionCompleted();
}
next = next.successor;
}
}
return changes;
}
public virtual bool Obsoletes(PDAction action) {
return this.BaseObjectId.Equals(action.BaseObjectId);
}
protected PDAction(Guid baseObjectId) {
this.baseObjectId = baseObjectId;
}
[StorableConstructor]
protected PDAction(bool deserializing) : base(deserializing) { }
protected PDAction(PDAction original, Cloner cloner)
: base(original, cloner) {
baseObjectId = original.baseObjectId;
successor = cloner.Clone(original.successor);
started = original.started;
}
}
}