public CalculatorFramework()
 {
     Calculator = new Calculator();
     CommandFactory.GetInstance().Calculator = Calculator;
     CurrentInput = "";
     SavedProgram = "";
     CurrentState = new CalculationState(this);
 }
        public static string CalcStateToStringDE(CalculationState _state)
        {
            switch (_state)
            {
            case CalculationState.NO_WRITING_ACCESS:
                return("Der Benutzer hat keine Schreibrechte!");

            case CalculationState.MISSING_DATA:
                return("Unvollständige Definition!");

            case CalculationState.PARAMS_NOT_OF_THIS_OR_CHILD_COMP:
                return("Zumindest ein Parameter ist kein Teil dieser Komponente oder einer ihrer Subkomponenten!");

            case CalculationState.PARAM_OUT_W_VALUE_FILED:
                return("Outputparameter kann kein Kennfeld referenzieren!");

            case CalculationState.PARAMS_OUT_DUPLICATE:
                return("Outputparameter ist bereits Output einer bestehenden Gleichung!");

            case CalculationState.PARAMS_IN_OUT_SAME:
                return("Der Outputparameter darf nicht als Input vorkommen!");

            case CalculationState.INVALID_SYNTAX:
                return("Syntaxfehler!");

            case CalculationState.CAUSES_CALCULATION_LOOP:
                return("Diese Gleichung verursacht eine Endlosschleife!");

            case CalculationState.VALID:
                return("OK");

            case CalculationState.UNKNOWN_ERROR:
                return("Unbekannter Fehler!");

            default:
                return("Unbekannter Fehler!");
            }
        }
        public string FindShortestStringContainingAllPossiblePasscodes(int lengthOfPasscode, int numDigitsOnPadlock)
        {
            var allPossiblePasscodes = this._outcomeGenerator.GenerateAllOutcomes(lengthOfPasscode, numDigitsOnPadlock, FirstCharacterToProcess);
            var digitsOnPadlock      = Enumerable.Range(0, numDigitsOnPadlock).Select(digit => (char)(FirstCharacterToProcess + digit)).ToArray();
            Dictionary <char, char?> nextDigitToProcess = new Dictionary <char, char?>();

            for (int i = 0; i < numDigitsOnPadlock; i++)
            {
                nextDigitToProcess[digitsOnPadlock[i]] = i < numDigitsOnPadlock - 1 ? digitsOnPadlock[i + 1] : (char?)null;
            }

            var totalNumberOfPasscodes = (int)Math.Pow(numDigitsOnPadlock, lengthOfPasscode);

            foreach (var startingPasscode in allPossiblePasscodes)
            {
                var passcodesHashSet = allPossiblePasscodes.ToHashSet();
                passcodesHashSet.Remove(startingPasscode);
                var currentPasscodeString = startingPasscode;

                var stateToProcess = new CalculationState
                {
                    CurrentString         = currentPasscodeString,
                    RemainingPasscodes    = passcodesHashSet,
                    NumPasscodesRemaining = totalNumberOfPasscodes - 1
                };

                while (stateToProcess != null && !stateToProcess.IsTerminalState)
                {
                    stateToProcess = this.GetNextStateToProcess(stateToProcess, lengthOfPasscode, digitsOnPadlock, nextDigitToProcess);
                }
                if (stateToProcess.IsTerminalState)
                {
                    return(stateToProcess.CurrentString);
                }
            }
            return(null);
        }
Example #4
0
 public Logics()
 {
     sql              = new Sql();
     custom           = new Custom();
     calculationState = CalculationState.Start;
 }
Example #5
0
 public void ExitProgramm()
 {
     calculationState = CalculationState.Start;
     sql.ConnectionClose();
     Environment.Exit(0);
 }
Example #6
0
 public override void Calculate(CalculationState calculationState)
 {
     calculationState.Output += Weight * calculationState.Input;
 }
        private CalculationState GetNextStateToProcess(CalculationState currentState,
                                                       int lengthOfPasscode,
                                                       char[] digitsOnPadlock,
                                                       Dictionary <char, char?> nextDigitToProcess)
        {
            // first see if it's possible to continue with this state
            foreach (var digit in digitsOnPadlock)
            {
                var nextPasscodeAdded = $"{currentState.CurrentString.Substring(currentState.CurrentString.Length - lengthOfPasscode + 1)}{digit}";
                if (currentState.RemainingPasscodes.Contains(nextPasscodeAdded))
                {
                    var newRemainingPasscodes = currentState.RemainingPasscodes;
                    newRemainingPasscodes.Remove(nextPasscodeAdded);
                    var newPasscodeString = $"{currentState.CurrentString}{digit}";
                    return(new CalculationState {
                        CurrentString = newPasscodeString,
                        RemainingPasscodes = newRemainingPasscodes,
                        NumPasscodesRemaining = currentState.NumPasscodesRemaining - 1
                    });
                }
            }

            // if it's not possible to continue with the current state, go back until we find a state where can continue
            var updatedPasscodeStringForNextState = currentState.CurrentString;
            var updatedRemainingPasscodes         = currentState.RemainingPasscodes;
            var updatedNumPasscodesRemaining      = currentState.NumPasscodesRemaining;

            while (true)
            {
                var lastPasscodeAdded = updatedPasscodeStringForNextState.Substring(updatedPasscodeStringForNextState.Length - lengthOfPasscode);
                var lastDigitAdded    = lastPasscodeAdded[lastPasscodeAdded.Length - 1];

                // remove the most recent digit added to the string because it was incorrect
                updatedRemainingPasscodes.Add(lastPasscodeAdded);
                updatedPasscodeStringForNextState = updatedPasscodeStringForNextState.Substring(0, updatedPasscodeStringForNextState.Length - 1);
                updatedNumPasscodesRemaining     += 1;

                // try each digit we haven't tried before. If we reach a branch that could be correct, add that digit and continue processing
                var nextDigit = nextDigitToProcess[lastDigitAdded];
                while (nextDigit != null)
                {
                    var nextPasscodeAdded =
                        $"{updatedPasscodeStringForNextState.Substring(updatedPasscodeStringForNextState.Length - lengthOfPasscode + 1)}{nextDigit}";
                    if (updatedRemainingPasscodes.Contains(nextPasscodeAdded))
                    {
                        updatedRemainingPasscodes.Remove(nextPasscodeAdded);
                        updatedPasscodeStringForNextState = $"{updatedPasscodeStringForNextState}{nextDigit}";
                        updatedNumPasscodesRemaining     -= 1;
                        return(new CalculationState
                        {
                            CurrentString = updatedPasscodeStringForNextState,
                            RemainingPasscodes = updatedRemainingPasscodes,
                            NumPasscodesRemaining = updatedNumPasscodesRemaining
                        });
                    }
                    nextDigit = nextDigitToProcess[nextDigit.Value];
                }

                // if no digits are valid, we'll need to remove the previous digit as well
                // first check if we've exhausted the current initial password. If so, return null and try another
                if (updatedPasscodeStringForNextState.Length <= lengthOfPasscode)
                {
                    return(null);
                }
            }
        }
