Skip to content

Commit

Permalink
Added Automatic Calculation Customization Options and Engine Preferen…
Browse files Browse the repository at this point in the history
…ces #176
  • Loading branch information
saucepleez committed Dec 30, 2019
1 parent bd1b01d commit 8994950
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 68 deletions.
3 changes: 2 additions & 1 deletion taskt/Core/ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class EngineSettings
public bool OverrideExistingAppInstances { get; set; }
public bool AutoCloseMessagesOnServerExecution { get; set; }
public bool AutoCloseDebugWindowOnServerExecution { get; set; }

public bool AutoCalcVariables { get; set; }
public EngineSettings()
{
ShowDebugWindow = true;
Expand All @@ -138,6 +138,7 @@ public EngineSettings()
OverrideExistingAppInstances = false;
AutoCloseMessagesOnServerExecution = true;
AutoCloseDebugWindowOnServerExecution = true;
AutoCalcVariables = true;
}
}
/// <summary>
Expand Down
89 changes: 89 additions & 0 deletions taskt/Core/Automation/Commands/MathCalculationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using taskt.UI.Forms;
using taskt.UI.CustomControls;
using System.Data;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Data Commands")]
[Attributes.ClassAttributes.Description("This command allows you to perform a math calculation and apply it to a variable.")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to perform a math calculation.")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class MathCalculationCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please supply the input to be computed (ex. (2 + 1)")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Specify either text or a variable that contains valid math.")]
[Attributes.PropertyAttributes.SampleUsage("")]
[Attributes.PropertyAttributes.Remarks("You can use known numbers or variables.")]
public string v_InputValue { get; set; }


[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the math calculation")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
public string v_applyToVariableName { get; set; }

public MathCalculationCommand()
{
this.CommandName = "MathCalculationCommand";
this.SelectionName = "Math Calculation";
this.CommandEnabled = true;
this.CustomRendering = true;

this.v_InputValue = "(2 + 5) * 3";

}

public override void RunCommand(object sender)
{
//get variablized string
var variableMath = v_InputValue.ConvertToUserVariable(sender);

try
{
//perform compute
DataTable dt = new DataTable();
var result = dt.Compute(variableMath, "");

//store string in variable
result.ToString().StoreInUserVariable(sender, v_applyToVariableName);
}
catch (Exception ex)
{
//throw exception is math calc failed
throw ex;
}
}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

//create standard group controls
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_InputValue", this, editor));

RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_applyToVariableName", this));
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_applyToVariableName", this).AddVariableNames(editor);
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_applyToVariableName", this, new Control[] { VariableNameControl }, editor));
RenderedControls.Add(VariableNameControl);


return RenderedControls;

}

public override string GetDisplayValue()
{
return base.GetDisplayValue() + $" [Apply Calculation Result to '{v_applyToVariableName}']";
}
}
}
2 changes: 2 additions & 0 deletions taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ namespace taskt.Core.Automation.Commands
[XmlInclude(typeof(SeleniumBrowserSwitchFrameCommand))]
[XmlInclude(typeof(ParseJsonModelCommand))]
[XmlInclude(typeof(EncryptionCommand))]
[XmlInclude(typeof(MathCalculationCommand))]
[XmlInclude(typeof(SetEnginePreferenceCommand))]
public abstract class ScriptCommand
{
[XmlAttribute]
Expand Down
67 changes: 67 additions & 0 deletions taskt/Core/Automation/Commands/SetEnginePreferenceCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.UI.CustomControls;
using taskt.UI.Forms;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Engine Commands")]
[Attributes.ClassAttributes.Description("This command allows you to set preferences for engine behavior.")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to change the engine behavior.")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class SetEnginePreferenceCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Select Parameter Type")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Enable Automatic Calculations")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Disable Automatic Calculations")]
[Attributes.PropertyAttributes.InputSpecification("")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_PreferenceType { get; set; }

public SetEnginePreferenceCommand()
{
this.CommandName = "SetEnginePreferenceCommand";
this.SelectionName = "Set Engine Preference";
this.CommandEnabled = true;
this.CustomRendering = true;

}
public override void RunCommand(object sender)
{
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;

var preference = v_PreferenceType.ConvertToUserVariable(sender);

switch (preference)
{
case "Enable Automatic Calculations":
engine.AutoCalculateVariables = true;
break;
case "Disable Automatic Calculations":
engine.AutoCalculateVariables = false;
break;
default:
throw new NotImplementedException($"The preference '{preference}' is not implemented.");
}


}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_PreferenceType", this, editor));

return RenderedControls;
}
public override string GetDisplayValue()
{
return base.GetDisplayValue() + $" [{v_PreferenceType}]";
}
}
}
5 changes: 5 additions & 0 deletions taskt/Core/Automation/Engine/AutomationEngineInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class AutomationEngineInstance
public event EventHandler<ScriptFinishedEventArgs> ScriptFinishedEvent;
public event EventHandler<LineNumberChangedEventArgs> LineNumberChangedEvent;

