using System; using System.Collections; using IwbZero.Expr; using IwbZero.IwbBase; using IwbZero.IwbDataQuery; using IwbZero.Processes.StatementTypes; using IwbZero.ToolCommon.AssemblyHelpers; using IwbZero.ToolCommon.LogHelpers; using IwbZero.ToolCommon.StringModel; namespace IwbZero.Processes.Runner { public class ProcessRunner { public int Depth { get; private set; } private string _parameterMapping = ""; private IwbProcess _process; private readonly Hashtable _variables = new Hashtable(); public bool Run(IwbProcess poProcess, string pcParameterMappings) { DataSources.Clear(); FilterFields.Clear(); _variables.Clear(); _parameterMapping = pcParameterMappings; _process = poProcess; InitializeProcessVariables(); //var processResult = GenerateAndStampInstanceId(pcParameterMappings); var processResult = "Y".ValB(); if (processResult) { //LogText(string.Format("The process started with instance id {0}.", ProcessInstanceId)); if (_process.ProcessScript != "") { IwbXmlNode node = IwbXmlNode.ParseGenericXml(poProcess.ProcessScript); if (node != null) { foreach (ProcessParameter parameter in ProcessMethods.ParseProcessParameter(poProcess.ProcessParameters)) { FilterFields.Add(IwbVariableType.Global + parameter.Id.UAndT(), ""); } if (pcParameterMappings != "") { ArrayList list2 = StringHelper.MappingToArrayList(pcParameterMappings); if (list2 != null) { foreach (IwbIdName name in list2) { SetValue(IwbVariableType.Global + name.Id, name.Name, false); } } } RunStatements(node.Nodes); } } else { object obj2 = AssemblyHelper.CreateInstance(_process.ProcessClass, _process.ProcessLib); if (obj2 == null) { typeof(ProcessRunner).LogError( $"Unable to create static process runner at {_process.ProcessClass}."); //AbortProcessWithMessage(string.Format("Unable to create static process runner at {0}.", _Process.ProcessClass)); } else if (!(obj2 is IProcessRunner)) { typeof(ProcessRunner).LogError($"The process runner, {_process.ProcessClass}, is invalid."); //AbortProcessWithMessage(string.Format("The process runner, {0}, is invalid.", _Process.ProcessClass)); } else { IProcessRunner runner = (IProcessRunner)obj2; if (!runner.Run(_process, pcParameterMappings)) { typeof(ProcessRunner).LogError(runner.ErrorMessage); //AbortProcessWithMessage(runner.ErrorMessage); } } } processResult = ProcessResult; //StampProcessHistory(); //_History.SetField(cn.ProcLog, _LogText); //_History.SaveTable(_UserSession); } if (ProcessMessage == "") { ProcessMessage = ProcessResult ? $"{_process.ProcessName}执行成功" : $"{_process.ProcessName}执行失败"; } typeof(ProcessRunner).LogInfo(ProcessMessage); //CloseProgressForm(); return processResult; } public bool RunStatements(ArrayList poStatements) { bool flag = true; Depth++; int count = poStatements.Count; for (int i = 0; i < count; i++) { var poStatement = (IwbXmlNode)poStatements[i]; string childValue = poStatement.GetChildValue("Comment"); //ShowStepInfo(count, i + 1, childValue); string processMessage = ProcessMessage; try { bool flag2 = poStatement.GetChildValue("RunWhenFail").StrToBool(); bool processResult = ProcessResult; if (childValue != "") { typeof(ProcessRunner).LogInfo("\r\n\r\n"); typeof(ProcessRunner).LogInfo(childValue); typeof(ProcessRunner).LogInfo($"Process Result: {processResult}"); typeof(ProcessRunner).LogInfo($"RunWhenFail option: {(flag2 ? "On" : "Off")}"); } if (processResult || flag2) { flag = ProcessMethods.RunOneStatement(this, poStatement); } } catch (Exception e) { typeof(ProcessRunner).LogError(e); // AbortProcessWithMessage(exception.Message); } if (!ProcessResult) { flag = false; } if (ProcessMessage != processMessage) { typeof(ProcessRunner).LogInfo(processMessage); } } Depth--; return flag; } private void InitializeProcessVariables() { CreateVariable(PdStmtType.ProcMessage.VariableName, PdStmtType.ProcMessage.VariableType); CreateVariable(PdStmtType.ProcInstanceId.VariableName, PdStmtType.ProcInstanceId.VariableType); CreateVariable(PdStmtType.ProcResult.VariableName, PdStmtType.ProcResult.VariableType); SetValue(PdStmtType.ProcMessage.VariableName, "", false); SetValue(PdStmtType.ProcInstanceId.VariableName, "0", false); SetValue(PdStmtType.ProcResult.VariableName, "1", false); } public void CreateVariable(string pcVariableId, string pcVariableType) { pcVariableId = pcVariableId.UAndT(); var variable = new IwbRtVariable(pcVariableId, pcVariableType); _variables[pcVariableId] = variable; } public string Evaluate(string pcExpression) { string str = ""; var poRoot = new ExprObject(pcExpression); if (EvalExpr.Parse(poRoot, true)) { TransProcessVariables(poRoot); str = EvalExpr.Evaluate(poRoot); } return str; } public bool EvaluateBool(string pcExpression) { bool flag = true; if (pcExpression != "") { flag = Evaluate(pcExpression).StrToBool(); } return flag; } private void TransProcessVariables(ExprObject poRoot) { foreach (ExprObject expr in poRoot.ChildNodes) { if (expr.ElementType == ExprElements.Variable) { if (expr.Expr.Trim() != "") { expr.Expr = GetValue(expr.Expr); expr.ElementType = ExprElements.Value; } } else if ((expr.ElementType == ExprElements.Expression) || (expr.ElementType == ExprElements.Function)) { TransProcessVariables(expr); } } } //private bool GenerateAndStampInstanceId(string pcParameterMappings) //{ // bool flag = false; // _History = new npTable(); // _History.Open(tn.ddProcessHistory, "1=2", ""); // if (_History.IsOpened) // { // int num = DBConn.GetNextRecordId(0, "System.ProcessInstanceId", 1); // if (num > 0) // { // SetValue(PdStmtType.ProcInstanceId.VariableName, num.ToString(), false); // if (_Process != null) // { // _History.NewRow(); // _History.SetField(cn.ProcInstanceId, num.ToString()); // _History.SetField(cn.ProcessId, _Process.ProcessId.ToString()); // _History.SetField(cn.ProcessName, _Process.ProcessName); // _History.SetField(cn.ProcStartTime, ReportLib.DtToStr(AppEnv.CurrentTime)); // _History.SetField(cn.ParameterMapping, pcParameterMappings); // } // flag = true; // } // } // if (!flag) // { // AbortProcessWithMessage("Failed to generate instance id."); // } // return flag; //} public IwbDataSource GetDataSource(string pcDataSourceId) { IwbDataSource source = null; pcDataSourceId = pcDataSourceId.UAndT(); if (DataSources.ContainsKey(pcDataSourceId)) { source = (IwbDataSource)DataSources[pcDataSourceId]; } return source; } public string GetValue(string pcIdentity) { string str = pcIdentity; pcIdentity = pcIdentity.UAndT(); if (pcIdentity.IndexOf(IwbVariableType.Global, StringComparison.Ordinal) == 0) { if (FilterFields.ContainsKey(pcIdentity)) { return FilterFields[pcIdentity].ToString(); } //return UserSession.GetSessionVariableValue(pcIdentity); } if (_variables.ContainsKey(pcIdentity)) { var variable = (IwbRtVariable)_variables[pcIdentity]; return variable.GetStringValue(); } Array array = pcIdentity.StrToArrayEx(PdStmtType.Separator); if (array.Length == 2) { string pcDataSourceId = array.GetValue(0).ToString(); string name = array.GetValue(1).ToString(); var dataSource = GetDataSource(pcDataSourceId); if (dataSource?.Data != null && dataSource.Data.IsOpened) { str = dataSource.Data.GetString(name); } } return str; } public void SetValue(string pcIdentifier, string pcExpression, bool plToEvaluate) { pcIdentifier = pcIdentifier.UAndT(); string pcValue = pcExpression; if (plToEvaluate) { pcValue = Evaluate(pcExpression); } if (pcIdentifier.IndexOf(IwbVariableType.Global, StringComparison.Ordinal) == 0) { if (FilterFields.ContainsKey(pcIdentifier)) { FilterFields[pcIdentifier] = pcValue; } //else //{ // UserSession.SetSessionVariable(pcIdentifier, pcValue); //} } else if (_variables.ContainsKey(pcIdentifier)) { var variable = (IwbRtVariable)_variables[pcIdentifier]; variable.VarValue = pcValue; } else { Array array = pcIdentifier.StrToArrayEx( "$"); if (array.Length == 2) { string pcDataSourceId = array.GetValue(0).ToString(); string name = array.GetValue(1).ToString(); IwbDataSource dataSource = GetDataSource(pcDataSourceId); if (((dataSource != null) && (dataSource.Data != null)) && ( dataSource.Data.IsOpened)) { dataSource.Data.SetField(name, pcValue); } } } } public Hashtable DataSources { get; } = new Hashtable(); public Hashtable FilterFields { get; } = new Hashtable(); //private string LineTimestamp => (DateTime.Now.DtToStr() + " "); public string ParameterMapping => _parameterMapping; public string ProcessInstanceId => GetValue(PdStmtType.ProcInstanceId.VariableName); public string ProcessMessage { get => GetValue(PdStmtType.ProcMessage.VariableName); set => SetValue(PdStmtType.ProcMessage.VariableName, value, false); } public bool ProcessResult => (StringHelper.ValI(GetValue(PdStmtType.ProcResult.VariableName)) != 0); } }