Changeset 13633


Ignore:
Timestamp:
Oct 1, 2019 3:27:12 PM (2 months ago)
Author:
Jonathan Kämpe
Message:

#5837 Moved some classes

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  
    1818class MappedExternalFunction<K extends Variable<V, T>, V extends Value, T extends Type<V>, E extends External<K>> extends ExternalFunctionImpl<K,V,T,E> {
    1919
    20     private Map<String, ExternalFunction<K, V>> lives = new HashMap<>();
     20    private final Map<String, ExternalFunction<K, V>> lives = new HashMap<>();
    2121
    22     private LinkedHashSet<ExternalFunction<K, V>> livingCachedExternals;
     22    private final LinkedHashSet<ExternalFunction<K, V>> livingCachedExternals;
    2323    private final int externalConstantEvaluationMaxProc;
    2424
     
    3939        ExternalFunction<K, V> ef = lives.get(name);
    4040        if (ef == null) {
    41             LiveExternalFunction lef = new LiveExternalFunction();
     41            LiveExternalFunction<K,V,T,E> lef = new LiveExternalFunction<>(compiler, this, livingCachedExternals, externalConstantEvaluationMaxProc);
    4242            try {
    4343                lef.ready(ext, values, timeout);
     
    6868    }
    6969
    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         @Override
    82         public String getMessage() {
    83             return MappedExternalFunction.this.getMessage();
    84         }
    85 
    86         @Override
    87         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 inputs
    117                 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 used
    125             livingCachedExternals.remove(this);
    126             livingCachedExternals.add(this);
    127 
    128             // If we are over the allowed number of cached processes
    129             // we kill the least recently used.
    130             if (livingCachedExternals.size() > externalConstantEvaluationMaxProc) {
    131                 livingCachedExternals.iterator().next().destroyProcess();
    132             }
    133         }
    134 
    135         @Override
    136         public void destroyProcess() {
    137             if (com != null) {
    138                 livingCachedExternals.remove(this);
    139                 com.destroy();
    140                 com = null;
    141             }
    142         }
    143 
    144         @Override
    145         public void remove() {
    146             // Removing this executable is handled by surrounding MappedExternalFunction
    147             throw new UnsupportedOperationException();
    148         }
    149     }
    15070}
Note: See TracChangeset for help on using the changeset viewer.