DotNet Reference

.Net Reference

NetDecisionBuilder.cs
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 using System;
15 using System.Collections;
16 
18 {
28  {
33  public override Decision NextWrapper(Solver solver)
34  {
35  try
36  {
37  return Next(solver);
38  }
39  catch (ApplicationException /*e*/)
40  {
41  // TODO(user): Catch only fail exceptions.
42  return solver.MakeFailDecision();
43  }
44  }
48  public virtual Decision Next(Solver solver)
49  {
50  return null;
51  }
52  }
53 
62  public class NetDecision : Decision
63  {
68  public override void ApplyWrapper(Solver solver)
69  {
70  try
71  {
72  Apply(solver);
73  }
74  catch (ApplicationException /*e*/)
75  {
76  // TODO(user): Catch only fail exceptions.
77  solver.ShouldFail();
78  }
79  }
83  public virtual void Apply(Solver solver)
84  {
85  // By default, do nothing
86  }
87 
88  public override void RefuteWrapper(Solver solver)
89  {
90  try
91  {
92  Refute(solver);
93  }
94  catch (ApplicationException /*e*/)
95  {
96  // TODO(user): Catch only fail exceptions.
97  solver.ShouldFail();
98  }
99  }
103  public virtual void Refute(Solver solver)
104  {
105  }
106  }
107 
108  public class NetDemon : Demon
109  {
113  public override void RunWrapper(Solver solver)
114  {
115  try
116  {
117  Run(solver);
118  }
119  catch (ApplicationException /*e*/)
120  {
121  // TODO(user): Check that this is indeed a fail. Try implementing
122  // custom exceptions (hard).
123  solver.ShouldFail();
124  }
125  }
129  public virtual void Run(Solver solver)
130  {
131  }
132  public override int Priority()
133  {
134  return Solver.NORMAL_PRIORITY;
135  }
136  public override string ToString()
137  {
138  return "NetDemon";
139  }
140  }
141 
142  public class NetConstraint : Constraint
143  {
144  public NetConstraint(Solver s) : base(s)
145  {
146  }
147 
148  public override void InitialPropagateWrapper()
149  {
150  try
151  {
153  }
154  catch (ApplicationException /*e*/)
155  {
156  solver().ShouldFail();
157  }
158  }
159  public virtual void InitialPropagate()
160  {
161  }
162  public override string ToString()
163  {
164  return "NetConstraint";
165  }
166  }
167 
168  public class IntVarEnumerator : IEnumerator
169  {
170  private IntVarIterator iterator_;
171 
172  // Enumerators are positioned before the first element
173  // until the first MoveNext() call.
174  private bool first_ = true;
175 
177  {
178  iterator_ = iterator;
179  }
180 
181  public bool MoveNext()
182  {
183  if (first_)
184  {
185  iterator_.Init();
186  first_ = false;
187  }
188  else
189  {
190  iterator_.Next();
191  }
192  return iterator_.Ok();
193  }
194 
195  public void Reset()
196  {
197  first_ = true;
198  }
199 
200  object IEnumerator.Current
201  {
202  get {
203  return Current;
204  }
205  }
206 
207  public long Current
208  {
209  get {
210  if (!first_ && iterator_.Ok())
211  {
212  return iterator_.Value();
213  }
214  else
215  {
216  throw new InvalidOperationException();
217  }
218  }
219  }
220  }
221 
222  public partial class IntVarIterator : BaseObject, IEnumerable
223  {
224  IEnumerator IEnumerable.GetEnumerator()
225  {
226  return (IEnumerator)GetEnumerator();
227  }
228 
230  {
231  return new IntVarEnumerator(this);
232  }
233  }
234 } // namespace Google.OrTools.ConstraintSolver
override void InitialPropagateWrapper()
void ShouldFail()
NetConstraint(Solver s)
virtual Decision Next(Solver solver)
This is the new method to subclass when defining a .Net decision builder.
Definition: Demon.cs:18
bool MoveNext()
virtual bool Ok()
override void RunWrapper(Solver solver)
This methods wraps the calls to next() and catches fail exceptions.
void Reset()
virtual void Refute(Solver solver)
This is a new method to subclass when defining a .Net decision.
using System
Definition: Program.cs:14
IntVarEnumerator(IntVarIterator iterator)
virtual long Value()
override string ToString()
This class acts as a intermediate step between a c++ decision builder and a .Net one.
override Decision NextWrapper(Solver solver)
This methods wraps the calls to next() and catches fail exceptions.
IntVarEnumerator GetEnumerator()
Solver solver()
virtual void InitialPropagate()
virtual void Next()
virtual void Run(Solver solver)
This is the new method to subclass when defining a .Net decision builder.
This class acts as a intermediate step between a c++ decision and a .Net one.
virtual void Init()
Decision MakeFailDecision()
virtual void Apply(Solver solver)
This is a new method to subclass when defining a .Net decision.
override int Priority()
override void RefuteWrapper(Solver solver)
override void ApplyWrapper(Solver solver)
This methods wraps the calls to Apply() and catches fail exceptions.
override string ToString()
static readonly int NORMAL_PRIORITY
Definition: BaseObject.cs:18
Definition: Decision.cs:18