-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeGraph.java
180 lines (169 loc) · 7.27 KB
/
NodeGraph.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
package com.saptakdas.chemistry.chemicalequation.chemicalnodechart;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.util.mxCellRenderer;
import org.jgrapht.ext.JGraphXAdapter;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.LinkedList;
/**
* This class can be used to represent node graph structure.
* @author Saptak Das
*/
public class NodeGraph{
/**
* Inner Class to represent nodes in graph
*/
public static class Node {
private static Hashtable reactants;
private static Hashtable products;
LinkedList elementsUsed=new LinkedList();
String type;
int layer;
LinkedList children;
LinkedList leaves;
LinkedList reactantBool;
LinkedList productBool;
private String message;
/**
* @param type Type of node
* @param otherArgs Passes any other necessary information to create node
*/
public Node(String type, Hashtable otherArgs){
System.out.println(type);
System.out.println(otherArgs);
if (type.equals("origin")){
this.type="origin";
this.layer=1;
this.children=new LinkedList();
this.leaves=new LinkedList();
this.reactantBool= ChemicalEquationNodeChart.makeCoefficientBooleans(reactants);
this.productBool=ChemicalEquationNodeChart.makeCoefficientBooleans(products);
changeCoefficentBoolean(reactantBool,productBool, (LinkedList) otherArgs.get("Elements Used"));
this.elementsUsed=(LinkedList) otherArgs.get("Elements Used");
}
else if (type.equals("child")){
var parentInfo=((Node) otherArgs.get("Parent"));
this.type="child";
this.layer=parentInfo.layer+1;
this.children=new LinkedList();
this.leaves=new LinkedList();
LinkedList<Boolean> parentReactantBool=parentInfo.reactantBool;
LinkedList<Boolean> parentProductBool=parentInfo.productBool;
changeCoefficentBoolean(parentReactantBool,parentProductBool, (LinkedList) otherArgs.get("Elements Used"));
this.elementsUsed=(LinkedList) otherArgs.get("Elements Used");
}
else{
//This is a leaf node
this.type="leaf";
this.message= (String) otherArgs.get("Message");
}
}
@Override
public String toString() {
if (type.equals("leaf"))
return message;
else
return String.valueOf(elementsUsed);
}
/**
* Provides reactant and product Hashtable representations
* @param reactantsHashtable Hashtable representation of reactant side
* @param productsHashtable Hashtable representation of product side
*/
public static void addEquation(Hashtable reactantsHashtable, Hashtable productsHashtable){
reactants=reactantsHashtable;
products=productsHashtable;
}
private static Hashtable getReactants() {
return reactants;
}
private static Hashtable getProducts() {
return products;
}
/**
* Changes coefficient boolean representing filled or unfilled variable.
* @param oldReactant Current Reactant Boolean Coefficient LinkedList
* @param oldProduct Current Product Boolean Coefficient LinkedList
* @param elements Remaining unused elements
*/
private void changeCoefficentBoolean(LinkedList oldReactant, LinkedList oldProduct, LinkedList elements){
LinkedList oldReactantBool=ChemicalEquationNodeChart.copyLinkedlist(oldReactant);
LinkedList oldProductBool=ChemicalEquationNodeChart.copyLinkedlist(oldProduct);
for (Object e:elements){
var element=(String) e;
LinkedList<Integer> reactantIndex=new LinkedList();
LinkedList<Integer> productIndex=new LinkedList();
for (int i=0; i<getReactants().size(); i++){
Hashtable compound=(Hashtable) getReactants().get(i);
if (compound.containsKey(element)) reactantIndex.addLast(i);
}
for (int i=0; i<getProducts().size(); i++){
Hashtable compound=(Hashtable) getProducts().get(i);
if (compound.containsKey(element)) productIndex.addLast(i);
}
//Changing Indexes
for (Integer rIndex: reactantIndex)
oldReactantBool.set(rIndex,true);
for (Integer pIndex: productIndex)
oldProductBool.set(pIndex,true);
}
reactantBool=oldReactantBool;
productBool=oldProductBool;
}
}
public static void main(String[] args) throws IOException {
DefaultDirectedGraph<Node, DefaultEdge> g = new DefaultDirectedGraph(DefaultEdge.class);
Hashtable hashtable1=new Hashtable();
Hashtable hashtable2=new Hashtable();
LinkedList linkedList1=new LinkedList();
LinkedList linkedList2=new LinkedList();
linkedList2.add("O");
hashtable2.put("Elements Used",linkedList2);
linkedList1.add("Mn");
hashtable1.put("Elements Used",linkedList1);
Node x1= new Node("origin", hashtable1);
Node x2= new Node("origin", hashtable2);
Node x3 = new Node("origin", createHashtable("H"));
g.addVertex(x1);
g.addVertex(x2);
g.addVertex(x3);
g.addEdge(x1, x2);
g.addEdge(x3, x2);
displayGraph(g);
}
/**
* @param element String element
* @return New Hashtable filled with LinkedList that has an element
*/
private static Hashtable createHashtable(String element){
Hashtable hashtable=new Hashtable();
LinkedList linkedList=new LinkedList();
linkedList.add(element);
hashtable.put("Elements Used",linkedList);
return hashtable;
}
/**
* Display .png file for node graph
* @param graphName DefaultDirectedGraph variable
*/
public static void displayGraph(DefaultDirectedGraph graphName) {
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<String, DefaultEdge>(graphName);
mxIGraphLayout layout = new mxHierarchicalLayout(graphAdapter);
layout.execute(graphAdapter.getDefaultParent());
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
File imgFile = new File("src/GraphPics/graph.png");
try {
imgFile.createNewFile();
ImageIO.write(image, "PNG", imgFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}