public string[] Calculate( Data basefilter, Data filter, StorageMethods.Database storageMethod, WeightingFilterCollection weights ) { if (this.Assembly == null) { return new string[] { "0", "0" } } ; try { //return (decimal)new System.Data.DataTable().Compute(equation, null); //EquationEvaluator evaluator = new EquationEvaluator(this); List <object> argumentsWeighted = new List <object>(); List <object> argumentsUnweighted = new List <object>(); foreach (string key in this.Values.Keys) { if (this.Values[key].Type == EquationPlaceHolderType.Value) { Data data = this.Values[key].GetValue(basefilter, filter, storageMethod, weights); argumentsWeighted.Add(data.Value); argumentsUnweighted.Add(data.UnweightedValue); } else { Data data = this.Values[key].GetValue(basefilter, filter, storageMethod, weights); argumentsWeighted.Add(data.Responses); argumentsUnweighted.Add(data.Responses); //argumentsWeighted.Add(data.Responses.Select(x => x.Value[0]).ToArray()); //argumentsUnweighted.Add(data.Responses.Select(x => 1.0).ToArray()); } } return(new string[] { this.Assembly.Evaluate(argumentsWeighted.ToArray()), this.Assembly.Evaluate(argumentsUnweighted.ToArray()) }); } catch (Exception ex) { Log(string.Format( "<Error><![CDATA[{0}]]></Error><Equation><![CDATA[{1}]]></Equation>", ex.ToString(), this.EquationString )); return(new string[] { "0", "0" }); } }
public Data GetValue( Data basefilter, Data filter, StorageMethods.Database storageMethod, WeightingFilterCollection weights ) { if (!this.Valid) { return(new Data()); } Data result; if (this.IdWeightingVariable.HasValue) { XmlDocument document = new XmlDocument(); XmlNode xmlNode = document.CreateElement("WeightingVariables"); xmlNode.AddAttribute("DefaultWeighting", this.IdWeightingVariable.Value.ToString()); xmlNode.AddAttribute("IsTaxonomy", this.IsTaxonomy); weights = new WeightingFilterCollection(null, this.Equation.Core, xmlNode); weights.LoadRespondents(filter); } else if (this.WeightingVariable == "NONE") { weights = null; } if (this.Unfiltered) { filter = null; } if (this.Unfiltered2) { filter = basefilter; } if (this.Filter != null) { filter = this.Filter.Evalute(filter, filter); } /*if (this.IdFilterVariable.HasValue) * { * if (this.IdFilterCategory.HasValue) * { * filter = storageMethod.GetRespondents( * this.IdFilterCategory.Value, * this.IdFilterVariable.Value, * this.IsTaxonomy, * this.Equation.CaseDataLocation, * filter, * null, * null * ); * } * else * { * filter = storageMethod.GetRespondents( * this.IdFilterVariable.Value, * this.IsTaxonomy, * this.Equation.CaseDataLocation, * filter, * null * ); * } * }*/ if (this.IdCategory.HasValue) { result = storageMethod.GetRespondents( this.IdCategory.Value, this.IdVariable, this.IsTaxonomy, this.Equation.Core.CaseDataLocation, filter, weights ); } else { if (this.VariableType != VariableType.Numeric) { result = storageMethod.GetRespondents( this.IdVariable, this.IsTaxonomy, this.Equation.Core.CaseDataLocation, filter, weights ); } else { result = storageMethod.GetRespondentsNumeric( this.IdVariable, this.IsTaxonomy, this.Equation.CaseDataLocation, filter, weights ); if (this.Category == "Mean") { result.Value = result.Value / result.Base; } } } return(result); }
public List <EquationValidationError> SecurityCheck( Data filter, StorageMethods.Database storageMethod, WeightingFilterCollection weights ) { //return new List<EquationValidationError>(); List <EquationValidationError> result = new List <EquationValidationError>(); string equation = this.Render(false); string fileName = Path.Combine( Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "")), "Resources", "EquationValidatorConfiguration.xml" ); XmlDocument document = new XmlDocument(); document.Load(fileName); List <string> validKeys = new List <string>(); foreach (XmlNode xmlNode in document.DocumentElement.ChildNodes) { validKeys.Add(xmlNode.InnerText); } // To remove the string values: int index = 0; while (index != -1) { if (index + 1 >= equation.Length) { break; } index = equation.IndexOf('"', index + 1); if (index == -1) { break; } int index2 = equation.IndexOf('"', index + 1); if (index2 == -1) { break; } equation = equation.Remove(index + 1, (index2 - 1) - index); index += 2; } string[] parts = equation.Split(new string[] { " ", ",", "(", ")", "+", "-", "~", "*", "/", "==", "<", "<=", ">", ">=", "?", ":", ";", "=", "{", "}", "\t" }, StringSplitOptions.RemoveEmptyEntries); foreach (string part in parts) { if (part.StartsWith("\"") && part.EndsWith("\"") && part.Count(x => x == '"') == 2) { continue; } if (part.StartsWith("[") && part.EndsWith("]")) { int v; if (int.TryParse(part.Substring(1, part.Length - 2), out v)) { continue; } } double value; if (double.TryParse(part, out value)) { continue; } if (part.StartsWith("var_")) { continue; } if (!validKeys.Contains(part)) { result.Add(new EquationValidationError( EquationValidationErrorType.MethodNotSupported, part )); } } return(result); }