-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChemicalEquationNodeChart.java
738 lines (713 loc) · 33.6 KB
/
ChemicalEquationNodeChart.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
package com.saptakdas.chemistry.chemicalequation.chemicalnodechart;
import com.saptakdas.chemistry.chemicalequation.chemicalnodechart.NodeGraph.Node;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import java.util.Collections;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Scanner;
/**
* This class can be used to create a graph of the balancing of a chemical equation.
* @author Saptak Das
*/
public class ChemicalEquationNodeChart {
public static void main(String[] args) {
String reactantsString= input("Enter Reactants Side: ");
String productsString = input("Enter Products Side: ");
//Prepping equation
String usePolyatomicReplacement= Character.toString(input("Use polyatomic substitution(T or F): ").toLowerCase().charAt(0));
if(usePolyatomicReplacement.equals("t")){
String[] replacementResults=polyatomicReplacement(reactantsString, productsString);
reactantsString=replacementResults[0];
productsString=replacementResults[1];
}
Hashtable reactants = parseString(reactantsString);
Hashtable products = parseString(productsString);
LinkedList reactantsElements = getElements(reactantsString);
Collections.sort(reactantsElements);
LinkedList productsElements = getElements(productsString);
Collections.sort(productsElements);
LinkedList elements;
boolean contain;
for (int i=0; i<reactantsElements.size(); i++) {
contain=productsElements.contains(reactantsElements.get(i));
if (!contain){
System.out.println("Error: Same elements need to be on both sides of the equation.");
System.exit(0);
}
}
for (int i=0; i<productsElements.size(); i++) {
contain=reactantsElements.contains(productsElements.get(i));
if (!contain){
System.out.println("Error: Same elements need to be on both sides of the equation.");
System.exit(0);
}
}
elements=reactantsElements;
System.out.println(elements);
System.out.println(reactants);
System.out.println(products);
//Finds Variable Space
System.out.println("If you specify false, variable space will default to ideal.");
String specifyVariableSpace= Character.toString(input("Specify Variable Space(T or F): ").toLowerCase().charAt(0));
int variableSpace;
if (specifyVariableSpace.equals("t")) {
variableSpace = Integer.valueOf(input("Enter Variable Space: "));
}
else {
variableSpace = idealVariableSpace(elements, reactants, products);
}
System.out.println(variableSpace);
//Start of Node Chart Generation
makeNodeChart(variableSpace,elements, reactants, products);
}
/**
* @param iterable LinkedList of possible combination values
* @param k int value of length of solution
* @return LinkedList of combinations
*/
public static LinkedList getCombinations(LinkedList iterable, int k){
var n=iterable.size();
var storageList=new LinkedList();
var list=new LinkedList();
//Prepping
for (int i=0; i<k; i++){
list.addLast(i+1);
}
//Algorithm
boolean changed;
while (((int) list.get(0))<(n-k+2)){
changed=false;
for (int i=0; i<list.size(); i++) {
var index=list.size()-i-1;
if ((int) list.get(index) > n) {
list.set(index-1,(int) list.get(index-1)+1);
for (int j=index; j<list.size(); j++){
list.set(j, (int) list.get(j-1)+1);
}
changed=true;
}
}
if (!changed) {
storageList.addLast(indexesToCombinations(list, iterable));
list.set(list.size() - 1, (int) list.get(list.size() - 1) + 1);
}
}
return storageList;
}
/**
* @param list LinkedList
* @param iterable LinkedList
* @return LinkedList of combinations
*/
private static LinkedList indexesToCombinations(LinkedList<Integer> list, LinkedList iterable){
var returnlist=new LinkedList();
for (int index:list) {
int i=index-1;
returnlist.addLast(iterable.get(i));
}
return returnlist;
}
/**
* For getting input
* @param text Input information
* @return String value of what was returned.
*/
public static String input(String text) {
Scanner scanner = new Scanner(System.in);
System.out.print(text);
return scanner.nextLine();
}
/**
* Replaces common polyatomic ions with their own custom element.
* @param reactant_String String representation of reactant side
* @param product_String String representation of product side
* @return String[] Packaged version of new reactant and product strings
*/
private static String[] polyatomicReplacement(String reactant_String, String product_String) {
reactant_String = reactant_String.replace(" ", "");
product_String = product_String.replace(" ", "");
String[] replacementNames = {"A", "D", "E", "G", "J", "L", "M", "Q", "R", "X"};
String[] commonIons = {"NH4", "C2H3O2", "HCO3", "HSO4", "ClO", "ClO3", "ClO2", "OCN", "CN", "H2PO4", "OH", "NO3", "NO2", "ClO4", "MnO4", "SCN", "CO3", "CrO4", "Cr2O7", "HPO4", "SO4", "SO3", "S2O3", "BO3", "PO4"};
int index = 0;
for (String ion : commonIons) {
if (reactant_String.contains(ion) && product_String.contains(ion)) {
reactant_String = reactant_String.replace(ion, replacementNames[index]);
product_String = product_String.replace(ion, replacementNames[index]);
index++;
}
}
return new String[]{reactant_String, product_String};
}
/**
* Gets all elements from an equation string
* @param inputString String representation of each side of a chemical equation
* @return LinkedList Gets a LinkedList of all the elements on the given side
*/
static LinkedList getElements(String inputString){
LinkedList<String> elements=new LinkedList<String>();
String elementString="";
char character = 0;
for (int i=0; i<inputString.length(); i++){
character=inputString.charAt(i);
if (Character.isLetter(character)){
if (String.valueOf(character).toUpperCase().equals(String.valueOf(character))){
if (elementString!=""){
if (!elements.contains(elementString)){
elements.add(elementString);
}
elementString="";
}
}
elementString = elementString.concat(Character.toString(character));
}
else if (Character.toString(character).equals("+")){
if (!elements.contains(elementString)){
elements.add(elementString);
}
elementString="";
}
}
if (!Character.toString(character).equals("")){
if (!elements.contains(elementString)) {
elements.add(elementString);
}
}
return elements;
}
/**
* Parses compound to give all elements in compound
* @param inputString String representation of a compound
* @return Hashtable Representation of compound as Hashtable
*/
private static Hashtable parseCompound(String inputString) {
Hashtable<String, Integer> dictionary = new Hashtable<String, Integer>();
String symbol = "";
String numString = "";
for (int i = 0; i < inputString.length(); i++) {
char character = inputString.charAt(i);
//System.out.println(i);
//System.out.println(character);
//System.out.println("");
if (Character.isLetter(character)) {
//Checks that this is a letter
if (String.valueOf(character).toUpperCase().equals(String.valueOf(character))){
//This is uppercase
if (symbol!=""){
//Symbol is filled and needs to be dumped
try{
dictionary.put(symbol, Integer.valueOf(numString));
}catch (NumberFormatException exception) {
dictionary.put(symbol, 1);
}
symbol="";
numString="";
}
symbol = symbol.concat(String.valueOf(character));
}
else{
symbol = symbol.concat(String.valueOf(character));
}
} else if (Character.isDigit(character)) {
//This is a number
numString = numString.concat(String.valueOf(character));
}
//System.out.println(i);
//System.out.println("Character: "+character);
//System.out.println(symbol);
//System.out.println(numString);
//System.out.println(dictionary);
//System.out.println("--Spacer--");
}
if (numString.equals("")){
numString="1";
}
dictionary.put(symbol, Integer.valueOf(numString));
return dictionary;
}
/**
* Parses whole equation string.
* @param inputString String representation of each side
* @return Hashtable Hashtable representation of given side
*/
static Hashtable parseString(String inputString){
Hashtable<Integer, Hashtable> compoundTable=new Hashtable<Integer, Hashtable>();
String storeString = "";
Integer index=0;
for (int j=0; j<inputString.length(); j++) {
if (Character.toString(inputString.charAt(j)).equals("+")) {
compoundTable.put(index, parseCompound(storeString));
storeString="";
index=index+1;
}
else {
storeString=storeString.concat(Character.toString(inputString.charAt(j)));
}
}
compoundTable.put(index, parseCompound(storeString));
return compoundTable;
}
/**
* Main Logic to get combinations and show result in graph picture.
* @param variableSpace The number of variables that will be used to solve
* @param elements LinkedList of all elements
* @param reactants Hashtable representation of reactant side
* @param products Hashtable representation of product side
*/
private static void makeNodeChart(int variableSpace, LinkedList elements, Hashtable reactants, Hashtable products) {
//Get all possible start nodes
LinkedList<String> startNodes=new LinkedList<>();
int reactantCounter=0;
int productCounter=0;
for (int i=0; i<elements.size(); i++){
reactantCounter=0;
productCounter=0;
String element= (String) elements.get(i);
for (int j=0; j<reactants.size(); j++){
Hashtable compound=(Hashtable) reactants.get(j);
if (compound.containsKey(element)){
reactantCounter+=1;
}
}
for (int j=0; j<products.size(); j++){
Hashtable compound=(Hashtable) products.get(j);
if (compound.containsKey(element)){
productCounter+=1;
}
}
if (reactantCounter==1 && productCounter==1){
startNodes.add(element);
}
}
//Generating Origin Nodes based on Variable Space
Node.addEquation(reactants, products);
DefaultDirectedGraph<Node, DefaultEdge> graph = new DefaultDirectedGraph(DefaultEdge.class);
Node newNode = null;
//Note nodeTable contains only parent and child nodes
Hashtable<Integer,LinkedList<Node>> nodeTable=new Hashtable();
var nodeLinkedlist=new LinkedList();
if (variableSpace==1){
//Origin Nodes are just Root Nodes
for (String element:startNodes){
newNode=new Node("origin", filledHashtable("Elements Used", filledLinkedlist(element)));
nodeLinkedlist.addLast(newNode);
}
}
else {
//Get all combinations
var combinations=getCombinations(startNodes,variableSpace);
for (Object obj :combinations){
LinkedList elementsList=(LinkedList) obj;
//Trimming number of combinations
if (checkCombination(elementsList,reactants,products)) {
newNode = new Node("origin", filledHashtable("Elements Used", elementsList));
nodeLinkedlist.addLast(newNode);
}
}
}
nodeTable.put(1,nodeLinkedlist);
//Child Nodes and Connections
var layer = 2;
while (layer<=elements.size()+2-variableSpace){
var possibleElements=new Hashtable<String, LinkedList>();
LinkedList lastNodeLayer=nodeTable.get(layer-1);
for (Object n:lastNodeLayer){
//Get all needed nodes
Node node=(Node) n;
var nodeList=new LinkedList();
if ((!node.reactantBool.contains(false) && !node.productBool.contains(false))) {
node.leaves.addLast("success");
System.out.println("Is this getting executed?"+node.leaves);
continue;
}
nodeList.addLast(node);
System.out.println("Elements: "+elements);
LinkedList<String> elementsRemaining=removeElements(elements,node.elementsUsed);
System.out.println("Node: "+node);
System.out.println(elementsRemaining);
System.out.println("---------------------------------------------------------------------------------------------->"+elementsRemaining);
System.out.println("Already Used: "+node.elementsUsed);
for (String element: elementsRemaining){
System.out.println("Element: "+element);
reactantCounter=0;
productCounter=0;
var reactantFilledCounter=0;
var productFilledCounter=0;
var reactantUnfilledCounter=0;
var productUnfilledCounter=0;
var reactantIndexes=new LinkedList();
var productIndexes=new LinkedList();
Integer unfilledIndex=null;
Boolean reactantUnfilled=true;
for (int i=0; i<reactants.size(); i++){
Hashtable compound=(Hashtable) reactants.get(i);
if (compound.containsKey(element)){
reactantCounter++;
if ((Boolean) node.reactantBool.get(i)){
//Value is filled
reactantIndexes.addLast(i);
reactantFilledCounter++;
}
else{
unfilledIndex=i;
reactantUnfilledCounter++;
}
}
}
for (int i=0; i<products.size(); i++){
Hashtable compound=(Hashtable) products.get(i);
if (compound.containsKey(element)){
productCounter++;
if ((Boolean) node.productBool.get(i)){
//Value is filled
productIndexes.addLast(i);
productFilledCounter++;
}
else{
unfilledIndex=i;
reactantUnfilled=false;
productUnfilledCounter++;
}
}
}
System.out.println("<------------------------------------------------------------------------------------------------------------------------------------>");
System.out.println("Element: "+element);
System.out.println("Node Elements Used: "+ node.elementsUsed);
System.out.println("Boolean Charts: "+node.reactantBool+" and "+ node.productBool);
System.out.println("Counters: "+reactantCounter+" and "+productCounter);
System.out.println("Unfilled Counters: "+reactantUnfilledCounter+" and "+productUnfilledCounter);
System.out.println("Filled Counter: "+reactantFilledCounter+" and "+productFilledCounter);
System.out.println("<------------------------------------------------------------------------------------------------------------------------------------>");
if ((reactantCounter==1 && productCounter==1) && (reactantUnfilledCounter==1 ^ productUnfilledCounter==1) && (reactantFilledCounter==1 ^ productFilledCounter==1)){
//One to one semi-established relationship
System.out.println("Simple relationship");
if (!possibleElements.containsKey(element)) {
possibleElements.put(element, nodeList);
System.out.println("-------------------Putting element " + element + " and node " + node);
System.out.println("Possible Elements: "+possibleElements);
}
else if (possibleElements.containsKey(element) && !possibleElements.get(element).contains(node)){
System.out.println("-------------------Putting element "+element+" and node "+node);
var oldValue=copyLinkedlist(possibleElements.get(element));
oldValue.addLast(node);
possibleElements.remove(element);
possibleElements.put(element, oldValue);
System.out.println("Possible Elements: "+possibleElements);
}
//Add this as a child node path for current node
LinkedList childElements=copyLinkedlist(node.elementsUsed);
childElements.addLast(element);
node.children.addLast(childElements);
}
else if((reactantCounter+productCounter-1==reactantFilledCounter+productFilledCounter) && (reactantUnfilledCounter==1 ^ productUnfilledCounter==1)){
//This is a complex relationship
System.out.println("Complex relationship");
if (!possibleElements.containsKey(element)) {
possibleElements.put(element, nodeList);
System.out.println("-------------------Putting element " + element + " and node " + node);
System.out.println("Possible Elements: "+possibleElements);
}
else if (possibleElements.containsKey(element) && !possibleElements.get(element).contains(node)){
System.out.println("-------------------Putting element "+element+" and node "+node);
var oldValue=copyLinkedlist(possibleElements.get(element));
oldValue.addLast(node);
possibleElements.remove(element);
possibleElements.put(element, oldValue);
System.out.println("Possible Elements: "+possibleElements);
}
//Add this as a child node path for current node
LinkedList childElements=copyLinkedlist(node.elementsUsed);
childElements.addLast(element);
node.children.addLast(childElements);
}
else {
//Remove this afterwards
System.out.println("-----------------------------Nothing was added.");
}
}
if (possibleElements==new Hashtable()){
//this is a deadend leaf node
node.leaves.addLast("deadend");
}
}
//Process all possible elements into nodes
System.out.println("Possible Elements: "+possibleElements);
if (possibleElements!=new Hashtable()){
//You will create at least one child node using possible elements.
//Work on first adding all child nodes
LinkedList<Node> nodeContainer=new LinkedList();
var allKeys=possibleElements.keys();
while (allKeys.hasMoreElements()) {
String key=allKeys.nextElement();
var correspondingLinkedlist=(LinkedList<Node>) possibleElements.get(key);
for (Node node: correspondingLinkedlist) {
LinkedList<String> elementsList=copyLinkedlist(node.elementsUsed);
if (!elementsList.contains(key)) {
elementsList.addLast(key);
}
else
continue;
if (!checkForCopies(elementsList,elements)) {
Boolean checkBoolean = false;
//Check if node combination exists already
if (nodeContainer != new LinkedList()) {
for (Node checkNode : nodeContainer) {
checkBoolean = checkLinkedlistEquality(checkNode.elementsUsed, elementsList);
if (checkBoolean)
break;
}
}
//Else no checking needed
//Skip this combination
if (checkBoolean) {
continue;
}
//Create new child node
Hashtable information = new Hashtable();
information.put("Elements Used", elementsList);
information.put("Parent", node);
System.out.println("Elements List: "+ elementsList);
Node newChildNode = new Node("child", information);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Boolean Charts: "+newChildNode.reactantBool+" and "+newChildNode.productBool);
nodeContainer.addLast(newChildNode);
}
}
}
if (nodeContainer!=new LinkedList())
nodeTable.put(layer, nodeContainer);
}
layer++;
}
System.out.println("This is the node table"+nodeTable);
//Create all nodes and edges
//Establish all edges
System.out.println("---------------------------------------------This is for establishing the edges------------------------------------------------");
for (int i=1; i<nodeTable.size()+1; i++){
LinkedList<Node> layerNodes=nodeTable.get(i);
for (Node node:layerNodes){
LinkedList<String> parentUsedElements=node.elementsUsed;
for (Node possibleChildNodes: nodeTable.get(i+1)){
LinkedList<String> childUsedElement=possibleChildNodes.elementsUsed;
var contains=true;
System.out.println("Possible Parent: "+parentUsedElements);
System.out.println("Possible Child: "+ childUsedElement);
for (String element: parentUsedElements){
contains=childUsedElement.contains(element);
if (!contains) break;
}
if (!contains) continue;
else{
if (!node.children.contains(childUsedElement)) {
node.children.addLast(childUsedElement);
System.out.println("Parent established edge with child!!!");
}
}
}
}
}
System.out.println("-----------------------------------------------------------------end-----------------------------------------------------------");
//Create all vertices
for (int i=1; i<nodeTable.size()+1; i++){
LinkedList<Node> layerNodes=nodeTable.get(i);
for (Node node:layerNodes){
System.out.println(node);
graph.addVertex(node);
}
}
//Add all leaf nodes and leaf edges
for (int i=1; i<nodeTable.size()+1; i++){
LinkedList<Node> layerNodes=nodeTable.get(i);
for (Node node:layerNodes){
System.out.println(node);
for (Object msg: node.leaves) {
String message=(String) msg;
Hashtable information=new Hashtable();
information.put("Message", message);
Node newLeafNode=new Node("leaf", information);
graph.addVertex(newLeafNode);
graph.addEdge(node, newLeafNode);
}
}
}
//Add all other edges
for (int i=1; i<nodeTable.size()+1; i++){
LinkedList<Node> layerNodes=nodeTable.get(i);
for (Node node:layerNodes) {
if (node.children != new LinkedList()) {
for (Object c : node.children) {
var child = (LinkedList) c;
LinkedList<Node> nextLayer = nodeTable.get(i + 1);
for (Node possibleChildNode:nextLayer){
if(checkLinkedlistEquality(possibleChildNode.elementsUsed, child)){
graph.addEdge(node,possibleChildNode);
}
}
}
}
}
}
//Display
NodeGraph.displayGraph(graph);
}
/**
* @param elementsUsed LinkedList of elements used
* @param elements LinkedList of the current remaining elements
* @return boolean True if there are no copies of Strings in LinkedList elementsUsed that are in LinkedList elements
*/
private static boolean checkForCopies(LinkedList<String> elementsUsed, LinkedList<String> elements){
for (String element: elements){
var counter=1;
for (String element2: elements){
if (element.equals(element2))
counter++;
if (counter>1){
return false;
}
}
}
return true;
}
/**
* @param a LinkedList
* @param b LinkedList
* @return boolean True if all elements in a are in b
*/
private static boolean checkLinkedlistEquality(LinkedList<String> a, LinkedList<String> b){
for (String objA: a){
if (!b.contains(objA))
return false;
}
return true;
}
/**
* Removes used elements.
* @param allElements All remaining elements
* @param elements Elements used
* @return LinkedList Remaining unused elements
*/
private static LinkedList removeElements(LinkedList<String> allElements, LinkedList<String> elements){
var elementsRemaining=new LinkedList<String>();
for (String element: allElements)
elementsRemaining.addLast(element);
for (String e:elements){
elementsRemaining.remove(e);
}
return elementsRemaining;
}
/**
* Check current combination.
* @param elementsList Current list of elements
* @param reactants Hashtable representation of reactants
* @param products Hashtable representation of products
* @return boolean True if combination is valid.
*/
private static boolean checkCombination(LinkedList elementsList, Hashtable reactants, Hashtable products){
var reactantBoolean=makeCoefficientBooleans(reactants);
var productBoolean=makeCoefficientBooleans(products);
Integer reactantIndex=null;
Integer productIndex=null;
for (Object e: elementsList) {
String element=(String) e;
reactantIndex=null;
productIndex=null;
for (int i=0; i<reactants.size(); i++){
Hashtable compound= (Hashtable) reactants.get(i);
if (compound.containsKey(element))
reactantIndex=i;
}
for (int i=0; i<products.size(); i++){
Hashtable compound= (Hashtable) products.get(i);
if (compound.containsKey(element))
productIndex=i;
}
if (reactantIndex!=null && productIndex!=null){
if (reactantBoolean.get(reactantIndex)!=true && productBoolean.get(productIndex)!=true){
reactantBoolean.set(reactantIndex,true);
productBoolean.set(productIndex, true);
}
else{
return false;
}
}
}
return true;
}
/**
* @param element Element to add in new LinkedList
* @return LinkedList that is filled with element
*/
private static LinkedList filledLinkedlist(String element) {
LinkedList<String> list=new LinkedList();
list.add(element);
return list;
}
/**
* @param toBeCopied LinkedList to be copied
* @return Copied LinkedList
*/
public static LinkedList copyLinkedlist(LinkedList toBeCopied){
var linkedlist=new LinkedList();
for (Object object: toBeCopied)
linkedlist.addLast(object);
return linkedlist;
}
private static Hashtable<String, LinkedList> filledHashtable(String key, LinkedList value){
Hashtable hashtable=new Hashtable();
hashtable.put(key, value);
return hashtable;
}
/**
* Calculates ideal variable space needed to solve equation.
* @param elements LinkedList of all elements
* @param reactants Hashtable representation of reactants
* @param products Hashtable representation of products
* @return int value of number of variables needed
*/
private static int idealVariableSpace(LinkedList elements, Hashtable reactants, Hashtable products){
int mainCounter=0;
int reactantCounter=0;
int productCounter=0;
LinkedList<Boolean> reactantBooleans=makeCoefficientBooleans(reactants);
LinkedList<Boolean> productBooleans=makeCoefficientBooleans(products);
Integer reactantIndex=null;
Integer productIndex=null;
for (int i=0; i<elements.size(); i++){
reactantCounter=0;
productCounter=0;
reactantIndex=null;
productIndex=null;
String element= (String) elements.get(i);
for (int j=0; j<reactants.size(); j++){
Hashtable compound=(Hashtable) reactants.get(j);
if (compound.containsKey(element)){
reactantCounter+=1;
reactantIndex=j;
}
}
for (int j=0; j<products.size(); j++){
Hashtable compound=(Hashtable) products.get(j);
if (compound.containsKey(element)){
productCounter+=1;
productIndex=j;
}
}
if ((reactantCounter==1 && productCounter==1) && (!(reactantBooleans.get(reactantIndex)) && !(productBooleans.get(productIndex)))){
mainCounter+=1;
reactantBooleans.set(reactantIndex,true);
productBooleans.set(productIndex,true);
}
}
return mainCounter;
}
/**
* @param side Hashtable representing equation side
* @return LinkedList of size of Hashtable size filled with booleans representing each compound
*/
public static LinkedList<Boolean> makeCoefficientBooleans(Hashtable side){
LinkedList<Boolean> booleanList=new LinkedList<Boolean>();
for (int i=0; i<side.size(); i++){
booleanList.add(false);
}
return booleanList;
}
}