Java Reference

Java Reference

BoolVar.java
Go to the documentation of this file.
1 // Copyright 2010-2021 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 package com.google.ortools.sat;
15 
18 
20 public final class BoolVar extends IntVar implements Literal {
21  BoolVar(CpModelProto.Builder builder, Domain domain, String name) {
22  super(builder, domain, name);
23  this.negation = null;
24  }
25 
26  BoolVar(CpModelProto.Builder builder, int index) {
27  super(builder, index);
28  this.negation = null;
29  }
30 
32  @Override
33  public Literal not() {
34  if (negation == null) {
35  negation = new NotBoolVar(this);
36  }
37  return negation;
38  }
39 
40  @Override
41  public String toString() {
42  if (varBuilder.getName().isEmpty()) {
44  if (varBuilder.getDomain(0) == 0) {
45  return "false";
46  } else {
47  return "true";
48  }
49  } else {
50  return String.format("boolvar_%d(%s)", getIndex(), displayBounds());
51  }
52  } else {
54  if (varBuilder.getDomain(0) == 0) {
55  return String.format("%s(false)", varBuilder.getName());
56  } else {
57  return String.format("%s(true)", varBuilder.getName());
58  }
59  } else {
60  return String.format("%s(%s)", getName(), displayBounds());
61  }
62  }
63  }
64 
65  private NotBoolVar negation = null;
66 }
Literal not()
Returns the negation of a boolean variable.
Definition: BoolVar.java:33
An Boolean variable.
Definition: BoolVar.java:20
String displayBounds()
Returns the domain as a string without the enclosing [].
Interface to describe a boolean variable or its negation.
Definition: Literal.java:17
final IntegerVariableProto.Builder varBuilder
The negation of a boolean variable.
Definition: NotBoolVar.java:20
String getName()
Returns the name of the variable given upon creation.
int getIndex()
Returns the index of the variable in the underlying CpModelProto.
We call domain any subset of Int64 = [kint64min, kint64max].