public bool AutoCalculateVariables { get; set; }
public string TasktResult { get; set; } = "";

public Serilog.Core.Logger engineLogger;
Expand All @@ -62,6 +63,10 @@ public AutomationEngineInstance()
AppInstances = new Dictionary<string, object>();
ServiceResponses = new List<IRestResponse>();
DataTables = new List<DataTable>();

//this value can be later overriden by script
AutoCalculateVariables = engineSettings.AutoCalcVariables;

}

public void ExecuteScriptAsync(UI.Forms.frmScriptEngine scriptEngine, string filePath, List<Core.Script.ScriptVariable> variables = null)
Expand Down
72 changes: 39 additions & 33 deletions taskt/Core/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,42 +217,48 @@ public static string ConvertToUserVariable(this String str, object sender)
}




//track math chars
var mathChars = new List<Char>();
mathChars.Add('*');
mathChars.Add('+');
mathChars.Add('-');
mathChars.Add('=');
mathChars.Add('/');

//if the string matches the char then return
//as the user does not want to do math
if (mathChars.Any(f => f.ToString() == str) || (mathChars.Any(f => str.StartsWith(f.ToString()))))
{
return str;
if (!engine.AutoCalculateVariables)
{
return str;
}

//bypass math for types that are dates
DateTime dateTest;
if ((DateTime.TryParse(str, out dateTest) && (str.Length > 6)))
{
return str;
else
{
//track math chars
var mathChars = new List<Char>();
mathChars.Add('*');
mathChars.Add('+');
mathChars.Add('-');
mathChars.Add('=');
mathChars.Add('/');

//if the string matches the char then return
//as the user does not want to do math
if (mathChars.Any(f => f.ToString() == str) || (mathChars.Any(f => str.StartsWith(f.ToString()))))
{
return str;
}

//bypass math for types that are dates
DateTime dateTest;
if ((DateTime.TryParse(str, out dateTest) && (str.Length > 6)))
{
return str;
}

//test if math is required
try
{
DataTable dt = new DataTable();
var v = dt.Compute(str, "");
return v.ToString();
}
catch (Exception)
{
return str;
}
}


//test if math is required
try
{
DataTable dt = new DataTable();
var v = dt.Compute(str, "");
return v.ToString();
}
catch (Exception)
{
return str;
}

}
/// <summary>
/// Stores value of the string to a user-defined variable.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<Script xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Commands>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="552bb9f0-b5a4-415b-8371-7189cf9d37c6" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="1" PauseBeforeExeucution="false" v_Comment="Sample Demonstrates Automatic Calculation Preferences" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="c19b5da1-a1a8-4383-aa19-5e107ee98b3e" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="2" PauseBeforeExeucution="false" v_Comment="Default Setting comes from: Settings &gt; Engine &gt; Calculate Variables Automatically" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="6cfd5535-4390-49b1-9f78-f84a7d8b3e77" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="3" PauseBeforeExeucution="false" v_Comment="Override and Enable Automatic Calculations" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="SetEnginePreferenceCommand" CommandID="d310dc06-8884-422e-a4d1-ff3748fdab2c" CommandName="SetEnginePreferenceCommand" IsCommented="false" SelectionName="Set Engine Preference" DefaultPause="0" LineNumber="4" PauseBeforeExeucution="false" CommandEnabled="true" v_PreferenceType="Enable Automatic Calculations" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="VariableCommand" CommandID="cbc24146-54ae-43e5-b2f7-51045a27773c" CommandName="VariableCommand" IsCommented="false" SelectionName="Set Variable" DefaultPause="0" LineNumber="5" PauseBeforeExeucution="false" CommandEnabled="true" v_userVariableName="vRes" v_Input="2+1" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="bc1375d2-2823-480f-965b-0e473a569287" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="6" PauseBeforeExeucution="false" v_Comment="Should Output 3" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="f199fa4b-1870-4e9a-9ae9-0c9fd976c348" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="7" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vRes}" v_AutoCloseAfter="0" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="1a958572-7a5c-4989-af6d-2d94b50dfd6e" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="8" PauseBeforeExeucution="false" v_Comment="Override Disable Automatic Calculations" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="SetEnginePreferenceCommand" CommandID="cf12d8f8-1373-44fd-8783-f41c107678a1" CommandName="SetEnginePreferenceCommand" IsCommented="false" SelectionName="Set Engine Preference" DefaultPause="0" LineNumber="9" PauseBeforeExeucution="false" CommandEnabled="true" v_PreferenceType="Disable Automatic Calculations" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="VariableCommand" CommandID="53eb918c-7713-4422-ac65-f6d8ca4ab109" CommandName="VariableCommand" IsCommented="false" SelectionName="Set Variable" DefaultPause="0" LineNumber="10" PauseBeforeExeucution="false" CommandEnabled="true" v_userVariableName="vRes" v_Input="2+1" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="CommentCommand" CommandID="45107c6e-ecbd-437d-9a1f-fbb55f88d79a" CommandName="CommentCommand" IsCommented="false" SelectionName="Add Code Comment" DefaultPause="0" LineNumber="11" PauseBeforeExeucution="false" v_Comment="Should Output '2+1'" CommandEnabled="true" />
</ScriptAction>
<ScriptAction>
<ScriptCommand xsi:type="MessageBoxCommand" CommandID="127e32e1-a496-4de2-97cd-5ffc2be3595d" CommandName="MessageBoxCommand" IsCommented="false" SelectionName="Show Message" DefaultPause="0" LineNumber="12" PauseBeforeExeucution="false" CommandEnabled="true" v_Message="{vRes}" v_AutoCloseAfter="0" />
</ScriptAction>
</Commands>
<Variables />
</Script>
6 changes: 4 additions & 2 deletions taskt/UI/CustomControls/CustomControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ public static class Images
uiImages.Add("FormatDataCommand", taskt.Properties.Resources.command_function);
uiImages.Add("ParseDatasetRowCommand", taskt.Properties.Resources.command_function);
uiImages.Add("ModifyVariableCommand", taskt.Properties.Resources.command_function);
uiImages.Add("DateCalculationCommand", taskt.Properties.Resources.command_function);
uiImages.Add("DateCalculationCommand", taskt.Properties.Resources.command_function);
uiImages.Add("MathCalculationCommand", taskt.Properties.Resources.command_function);
uiImages.Add("RegExExtractorCommand", taskt.Properties.Resources.command_function);
uiImages.Add("TextExtractorCommand", taskt.Properties.Resources.command_function);
uiImages.Add("PDFTextExtractionCommand", taskt.Properties.Resources.command_function);
Expand Down Expand Up @@ -538,7 +539,8 @@ public static class Images
uiImages.Add("NLGGeneratePhraseCommand", taskt.Properties.Resources.command_nlg);
uiImages.Add("NLGSetParameterCommand", taskt.Properties.Resources.command_nlg);
uiImages.Add("NLGCreateInstanceCommand", taskt.Properties.Resources.command_nlg);
uiImages.Add("ShowEngineContextCommand", taskt.Properties.Resources.command_window);
uiImages.Add("ShowEngineContextCommand", taskt.Properties.Resources.command_window);
uiImages.Add("SetEnginePreferenceCommand", taskt.Properties.Resources.command_window);
uiImages.Add("DatabaseDefineConnectionCommand", taskt.Properties.Resources.command_database);
uiImages.Add("DatabaseExecuteQueryCommand", taskt.Properties.Resources.command_database);
uiImages.Add("RemoteTaskCommand", taskt.Properties.Resources.command_remote);
Expand Down
Loading

0 comments on commit 8994950

Please sign in to comment.