Example #8
0
        public override void Calculate(CalculationState calculationState)
        {
            var r2 = calculationState.Input.LengthSquared + 10e-30;

            calculationState.Output += Weight / r2 * calculationState.Input;
        }
        /// <summary>
        /// For calculations within a network involving the size of the instance.
        /// </summary>
        private void AddSizeParamsForNWCalculations()
        {
            if (this.ContainedParameters == null)
            {
                return;
            }

            // create the size parameters
            List <Parameter.Parameter> parameters = Parameter.Parameter.GetSizeParametersForInstancing();

            this.AddParametersFromList(parameters);

            // create the corresponding calculations
            ObservableConcurrentDictionary <string, Parameter.Parameter> p_in;
            ObservableConcurrentDictionary <string, Parameter.Parameter> p_out;
            Calculation      calc;
            CalculationState calc_state = CalculationState.VALID;

            p_in = new ObservableConcurrentDictionary <string, Parameter.Parameter> {
                { "x2", parameters[3] }, { "x3", parameters[4] }
            };
            p_out = new ObservableConcurrentDictionary <string, Parameter.Parameter> {
                { "out01", parameters[7] }
            };
            calc = new Calculation("x2*x3", "Amax-calc", p_in, p_out);
            this.TestAndSaveCalculationInEditMode(ComponentManagerType.ADMINISTRATOR, calc, ref calc_state);
            if (calc_state != CalculationState.VALID)
            {
                MessageBox.Show(CalculationFactory.CalcStateToStringDE(calc_state),
                                "Fehler bei der automatisierten Gleichungserstellung", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            calc_state = CalculationState.VALID;
            p_in       = new ObservableConcurrentDictionary <string, Parameter.Parameter> {
                { "x2", parameters[0] }, { "x3", parameters[1] }
            };
            p_out = new ObservableConcurrentDictionary <string, Parameter.Parameter> {
                { "out01", parameters[6] }
            };
            calc = new Calculation("x2*x3", "Amin-calc", p_in, p_out);
            this.TestAndSaveCalculationInEditMode(ComponentManagerType.ADMINISTRATOR, calc, ref calc_state);
            if (calc_state != CalculationState.VALID)
            {
                MessageBox.Show(CalculationFactory.CalcStateToStringDE(calc_state),
                                "Fehler bei der automatisierten Gleichungserstellung", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            calc_state = CalculationState.VALID;
            p_in       = new ObservableConcurrentDictionary <string, Parameter.Parameter> {
                { "x2", parameters[7] }, { "x3", parameters[5] }
            };
            p_out = new ObservableConcurrentDictionary <string, Parameter.Parameter> {
                { "out01", parameters[9] }
            };
            calc = new Calculation("x2*x3", "Vmax-calc", p_in, p_out);
            this.TestAndSaveCalculationInEditMode(ComponentManagerType.ADMINISTRATOR, calc, ref calc_state);
            if (calc_state != CalculationState.VALID)
            {
                MessageBox.Show(CalculationFactory.CalcStateToStringDE(calc_state),
                                "Fehler bei der automatisierten Gleichungserstellung", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            calc_state = CalculationState.VALID;
            p_in       = new ObservableConcurrentDictionary <string, Parameter.Parameter> {
                { "x2", parameters[6] }, { "x3", parameters[2] }
            };
            p_out = new ObservableConcurrentDictionary <string, Parameter.Parameter> {
                { "out01", parameters[8] }
            };
            calc = new Calculation("x2*x3", "Vmin-calc", p_in, p_out);
            this.TestAndSaveCalculationInEditMode(ComponentManagerType.ADMINISTRATOR, calc, ref calc_state);
            if (calc_state != CalculationState.VALID)
            {
                MessageBox.Show(CalculationFactory.CalcStateToStringDE(calc_state),
                                "Fehler bei der automatisierten Gleichungserstellung", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }