Code Examples
A repository of 155 code examples for BeepBeep
EngineOverheatSimple.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2019 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 mining.learning;
19 
20 import static ca.uqac.lif.cep.Connector.connect;
21 
22 import ca.uqac.lif.cep.Connector;
23 import ca.uqac.lif.cep.functions.ApplyFunction;
24 import ca.uqac.lif.cep.functions.Constant;
25 import ca.uqac.lif.cep.functions.FunctionTree;
26 import ca.uqac.lif.cep.functions.IdentityFunction;
27 import ca.uqac.lif.cep.functions.IfThenElse;
28 import ca.uqac.lif.cep.functions.StreamVariable;
29 import ca.uqac.lif.cep.graphviz.CallGraphviz;
30 import ca.uqac.lif.cep.io.ReadLines;
31 import ca.uqac.lif.cep.peg.forecast.SelfLearningPrediction;
32 import ca.uqac.lif.cep.peg.weka.UpdateClassifier;
33 import ca.uqac.lif.cep.peg.weka.WekaUtils;
34 import ca.uqac.lif.cep.tmf.Pump;
35 import ca.uqac.lif.cep.tuples.FetchAttribute;
36 import ca.uqac.lif.cep.tuples.TupleFeeder;
37 import ca.uqac.lif.cep.util.Bags;
38 import ca.uqac.lif.cep.util.Numbers;
39 import java.io.InputStream;
40 import plots.BitmapJFrame;
41 import weka.classifiers.trees.J48;
42 import weka.core.Attribute;
43 
44 /**
45  * Train a classifier to predict the overheating of an engine.
46  * <p>
47  * In this example, our input stream is a log of data obtained from a
48  * (fake) combustion engine. Each datapoint is made of a timestamp,
49  * the current speed (RPM) of the engine, the current gear of the
50  * transmission, and the current engine temperature.
51  */
53 {
54  public static void main(String[] args) throws Exception
55  {
56  // Read tuples from the file
57  InputStream is = EngineOverheatSimple.class.getResourceAsStream("Engine.csv");
58  ReadLines reader = new ReadLines(is);
59  TupleFeeder tuples = new TupleFeeder();
60  connect(reader, tuples);
61 
62  // The beta processor extracts the value of attributes RPM and Gr
63  // and puts them into an array
64  ApplyFunction beta = new ApplyFunction(new FunctionTree(
65  new Bags.ToArray(Number.class, String.class),
66  new FunctionTree(Numbers.numberCast, new FetchAttribute("RPM")),
67  new FunctionTree(new FetchAttribute("Gr"), StreamVariable.X)
68  ));
69 
70  // The kappa processor fetches the value of attribute Temp, and
71  // checks if it is greater than 60; if so, it returns "overheat",
72  // otherwise it returns "normal"
73  ApplyFunction kappa = new ApplyFunction(new FunctionTree(IfThenElse.instance,
74  new FunctionTree(Numbers.isGreaterThan,
75  new FunctionTree(Numbers.numberCast, new FetchAttribute("Temp")),
76  new Constant(60)),
77  new Constant("overheat"),
78  new Constant("normal")
79  ));
80 
81  // The learning attributes for the classifier will be called RPM,
82  // Gr and Overheat
83  Attribute[] attributes = new Attribute[] {
84  new Attribute("RPM"),
85  WekaUtils.createAttribute("Gr", "N", "1", "2", "3", "4"),
86  WekaUtils.createAttribute("State", "overheat", "normal")
87  };
88 
89  // Update a J48 classifier using input events and these attributes
90  UpdateClassifier uc = new UpdateClassifier(new J48(), "Engine", attributes);
91 
92  // Train a classifier by comparing windows of width 1, offset by 3 events
93  SelfLearningPrediction ct = new SelfLearningPrediction(new IdentityFunction(1), beta, 3, 1, kappa, 1, uc);
94  connect(tuples, ct);
95 
96  // Just to be fancy, let's draw the classifier
97  ApplyFunction get_graph = new ApplyFunction(WekaUtils.GetGraph.instance);
98  connect(ct, get_graph);
99  CallGraphviz cg = new CallGraphviz().use(CallGraphviz.Renderer.DOT);
100  connect(get_graph, cg);
101 
102  // Pump the drawing into a widget to display it
103  Pump pump = new Pump(500);
104  connect(cg, pump);
105  BitmapJFrame plot_frame = new BitmapJFrame();
106  Connector.connect(pump, plot_frame);
107 
108  // Let's see what comes out
109  plot_frame.start();
110  pump.start();
111  }
112 }
Manipulate tuples.
Generate tables, charts and plots from event streams.
Train a classifier to predict the overheating of an engine.
Receives a byte array as an input, and shows it in a Swing window as a picture.