User Tools

Site Tools


zalozeni:logika:runlogic

runLogic()

metoda runLogic() bezi nekonecnou smycku, ve ktere po uplynuti daneho casoveho intervalu se provedou v zavislosti na DokuwikiRunningSubstatus patricne operace. Vyskoceni z teto smysky je umozneno prepnutim running=false, coz je promenna, jejiz hodnotu nastavuje za nas abstraktni classa ExperimentLogic.

Promenne lastReadOutTick a intervaReadOut urcuji, zda je cas vycist nove hodnoty. Vycitani se provadi prostrednictim observeru, ktere po cteni automaticky raisuji prislusny event. Pokud je cteni neuspesne, je vyhozena chyba. Nasledne chyby se nascitavaji do promenne consecutiveErrorCount.

Promenne lastPersistTick a persistInterva urcuji, zda je cas persistovat namerenne hodnoty. Hodnoty k persistovani jsou zpravidla poskytovany vice observery, takze je treba tyto hodnoty spojit do jedne tridy DokuwikiDataRow a raisnout DokuwikiNewDataEvent.

Prepinani mezi jednotlivymi DokuwikiRunningSubstatus muze vyvolat bud metoda runLogic() samotna (napr. po uplynuti nejakeho casu nebo po splneni nejake podminky), nebo muze byt volana z grafiky uzivatelem (stisk tlacitka). Proto je pro prepnuti DokuwikiRunningSubstatus raisovan DokuwikiNewRunningSubstatusEvent. Naslouchani na tento event je uskutecneno v metode setExpInstance(), ktera je rovnez prilozena.

@Override
public ChangingStatusReturnCode runLogic() {
  while (true) {
 
    if (!running)
      break;
 
    try {
      Thread.sleep(1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
 
    try {
      switch (runningSubstatus) {
      case PREPARATION:
        if (System.currentTimeMillis() - lastReadOutTick > intervalReadOut) {
          tempObserver.readValue();
          lastReadOutTick = System.currentTimeMillis();
 
          // nejaka podminka je splnena, takze se prepnu do RUNNING
          if (true) {
            raiseExpInstanceEvent(new DokuwikiNewRunningSubstatusEvent(DokuwikiRunningSubstatus.RUNNING));
          }
        }
        break;
      case RUNNING:
        if (System.currentTimeMillis() - lastReadOutTick > intervalReadOut) {
          tempObserver.readValue();
          counterObserver.readValue();
          lastReadOutTick = System.currentTimeMillis();
 
          // persistovani
          if (System.currentTimeMillis() - lastPersistTick > intervalPersist) {
            DokuwikiDataRow dataRow = new DokuwikiDataRow(getActualExperimentTime());
            dataRow.setValue1(tempObserver.getTemp());
            dataRow.setValue2(counterObserver.getCounter());
            dataService.saveDataRow(dataRow);
            raiseExpInstanceEvent(new DokuwikiNewDataEvent(dataRow));
            lastPersistTick = System.currentTimeMillis();
          }
 
          // normalni zakonceni
          if (Math.abs(tempObserver.getTemp() - 600) < 5) {
            raiseExpInstanceEvent(new DokuwikiNewRunningSubstatusEvent(DokuwikiRunningSubstatus.FINISHED));
          }
        }
 
        break;
      case FINISHED:
        DokuwikiInstanceCalculatorFacotry.calculate(expInstance, CalculatorEnum.BY_NORM_CALCULATOR);
        return ChangingStatusReturnCode.SUCCESSFUL_END;
      }
 
      consecutiveErrorCount = 0;
 
    } catch (IOException e) {
      consecutiveErrorCount++;
      e.printStackTrace();
    } catch (GetValueException e) {
      consecutiveErrorCount++;
      e.printStackTrace();
    } catch (Exception e) {
      consecutiveErrorCount++;
      e.printStackTrace();
    }
 
  }
 
  if (consecutiveErrorCount > MAX_CONSECUTIVE_ERRORS) {
    return ChangingStatusReturnCode.ERROR;
  }
 
  return ChangingStatusReturnCode.OK;
}
@Override
public void setExpInstance(ExperimentInstance expInstance) {
  super.setExpInstance(expInstance);
 
  instrument = (DokuwikiInstrument) instrument;
  instanceSS = (DokuwikiExpInstanceSettignsService) expInstance.getSettignsService();
  dataService = (DokuwikiDataService) expInstance.getDataService();
 
  ServiceLocator.getInstance().getLogicModuleManager().registerListener(expInstance, new ExperimentLogicListener() {
    @Override
    public void processEvent(LogicModuleEvent event) {
      if (event instanceof DokuwikiNewRunningSubstatusEvent) {
        DokuwikiNewRunningSubstatusEvent pevent = (DokuwikiNewRunningSubstatusEvent) event;
        ChangingStatusReturnCode status;
        switch (pevent.getSubstatus()) {
        case PREPARATION:
          status = initSamplePreparation();
          break;
        case RUNNING:
          status = initRunning();
          break;
        case FINISHED:
          status = initFinished();
          break;
        default:
          status = ChangingStatusReturnCode.ERROR;
        }
        if (status == ChangingStatusReturnCode.OK) {
          changeRunningSubstatus(pevent.getSubstatus());
        }
      }
    }
  });
}
 
private void changeRunningSubstatus(DokuwikiRunningSubstatus substatus) {
  runningSubstatus = substatus;
  if (instanceSS != null) {
    instanceSS.setRunningSubstatus(substatus);
  }
}
zalozeni/logika/runlogic.txt · Last modified: 2016/08/01 09:59 by jlochman