forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetDecisionBuilder.cs
205 lines (189 loc) · 4.87 KB
/
NetDecisionBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2010-2018 Google LLC
// 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.
using System;
using System.Collections;
namespace Google.OrTools.ConstraintSolver
{
/**
* This class acts as a intermediate step between a c++ decision builder and a
* .Net one. Its main purpose is to catch the .Net application exception
* launched when a failure occurs during the Next() call, and to return
* silently a System.ApplicationException that will propagate the failure back
* to the C++ code.
*
*/
public class NetDecisionBuilder : DecisionBuilder
{
/**
* This methods wraps the calls to next() and catches fail exceptions.
* It currently catches all application exceptions.
*/
public override Decision NextWrapper(Solver solver)
{
try
{
return Next(solver);
}
catch (ApplicationException /*e*/)
{
// TODO(user): Catch only fail exceptions.
return solver.MakeFailDecision();
}
}
/**
* This is the new method to subclass when defining a .Net decision builder.
*/
public virtual Decision Next(Solver solver)
{
return null;
}
}
/**
* This class acts as a intermediate step between a c++ decision and a
* .Net one. Its main purpose is to catch the .Net application
* exception launched when a failure occurs during the
* Apply()/Refute() calls, and to set the ShouldFail() flag on the
* solver that will propagate the failure back to the C++ code.
*
*/
public class NetDecision : Decision
{
/**
* This methods wraps the calls to Apply() and catches fail exceptions.
* It currently catches all application exceptions.
*/
public override void ApplyWrapper(Solver solver)
{
try
{
Apply(solver);
}
catch (ApplicationException /*e*/)
{
// TODO(user): Catch only fail exceptions.
solver.ShouldFail();
}
}
/**
* This is a new method to subclass when defining a .Net decision.
*/
public virtual void Apply(Solver solver)
{
// By default, do nothing
}
public override void RefuteWrapper(Solver solver)
{
try
{
Refute(solver);
}
catch (ApplicationException /*e*/)
{
// TODO(user): Catch only fail exceptions.
solver.ShouldFail();
}
}
/**
* This is a new method to subclass when defining a .Net decision.
*/
public virtual void Refute(Solver solver)
{
}
}
public class NetDemon : Demon
{
/**
* This methods wraps the calls to next() and catches fail exceptions.
*/
public override void RunWrapper(Solver solver)
{
try
{
Run(solver);
}
catch (ApplicationException /*e*/)
{
// TODO(user): Check that this is indeed a fail. Try implementing
// custom exceptions (hard).
solver.ShouldFail();
}
}
/**
* This is the new method to subclass when defining a .Net decision builder.
*/
public virtual void Run(Solver solver) {}
public override int Priority() {
return Solver.NORMAL_PRIORITY;
}
public override string ToString() {
return "NetDemon";
}
}
public class NetConstraint : Constraint {
public NetConstraint(Solver s) : base(s) {}
public override void InitialPropagateWrapper() {
try {
InitialPropagate();
} catch (ApplicationException /*e*/) {
solver().ShouldFail();
}
}
public virtual void InitialPropagate() {}
public override string ToString() {
return "NetConstraint";
}
}
public class IntVarEnumerator : IEnumerator {
private IntVarIterator iterator_;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
private bool first_ = true;
public IntVarEnumerator(IntVarIterator iterator) {
iterator_ = iterator;
}
public bool MoveNext() {
if (first_) {
iterator_.Init();
first_ = false;
} else {
iterator_.Next();
}
return iterator_.Ok();
}
public void Reset() {
first_ = true;
}
object IEnumerator.Current {
get {
return Current;
}
}
public long Current {
get {
if (!first_ && iterator_.Ok()) {
return iterator_.Value();
} else {
throw new InvalidOperationException();
}
}
}
}
public partial class IntVarIterator : BaseObject, IEnumerable {
IEnumerator IEnumerable.GetEnumerator() {
return (IEnumerator) GetEnumerator();
}
public IntVarEnumerator GetEnumerator() {
return new IntVarEnumerator(this);
}
}
} // namespace Google.OrTools.ConstraintSolver