-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.java
240 lines (188 loc) · 7.15 KB
/
Device.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.suhaybabsi.thermodesigner.devices;
import com.suhaybabsi.thermodesigner.core.CalculationError;
import com.suhaybabsi.thermodesigner.core.CalculationException;
import com.suhaybabsi.thermodesigner.core.ConfigurationException;
import com.suhaybabsi.thermodesigner.core.ThermalEquation;
import com.suhaybabsi.thermodesigner.json.ResultRecord;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
/**
*
* @author suhaybal-absi
*/
public abstract class Device {
private String name;
private String jsonType;
private Object data;
protected Device(String name, String jsonType) {
this.name = name;
this.jsonType = jsonType;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public void calculateExergy(){}
private List<ThermalEquation> equationsList = null;
protected abstract void configureEquations() throws ConfigurationException;
public void calculateModel() throws CalculationException{
ArrayList<ThermalEquation> toBeSolved = new ArrayList<ThermalEquation>();
HashMap<ThermalEquation, CalculationError> errorsMap =
new HashMap<ThermalEquation, CalculationError>();
toBeSolved.addAll(equationsList);
outerLoop:while(toBeSolved.size() > 0){
for(ThermalEquation eq:toBeSolved){
int unknownNum = eq.getUnknownNum();
//System.out.println("eq: "+ eq);
//System.out.println("un: "+ unknownNum);
if (unknownNum <= 1) {
CalculationError error = eq.solve();
//System.out.println("error: "+ (error != null));
if(error == null){
toBeSolved.remove(eq);
continue outerLoop;
}else{
errorsMap.put(eq, error);
}
}
}
break;
}
if(toBeSolved.size() > 0){
String equationsText = "";
for(ThermalEquation eq:toBeSolved){
equationsText += "\t" + eq + ", For:\n";
if(eq.getCurrentUnknowns().size() > 0){
equationsText += "\t\t" + eq.getCurrentUnknowns() + "\n";
}
CalculationError error = errorsMap.get(eq);
if(error != null){
equationsText += "\t\t" + error.getMessage() + "\n";
}
}
String message = "Device couldn't be solved{\n\tEquations:\n"+ equationsText +"\n}";
throw new CalculationException(this, message);
}
}
protected boolean configuredProperly = false;
public void configure() throws ConfigurationException{
if(configuredProperly == false){
equationsList = new ArrayList<ThermalEquation>();
configureEquations();
configuredProperly = true;
}
}
protected void addEquation(ThermalEquation eq){
eq.setDevice(this);
equationsList.add(eq);
}
public void reset() {
for(ThermalEquation eq:equationsList){
eq.reset();
}
}
public boolean isSolved() {
int solvedNum = 0;
for (ThermalEquation eq : equationsList) {
int unknownNum = eq.getUnknownNum();
if (unknownNum == 0) {
solvedNum++;
}
}
return solvedNum == equationsList.size();
}
public boolean hasMathematicalModel(){
return equationsList == null || equationsList.isEmpty();
}
public List<ThermalEquation> getEquationsList() {
return equationsList;
}
public String getJsonType() {
return jsonType;
}
protected void setJsonType(String type) {
this.jsonType = type;
}
public static class FieldResult {
String abbrev;
String name;
Object value;
public static FieldResult create(String name, Object value){
FieldResult rf = new FieldResult();
rf.name = name;
rf.value = value;
return rf;
}
public static FieldResult create(String abbrev, String name, Object value){
FieldResult rf = new FieldResult();
rf.abbrev = abbrev;
rf.name = name;
rf.value = value;
return rf;
}
private FieldResult(){}
}
protected List<FieldResult> getEnergyOutputResults(){return getEnergyResults();};
public List<FieldResult> getEnergyResults(){return null;};
public List<FieldResult> getExergyResults(){return null;};
public JSONObject getJSONResults() throws JSONException{
return getJSONResults(false);
}
public JSONObject getJSONResults(boolean exergy) throws JSONException{
ResultRecord res = new ResultRecord(this);
List<FieldResult> energyResults = getEnergyResults();
energyResults = (energyResults == null) ? new ArrayList<FieldResult>() : energyResults;
if(exergy){
List<FieldResult> exergyResults = getExergyResults();
if(exergyResults != null){
energyResults.addAll(exergyResults);
}
}
for(FieldResult field:energyResults){
//System.out.println("Json: "+field.abbrev+", Name: "+field.name+", Value: "+field.value);
if (field.value == null) {
res.add(field.abbrev, "N/A");
} else if(field.value instanceof Double){
res.add(field.abbrev, (Double) field.value);
} else {
res.add(field.abbrev, field.value.toString());
}
}
return res.wrap();
}
public String printEnergyResults(){
return printResults(getEnergyOutputResults());
}
public String printExergyResults(){
return printResults(getExergyResults());
}
private String printResults(List<FieldResult> results){
if (results == null) {return "";}
String str = "";
for(FieldResult rf : results){
str += "\n\t" + rf.name + ": " + rf.value;
}
return str;
}
@Override
public String toString() {
return "{ "+getName()+" - "+ printEnergyResults() + "\n}";
}
}