Changeset 13633
- Timestamp:
- Oct 1, 2019 3:27:12 PM (2 months ago)
- Location:
- branches/dev-cw-evaluator/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/common/evaluation
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/dev-cw-evaluator/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/common/evaluation/MappedExternalFunction.java
r13632 r13633 18 18 class MappedExternalFunction<K extends Variable<V, T>, V extends Value, T extends Type<V>, E extends External<K>> extends ExternalFunctionImpl<K,V,T,E> { 19 19 20 private Map<String, ExternalFunction<K, V>> lives = new HashMap<>();20 private final Map<String, ExternalFunction<K, V>> lives = new HashMap<>(); 21 21 22 private LinkedHashSet<ExternalFunction<K, V>> livingCachedExternals;22 private final LinkedHashSet<ExternalFunction<K, V>> livingCachedExternals; 23 23 private final int externalConstantEvaluationMaxProc; 24 24 … … 39 39 ExternalFunction<K, V> ef = lives.get(name); 40 40 if (ef == null) { 41 LiveExternalFunction lef = new LiveExternalFunction();41 LiveExternalFunction<K,V,T,E> lef = new LiveExternalFunction<>(compiler, this, livingCachedExternals, externalConstantEvaluationMaxProc); 42 42 try { 43 43 lef.ready(ext, values, timeout); … … 68 68 } 69 69 70 /**71 * Represents a (possible) living external function process.72 */73 private class LiveExternalFunction implements ExternalFunction<K, V> {74 75 protected ProcessCommunicator<V, T> com;76 77 public LiveExternalFunction() {78 super();79 }80 81 @Override82 public String getMessage() {83 return MappedExternalFunction.this.getMessage();84 }85 86 @Override87 public int evaluate(External<K> ext, Map<K, V> values, int timeout) throws IOException {88 compiler.log().debug("Evaluating live external function: " + ext.getName());89 try {90 ready(ext, values, timeout);91 long time = System.currentTimeMillis();92 MappedExternalFunction.this.evaluate(ext, values, timeout, com);93 time = System.currentTimeMillis() - time;94 compiler.log().debug("Finished evaluating live external function, time: " + time + "ms");95 } catch (ProcessCommunicator.AbortConstantEvaluationException e) {96 97 } catch (ConstantEvaluationException e) {98 destroyProcess();99 throw e;100 } catch (IOException e) {101 destroyProcess();102 throw e;103 }104 return 0;105 }106 107 /**108 * Make sure process is ready for evaluation call.109 */110 protected void ready(External<K> ext, Map<K, V> values, int timeout) throws IOException {111 if (com == null) {112 long time1 = System.currentTimeMillis();113 // Start process if not live.114 com = createProcessCommunicator();115 long time2 = System.currentTimeMillis();116 // Send external object constructor inputs117 MappedExternalFunction.this.setup(ext, values, timeout, com);118 long time3 = System.currentTimeMillis();119 compiler.log().debug("Setup live external function: " + ext.getName()120 + ", createProcessCommunicator() time: " + (time2 - time1)121 + "ms, setup time: " + (time3 - time2) + "ms");122 }123 124 // Mark as most recently used125 livingCachedExternals.remove(this);126 livingCachedExternals.add(this);127 128 // If we are over the allowed number of cached processes129 // we kill the least recently used.130 if (livingCachedExternals.size() > externalConstantEvaluationMaxProc) {131 livingCachedExternals.iterator().next().destroyProcess();132 }133 }134 135 @Override136 public void destroyProcess() {137 if (com != null) {138 livingCachedExternals.remove(this);139 com.destroy();140 com = null;141 }142 }143 144 @Override145 public void remove() {146 // Removing this executable is handled by surrounding MappedExternalFunction147 throw new UnsupportedOperationException();148 }149 }150 70 }
Note: See TracChangeset
for help on using the changeset viewer.