int checkconsistency { // Checks that the diagram is a DFA. // Return codes: // -1 Missing initial or final state // 0 Array is consistent // >0 State ID of first detected inconsistent state. CArray TransitionTypes = CCanvas.AllType; CArray states_array = CCanvas.getAllStates(); int initial = 0; // Number of initial states int final = 0; // Number of final states foreach (currentState) in (states_array) { CArray outTransitions = currentState.output; CArray outTranTypes; foreach (outTransition) in (outTransitions) { // Get types for each transition CArray types = outTransition.type; foreach (Ttype) in (types) { if (outTranTypes contains Ttype) { // ERROR: Two identical transition types from same state. Inconsistent. return currentState.ID; } else { push(outTranTypes,Ttype); } } if (outTransition.fromStateID == outTransition.toStateID ) { // ERROR: Reflexive state. Inconsistent. return currentState.ID; } } // Now compare two arrays: outTranTypes for currentState and TransitionTypes. // if (sizeof(outTranTypes) == sizeof(TransitionTypes)) { foreach (item) in (outTranTypes) { if (transition is not in TransitionTypes array) { // ERROR: Unlisted transition. Inconsistent. return currentState.ID; } } else { // ERROR: Number of transition types does not match. Inconsistent. return currentState.ID; } // Increment initial or final counts, check if too many if (currentState.status == 1 || currentState.status == 3) { initial++; if (initial > 1) { // ERROR: Two initial states. Inconsistent. return currentState.ID; } } if (currentState.status == 2 || currentState.status == 3) { final++; if (final > 1) { // ERROR: Two final states. Inconsistent. return currentState.ID; } } } if (initial != 1 || final != 1) { // Should be 0 in this case. Insufficient initial/final states. return -1; } else { // It's consistent. Congratulations. I'll give you a zero-shaped cookie. return 0; } }