public ActionResult CalculateResults(SmartAgentVM obj) { obj = FixDaniesObject(obj); DataLayer dl = new DataLayer(); // get all rows from database List <SmartAgent> smartAgent_LST = (from u in dl.smartAgent select u).ToList <SmartAgent>(); int[] arr = Enumerable.Repeat(-1, smartAgent_LST.Count).ToArray(); int importantField = 3; // the value of important fields will be greater // first question: for (int i = 0; i < smartAgent_LST.Count; ++i) { if (obj.Friends && smartAgent_LST[i].Friends != 1) { arr[i] = smartAgent_LST[i].Friends * importantField; } else if (obj.Family && smartAgent_LST[i].Family != 1) { arr[i] = smartAgent_LST[i].Family * importantField; } else if (obj.Partner && smartAgent_LST[i].Partner != 1) { arr[i] = smartAgent_LST[i].Partner * importantField; } else if (obj.Alone && smartAgent_LST[i].JustMe != 1) { arr[i] = smartAgent_LST[i].JustMe * importantField; } } // second question: for (int i = 0; i < smartAgent_LST.Count; ++i) { if (arr[i] == -1) { continue; } if (obj.Extreme && smartAgent_LST[i].Extreme != 1) { arr[i] += smartAgent_LST[i].Extreme; } if (obj.Romantic && smartAgent_LST[i].Romantic != 1) { arr[i] += smartAgent_LST[i].Romantic; } if (obj.Trek && smartAgent_LST[i].Trek != 1) { arr[i] += smartAgent_LST[i].Trek; } if (obj.Nightlife && smartAgent_LST[i].NightLife != 1) { arr[i] += smartAgent_LST[i].NightLife; } if (obj.Culture_Art && smartAgent_LST[i].CultureArt != 1) { arr[i] += smartAgent_LST[i].CultureArt; } if (obj.Rest && smartAgent_LST[i].JustRest != 1) { arr[i] += smartAgent_LST[i].JustRest; } if (obj.FamilyFun && smartAgent_LST[i].FamilyTrip != 1) { arr[i] += smartAgent_LST[i].FamilyTrip; } } // third question: for (int i = 0; i < smartAgent_LST.Count; ++i) { if (arr[i] == -1) { continue; } if (obj.Fall && smartAgent_LST[i].Autumn != 1) { arr[i] += smartAgent_LST[i].Autumn * importantField; } else if (obj.Winter && smartAgent_LST[i].Winter != 1) { arr[i] += smartAgent_LST[i].Winter * importantField; } else if (obj.Spring && smartAgent_LST[i].Spring != 1) { arr[i] += smartAgent_LST[i].Spring * importantField; } else if (obj.Summer && smartAgent_LST[i].Summer != 1) { arr[i] += smartAgent_LST[i].Summer * importantField; } // if this location is not suitable at all - rule out this location (set -1) if (obj.Fall && smartAgent_LST[i].Autumn == 1) { arr[i] = -1; } else if (obj.Winter && smartAgent_LST[i].Winter == 1) { arr[i] = -1; } else if (obj.Spring && smartAgent_LST[i].Spring == 1) { arr[i] = -1; } else if (obj.Summer && smartAgent_LST[i].Summer == 1) { arr[i] = -1; } } // forth question: for (int i = 0; i < smartAgent_LST.Count; ++i) { if (arr[i] == -1) { continue; } if (obj.Music && smartAgent_LST[i].Music != 1) { arr[i] += smartAgent_LST[i].Music; } if (obj.Sport && smartAgent_LST[i].Sport != 1) { arr[i] += smartAgent_LST[i].Sport; } if (obj.Food_Drinks && smartAgent_LST[i].FoodDrinks != 1) { arr[i] += smartAgent_LST[i].FoodDrinks; } if (obj.Museums && smartAgent_LST[i].Museums != 1) { arr[i] += smartAgent_LST[i].Museums; } if (obj.Photography && smartAgent_LST[i].Photography != 1) { arr[i] += smartAgent_LST[i].Photography; } if (obj.Shopping && smartAgent_LST[i].Shopping != 1) { arr[i] += smartAgent_LST[i].Shopping; } } // fifth question: for (int i = 0; i < smartAgent_LST.Count; ++i) { if (arr[i] == -1) { continue; } if (obj.Urban && smartAgent_LST[i].Urban != 1) { arr[i] += smartAgent_LST[i].Urban; } if (obj.Coastal && smartAgent_LST[i].BeachLakesRivers != 1) { arr[i] += smartAgent_LST[i].BeachLakesRivers; } if (obj.Tropical && smartAgent_LST[i].ForestsNature != 1) { arr[i] += smartAgent_LST[i].ForestsNature; } if (obj.Snowy && smartAgent_LST[i].Snowy != 1) { arr[i] += smartAgent_LST[i].Snowy; } } // sort array from high to low int[] indexes = new int[smartAgent_LST.Count]; for (int i = 0; i < smartAgent_LST.Count; ++i) { indexes[i] = i; } Array.Sort(arr, indexes); // return top 5 highest results List <SmartAgentResultsVM> results = new List <SmartAgentResultsVM>(); for (int i = smartAgent_LST.Count - 1; i >= smartAgent_LST.Count - 5; --i) { if (arr[i] == -1) { break; } SmartAgentResultsVM temp = new SmartAgentResultsVM() { Location = smartAgent_LST[indexes[i]].Location, Image = smartAgent_LST[indexes[i]].Image, Description = smartAgent_LST[indexes[i]].Description, }; results.Add(temp); } return(View("SmartAgentResults", results)); // go to danies view }
public ActionResult GetBestLocationsByJson(string locApi) { DataLayer dl = new DataLayer(); // Data mining part: List <string> locations = DataMining(locApi); // get all rows from database List <SmartAgent> smartAgent_LST = (from u in dl.smartAgent select u).ToList <SmartAgent>(); List <SmartAgentResultsVM> results = new List <SmartAgentResultsVM>(); // the list we will return to the view Random rnd = new Random(); for (int k = 0; k < smartAgent_LST.Count; ++k) // this loop search for the location acordint to the data mining { string[] tempLoc = smartAgent_LST[k].Location.Split(','); string countryName = tempLoc[1]; if (tempLoc.Length == 2 && tempLoc[0] == "Andorra") { countryName = tempLoc[0]; } else if (countryName[0] == ' ') { countryName = countryName.Substring(1); } if (countryName.ToLower() == locations[0].ToLower() || countryName.ToLower() == locations[1].ToLower() || countryName.ToLower() == locations[2].ToLower() || countryName.ToLower() == locations[3].ToLower()) { SmartAgentResultsVM temp2 = new SmartAgentResultsVM() { Location = smartAgent_LST[k].Location, Image = smartAgent_LST[k].Image, Description = smartAgent_LST[k].Description, }; results.Add(temp2); smartAgent_LST.RemoveAt(k); } } if (results.Count < 4) // if not all the location were filled (could happen if the locations we have from the data mining are not all in the SmartAgent table) { // so add some random locations int size = 4 - results.Count; for (int i = 0, j = smartAgent_LST.Count; i < size; ++i, --j) { int random = rnd.Next(0, j); // get random location SmartAgentResultsVM temp = new SmartAgentResultsVM() { Location = smartAgent_LST[random].Location, Image = smartAgent_LST[random].Image, Description = smartAgent_LST[random].Description, }; results.Add(temp); smartAgent_LST.RemoveAt(random); } } else { // all the locations were filled by the data mining locations while (results.Count > 4) // if there were more then 4 locations that was added to the list, remove them { results.RemoveAt(results.Count - 1); } } return(Json(results, JsonRequestBehavior.AllowGet)); }