// This constructor takes in a JSON string and checks the object against the schema in Criteria-Weights-Schema // If the JSON is valid then all of the data is extracted from the JSON and is used to create the criteria objects // To be assessed against the schedules public Evaluator(string criteriaJsonString, bool test = false) { JsonSchema schema; if (test) { schema = JsonSchema.Parse(File.ReadAllText("./../../../ScheduleEvaluator/Criteria-Weights-Schema.json")); } else { // So this file can not be tested due to the fact that the test dll gets executed from a different // directory schema = JsonSchema.Parse(File.ReadAllText("./../../Criteria-Weights-Schema.json")); } JObject criteriaJObject = JObject.Parse(criteriaJsonString); if (!criteriaJObject.IsValid(schema)) { throw new System.ArgumentException("Invalid JSON Schema"); } JToken crits = criteriaJObject["Criteria"]; // This for loop gets the number of criteria. It is super inefficent and should be replaced // with a non iterative approach int count = 0; foreach (JToken crit in crits) { count++; } CritTyp[] criteriaTypes = new CritTyp[count]; double[] weights = new double[count]; int i = 0; foreach (JToken crit in crits) { Debug.WriteLine(crit); criteriaTypes[i] = CriteriaFactory.FromString(crit["CriteriaName"].ToString()); weights[i] = Convert.ToDouble(crit["Weight"].ToString()); i++; } CriteriaFactory fact; try { fact = new CriteriaFactory(criteriaTypes, weights); } catch (ArgumentException ag) { Console.WriteLine("Error in creating Criterias: {0}", ag); throw; } criterias = fact.Criterias; client = new HttpClient(); }
public CriteriaFactory(CritTyp[] types, double[] weights) { // Check to make sure the paramaters are legal. if (types.Length != weights.Length) { throw new ArgumentException("Must have equal number of weights and types"); } if (types.Length == 0 || types.Length == 0) { throw new ArgumentException("Criteria Types and Weights must have elements"); } // This is preliminary weight validation. This should be implemented lated to follow the actual rules that // we want. //double sum = 0; //foreach (double val in weights) // sum += val; //if (sum != 1.0) // throw new ArgumentException("Weights must sum to 1.0"); Criterias = new Criteria[types.Length]; for (int i = 0; i < types.Length; i++) { CritTyp ct = types[i]; double w = weights[i]; if (ct == CritTyp.AllPrereqs) { Criterias[i] = new AllPrereqs(w); } else if (ct == CritTyp.CoreClassesLastYear) { Criterias[i] = new CoreClassesLastYear(w); } else if (ct == CritTyp.CoreCreditsAQuarter) { Criterias[i] = new CoreCreditsAQuarter(w); } else if (ct == CritTyp.ElectiveRelevancy) { Criterias[i] = new ElectiveRelevancy(w); } else if (ct == CritTyp.EnglishStart) { Criterias[i] = new EnglishStart(w); } else if (ct == CritTyp.MajorSpecificBreaks) { Criterias[i] = new MajorSpecificBreaks(w); } else if (ct == CritTyp.MathBreaks) { Criterias[i] = new MathBreaks(w); } else if (ct == CritTyp.MaxQuarters) { Criterias[i] = new MaxQuarters(w); } else if (ct == CritTyp.PreRequisiteOrder) { Criterias[i] = new PreRequisiteOrder(w); } else if (ct == CritTyp.TimeOfDay) { Criterias[i] = new TimeOfDay(w); } else if (ct == CritTyp.EnglishClassStart) { Criterias[i] = new EnglishClassStart(w); } else { throw new ArgumentException("Illegal Criteria Type"); } } }