Search in sources :

Example 1 with RealTimeClock

use of de.neemann.digital.draw.model.RealTimeClock in project Digital by hneemann.

the class Main method createAndStartModel.

private boolean createAndStartModel(boolean globalRunClock, ModelEvent updateEvent, ModelModifier modelModifier) {
    try {
        circuitComponent.removeHighLighted();
        long time = System.currentTimeMillis();
        modelCreator = new ModelCreator(circuitComponent.getCircuit(), library);
        if (model != null) {
            modelSync.access(() -> model.close());
            circuitComponent.getCircuit().clearState();
            model = null;
        }
        model = modelCreator.createModel(true);
        time = System.currentTimeMillis() - time;
        LOGGER.debug("model creation: " + time + " ms");
        model.setWindowPosManager(windowPosManager);
        statusLabel.setText(Lang.get("msg_N_nodes", model.size()));
        realTimeClockRunning = false;
        modelSync = null;
        if (globalRunClock) {
            int threadRunnerCount = 0;
            for (Clock c : model.getClocks()) if (c.getFrequency() > 0) {
                if (modelSync == null)
                    modelSync = new LockSync();
                final RealTimeClock realTimeClock = new RealTimeClock(model, c, timerExecutor, this, modelSync, this);
                model.addObserver(realTimeClock);
                if (realTimeClock.isThreadRunner())
                    threadRunnerCount++;
                realTimeClockRunning = true;
            }
            if (threadRunnerCount > 1)
                throw new RuntimeException(Lang.get("err_moreThanOneFastClock"));
        }
        if (modelSync == null)
            modelSync = NoSync.INST;
        circuitComponent.setModeAndReset(true, modelSync);
        if (realTimeClockRunning) {
            // if clock is running, enable automatic update of gui
            GuiModelObserver gmo = new GuiModelObserver(circuitComponent, updateEvent);
            modelCreator.connectToGui(gmo);
            model.addObserver(gmo);
        } else
            // all repainting is initiated by user actions!
            modelCreator.connectToGui(null);
        doStep.setEnabled(false);
        runToBreakAction.setEnabled(!realTimeClockRunning && model.isFastRunModel());
        ElementAttributes settings = circuitComponent.getCircuit().getAttributes();
        if (settings.get(Keys.SHOW_DATA_TABLE) || windowPosManager.isVisible("probe"))
            showMeasurementDialog(updateEvent);
        if (settings.get(Keys.SHOW_DATA_GRAPH) || windowPosManager.isVisible("dataSet"))
            showMeasurementGraph(updateEvent);
        if (settings.get(Keys.SHOW_DATA_GRAPH_MICRO))
            showMeasurementGraph(ModelEvent.MICROSTEP);
        if (modelModifier != null)
            modelModifier.preInit(model);
        model.init();
        return true;
    } catch (NodeException | PinException | RuntimeException | ElementNotFoundException e) {
        if (model != null)
            showErrorAndStopModel(Lang.get("msg_errorCreatingModel"), e);
        else
            showErrorWithoutARunningModel(Lang.get("msg_errorCreatingModel"), e);
    }
    return false;
}
Also used : RealTimeClock(de.neemann.digital.draw.model.RealTimeClock) ElementAttributes(de.neemann.digital.core.element.ElementAttributes) ElementNotFoundException(de.neemann.digital.draw.library.ElementNotFoundException) RealTimeClock(de.neemann.digital.draw.model.RealTimeClock) Clock(de.neemann.digital.core.wiring.Clock) ModelCreator(de.neemann.digital.draw.model.ModelCreator) LockSync(de.neemann.digital.gui.sync.LockSync)

Aggregations

ElementAttributes (de.neemann.digital.core.element.ElementAttributes)1 Clock (de.neemann.digital.core.wiring.Clock)1 ElementNotFoundException (de.neemann.digital.draw.library.ElementNotFoundException)1 ModelCreator (de.neemann.digital.draw.model.ModelCreator)1 RealTimeClock (de.neemann.digital.draw.model.RealTimeClock)1 LockSync (de.neemann.digital.gui.sync.LockSync)1