public void EvaluateProgramCondition(object evalArguments) { ProgramBlock program = (evalArguments as EvaluateProgramConditionArgs).Program; ConditionEvaluationCallback callback = (evalArguments as EvaluateProgramConditionArgs).Callback; // bool isConditionSatisfied = false; // while (isEngineRunning && program.IsEnabled) { if (program.IsRunning || !isEngineEnabled) { Thread.Sleep(1000); continue; } // try { isConditionSatisfied = false; // if (program.Type.ToLower() != "wizard") { var result = program.EvaluateCondition(); if (result != null && result.Exception != null) { // runtime error occurred, script is being disabled // so user can notice and fix it List <ProgramError> error = new List <ProgramError>() { program.GetFormattedError(result.Exception, true) }; program.ScriptErrors = JsonConvert.SerializeObject(error); program.IsEnabled = false; RaiseProgramModuleEvent(program, Properties.RUNTIME_ERROR, "TC: " + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' ')); } else { isConditionSatisfied = (result != null ? (bool)result.ReturnValue : false); } } else { // it is a Wizard Script isConditionSatisfied = (program.Conditions.Count > 0); for (int c = 0; c < program.Conditions.Count; c++) { // check for OR logic operator if (program.Conditions[c].ComparisonOperator == ComparisonOperator.LogicOrJoint) { if (isConditionSatisfied) { break; } else { isConditionSatisfied = (c < program.Conditions.Count - 1); continue; } } // bool res = false; try { res = VerifyProgramCondition(program.Conditions[c]); } catch { // TODO: report/handle exception } isConditionSatisfied = (isConditionSatisfied && res); } } // bool lastResult = program.LastConditionEvaluationResult; program.LastConditionEvaluationResult = isConditionSatisfied; // if (program.ConditionType == ConditionType.OnSwitchTrue) { isConditionSatisfied = (isConditionSatisfied == true && isConditionSatisfied != lastResult); } else if (program.ConditionType == ConditionType.OnSwitchFalse) { isConditionSatisfied = (isConditionSatisfied == false && isConditionSatisfied != lastResult); } else if (program.ConditionType == ConditionType.OnTrue || program.ConditionType == ConditionType.Once) { // noop } else if (program.ConditionType == ConditionType.OnFalse) { isConditionSatisfied = !isConditionSatisfied; } } catch (Exception ex) { // a runtime error occured List <ProgramError> error = new List <ProgramError>() { program.GetFormattedError(ex, true) }; program.ScriptErrors = JsonConvert.SerializeObject(error); program.IsEnabled = false; RaiseProgramModuleEvent(program, Properties.RUNTIME_ERROR, "TC: " + ex.Message.Replace('\n', ' ').Replace('\r', ' ')); } // callback(program, isConditionSatisfied); // Thread.Sleep(500); } }
private bool WillProgramRun(ProgramBlock program) { bool isConditionSatisfied = false; // evaluate and get result from the code lock (program.OperationLock) try { program.WillRun = false; // var result = program.EvaluateCondition(); if (result != null && result.Exception != null) { // runtime error occurred, script is being disabled // so user can notice and fix it List <ProgramError> error = new List <ProgramError>() { program.GetFormattedError(result.Exception, true) }; program.ScriptErrors = JsonConvert.SerializeObject(error); program.IsEnabled = false; RaiseProgramModuleEvent(program, Properties.RuntimeError, "TC: " + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' ')); } else { isConditionSatisfied = (result != null ? (bool)result.ReturnValue : false); } // bool lastResult = program.LastConditionEvaluationResult; program.LastConditionEvaluationResult = isConditionSatisfied; // if (program.ConditionType == ConditionType.OnSwitchTrue) { isConditionSatisfied = (isConditionSatisfied == true && isConditionSatisfied != lastResult); } else if (program.ConditionType == ConditionType.OnSwitchFalse) { isConditionSatisfied = (isConditionSatisfied == false && isConditionSatisfied != lastResult); } else if (program.ConditionType == ConditionType.OnTrue || program.ConditionType == ConditionType.Once) { // noop } else if (program.ConditionType == ConditionType.OnFalse) { isConditionSatisfied = !isConditionSatisfied; } } catch (Exception ex) { // a runtime error occured if (!ex.GetType().Equals(typeof(System.Reflection.TargetException))) { List <ProgramError> error = new List <ProgramError>() { program.GetFormattedError(ex, true) }; program.ScriptErrors = JsonConvert.SerializeObject(error); program.IsEnabled = false; RaiseProgramModuleEvent(program, Properties.RuntimeError, "TC: " + ex.Message.Replace('\n', ' ').Replace('\r', ' ')); } } return(isConditionSatisfied && program.IsEnabled); }
private bool WillProgramRun(ProgramBlock program) { bool isConditionSatisfied = false; // evaluate and get result from the code lock (program.OperationLock) try { program.WillRun = false; // var result = program.EvaluateCondition(); if (result != null && result.Exception != null) { // runtime error occurred, script is being disabled // so user can notice and fix it List<ProgramError> error = new List<ProgramError>() { program.GetFormattedError(result.Exception, true) }; program.ScriptErrors = JsonConvert.SerializeObject(error); program.IsEnabled = false; RaiseProgramModuleEvent(program, Properties.RuntimeError, "TC: " + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' ')); } else { isConditionSatisfied = (result != null ? (bool)result.ReturnValue : false); } // bool lastResult = program.LastConditionEvaluationResult; program.LastConditionEvaluationResult = isConditionSatisfied; // if (program.ConditionType == ConditionType.OnSwitchTrue) { isConditionSatisfied = (isConditionSatisfied == true && isConditionSatisfied != lastResult); } else if (program.ConditionType == ConditionType.OnSwitchFalse) { isConditionSatisfied = (isConditionSatisfied == false && isConditionSatisfied != lastResult); } else if (program.ConditionType == ConditionType.OnTrue || program.ConditionType == ConditionType.Once) { // noop } else if (program.ConditionType == ConditionType.OnFalse) { isConditionSatisfied = !isConditionSatisfied; } } catch (Exception ex) { // a runtime error occured if (!ex.GetType().Equals(typeof(System.Reflection.TargetException))) { List<ProgramError> error = new List<ProgramError>() { program.GetFormattedError(ex, true) }; program.ScriptErrors = JsonConvert.SerializeObject(error); program.IsEnabled = false; RaiseProgramModuleEvent(program, Properties.RuntimeError, "TC: " + ex.Message.Replace('\n', ' ').Replace('\r', ' ')); } } return isConditionSatisfied && program.IsEnabled; }