Code Examples
A repository of 155 code examples for BeepBeep
Formula1.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2022 Sylvain Hallé
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package qbf;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.functions.ApplyFunction;
22 import ca.uqac.lif.cep.functions.FunctionTree;
23 import ca.uqac.lif.cep.functions.StreamVariable;
24 import ca.uqac.lif.cep.io.Print;
25 import ca.uqac.lif.cep.tmf.Fork;
26 import ca.uqac.lif.cep.tmf.Pump;
27 import ca.uqac.lif.cep.tmf.QueueSource;
28 import ca.uqac.lif.cep.util.Booleans;
31 
32 import static ca.uqac.lif.cep.Connector.connect;
33 
34 /**
35  * Solves the QBF problem for the formula:
36  * <blockquote>
37  * &forall;x &exist;y &exist;z : (x &or; y) &and; (&not;x &or; c) &and; (&not;y &or; &not; z)
38  * </blockquote>
39  * The pipeline to solve this problem is the following:
40  * <p>
41  * <img src="./doc-files/qbf/Formula1.png" alt="Pipeline">
42  * @author Sylvain Hallé
43  */
44 public class Formula1
45 {
46  public static void main(String[] args)
47  {
48  /* Create a source that repeatedly alternates between the values true and
49  * false. */
50  QueueSource qs = new QueueSource().setEvents(true, false).loop(true);
51 
52  /* Add a pump pushing events from the source downstream. */
53  Pump pump = new Pump();
54  Connector.connect(qs, pump);
55 
56  /* Fork the input and create the processors that enumerate the combinations
57  * of values for each variable. */
58  Fork f = new Fork(3);
59  connect(pump, f);
60  InputVariable x1 = new InputVariable(3, 1);
61  InputVariable x2 = new InputVariable(3, 2);
62  InputVariable x3 = new InputVariable(3, 3);
63  connect(f, 0, x1, 0);
64  connect(f, 1, x2, 0);
65  connect(f, 2, x3, 0);
66 
67  /* Connect the inputs to the Boolean function to evaluate. */
68  ApplyFunction af = new ApplyFunction(new Expression());
69  connect(x1, 0, af, 0);
70  connect(x2, 0, af, 1);
71  connect(x3, 0, af, 2);
72 
73  /* Link the output of this formula to the constructs for each quantified
74  * variable, starting from the innermost. */
75  ExistentialVariable exists_x3 = new ExistentialVariable();
76  connect(af, exists_x3);
77  ExistentialVariable exists_x2 = new ExistentialVariable();
78  connect(exists_x3, exists_x2);
79  UniversalVariable forall_x1 = new UniversalVariable();
80  connect(exists_x2, forall_x1);
81 
82  /* Connect the output of the pipline to a Print processor to display the
83  * result to the standard output. */
84  Print p = new Print();
85  Connector.connect(forall_x1, p);
86 
87  /* Since there are 3 variables, turn the crank on the pump 2^3 = 8 times to
88  * enumerate all possible valuations. Turning the crank fewer than 8 times
89  * will result in no output being produced. */
90  pump.turn(8);
91  }
92 
93  /**
94  * Representation of the Boolean expression
95  * (x &or; y) &and; (&not;x &or; c) &and; (&not;y &or; &not; z).
96  */
97  protected static class Expression extends FunctionTree
98  {
99  public Expression()
100  {
101  super(Booleans.and,
102  new FunctionTree(Booleans.and,
103  new FunctionTree(Booleans.or, StreamVariable.X, StreamVariable.Y),
104  new FunctionTree(Booleans.or,
105  new FunctionTree(Booleans.not, StreamVariable.X), StreamVariable.Z)
106  ),
107  new FunctionTree(Booleans.or,
108  new FunctionTree(Booleans.not, StreamVariable.Y),
109  new FunctionTree(Booleans.not, StreamVariable.Z))
110  );
111  }
112  }
113 
114 }
Solves the QBF problem for the formula: ∀x ∃y ∃z : (x ∨ y) ∧ (¬x ∨ c) ∧ (¬y ∨ ¬ z) The pipeli...
Definition: Formula1.java:44
Shows how a specially designed pipeline can solve the satisfiability problem for True Quantified Bool...
Definition: Formula1.java:18