DotNet Reference

DotNet 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  public override int Priority() {
131  return Solver.NORMAL_PRIORITY;
132  }
133  public override string ToString() {
134  return "NetDemon";
135  }
136 }
137 
138 public class NetConstraint : Constraint {
139  public NetConstraint(Solver s) : base(s) {}
140 
141  public override void InitialPropagateWrapper() {
142  try {
144  } catch (ApplicationException /*e*/) {
145  solver().ShouldFail();
146  }
147  }
148  public virtual void InitialPropagate() {}
149  public override string ToString() {
150  return "NetConstraint";
151  }
152 }
153 
154 public class IntVarEnumerator : IEnumerator {
155  private IntVarIterator iterator_;
156 
157  // Enumerators are positioned before the first element
158  // until the first MoveNext() call.
159  private bool first_ = true;
160 
161  public IntVarEnumerator(IntVarIterator iterator) {
162  iterator_ = iterator;
163  }
164 
165  public bool MoveNext() {
166  if (first_) {
167  iterator_.Init();
168  first_ = false;
169  } else {
170  iterator_.Next();
171  }
172  return iterator_.Ok();
173  }
174 
175  public void Reset() {
176  first_ = true;
177  }
178 
179  object IEnumerator.Current {
180  get {
181  return Current;
182  }
183  }
184 
185  public long Current {
186  get {
187  if (!first_ && iterator_.Ok()) {
188  return iterator_.Value();
189  } else {
190  throw new InvalidOperationException();
191  }
192  }
193  }
194 }
195 
196 public partial class IntVarIterator : BaseObject, IEnumerable {
197  IEnumerator IEnumerable.GetEnumerator() {
198  return (IEnumerator) GetEnumerator();
199  }
200 
202  return new IntVarEnumerator(this);
203  }
204 }
205 } // 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()
Definition: Assignment.cs:11
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.
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()
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()
Solver solver()
static readonly int NORMAL_PRIORITY
Definition: Decision.cs:18