/// <summary> /// Adds a variable to the collection of variables of the given type /// </summary> /// <typeparam name="T">The type matching the target collection of variables</typeparam> /// <param name="v">The variable to add to the collection of matching type</param> /// <returns>Returns true if the variable was successfully added</returns> public bool AddVariable <T>(T v) { Type varType = typeof(T); if (varType == typeof(IndependentVariable)) { IndependentVariable iv = (IndependentVariable)Convert.ChangeType(v, typeof(IndependentVariable)); if (!IndVars.ContainsKey(iv.Name)) { IndVars.Add(iv.Name, iv); } if (!AllVars.ContainsKey(iv.Name)) { AllVars.Add(iv.Name, iv); } } else { DependentVariable dv = (DependentVariable)Convert.ChangeType(v, typeof(DependentVariable)); if (!DepVars.ContainsKey(dv.Name)) { DepVars.Add(dv.Name, dv); } if (!AllVars.ContainsKey(dv.Name)) { AllVars.Add(dv.Name, dv); } } return(true); }
/// <summary> /// Returns the value of the provided token. /// </summary> /// <param name="token">String of variables</param> /// <returns>Value of the provided token</returns> public int GetValue(string token) { if (char.IsDigit(token[0])) { // Return int converted value of token return(Convert.ToInt32(token)); } else if (token[0] != '{') { IndependentVariable indVar = TryGetVariable <IndependentVariable>(token) as IndependentVariable; DependentVariable depVar = TryGetVariable <DependentVariable>(token) as DependentVariable; if (indVar != null) { if (indVar.Value) { return(1); } else { return(0); } } else if (depVar != null) { if (depVar.Value) { return(1); } else { return(0); } } else { return(-1); // If variable doesn't exist } } else { // Create binary string builder StringBuilder binary = new StringBuilder(); // For each variable in the token foreach (string var in GetVariables(token)) { // Add variable's value to binary string binary.Append(GetValue(var)); } // Return int converted binary string as value return(Convert.ToInt32(binary.ToString(), 2)); } }
public override List <IObjectCodeElement> Parse() { List <IObjectCodeElement> output = new List <IObjectCodeElement>(); MatchCollection matches = OutputRegex.Matches(Text); foreach (Match match in matches) { string token = match.Value; if (token == " ") { output.Add(new SpaceFeed()); } else if (token == "\n") { // Output newline output.Add(new LineFeed()); } else if (token.Contains("(")) { output.Add(new Comment(token)); } else if (token == "," || token == "{" || token == "}" || token == ":" || token == ")") { output.Add(new Operator(token)); } else { IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(token) as IndependentVariable; DependentVariable depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(token) as DependentVariable; if (indVar != null) { output.Add(indVar); } else if (depVar != null) { output.Add(depVar); } } } // Output ending semicolon output.Add(new Operator(";")); // Output new line output.Add(new LineFeed()); // Return output list return(output); }
public DataTable AddValuesToCopyOf(DataTable table, DependentVariable <T> dependentVariable) { AddVariableColumn(dependentVariable, table); DataTable newTable = table.Copy(); if (table.Rows.Count == 0) { return(table); } foreach (DataRow newTableRow in newTable.Rows) { newTableRow[dependentVariable.Name] = dependentVariable.DefaultValue; } return(newTable); }
public DataTable AddValuesToCopyOf(DataTable table, DependentVariable <T> dependentVariable) { AddVariableColumn(dependentVariable, table); DataTable newTable = table.Copy(); if (table.Rows.Count == 0) { throw new ArgumentException("Can't add dependent variable values to empty trialTable"); } foreach (DataRow newTableRow in newTable.Rows) { newTableRow[dependentVariable.Name] = dependentVariable.DefaultValue; } return(newTable); }
/// <summary> /// Parses the text of this statement into a list of output elements. /// </summary> public override List <IObjectCodeElement> Parse() { // Create output list to return var output = new List <IObjectCodeElement>(); MatchCollection matches = OutputRegex.Matches(Text); foreach (Match match in matches) { string token = match.Value; if (token == " ") { output.Add(new SpaceFeed()); } else if (token == "\n") { // Output newline output.Add(new LineFeed()); } else if (token == "(" || token == ")") { output.Add(Expression.Parentheses[match.Index]); // Output the corresponding parenthesis } else if (Parser.OperatorsList.Contains(token) || token == "{" || token == "}" || token == "=") { output.Add(new Operator(token)); } else { string name = token.TrimStart('~'); if (!char.IsDigit(name[0])) { IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(name) as IndependentVariable; DependentVariable depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(name) as DependentVariable; if (indVar != null) { if (token[0] != '~') { output.Add(indVar); } else { output.Add(new IndependentVariable(token, !indVar.Value)); } } else if (depVar != null) { if (token[0] != '~') { output.Add(depVar); } else { output.Add(new DependentVariable(token, !depVar.Value)); } } } else { output.Add(new Constant(token)); } } } // Output ending semicolon output.Add(new Operator(";")); // Output new line output.Add(new LineFeed()); // Return output list return(output); }
/// <summary> /// Parses the text of this statement into a list of output elements. /// </summary> public override List <IObjectCodeElement> Parse() { List <IObjectCodeElement> output = new List <IObjectCodeElement>(); // Find format specifiers and extra spacing MatchCollection matches = OutputRegex.Matches(Text); foreach (Match match in matches) { string token = match.Value; if (token == " ") { output.Add(new SpaceFeed()); } else if (token == "\n") { // Output newline output.Add(new LineFeed()); } else { if (token[0] != '%') { if (char.IsDigit(token[0])) { output.Add(new Constant(token)); } else { IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(token) as IndependentVariable; DependentVariable depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(token) as DependentVariable; if (indVar != null) { output.Add(indVar); } else if (depVar != null) { output.Add(depVar); } } } else { bool clickable = true; string variableList = match.Groups["Vars"].Value; string[] variables = Parser.WhitespaceRegex.Split(variableList); var binaryBuilder = new StringBuilder(); foreach (string variable in variables) { if (char.IsDigit(variable[0])) { binaryBuilder.Append(variable); if (clickable) { clickable = false; } } else { IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(variable) as IndependentVariable; DependentVariable depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(variable) as DependentVariable; if (indVar != null) { if (indVar.Value) { binaryBuilder.Append(1); } else { binaryBuilder.Append(0); } } else { if (depVar.Value) { binaryBuilder.Append(1); } else { binaryBuilder.Append(0); } if (clickable) { clickable = false; } } } } string binary = binaryBuilder.ToString(); char format = char.ToUpper(match.Groups["Format"].Value[0]); string formatOutput = format == 'B' ? binary : Format(format, binary); string formatNextOutput = clickable ? GetNextValue(binary) : null; int outputWidth = format == 'B' ? formatOutput.Length : GetWidth(format, variables.Length); if (formatOutput.Length < outputWidth) { if (format == 'H') { formatOutput = string.Concat(new string('0', outputWidth - formatOutput.Length), formatOutput); } else { for (int i = 0; i < outputWidth - formatOutput.Length; i++) { output.Add(new SpaceFeed()); } } } output.Add(new Formatter(formatOutput, $"{{{variableList}", formatNextOutput)); } } } // Output ending semicolon output.Add(new Operator(";")); // Output new line output.Add(new LineFeed()); // Return output list return(output); }
/// <summary> /// Parses the text of this statement into a list of output elements. /// </summary> public override List <IObjectCodeElement> Parse() { // Create output list to return var output = new List <IObjectCodeElement>(); // Get input and output seperator index int seperatorIndex = Text.IndexOf(':'); // Start no contact index at 0 int currentNoContactIndex = 0; // Get all output matches MatchCollection matches = OutputRegex.Matches(Text); // For each output match foreach (Match match in matches) { string token = match.Value; if (token == " ") { output.Add(new SpaceFeed()); } else if (token == "\n") { // Output newline output.Add(new LineFeed()); } else if (token.Contains("(")) { output.Add(new Instantiation(token)); } else if (token == "," || token == "{" || token == "}" || token == ":" || token == ")") { output.Add(new Operator(token)); } else { if (match.Index > seperatorIndex && token == "NC") { output.Add(new DependentVariable(token, Instantiation.NoContactValues[currentNoContactIndex++])); } else { if (char.IsDigit(token[0])) { output.Add(new Constant(token)); } else { IndependentVariable indVar = DesignController.ActiveDesign.Database.TryGetVariable <IndependentVariable>(token) as IndependentVariable; DependentVariable depVar = DesignController.ActiveDesign.Database.TryGetVariable <DependentVariable>(token) as DependentVariable; if (indVar != null) { output.Add(indVar); } else if (depVar != null) { output.Add(depVar); } } } } } // Output ending semicolon output.Add(new Operator(";")); // Output new line output.Add(new LineFeed()); // Return output list return(output); }
//Sort Independent variables into mixing categories so they go in order public SortedVariableContainer(List <Variable> allData, bool block) { int numberOfBlockIvs = 0; int numberOfNonBlockIvs = 0; foreach (Variable variable in allData) { if (variable.TypeOfVariable == VariableType.Independent) { IndependentVariable independentVariable = (IndependentVariable)variable; if (independentVariable.Block) { numberOfBlockIvs++; } else { numberOfNonBlockIvs++; } if (block && independentVariable.Block || !block && !independentVariable.Block) { switch (independentVariable.MixingTypeOfVariable) { case VariableMixingType.Balanced: BalancedIndependentVariables.Add(independentVariable); break; case VariableMixingType.Looped: LoopedIndependentVariables.Add(independentVariable); break; case VariableMixingType.EvenProbability: case VariableMixingType.CustomProbability: ProbabilityIndependentVariables.Add(independentVariable); break; default: throw new ArgumentOutOfRangeException(); } } } else if (variable.TypeOfVariable == VariableType.Dependent) { DependentVariable dependentVariable = (DependentVariable)variable; DependentVariables.Add(dependentVariable); } else if (variable.TypeOfVariable == VariableType.Participant) { ParticipantVariable participantVariable = (ParticipantVariable)variable; ParticipantVariables.Add(participantVariable); } } bool thereAreBlockIvsButNoNormalIvs = numberOfBlockIvs > 0 && numberOfNonBlockIvs == 0; if (!block && thereAreBlockIvsButNoNormalIvs) { throw new ExperimentDesign.InvalidExperimentDesignException($"You defined {numberOfBlockIvs} block variable(s), " + $"when there are {numberOfNonBlockIvs} unblocked independent variables." + $"You can safely unblock the variable " + $"to make it a normal variable"); } }