1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.PDPSimulation.DomainModel;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.PDPSimulation {
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class PDAction: Item {
|
---|
34 | [Storable]
|
---|
35 | private Guid baseObjectId;
|
---|
36 |
|
---|
37 | public Guid BaseObjectId {
|
---|
38 | get { return baseObjectId; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | protected PDAction successor;
|
---|
43 |
|
---|
44 | public PDAction Successor {
|
---|
45 | get { return successor; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void AppendAction(PDAction action, bool recursive) {
|
---|
49 | if (!action.BaseObjectId.Equals(this.BaseObjectId))
|
---|
50 | throw new Exception("Can only append actions of the same object");
|
---|
51 |
|
---|
52 | PDAction predecessor = this;
|
---|
53 | while (predecessor.successor != null &&
|
---|
54 | (recursive || predecessor.ActionCompleted()))
|
---|
55 | predecessor = predecessor.successor;
|
---|
56 |
|
---|
57 | predecessor.successor = action;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public bool IsCompleted {
|
---|
61 | get {
|
---|
62 | return ActionCompleted() &&
|
---|
63 | (successor == null || successor.IsCompleted);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected abstract bool ActionInterruptable();
|
---|
68 |
|
---|
69 | public bool Interruptable() {
|
---|
70 | PDAction action = this;
|
---|
71 |
|
---|
72 | while (action != null && action.ActionCompleted())
|
---|
73 | action = action.successor;
|
---|
74 |
|
---|
75 | return action == null || action.ActionInterruptable();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public abstract bool ActionCompleted();
|
---|
79 |
|
---|
80 | protected abstract double Perform(BaseObject baseObject, double simulationTime, double tickTime, List<PDChange> changes);
|
---|
81 |
|
---|
82 | public List<PDChange> Perform(BaseObject baseObject, double simulationTime, double timeStep) {
|
---|
83 | List<PDChange> changes = new List<PDChange>();
|
---|
84 |
|
---|
85 | lock (this) {
|
---|
86 | double startTime = simulationTime;
|
---|
87 |
|
---|
88 | double tickTime = timeStep;
|
---|
89 | bool complete = ActionCompleted();
|
---|
90 | if (!complete && tickTime > 0) {
|
---|
91 | simulationTime = Perform(baseObject, simulationTime, tickTime, changes);
|
---|
92 | complete = ActionCompleted();
|
---|
93 | }
|
---|
94 |
|
---|
95 | PDAction next = successor;
|
---|
96 | while (complete && next != null) {
|
---|
97 | tickTime = timeStep - (simulationTime - startTime);
|
---|
98 |
|
---|
99 | complete = next.ActionCompleted();
|
---|
100 | if (!complete && tickTime > 0) {
|
---|
101 | simulationTime = next.Perform(baseObject, simulationTime, tickTime, changes);
|
---|
102 | complete = next.ActionCompleted();
|
---|
103 | }
|
---|
104 |
|
---|
105 | next = next.successor;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | return changes;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public virtual bool Obsoletes(PDAction action) {
|
---|
113 | return this.BaseObjectId.Equals(action.BaseObjectId);
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected PDAction(Guid baseObjectId) {
|
---|
117 | this.baseObjectId = baseObjectId;
|
---|
118 | }
|
---|
119 |
|
---|
120 | [StorableConstructor]
|
---|
121 | protected PDAction(bool deserializing) : base(deserializing) { }
|
---|
122 | protected PDAction(PDAction original, Cloner cloner)
|
---|
123 | : base(original, cloner) {
|
---|
124 | baseObjectId = original.baseObjectId;
|
---|
125 | successor = cloner.Clone<PDAction>(original.successor);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|