0001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
0002 *
0003 * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>.
0004 *
0005 * "Hilbert II" is free software; you can redistribute
0006 * it and/or modify it under the terms of the GNU General Public
0007 * License as published by the Free Software Foundation; either
0008 * version 2 of the License, or (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0013 * GNU General Public License for more details.
0014 */
0015
0016 package org.qedeq.kernel.bo.service.internal;
0017
0018 import java.util.ArrayList;
0019 import java.util.List;
0020
0021 import org.qedeq.base.trace.Trace;
0022 import org.qedeq.base.utility.StringUtility;
0023 import org.qedeq.kernel.bo.log.ModuleEventLog;
0024 import org.qedeq.kernel.bo.module.KernelModuleReferenceList;
0025 import org.qedeq.kernel.bo.module.ModuleConstantsExistenceChecker;
0026 import org.qedeq.kernel.bo.module.ModuleLabels;
0027 import org.qedeq.kernel.bo.service.basis.InternalModuleServicePlugin;
0028 import org.qedeq.kernel.se.common.ModuleDataException;
0029 import org.qedeq.kernel.se.common.ModuleService;
0030 import org.qedeq.kernel.se.common.SourceFileExceptionList;
0031 import org.qedeq.kernel.se.dto.module.QedeqVo;
0032 import org.qedeq.kernel.se.state.AbstractState;
0033 import org.qedeq.kernel.se.state.DependencyState;
0034 import org.qedeq.kernel.se.state.FormallyProvedState;
0035 import org.qedeq.kernel.se.state.LoadingImportsState;
0036 import org.qedeq.kernel.se.state.LoadingState;
0037 import org.qedeq.kernel.se.state.WellFormedState;
0038
0039
0040 /**
0041 * Changes the states of {@link org.qedeq.kernel.bo.service.control.DefaultKernelQedeqBo}s.
0042 * All state changing is done here.
0043 *
0044 * @author Michael Meyling
0045 */
0046 public class StateManager {
0047
0048 /** This class. */
0049 private static final Class CLASS = StateManager.class;
0050
0051 /** Main BO to care about. */
0052 private final DefaultKernelQedeqBo bo;
0053
0054 /** Completeness during loading from web. */
0055 private int loadingCompleteness;
0056
0057 /** Describes QEDEQ module loading state. */
0058 private LoadingState loadingState;
0059
0060 /** Describes QEDEQ module loading imports state. */
0061 private LoadingImportsState loadingImportsState;
0062
0063 /** Describes QEDEQ module dependency state. */
0064 private DependencyState dependencyState;
0065
0066 /** Describes QEDEQ module well formed state. */
0067 private WellFormedState wellFormedState;
0068
0069 /** Describes QEDEQ module formally proved state. */
0070 private FormallyProvedState formallyProvedState;
0071
0072 /** Holds QEDEQ module plugin results. */
0073 private PluginResultManager pluginResults;
0074
0075 /** Failure exceptions for basic operations. */
0076 private SourceFileExceptionList errors;
0077
0078 StateManager(final DefaultKernelQedeqBo bo) {
0079 this.bo = bo;
0080 loadingState = LoadingState.STATE_UNDEFINED;
0081 loadingCompleteness = 0;
0082 loadingImportsState = LoadingImportsState.STATE_UNDEFINED;
0083 dependencyState = DependencyState.STATE_UNDEFINED;
0084 wellFormedState = WellFormedState.STATE_UNCHECKED;
0085 formallyProvedState = FormallyProvedState.STATE_UNCHECKED;
0086 pluginResults = new PluginResultManager();
0087 }
0088
0089 /**
0090 * Delete QEDEQ module. Invalidates all dependent modules.
0091 */
0092 public void delete() {
0093 checkIfDeleted();
0094 invalidateOtherDependentModulesToLoaded();
0095 setLoadingState(LoadingState.STATE_DELETED);
0096 bo.setQedeqVo(null);
0097 bo.getKernelRequiredModules().clear();
0098 bo.getDependentModules().clear();
0099 bo.setLabels(null);
0100 setDependencyState(DependencyState.STATE_UNDEFINED);
0101 setWellFormedState(WellFormedState.STATE_UNCHECKED);
0102 setFormallyProvedState(FormallyProvedState.STATE_UNCHECKED);
0103 setErrors(null);
0104 ModuleEventLog.getInstance().removeModule(bo);
0105 }
0106
0107 /**
0108 * Is the module in a failure state? That is the case if loading of module or imported modules
0109 * failed or the logical check failed. Possible plugin failures don't matter.
0110 *
0111 * @return Failure during loading or logical check occurred.
0112 */
0113 public boolean hasBasicFailures() {
0114 return loadingState.isFailure() || loadingImportsState.isFailure() || dependencyState.isFailure()
0115 || wellFormedState.isFailure() || formallyProvedState.isFailure();
0116 }
0117
0118 /**
0119 * Has the module any errors? This includes loading, importing, logical and plugin errors.
0120 *
0121 * @return Errors occurred.
0122 */
0123 public boolean hasErrors() {
0124 return hasBasicFailures() || (getErrors() != null && getErrors().size() > 0);
0125 }
0126
0127 /**
0128 * Has the module any warnings? This includes loading, importing, logical and plugin warnings.
0129 *
0130 * @return Warnings occurred.
0131 */
0132 public boolean hasWarnings() {
0133 return (getWarnings() != null && getWarnings().size() > 0);
0134 }
0135
0136 /**
0137 * Set completeness percentage.
0138 *
0139 * @param completeness Completeness of loading into memory.
0140 */
0141 public void setLoadingCompleteness(final int completeness) {
0142 this.loadingCompleteness = completeness;
0143 }
0144
0145 /**
0146 * Get loading completeness percentage.
0147 *
0148 * @return Completeness as percent number.
0149 */
0150 public int getLoadingCompleteness() {
0151 return this.loadingCompleteness;
0152 }
0153
0154 /**
0155 * Get loading state.
0156 *
0157 * @return Loading state.
0158 */
0159 public LoadingState getLoadingState() {
0160 return this.loadingState;
0161 }
0162
0163 /**
0164 * Is the module loaded?
0165 *
0166 * @return Is the module loaded?
0167 */
0168 public boolean isLoaded() {
0169 return loadingState == LoadingState.STATE_LOADED;
0170 }
0171
0172 /**
0173 * Set loading progress module state.
0174 *
0175 * @param state Module loading state. Must not be <code>null</code>.
0176 * @throws IllegalStateException State is a failure state or module loaded state.
0177 */
0178 public void setLoadingProgressState(final LoadingState state) {
0179 checkIfDeleted();
0180 if (state == LoadingState.STATE_LOADED) {
0181 throw new IllegalArgumentException(
0182 "this state could only be set by calling method setLoaded");
0183 }
0184 if (state != LoadingState.STATE_DELETED && state.isFailure()) {
0185 throw new IllegalArgumentException(
0186 "this is a failure state, call setLoadingFailureState for " + state);
0187 }
0188 // if module has no loading state we give him one before creating an event
0189 if (getLoadingState() == LoadingState.STATE_UNDEFINED) {
0190 setLoadingState(state);
0191 ModuleEventLog.getInstance().addModule(bo);
0192 }
0193 if (state == LoadingState.STATE_DELETED) {
0194 throw new IllegalArgumentException(
0195 "call delete for " + state);
0196 }
0197 setLoadingState(state);
0198 bo.setQedeqVo(null);
0199 bo.getKernelRequiredModules().clear();
0200 bo.getDependentModules().clear();
0201 bo.setLabels(null);
0202 setLoadingImportsState(LoadingImportsState.STATE_UNDEFINED);
0203 setDependencyState(DependencyState.STATE_UNDEFINED);
0204 setWellFormedState(WellFormedState.STATE_UNCHECKED);
0205 setErrors(null);
0206 ModuleEventLog.getInstance().stateChanged(bo);
0207 }
0208
0209 /**
0210 * Set failure module state.
0211 *
0212 * @param state Module loading state. Must not be <code>null</code>.
0213 * @param e Exception that occurred during loading. Must not be <code>null</code>.
0214 * @throws IllegalArgumentException <code>state</code> is no failure state
0215 */
0216 public void setLoadingFailureState(final LoadingState state,
0217 final SourceFileExceptionList e) {
0218 if (e == null) {
0219 throw new NullPointerException("Exception must not be null");
0220 }
0221 checkIfDeleted();
0222 if (!state.isFailure()) {
0223 throw new IllegalArgumentException(
0224 "this is no failure state, call setLoadingProgressState");
0225 }
0226 // if module has no loading state we give him one before creating an event
0227 if (getLoadingState() == LoadingState.STATE_UNDEFINED) {
0228 setLoadingState(state);
0229 ModuleEventLog.getInstance().addModule(bo);
0230 }
0231 bo.setQedeqVo(null);
0232 bo.getKernelRequiredModules().clear();
0233 bo.getDependentModules().clear();
0234 bo.setLabels(null);
0235 setLoadingState(state);
0236 setLoadingImportsState(LoadingImportsState.STATE_UNDEFINED);
0237 setDependencyState(DependencyState.STATE_UNDEFINED);
0238 setWellFormedState(WellFormedState.STATE_UNCHECKED);
0239 setFormallyProvedState(FormallyProvedState.STATE_UNCHECKED);
0240 setErrors(e);
0241 ModuleEventLog.getInstance().stateChanged(bo);
0242 }
0243
0244 /**
0245 * Set loading state to "loaded". Also puts <code>null</code> to
0246 * {@link DefaultKernelQedeqBo#getLabels()}.
0247 *
0248 * @param qedeq This module was loaded. Must not be <code>null</code>.
0249 * @param labels Module labels.
0250 * @throws NullPointerException One argument was <code>null</code>.
0251 */
0252 public void setLoaded(final QedeqVo qedeq, final ModuleLabels labels) {
0253 checkIfDeleted();
0254 if (qedeq == null) {
0255 throw new NullPointerException("Qedeq is null");
0256 }
0257 setLoadingState(LoadingState.STATE_LOADED);
0258 setLoadingImportsState(LoadingImportsState.STATE_UNDEFINED);
0259 setDependencyState(DependencyState.STATE_UNDEFINED);
0260 setWellFormedState(WellFormedState.STATE_UNCHECKED);
0261 setFormallyProvedState(FormallyProvedState.STATE_UNCHECKED);
0262 setErrors(null);
0263 bo.setQedeqVo(qedeq);
0264 bo.getKernelRequiredModules().clear();
0265 bo.getDependentModules().clear();
0266 bo.setLabels(labels);
0267 ModuleEventLog.getInstance().stateChanged(bo);
0268 }
0269
0270 /**
0271 * Set loading imports progress module state.
0272 *
0273 * @param state Module state. Must not be <code>null</code>.
0274 * @throws IllegalStateException Module is not yet loaded.
0275 * @throws IllegalArgumentException <code>state</code> is failure state or loaded required
0276 * state.
0277 * @throws NullPointerException <code>state</code> is <code>null</code>.
0278 */
0279 public void setLoadingImportsProgressState(final LoadingImportsState state) {
0280 checkIfDeleted();
0281 if (!isLoaded() && state != LoadingImportsState.STATE_UNDEFINED) {
0282 throw new IllegalStateException("module is not yet loaded");
0283 }
0284 if (state.isFailure()) {
0285 throw new IllegalArgumentException(
0286 "this is a failure state, call setLoadingImportsFailureState");
0287 }
0288 if (state == LoadingImportsState.STATE_LOADED_IMPORTED_MODULES) {
0289 throw new IllegalArgumentException(
0290 "this state could only be set by calling method setLoadedImports");
0291 }
0292 setLoadingImportsState(state);
0293 ModuleEventLog.getInstance().stateChanged(bo);
0294 }
0295
0296 /**
0297 * Set failure module state.
0298 *
0299 * @param state Module loading imports state. Must not be <code>null</code>.
0300 * @param e Exception that occurred during loading. Must not be <code>null</code>.
0301 * @throws IllegalStateException Module is not yet loaded.
0302 * @throws IllegalArgumentException <code>state</code> is no failure state.
0303 * @throws NullPointerException <code>state</code> is <code>null</code>.
0304 */
0305 public void setLoadingImportsFailureState(final LoadingImportsState state,
0306 final SourceFileExceptionList e) {
0307 if (e == null) {
0308 throw new NullPointerException("Exception must not be null");
0309 }
0310 checkIfDeleted();
0311 if (!isLoaded()) {
0312 throw new IllegalStateException("module is not yet loaded");
0313 }
0314 if (!state.isFailure()) {
0315 throw new IllegalArgumentException(
0316 "this is no failure state, call setLoadingProgressState");
0317 }
0318 setLoadingImportsState(state);
0319 setErrors(e);
0320 ModuleEventLog.getInstance().stateChanged(bo);
0321 }
0322
0323 /**
0324 * Get loading imports state.
0325 *
0326 * @return Dependency state.
0327 */
0328 public LoadingImportsState getLoadingImportsState() {
0329 return this.loadingImportsState;
0330 }
0331
0332 /**
0333 * Are all imported modules loaded?
0334 *
0335 * @return Are all imported module loaded?
0336 */
0337 public boolean hasLoadedImports() {
0338 return loadingImportsState == LoadingImportsState.STATE_LOADED_IMPORTED_MODULES;
0339 }
0340
0341 /**
0342 * Set dependency progress module state.
0343 *
0344 * @param state Module state. Must not be <code>null</code>.
0345 * @throws IllegalStateException Module is not yet loaded.
0346 * @throws IllegalArgumentException <code>state</code> is failure state or loaded required
0347 * state.
0348 * @throws NullPointerException <code>state</code> is <code>null</code>.
0349 */
0350 public void setDependencyProgressState(final DependencyState state) {
0351 checkIfDeleted();
0352 if (!isLoaded() && state != DependencyState.STATE_UNDEFINED) {
0353 throw new IllegalStateException("module is not yet loaded");
0354 }
0355 if (state.isFailure()) {
0356 throw new IllegalArgumentException(
0357 "this is a failure state, call setDependencyFailureState");
0358 }
0359 if (state == DependencyState.STATE_LOADED_REQUIRED_MODULES) {
0360 throw new IllegalArgumentException(
0361 "this state could only be set by calling method setLoadedRequiredModules");
0362 }
0363 setWellFormedState(WellFormedState.STATE_UNCHECKED);
0364 setFormallyProvedState(FormallyProvedState.STATE_UNCHECKED);
0365 setDependencyState(state);
0366 setErrors(null);
0367 ModuleEventLog.getInstance().stateChanged(bo);
0368 }
0369
0370 /**
0371 * Set failure module state.
0372 *
0373 * @param state Module dependency state. Must not be <code>null</code>.
0374 * @param e Exception that occurred during loading. Must not be <code>null</code>.
0375 * @throws IllegalStateException Module is not yet loaded.
0376 * @throws IllegalArgumentException <code>state</code> is no failure state.
0377 * @throws NullPointerException <code>state</code> is <code>null</code>.
0378 */
0379 public void setDependencyFailureState(final DependencyState state,
0380 final SourceFileExceptionList e) {
0381 if (e == null) {
0382 throw new NullPointerException("Exception must not be null");
0383 }
0384 checkIfDeleted();
0385 if (!isLoaded()) {
0386 throw new IllegalStateException("module is not yet loaded");
0387 }
0388 if (!state.isFailure()) {
0389 throw new IllegalArgumentException(
0390 "this is no failure state, call setLoadingProgressState");
0391 }
0392 setDependencyState(state);
0393 setErrors(e);
0394 ModuleEventLog.getInstance().stateChanged(bo);
0395 }
0396
0397 /**
0398 * Get dependency state.
0399 *
0400 * @return Dependency state.
0401 */
0402 public DependencyState getDependencyState() {
0403 return this.dependencyState;
0404 }
0405
0406 /**
0407 * Reset all (recursive) dependent modules (if any) to state loaded.
0408 */
0409 private void invalidateOtherDependentModulesToLoaded() {
0410 final String method = "invalidateOtherDependModulesToLoaded";
0411 Trace.begin(CLASS, this, method);
0412 Trace.param(CLASS, this, method, "bo", bo);
0413 if (hasLoadedRequiredModules()) {
0414 final KernelModuleReferenceList dependent = bo.getDependentModules();
0415 Trace.trace(CLASS, this, method, "begin list of dependent modules");
0416 // remember dependent modules
0417 final List list = new ArrayList();
0418 for (int i = 0; i < dependent.size(); i++) {
0419 Trace.param(CLASS, this, method, "" + i, dependent.getKernelQedeqBo(i));
0420 list.add(dependent.getKernelQedeqBo(i));
0421 }
0422 Trace.trace(CLASS, this, method, "end list of dependent modules");
0423 for (int i = 0; i < list.size(); i++) {
0424 DefaultKernelQedeqBo ref = (DefaultKernelQedeqBo) list.get(i);
0425 // work on it, if still in list of dependent modules
0426 if (dependent.contains(ref)) {
0427 ref.getStateManager().invalidateDependentModulesToLoaded();
0428 }
0429 }
0430 list.clear();
0431 dependent.clear();
0432
0433 final KernelModuleReferenceList required = bo.getKernelRequiredModules();
0434 for (int i = 0; i < required.size(); i++) {
0435 DefaultKernelQedeqBo ref = (DefaultKernelQedeqBo) required.getKernelQedeqBo(i);
0436 Trace.param(CLASS, this, method, "remove dependence from", ref);
0437 ref.getDependentModules().remove(bo);
0438 }
0439 required.clear();
0440 bo.getLabels().resetNodesToWellFormedUnchecked();
0441 }
0442 Trace.end(CLASS, this, method);
0443 }
0444
0445 /**
0446 * Reset this and all (recursive) dependent modules (if any) to state loaded.
0447 */
0448 private void invalidateDependentModulesToLoaded() {
0449 final String method = "invalidateDependentModulesToLoaded";
0450 Trace.begin(CLASS, this, method);
0451 Trace.param(CLASS, this, method, "bo", bo);
0452 if (hasLoadedRequiredModules()) {
0453 invalidateOtherDependentModulesToLoaded();
0454 invalidateThisModuleToLoaded();
0455 setLoadingState(LoadingState.STATE_LOADED);
0456 ModuleEventLog.getInstance().stateChanged(bo);
0457 }
0458 Trace.end(CLASS, this, method);
0459 }
0460
0461 // /**
0462 // * Reset this and all (recursive) dependent modules (if any) to state loaded.
0463 // */
0464 // private void invalidateDependentModulesToLoadedImports() {
0465 // final String method = "invalidateDependentModulesToLoadedImports";
0466 // Trace.begin(CLASS, this, method);
0467 // Trace.param(CLASS, this, method, "bo", bo);
0468 // if (hasLoadedImports()) {
0469 // final KernelModuleReferenceList dependent = bo.getDependentModules();
0470 // Trace.trace(CLASS, this, method, "begin list of dependent modules");
0471 // // remember dependent modules
0472 // final List list = new ArrayList();
0473 // for (int i = 0; i < dependent.size(); i++) {
0474 // Trace.param(CLASS, this, method, "" + i, dependent.getKernelQedeqBo(i));
0475 // list.add(dependent.getKernelQedeqBo(i));
0476 // }
0477 // Trace.trace(CLASS, this, method, "end list of dependent modules");
0478 // for (int i = 0; i < list.size(); i++) {
0479 // DefaultKernelQedeqBo ref = (DefaultKernelQedeqBo) list.get(i);
0480 // // work on it, if still in list of dependent modules
0481 // if (dependent.contains(ref)) {
0482 // ref.getStateManager().invalidateDependentModulesToLoadedImports();
0483 // }
0484 // }
0485 // list.clear();
0486 //
0487 // invalidateThisModuleToLoadedImports();
0488 // ModuleEventLog.getInstance().stateChanged(bo);
0489 // }
0490 // Trace.end(CLASS, this, method);
0491 // }
0492 //
0493 // /**
0494 // * Reset all (recursive) dependent modules (if any) to state loaded required.
0495 // */
0496 // private void invalidateOtherDependentModulesToLoadedRequired() {
0497 // if (isWellFormed()) {
0498 // final KernelModuleReferenceList dependent = bo.getDependentModules();
0499 // for (int i = 0; i < dependent.size(); i++) {
0500 // DefaultKernelQedeqBo ref = (DefaultKernelQedeqBo) dependent.getKernelQedeqBo(i);
0501 // ref.getStateManager().invalidateDependentModulesToLoadedRequired();
0502 // }
0503 // }
0504 // }
0505 //
0506 // /**
0507 // * Reset this and all (recursive) dependent modules (if any) to state loaded required.
0508 // */
0509 // private void invalidateDependentModulesToLoadedRequired() {
0510 // if (isWellFormed()) {
0511 // final KernelModuleReferenceList dependent = bo.getDependentModules();
0512 // for (int i = 0; i < dependent.size(); i++) {
0513 // DefaultKernelQedeqBo ref = (DefaultKernelQedeqBo) dependent.getKernelQedeqBo(i);
0514 // ref.getStateManager().invalidateDependentModulesToLoadedRequired();
0515 // }
0516 // invalidateThisModuleToLoadedReqired();
0517 // ModuleEventLog.getInstance().stateChanged(bo);
0518 // }
0519 // }
0520 //
0521 // /**
0522 // * Reset all (recursive) dependent modules (if any) to state well formed.
0523 // */
0524 // private void invalidateOtherDependentModulesToWellFormed() {
0525 // if (isFullyFormallyProved()) {
0526 // final KernelModuleReferenceList dependent = bo.getDependentModules();
0527 // for (int i = 0; i < dependent.size(); i++) {
0528 // DefaultKernelQedeqBo ref = (DefaultKernelQedeqBo) dependent.getKernelQedeqBo(i);
0529 // ref.getStateManager().invalidateDependentModulesToWellFormed();
0530 // }
0531 // }
0532 // }
0533 //
0534 // /**
0535 // * Reset this and all (recursive) dependent modules (if any) to state well formed.
0536 // */
0537 // private void invalidateDependentModulesToWellFormed() {
0538 // if (isFullyFormallyProved()) {
0539 // final KernelModuleReferenceList dependent = bo.getDependentModules();
0540 // for (int i = 0; i < dependent.size(); i++) {
0541 // DefaultKernelQedeqBo ref = (DefaultKernelQedeqBo) dependent.getKernelQedeqBo(i);
0542 // ref.getStateManager().invalidateDependentModulesToWellFormed();
0543 // }
0544 // invalidateThisModuleToWellFormed();
0545 // ModuleEventLog.getInstance().stateChanged(bo);
0546 // }
0547 // }
0548
0549 private void invalidateThisModuleToLoaded() {
0550 if (isLoaded()) {
0551 setLoadingState(LoadingState.STATE_LOADED);
0552 setLoadingImportsState(LoadingImportsState.STATE_UNDEFINED);
0553 setDependencyState(DependencyState.STATE_UNDEFINED);
0554 setWellFormedState(WellFormedState.STATE_UNCHECKED);
0555 setFormallyProvedState(FormallyProvedState.STATE_UNCHECKED);
0556 bo.getKernelRequiredModules().clear();
0557 setErrors(null);
0558 }
0559 }
0560
0561 // private void invalidateThisModuleToLoadedImports() {
0562 // if (hasLoadedImports()) {
0563 // setLoadingImportsState(LoadingImportsState.STATE_LOADED_IMPORTED_MODULES);
0564 // setDependencyState(DependencyState.STATE_UNDEFINED);
0565 // setWellFormedState(WellFormedState.STATE_UNCHECKED);
0566 // setFormallyProvedState(FormallyProvedState.STATE_UNCHECKED);
0567 // setErrors(null);
0568 // }
0569 // }
0570 //
0571 // private void invalidateThisModuleToLoadedReqired() {
0572 // if (hasLoadedRequiredModules()) {
0573 // setDependencyState(DependencyState.STATE_LOADED_REQUIRED_MODULES);
0574 // setWellFormedState(WellFormedState.STATE_UNCHECKED);
0575 // setFormallyProvedState(FormallyProvedState.STATE_UNCHECKED);
0576 // setErrors(null);
0577 // }
0578 // }
0579 //
0580 // private void invalidateThisModuleToWellFormed() {
0581 // if (isWellFormed()) {
0582 // setWellFormedState(WellFormedState.STATE_CHECKED);
0583 // setFormallyProvedState(FormallyProvedState.STATE_UNCHECKED);
0584 // setErrors(null);
0585 // }
0586 // }
0587 //
0588 // private void invalidateThisModuleToFormallyProved() {
0589 // if (isFullyFormallyProved()) {
0590 // setFormallyProvedState(FormallyProvedState.STATE_CHECKED);
0591 // setErrors(null);
0592 // }
0593 // }
0594
0595 /**
0596 * Are all required modules loaded?
0597 *
0598 * @return All required modules are loaded?
0599 */
0600 public boolean hasLoadedRequiredModules() {
0601 return isLoaded() && dependencyState == DependencyState.STATE_LOADED_REQUIRED_MODULES;
0602 }
0603
0604 /**
0605 * Set loaded imports state.
0606 *
0607 * @param required URLs of all referenced modules. Must not be <code>null</code>.
0608 * @throws IllegalStateException Module is not yet loaded.
0609 */
0610 public void setLoadedImports(final KernelModuleReferenceList required) {
0611 checkIfDeleted();
0612 if (!isLoaded()) {
0613 throw new IllegalStateException(
0614 "Loaded imported modules can only be set if module is loaded."
0615 + "\"\nCurrently the status for the module"
0616 + "\"" + bo.getName() + "\" is \"" + bo.getLoadingState() + "\"");
0617 }
0618 setLoadingImportsState(LoadingImportsState.STATE_LOADED_IMPORTED_MODULES);
0619 setDependencyState(DependencyState.STATE_UNDEFINED);
0620 setWellFormedState(WellFormedState.STATE_UNCHECKED);
0621 setErrors(null);
0622 bo.getKernelRequiredModules().set(required);
0623 ModuleEventLog.getInstance().stateChanged(bo);
0624 }
0625
0626 /**
0627 * Set loaded required requirements state.
0628 *
0629 * @throws IllegalStateException Module is not yet loaded.
0630 */
0631 public void setLoadedRequiredModules() {
0632 checkIfDeleted();
0633 if (!isLoaded()) {
0634 throw new IllegalStateException(
0635 "Required modules can only be set if module is loaded."
0636 + "\"\nCurrently the status for the module"
0637 + "\"" + bo.getName() + "\" is \"" + bo.getLoadingState() + "\"");
0638 }
0639 KernelModuleReferenceList required = bo.getKernelRequiredModules();
0640 for (int i = 0; i < required.size(); i++) {
0641 DefaultKernelQedeqBo current = (DefaultKernelQedeqBo) required.getKernelQedeqBo(i);
0642 try {
0643 current.getDependentModules().add(required.getModuleContext(i),
0644 required.getLabel(i), bo);
0645 } catch (ModuleDataException me) { // should never happen
0646 throw new RuntimeException(me);
0647 }
0648 }
0649 setDependencyState(DependencyState.STATE_LOADED_REQUIRED_MODULES);
0650 setWellFormedState(WellFormedState.STATE_UNCHECKED);
0651 setErrors(null);
0652 ModuleEventLog.getInstance().stateChanged(bo);
0653 }
0654
0655 /**
0656 * Set logic checked state. Also set the predicate and function existence checker.
0657 *
0658 * @param checker Checks if a predicate or function constant is defined.
0659 */
0660 public void setWellFormed(final ModuleConstantsExistenceChecker checker) {
0661 checkIfDeleted();
0662 if (!hasLoadedRequiredModules()) {
0663 throw new IllegalStateException(
0664 "Checked can only be set if all required modules are loaded."
0665 + "\"\nCurrently the status for the module"
0666 + "\"" + bo.getName() + "\" is \"" + bo.getLoadingState() + "\"");
0667 }
0668 setWellFormedState(WellFormedState.STATE_CHECKED);
0669 bo.setExistenceChecker(checker);
0670 ModuleEventLog.getInstance().stateChanged(bo);
0671 }
0672
0673 /**
0674 * Set checking for well formed progress module state. Must not be <code>null</code>.
0675 *
0676 * @param state module state
0677 */
0678 public void setWellFormedProgressState(final WellFormedState state) {
0679 if (getDependencyState().getCode()
0680 < DependencyState.STATE_LOADED_REQUIRED_MODULES.getCode()
0681 && state != WellFormedState.STATE_UNCHECKED) {
0682 throw new IllegalArgumentException(
0683 "this state could only be set if all required modules are loaded ");
0684 }
0685 if (state.isFailure()) {
0686 throw new IllegalArgumentException(
0687 "this is a failure state, call setWellFormedFailureState");
0688 }
0689 if (state == WellFormedState.STATE_CHECKED) {
0690 throw new IllegalArgumentException(
0691 "set with setChecked(ExistenceChecker)");
0692 }
0693 setWellFormedState(state);
0694 setErrors(null);
0695 ModuleEventLog.getInstance().stateChanged(bo);
0696 }
0697
0698 /**
0699 * Set failure module state.
0700 *
0701 * @param state module state
0702 * @param e Exception that occurred during loading.
0703 * @throws IllegalArgumentException <code>state</code> is no failure state
0704 */
0705 public void setWellFormedFailureState(final WellFormedState state,
0706 final SourceFileExceptionList e) {
0707 if ((!isLoaded() || !hasLoadedRequiredModules())
0708 && state != WellFormedState.STATE_UNCHECKED) {
0709 throw new IllegalArgumentException(
0710 "this state could only be set if all required modules are loaded ");
0711 }
0712 if (!state.isFailure()) {
0713 throw new IllegalArgumentException(
0714 "this is no failure state, call setWellFormedProgressState");
0715 }
0716 setWellFormedState(state);
0717 setErrors(e);
0718 ModuleEventLog.getInstance().stateChanged(bo);
0719 }
0720
0721 /**
0722 * Set checking for formally proved progress module state. Must not be <code>null</code>.
0723 *
0724 * @param state module state
0725 */
0726 public void setFormallyProvedProgressState(final FormallyProvedState state) {
0727 if (getDependencyState().getCode()
0728 < DependencyState.STATE_LOADED_REQUIRED_MODULES.getCode()
0729 && state != FormallyProvedState.STATE_UNCHECKED) {
0730 throw new IllegalArgumentException(
0731 "this state could only be set if all required modules are loaded ");
0732 }
0733 if (state.isFailure()) {
0734 throw new IllegalArgumentException(
0735 "this is a failure state, call setFormallyProvedFailureState");
0736 }
0737 // if (state == FormallyProvedState.STATE_CHECKED) {
0738 // // FIXME 20130220 m31: do we need something simular?
0739 // throw new IllegalArgumentException(
0740 // "set with setChecked(ExistenceChecker)");
0741 // }
0742 setFormallyProvedState(state);
0743 setErrors(null);
0744 ModuleEventLog.getInstance().stateChanged(bo);
0745 }
0746
0747 /**
0748 * Set failure module state.
0749 *
0750 * @param state module state
0751 * @param e Exception that occurred during loading.
0752 * @throws IllegalArgumentException <code>state</code> is no failure state
0753 */
0754 public void setFormallyProvedFailureState(final FormallyProvedState state,
0755 final SourceFileExceptionList e) {
0756 if ((!isLoaded() || !hasLoadedRequiredModules())
0757 && state != FormallyProvedState.STATE_UNCHECKED) {
0758 throw new IllegalArgumentException(
0759 "this state could only be set if all required modules are well formed ");
0760 }
0761 if (!state.isFailure()) {
0762 throw new IllegalArgumentException(
0763 "this is no failure state, call setFormallyProvedProgressState");
0764 }
0765 setFormallyProvedState(state);
0766 setErrors(e);
0767 ModuleEventLog.getInstance().stateChanged(bo);
0768 }
0769
0770 /**
0771 * Checks if the current instance is already deleted.
0772 *
0773 * @throws IllegalStateException Module is already deleted.
0774 */
0775 private void checkIfDeleted() {
0776 if (getLoadingState() == LoadingState.STATE_DELETED) {
0777 throw new IllegalStateException("module is already deleted: " + bo.getUrl());
0778 }
0779 }
0780
0781 /**
0782 * Was the module successfully checked for well formedness errors?
0783 *
0784 * @return Successfully checked for being well formed?
0785 */
0786 public boolean isWellFormed() {
0787 return hasLoadedRequiredModules() && wellFormedState == WellFormedState.STATE_CHECKED;
0788 }
0789
0790 /**
0791 * Get the well formed state.
0792 *
0793 * @return Well formed state.
0794 */
0795 public WellFormedState getWellFormedState() {
0796 return this.wellFormedState;
0797 }
0798
0799 /**
0800 * Was the module successfully checked having formally correct proofs?
0801 *
0802 * @return Successfully checked for having formally correct proofs?
0803 */
0804 public boolean isFullyFormallyProved() {
0805 return isWellFormed() && formallyProvedState == FormallyProvedState.STATE_CHECKED;
0806 }
0807
0808 /**
0809 * Get the formally proved state.
0810 *
0811 * @return Formally proved state.
0812 */
0813 public FormallyProvedState getFormallyProvedState() {
0814 return this.formallyProvedState;
0815 }
0816
0817 /**
0818 * Get a description of the current state the module is in.
0819 *
0820 * @return Textual representation of module state.
0821 */
0822 public String getStateDescription() {
0823 String result = "";
0824 if (loadingState == LoadingState.STATE_LOADING_FROM_WEB) {
0825 result = loadingState.getText() + " (" + loadingCompleteness + "%)";
0826 } else if (!isLoaded()) {
0827 result = loadingState.getText();
0828 } else if (!hasLoadedImports()) {
0829 if (loadingImportsState == LoadingImportsState.STATE_UNDEFINED) {
0830 result = loadingState.getText();
0831 } else {
0832 result = loadingImportsState.getText();
0833 }
0834 } else if (!hasLoadedRequiredModules()) {
0835 if (dependencyState == DependencyState.STATE_UNDEFINED) {
0836 result = loadingImportsState.getText();
0837 } else {
0838 result = dependencyState.getText();
0839 }
0840 } else if (!isWellFormed()) {
0841 if (wellFormedState == WellFormedState.STATE_UNCHECKED) {
0842 result = dependencyState.getText();
0843 } else {
0844 result = wellFormedState.getText();
0845 }
0846 } else if (!isFullyFormallyProved()) {
0847 if (formallyProvedState == FormallyProvedState.STATE_UNCHECKED) {
0848 result = wellFormedState.getText();
0849 } else {
0850 result = formallyProvedState.getText();
0851 }
0852 } else {
0853 result = formallyProvedState.getText();
0854 }
0855 final String pluginState = pluginResults.getPluginStateDescription();
0856 if (pluginState.length() > 0) {
0857 result += "; " + pluginState;
0858 }
0859 return result;
0860 }
0861
0862 /**
0863 * Get the current state of associate module.
0864 *
0865 * @return Current module state.
0866 */
0867 public AbstractState getCurrentState() {
0868 if (!isLoaded()) {
0869 return loadingState;
0870 } else if (!hasLoadedImports()) {
0871 if (loadingImportsState == LoadingImportsState.STATE_UNDEFINED) {
0872 return loadingState;
0873 }
0874 return loadingImportsState;
0875 } else if (!hasLoadedRequiredModules()) {
0876 if (dependencyState == DependencyState.STATE_UNDEFINED) {
0877 return loadingImportsState;
0878 }
0879 return dependencyState;
0880 } else if (!isWellFormed()) {
0881 if (wellFormedState == WellFormedState.STATE_UNCHECKED) {
0882 return dependencyState;
0883 }
0884 return wellFormedState;
0885 } else if (!isFullyFormallyProved()) {
0886 if (formallyProvedState == FormallyProvedState.STATE_UNCHECKED) {
0887 return wellFormedState;
0888 }
0889 return formallyProvedState;
0890 } else {
0891 return formallyProvedState;
0892 }
0893 }
0894
0895 /**
0896 * Get the last successful state we were in.
0897 * As there are: undefined, loaded, loaded required, fully formally proved.
0898 *
0899 * @return Previous major error free state.
0900 */
0901 public AbstractState getLastSuccesfulState() {
0902 if (!isLoaded()) {
0903 return LoadingState.STATE_UNDEFINED;
0904 } else if (!hasLoadedImports()) {
0905 return LoadingState.STATE_LOADED;
0906 } else if (!hasLoadedRequiredModules()) {
0907 return LoadingImportsState.STATE_LOADED_IMPORTED_MODULES;
0908 } else if (!isWellFormed()) {
0909 return DependencyState.STATE_LOADED_REQUIRED_MODULES;
0910 } else if (!isFullyFormallyProved()) {
0911 return WellFormedState.STATE_CHECKED;
0912 } else {
0913 return FormallyProvedState.STATE_CHECKED;
0914 }
0915 }
0916
0917 /**
0918 * Set {@link LoadingState}. Doesn't do any status handling. Only for internal use.
0919 *
0920 * @param state Set this loading state.
0921 */
0922 protected void setLoadingState(final LoadingState state) {
0923 this.loadingState = state;
0924 }
0925
0926 /**
0927 * Set {@link LoadingImportsState}. Doesn't do any status handling. Only for internal use.
0928 *
0929 * @param state Set this loading state.
0930 */
0931 protected void setLoadingImportsState(final LoadingImportsState state) {
0932 this.loadingImportsState = state;
0933 }
0934
0935 /**
0936 * Set {@link DependencyState}. Doesn't do any status handling. Only for internal use.
0937 *
0938 * @param state Set this dependency state.
0939 */
0940 protected void setDependencyState(final DependencyState state) {
0941 this.dependencyState = state;
0942 }
0943
0944 /**
0945 * Set {@link WellFormedState}. Doesn't do any status handling. Only for internal use.
0946 *
0947 * @param state Set this logical state.
0948 */
0949 protected void setWellFormedState(final WellFormedState state) {
0950 this.wellFormedState = state;
0951 }
0952
0953 /**
0954 * Set {@link FormallyProvedState}. Doesn't do any status handling. Only for internal use.
0955 *
0956 * @param state Set this logical state.
0957 */
0958 protected void setFormallyProvedState(final FormallyProvedState state) {
0959 this.formallyProvedState = state;
0960 }
0961
0962 /**
0963 * Get all errors.
0964 *
0965 * @return Errors. Is a newly created list.
0966 */
0967 public SourceFileExceptionList getErrors() {
0968 final SourceFileExceptionList result = new SourceFileExceptionList(errors);
0969 result.add(pluginResults.getAllErrors());
0970 return result;
0971 }
0972
0973 /**
0974 * Get all warnings.
0975 *
0976 * @return Warnings. Is a newly created list.
0977 */
0978 public SourceFileExceptionList getWarnings() {
0979 final SourceFileExceptionList result = new SourceFileExceptionList();
0980 result.add(pluginResults.getAllWarnings());
0981 return result;
0982 }
0983
0984 /**
0985 * Set {@link SourceFileExceptionList}. Doesn't do any status handling. Only for internal use.
0986 *
0987 * @param errors Set this error list. If this is <code>null</code> the errors are cleared!
0988 */
0989 protected void setErrors(final SourceFileExceptionList errors) {
0990 this.errors = errors;
0991 // TODO mime 20100625: "if errors==null" this should be a new function "clearErrors" or other
0992 if (errors == null) {
0993 pluginResults = new PluginResultManager();
0994 }
0995 }
0996
0997 /**
0998 * Add the plugin execution errors and warnings.
0999 *
1000 * @param plugin Plugin that was executed.
1001 * @param errors Resulting errors.
1002 * @param warnings Resulting warnings.
1003 */
1004 public void addPluginResults(final ModuleService plugin, final SourceFileExceptionList errors,
1005 final SourceFileExceptionList warnings) {
1006 if (plugin instanceof InternalModuleServicePlugin) {
1007 throw new RuntimeException(
1008 "Programming error: an internal plugin should not add exeptions here!\n"
1009 + plugin.getClass().getName());
1010 }
1011 pluginResults.addResult(plugin, errors, warnings);
1012 ModuleEventLog.getInstance().stateChanged(bo);
1013 }
1014
1015 /**
1016 * Remove all plugin errors and warnings.
1017 */
1018 public void removeAllPluginResults() {
1019 pluginResults.removeAllResults();
1020 ModuleEventLog.getInstance().stateChanged(bo);
1021 }
1022
1023 /**
1024 * Print the dependence tree to <code>System.out</code>.
1025 */
1026 public void printDependencyTree() {
1027 System.out.println("DependencyTree");
1028 printDependencyTree(0);
1029 System.out.println();
1030 }
1031
1032 private void printDependencyTree(final int tab) {
1033 System.out.println(StringUtility.getSpaces(tab) + bo.getName());
1034 final int newTab = tab + bo.getName().length();
1035 final KernelModuleReferenceList dependent = bo.getDependentModules();
1036 for (int i = 0; i < dependent.size(); i++) {
1037 DefaultKernelQedeqBo ref = (DefaultKernelQedeqBo) dependent.getKernelQedeqBo(i);
1038 ref.getStateManager().printDependencyTree(newTab);
1039 }
1040 }
1041
1042 }
|