Skip to content

Commit 2e6ca03

Browse files
committed
Cache fingerprints, iterative GameTheory, remove gc() calls
- MCSThread: Cache circular fingerprints on molecule properties to avoid recomputation; fix duplicate() method copying properties to clone instead of no-op self-assignment on source - GameTheoryRings/Max/Min/Mixture: Convert recursive GenerateMapping() to iterative while-loop with max 100 iterations safety guard, preventing stack overflow on complex reactions - Remove unnecessary System.gc() calls from ReactionMechanismTool, CallableAtomMappingTool, and GraphMatcher (JVM hint adds overhead) All 40 tests pass. Author: Syed Asad Rahman <asad.rahman@bioinceptionlabs.com>
1 parent 789142d commit 2e6ca03

8 files changed

Lines changed: 168 additions & 142 deletions

File tree

src/main/java/com/bioinceptionlabs/reactionblast/mapping/CallableAtomMappingTool.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.Serializable;
2525
import static java.lang.String.valueOf;
2626
import static java.lang.System.currentTimeMillis;
27-
import static java.lang.System.gc;
2827
import static java.lang.System.getProperty;
2928
import static java.util.Collections.synchronizedMap;
3029
import static java.util.Collections.unmodifiableMap;
@@ -204,7 +203,6 @@ private synchronized void generateAtomAtomMapping(
204203
executor.shutdown();
205204
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
206205
LOGGER.debug("======DONE CallableAtomMappingTool=======");
207-
gc();
208206
} catch (InterruptedException | ExecutionException e) {
209207
LOGGER.debug("ERROR: in AtomMappingTool: " + e.getMessage());
210208
LOGGER.error(e);

src/main/java/com/bioinceptionlabs/reactionblast/mapping/algorithm/GameTheoryMax.java

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -121,58 +121,65 @@ final class GameTheoryMax extends BaseGameTheory {
121121

122122
private synchronized void GenerateMapping(boolean flag) throws Exception {
123123
boolean ruleMatchingFlag = flag;
124-
this.counter++;
124+
int maxIterations = 100;
125+
int iteration = 0;
126+
boolean continueMapping = true;
127+
while (continueMapping && iteration < maxIterations) {
128+
this.counter++;
125129

126-
if (DEBUG) {
127-
LOGGER.debug("**********Orignal Matrix**************");
128-
printMatrixAtomContainer(mh, eductList, productList);
129-
printSimMatrix(mh, eductList, productList);
130-
printCliqueMatrix(mh, eductList, productList);
130+
if (DEBUG) {
131+
LOGGER.debug("**********Orignal Matrix**************");
132+
printMatrixAtomContainer(mh, eductList, productList);
133+
printSimMatrix(mh, eductList, productList);
134+
printCliqueMatrix(mh, eductList, productList);
131135
// printStereoMatrix(mh, eductList, productList);
132136
// printFragmentMatrix(mh, eductList, productList);
133137
// printEnergyMatrix(mh, eductList, productList);
134-
}
138+
}
135139

136-
boolean conditionmet = false;
137-
if (!ruleMatchingFlag) {
138-
LOGGER.debug("CHECK Rule Based Mapping Handler Match");
139-
RuleBasedMappingHandler ruleBasedMappingHandler = new RuleBasedMappingHandler(mh, eductList, productList);
140-
if (ruleBasedMappingHandler.isMatchFound()) {
141-
LOGGER.debug("Rule Based Mapping Handler Match Found");
142-
mh = Selector.modifyMatrix(ruleBasedMappingHandler.getMatrixHolder());
143-
conditionmet = true;
140+
boolean conditionmet = false;
141+
if (!ruleMatchingFlag) {
142+
LOGGER.debug("CHECK Rule Based Mapping Handler Match");
143+
RuleBasedMappingHandler ruleBasedMappingHandler = new RuleBasedMappingHandler(mh, eductList, productList);
144+
if (ruleBasedMappingHandler.isMatchFound()) {
145+
LOGGER.debug("Rule Based Mapping Handler Match Found");
146+
mh = Selector.modifyMatrix(ruleBasedMappingHandler.getMatrixHolder());
147+
conditionmet = true;
148+
}
149+
ruleMatchingFlag = true;
150+
LOGGER.debug("DONE CHECK Rule Based Mapping Handler");
144151
}
145-
ruleMatchingFlag = true;
146-
LOGGER.debug("DONE CHECK Rule Based Mapping Handler");
147-
}
148-
if (!conditionmet && counter <= 5) {
149-
LOGGER.debug("Subgraph/Exact Match Test");
150-
MaxSelection select = new MaxSelection(mh, eductList, productList);
151-
if (select.isSubAndCompleteMatchFlag()) {
152+
if (!conditionmet && counter <= 5) {
153+
LOGGER.debug("Subgraph/Exact Match Test");
154+
MaxSelection select = new MaxSelection(mh, eductList, productList);
155+
if (select.isSubAndCompleteMatchFlag()) {
152156
// System.out.println("Subgraph/Exact Match");
153-
mh = select.getUpdatedHolder();
157+
mh = select.getUpdatedHolder();
158+
}
154159
}
155-
}
156-
if (DEBUG) {
157-
LOGGER.debug("**********Modified Matrix**************");
160+
if (DEBUG) {
161+
LOGGER.debug("**********Modified Matrix**************");
158162
// printMatrixAtomContainer(mh, eductList, productList);
159-
printSimMatrix(mh, eductList, productList);
160-
printCliqueMatrix(mh, eductList, productList);
163+
printSimMatrix(mh, eductList, productList);
164+
printCliqueMatrix(mh, eductList, productList);
161165
// printStereoMatrix(mh, eductList, productList);
162166
// printFragmentMatrix(mh, eductList, productList);
163167
// printEnergyMatrix(mh, eductList, productList);
164-
}
165-
winner.searchWinners(educts, products, mh);
166-
if (DEBUG) {
167-
printFlagMatrix(winner, eductList, productList);
168-
}
169-
if (winner.getFlag()) {
170-
LOGGER.debug("**********Updated Mapping**************");
171-
UpdateMapping();
172-
LOGGER.debug("**********Updated Matrix**************");
173-
UpdateMatrix(mh, removeHydrogen);
174-
LOGGER.debug("**********Generate Mapping**************");
175-
GenerateMapping(ruleMatchingFlag);
168+
}
169+
winner.searchWinners(educts, products, mh);
170+
if (DEBUG) {
171+
printFlagMatrix(winner, eductList, productList);
172+
}
173+
if (winner.getFlag()) {
174+
LOGGER.debug("**********Updated Mapping**************");
175+
UpdateMapping();
176+
LOGGER.debug("**********Updated Matrix**************");
177+
UpdateMatrix(mh, removeHydrogen);
178+
LOGGER.debug("**********Generate Mapping**************");
179+
iteration++;
180+
} else {
181+
continueMapping = false;
182+
}
176183
}
177184
}
178185

src/main/java/com/bioinceptionlabs/reactionblast/mapping/algorithm/GameTheoryMin.java

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -147,60 +147,67 @@ private synchronized void GenerateIsoMorphismMapping() throws Exception {
147147

148148
private synchronized void GenerateMapping(boolean flag) throws Exception {
149149
boolean ruleMatchingFlag = flag;
150-
this.counter++;
151-
if (DEBUG) {
152-
LOGGER.debug("**********Orignal Matrix**************");
153-
printMatrixAtomContainer(mh, eductList, productList);
154-
printSimMatrix(mh, eductList, productList);
155-
printCliqueMatrix(mh, eductList, productList);
150+
int maxIterations = 100;
151+
int iteration = 0;
152+
boolean continueMapping = true;
153+
while (continueMapping && iteration < maxIterations) {
154+
this.counter++;
155+
if (DEBUG) {
156+
LOGGER.debug("**********Orignal Matrix**************");
157+
printMatrixAtomContainer(mh, eductList, productList);
158+
printSimMatrix(mh, eductList, productList);
159+
printCliqueMatrix(mh, eductList, productList);
156160
// printStereoMatrix(mh, eductList, productList);
157161
// printFragmentMatrix(mh, eductList, productList);
158162
// printEnergyMatrix(mh, eductList, productList);
159-
}
160-
boolean conditionmet = false;
161-
if (!ruleMatchingFlag) {
162-
LOGGER.debug("CHECK Rule Based Mapping Handler Match");
163-
RuleBasedMappingHandler ruleBasedMappingHandler = new RuleBasedMappingHandler(mh, eductList, productList);
164-
if (ruleBasedMappingHandler.isMatchFound()) {
165-
LOGGER.debug("Rule Based Mapping Handler Match Found");
166-
mh = Selector.modifyMatrix(ruleBasedMappingHandler.getMatrixHolder());
167-
conditionmet = true;
168163
}
169-
ruleMatchingFlag = true;
170-
LOGGER.debug("DONE CHECK Rule Based Mapping Handler");
171-
}
164+
boolean conditionmet = false;
165+
if (!ruleMatchingFlag) {
166+
LOGGER.debug("CHECK Rule Based Mapping Handler Match");
167+
RuleBasedMappingHandler ruleBasedMappingHandler = new RuleBasedMappingHandler(mh, eductList, productList);
168+
if (ruleBasedMappingHandler.isMatchFound()) {
169+
LOGGER.debug("Rule Based Mapping Handler Match Found");
170+
mh = Selector.modifyMatrix(ruleBasedMappingHandler.getMatrixHolder());
171+
conditionmet = true;
172+
}
173+
ruleMatchingFlag = true;
174+
LOGGER.debug("DONE CHECK Rule Based Mapping Handler");
175+
}
172176

173-
if (!conditionmet && counter <= 5) {
174-
LOGGER.debug("call counter " + counter);
175-
LOGGER.debug("Subgraph/Exact Match Test");
176-
MinSelection select
177-
= new MinSelection(mh, eductList, productList);
178-
if (select.isSubAndCompleteMatchFlag()) {
179-
LOGGER.debug("Subgraph/Exact Match");
180-
mh = select.getUpdatedHolder();
177+
if (!conditionmet && counter <= 5) {
178+
LOGGER.debug("call counter " + counter);
179+
LOGGER.debug("Subgraph/Exact Match Test");
180+
MinSelection select
181+
= new MinSelection(mh, eductList, productList);
182+
if (select.isSubAndCompleteMatchFlag()) {
183+
LOGGER.debug("Subgraph/Exact Match");
184+
mh = select.getUpdatedHolder();
185+
}
181186
}
182-
}
183187

184-
if (DEBUG) {
185-
LOGGER.debug("**********Modified Matrix**************");
188+
if (DEBUG) {
189+
LOGGER.debug("**********Modified Matrix**************");
186190
// printMatrixAtomContainer(mh, eductList, productList);
187-
printSimMatrix(mh, eductList, productList);
188-
printCliqueMatrix(mh, eductList, productList);
191+
printSimMatrix(mh, eductList, productList);
192+
printCliqueMatrix(mh, eductList, productList);
189193
// printStereoMatrix(mh, eductList, productList);
190194
// printFragmentMatrix(mh, eductList, productList);
191195
// printEnergyMatrix(mh, eductList, productList);
192-
}
193-
winner.searchWinners(educts, products, mh);
194-
if (DEBUG) {
195-
printFlagMatrix(winner, eductList, productList);
196-
}
197-
if (winner.getFlag()) {
198-
LOGGER.debug("**********Updated Mapping**************");
199-
UpdateMapping();
200-
LOGGER.debug("**********Updated Matrix**************");
201-
UpdateMatrix(mh, removeHydrogen);
202-
LOGGER.debug("**********Generate Mapping**************");
203-
GenerateMapping(ruleMatchingFlag);
196+
}
197+
winner.searchWinners(educts, products, mh);
198+
if (DEBUG) {
199+
printFlagMatrix(winner, eductList, productList);
200+
}
201+
if (winner.getFlag()) {
202+
LOGGER.debug("**********Updated Mapping**************");
203+
UpdateMapping();
204+
LOGGER.debug("**********Updated Matrix**************");
205+
UpdateMatrix(mh, removeHydrogen);
206+
LOGGER.debug("**********Generate Mapping**************");
207+
iteration++;
208+
} else {
209+
continueMapping = false;
210+
}
204211
}
205212
}
206213

src/main/java/com/bioinceptionlabs/reactionblast/mapping/algorithm/GameTheoryMixture.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -149,38 +149,45 @@ private synchronized void GenerateIsoMorphismMapping() throws Exception {
149149

150150
private synchronized void GenerateMapping(boolean flag) throws Exception {
151151
boolean ruleMatchingFlag = flag;
152-
if (DEBUG) {
153-
printMatrixAtomContainer(mh, eductList, productList);
154-
LOGGER.debug("**********Orignal Matrix**************");
155-
printSimMatrix(mh, eductList, productList);
156-
printCliqueMatrix(mh, eductList, productList);
152+
int maxIterations = 100;
153+
int iteration = 0;
154+
boolean continueMapping = true;
155+
while (continueMapping && iteration < maxIterations) {
156+
if (DEBUG) {
157+
printMatrixAtomContainer(mh, eductList, productList);
158+
LOGGER.debug("**********Orignal Matrix**************");
159+
printSimMatrix(mh, eductList, productList);
160+
printCliqueMatrix(mh, eductList, productList);
157161
// printStereoMatrix(mh, eductList, productList);
158162
// printFragmentMatrix(mh, eductList, productList);
159163
// printEnergyMatrix(mh, eductList, productList);
160-
}
164+
}
161165

162-
if (!ruleMatchingFlag) {//First map the biggest fragment the call rules
163-
RuleBasedMappingHandler ruleBasedMappingHandler
164-
= new RuleBasedMappingHandler(mh, eductList, productList);
165-
if (ruleBasedMappingHandler.isMatchFound()) {
166-
LOGGER.debug("Rule Based Mapping Handler Match Found");
167-
mh = Selector.modifyMatrix(ruleBasedMappingHandler.getMatrixHolder());
166+
if (!ruleMatchingFlag) {//First map the biggest fragment the call rules
167+
RuleBasedMappingHandler ruleBasedMappingHandler
168+
= new RuleBasedMappingHandler(mh, eductList, productList);
169+
if (ruleBasedMappingHandler.isMatchFound()) {
170+
LOGGER.debug("Rule Based Mapping Handler Match Found");
171+
mh = Selector.modifyMatrix(ruleBasedMappingHandler.getMatrixHolder());
172+
}
173+
ruleMatchingFlag = true;
168174
}
169-
ruleMatchingFlag = true;
170-
}
171175

172-
winner.searchWinners(educts, products, mh);
173-
if (DEBUG) {
174-
printFlagMatrix(winner, eductList, productList);
175-
}
176-
if (winner.getFlag()) {
176+
winner.searchWinners(educts, products, mh);
177+
if (DEBUG) {
178+
printFlagMatrix(winner, eductList, productList);
179+
}
180+
if (winner.getFlag()) {
177181

178-
LOGGER.debug("**********Updated Mapping**************");
179-
UpdateMapping();
180-
LOGGER.debug("**********Updated Matrix**************");
181-
UpdateMatrix(mh, removeHydrogen);
182-
LOGGER.debug("**********Generate Mapping**************");
183-
GenerateMapping(ruleMatchingFlag);
182+
LOGGER.debug("**********Updated Mapping**************");
183+
UpdateMapping();
184+
LOGGER.debug("**********Updated Matrix**************");
185+
UpdateMatrix(mh, removeHydrogen);
186+
LOGGER.debug("**********Generate Mapping**************");
187+
iteration++;
188+
} else {
189+
continueMapping = false;
190+
}
184191
}
185192
}
186193

src/main/java/com/bioinceptionlabs/reactionblast/mapping/algorithm/GameTheoryRings.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,37 +159,44 @@ private synchronized void GenerateIsoMorphismMapping() throws Exception {
159159
}
160160

161161
private synchronized void GenerateMapping() throws Exception {
162-
LOGGER.debug("GenerateMapping");
163-
if (DEBUG) {
164-
LOGGER.debug("**********Orignal Matrix**************");
165-
printMatrixAtomContainer(mh, eductList, productList);
166-
printSimMatrix(mh, eductList, productList);
167-
printCliqueMatrix(mh, eductList, productList);
162+
int maxIterations = 100;
163+
int iteration = 0;
164+
boolean continueMapping = true;
165+
while (continueMapping && iteration < maxIterations) {
166+
LOGGER.debug("GenerateMapping");
167+
if (DEBUG) {
168+
LOGGER.debug("**********Orignal Matrix**************");
169+
printMatrixAtomContainer(mh, eductList, productList);
170+
printSimMatrix(mh, eductList, productList);
171+
printCliqueMatrix(mh, eductList, productList);
168172
// printStereoMatrix(mh, eductList, productList);
169173
// printFragmentMatrix(mh, eductList, productList);
170174
// printEnergyMatrix(mh, eductList, productList);
171-
}
175+
}
172176

173-
RuleBasedMappingHandler ruleBasedMappingHandler = new RuleBasedMappingHandler(mh, eductList, productList);
174-
if (ruleBasedMappingHandler.isMatchFound()) {
177+
RuleBasedMappingHandler ruleBasedMappingHandler = new RuleBasedMappingHandler(mh, eductList, productList);
178+
if (ruleBasedMappingHandler.isMatchFound()) {
175179
// LOGGER.debug("RuleBasedMappingHandler Match");
176-
mh = Selector.modifyMatrix(ruleBasedMappingHandler.getMatrixHolder());
180+
mh = Selector.modifyMatrix(ruleBasedMappingHandler.getMatrixHolder());
177181
// printSimMatrix(mh, eductList, productList);
178-
}
182+
}
179183

180-
winner.searchWinners(educts, products, mh);
184+
winner.searchWinners(educts, products, mh);
181185

182-
if (DEBUG) {
183-
printFlagMatrix(winner, eductList, productList);
184-
}
185-
if (winner.getFlag()) {
186+
if (DEBUG) {
187+
printFlagMatrix(winner, eductList, productList);
188+
}
189+
if (winner.getFlag()) {
186190

187-
LOGGER.debug("**********Updated Mapping**************");
188-
UpdateMapping();
189-
LOGGER.debug("**********Updated Matrix**************");
190-
UpdateMatrix(mh, removeHydrogen);
191-
LOGGER.debug("**********Generate Mapping**************");
192-
GenerateMapping();
191+
LOGGER.debug("**********Updated Mapping**************");
192+
UpdateMapping();
193+
LOGGER.debug("**********Updated Matrix**************");
194+
UpdateMatrix(mh, removeHydrogen);
195+
LOGGER.debug("**********Generate Mapping**************");
196+
iteration++;
197+
} else {
198+
continueMapping = false;
199+
}
193200
}
194201
}
195202

src/main/java/com/bioinceptionlabs/reactionblast/mapping/graph/GraphMatcher.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.IOException;
2222
import static java.lang.Runtime.getRuntime;
23-
import static java.lang.System.gc;
2423

2524
import java.util.ArrayList;
2625
import java.util.Collection;
@@ -346,7 +345,6 @@ Ring matcher is set true if both sides have rings else it set to false (IMP for
346345
jobMap.remove(removeKey);
347346
});
348347
jobReplicatorList.clear();
349-
gc();
350348

351349
} catch (Exception ex) {
352350
LOGGER.error(SEVERE, null, ex);

0 commit comments

Comments
 (0)