-
Notifications
You must be signed in to change notification settings - Fork 0
/
Addformulalogic.java
338 lines (294 loc) · 11.4 KB
/
Addformulalogic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package sample;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Modality;
import javafx.stage.Stage;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;
import net.objecthunter.exp4j.ValidationResult;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Created by Nykyta on 7/11/2017.
*/
public class Addformulalogic implements Initializable {
@FXML ChoiceBox<String> outputUnitChoiceBox;
@FXML TextField formulaContentIN;
@FXML TextFlow formulaPreview;
@FXML Label legalLabel;
@FXML TextField formulaNameIN;
@FXML VBox vBox1;
@FXML Button addUnitButton;
@FXML VBox vBox2;
@FXML Button addButton;
@FXML Pane addFormulaPane;
@FXML CheckBox littleStarCheckBox;
@FXML CheckBox bypassCheckBox;
private static final Pattern LITTLE_STAR=Pattern.compile("([\\d][A-Za-z])+|([A-Za-z][\\d])+");
private static final Pattern LETTERS=Pattern.compile("[A-Za-z]+");
ObservableList<Label> labels=FXCollections.observableArrayList();
ObservableList<ChoiceBox<String>> unitChoiceBoxes =FXCollections.observableArrayList();
ObservableList<String> parameters= FXCollections.observableArrayList();
ArrayList<Text> formulaPreviewArray=new ArrayList();
private boolean isLegal=false;
StringBuilder worked;
Matcher matcher;
Pattern oneSymbolAtOnce=Pattern.compile("(-?[\\dA-Za-z\\(\\)]+[\\*\\-\\/\\+\\.\\^])+([\\dA-Za-z\\(\\)])+");
@Override
public void initialize(URL location, ResourceBundle resources) {
updateFormulaPreview();
outputUnitChoiceBox.getItems().addAll(Database.unitsNamesForChoiceBoxList);
outputUnitChoiceBox.getSelectionModel().select(0);
legalLabel.setFont(Font.font("System",FontWeight.BOLD,16));
legalLabel.setText("Congratulations! Now write your formula");
legalLabel.setTextFill(Color.GREEN);
littleStarCheckBox.setSelected(true);
}
@FXML void writeFormulaToDatabase(){
if(isLegal) {
Database.addFormula(formulaNameIN.getText(), worked.toString(), outputUnitChoiceBox.getValue());
for(int z=0;z<unitChoiceBoxes.size();z++) {
Database.addParameter(formulaNameIN.getText(),labels.get(z).getText(),unitChoiceBoxes.get(z).getValue());
}
Database.instantiateTables();
Stage stage = (Stage) addButton.getScene().getWindow();
stage.close();
Dialogs.createDialog("Formula successfully saved!","information");
}
}
@FXML void updateFormulaName(){
checkIfLegal();
}
@FXML void updateFormulaPreview() {
worked = new StringBuilder(formulaContentIN.getText());
if(!littleStarCheckBox.isSelected()) {
//Adds *
matcher = LITTLE_STAR.matcher(worked.toString());
while (matcher.find()) {
worked.insert(matcher.start() + 1, '*');
matcher = LITTLE_STAR.matcher(worked.toString());
}
}
//Filters texts
formulaPreviewArray.clear();
for(int i=0;i<worked.length();i++){
Text temporary=new Text();
if(Character.toString(worked.charAt(i)).matches("[A-Za-z]")){
temporary.setFill(Color.BLUE);
temporary.setFont(Font.font("System",FontWeight.BOLD,16));
temporary.setText(Character.toString(worked.charAt(i)));
formulaPreviewArray.add(temporary);
}
else {
temporary.setFont(Font.font("System",16));
temporary.setText(Character.toString(worked.charAt(i)));
formulaPreviewArray.add(temporary);
}
}
//Adds all children to TextFlow
formulaPreview.getChildren().clear();
for(int i=0;i<formulaPreviewArray.size();i++){
formulaPreview.getChildren().add(formulaPreviewArray.get(i));
}
//update Hboxes
updateHboxes();
//check if legal
checkIfLegal();
}
void updateHboxes(){
parameters.clear();
vBox1.getChildren().clear();
vBox2.getChildren().clear();
unitChoiceBoxes.clear();
labels.clear();
matcher=LETTERS.matcher(worked);
while(matcher.find()){
if(!(worked.substring(matcher.start(), matcher.end()).matches("abs|acos|asin|atan|cbrt|ceil|cosh|exp|floor|log|log10|log2|sinh|tanh|signum|sin|cos|tan|sqrt"))) {
parameters.add(worked.substring(matcher.start(), matcher.end()));
}
}
parameters = FXCollections.observableArrayList(parameters.stream().distinct().collect(Collectors.toList()));
for(int i=0;i<parameters.size();i++){
labels.add(new Label(parameters.get(i)));
labels.get(i).setTextFill(Color.BLUE);
labels.get(i).setFont(Font.font("System",FontWeight.BOLD,15));
unitChoiceBoxes.add(new ChoiceBox<String>());
unitChoiceBoxes.get(i).setTooltip(new Tooltip("Select unit please"));
unitChoiceBoxes.get(i).getItems().addAll(Database.unitsNamesForChoiceBoxList);
unitChoiceBoxes.get(i).getSelectionModel().select(0);
unitChoiceBoxes.get(i).getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> checkIfLegal());
}
vBox1.getChildren().addAll(labels);
vBox2.getChildren().addAll(unitChoiceBoxes);
}
boolean performSameParametersCheck(){
for(int y=0;y<parameters.size();y++){
int test=0;
for(int x=0;x<parameters.size();x++){
if(parameters.get(x).equals(parameters.get(y))){
test++;
if(test==2){
return false;
}
}
}
}
return true;
}
public void checkIfLegal(){
boolean onlyLettersInName=true;
boolean noSameFormulaName=true;
boolean noNoData=true;
boolean isFormulaValid=true;
boolean onlyOneSymbolAtOnce=true;
boolean atLeastOneParam=true;
performMax8ParametersCheck();
if(performNo2SignsInARowCheck()){
onlyOneSymbolAtOnce=false;
legalLabel.setText("Illegal - 2 signs in a row (or something else)");
}
if(checkForDatas()){
noNoData=false;
legalLabel.setText("Illegal - create a unit before !");
}
if(performNoSameFormulaCheck()){
noSameFormulaName=false;
legalLabel.setText("Illegal - formula with this name already exists ");
}
if(performLetterCheckInFormulName()){
onlyLettersInName=false;
legalLabel.setText("Illegal - invalid formula name field");
}
if(atLeastOneParameter()){
atLeastOneParam=false;
legalLabel.setText("Illegal - one parameter minimum");
}
if(checkExpression()){
isFormulaValid=false;
legalLabel.setText("Illegal - something is not right in the formula");
}
if((atLeastOneParam&&onlyLettersInName&&noSameFormulaName&&isFormulaValid&&noNoData)|| bypassCheckBox.isSelected()==true){
isLegal = true;
addButton.setDisable(false);
if(onlyOneSymbolAtOnce) {
legalLabel.setTextFill(Color.GREEN);
legalLabel.setText("Congratulations, you are in law !");
}
else {
legalLabel.setTextFill(Color.ORANGE);
legalLabel.setText("Looks fishy but OK");
}
}
else {
legalLabel.setTextFill(Color.RED);
addButton.setDisable(true);
isLegal=false;
}
}
boolean performNo2SignsInARowCheck() {
matcher=oneSymbolAtOnce.matcher(worked.toString());
if(matcher.matches()){
return false;
}
else {
return true;
}
}
boolean performNoSameFormulaCheck(){
long amount= Database.formulaList.stream().filter(formula-> formula.getFormulaName().equals(formulaNameIN.getText())).count();
if(amount==0){
return false;
}
else {return true;}
}
boolean atLeastOneParameter(){
if(parameters.isEmpty()) {
return true;
}
else { return false;}
}
boolean atLeastOneParaameter(){
for(int i=0;i<unitChoiceBoxes.size();i++){
if(!(unitChoiceBoxes.get(i).getValue().equals("Select a unit, don't change anything if it isn't a parameter(notations supported by exp4j)"))){
return false;
}
}
return true;
}
boolean checkForDatas(){
if(Database.unitsNamesForChoiceBoxList.get(0).equals("(No Data)")){
return true;
}
else {return false;}
}
void performMax8ParametersCheck() {
if (parameters.size() >= 8) {
worked.setLength(0);
vBox1.getChildren().clear();
vBox2.getChildren().clear();
formulaContentIN.clear();
formulaPreview.getChildren().clear();
Dialogs.createDialog("Max 8 parameters!!!", "error");
}
}
boolean checkExpression(){
try {
Expression e = new ExpressionBuilder(worked.toString()).variables(new HashSet<String>(parameters)).build();
ValidationResult val = e.validate(false);
if (val.isValid()) {
return false;
}
else{ return true; }
}
catch (RuntimeException ex){
return true;
}
}
boolean performLetterCheckInFormulName(){
if(formulaNameIN.getText().matches("([A-Za-z\\d\\ ])+")){
return false;
}
else {return true;}
}
@FXML void addUnit(){
try {
Stage superStage = new Stage();
Parent superParent = FXMLLoader.load(getClass().getResource("Units.fxml"));
superStage.setScene(new Scene(superParent));
superStage.initModality(Modality.WINDOW_MODAL);
superStage.initOwner((Stage) addUnitButton.getScene().getWindow() );
superStage.setResizable(false);
superStage.setTitle("Number 7 : Units");
superStage.sizeToScene();
superStage.getIcons().add(new Image(getClass().getResourceAsStream("/number7logo.png")));
superStage.showAndWait();
updateFormulaPreview();
outputUnitChoiceBox.getItems().clear();
outputUnitChoiceBox.getItems().addAll(Database.unitsNamesForChoiceBoxList);
outputUnitChoiceBox.getSelectionModel().select(0);
}
catch (IOException e){
e.printStackTrace();
}
}
}