1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Data;
   4:  using System.Data.Odbc;
   5:  using System.Diagnostics;
   6:  using System.IO;
   7:  using System.Linq;
   8:  using System.Threading;
   9:  using System.Windows.Forms;
  10:  using static RetiSocialiAlgoritmo.Form2;
  11:   
  12:  namespace RetiSocialiAlgoritmo {
  13:      public partial class Form2 : Form {
  14:   
  15:          // Query per creazione dei file CSV da Mysql
  16:   
  17:          //SELECT idA as Source, idB as Target, similaritScore as Weight, same,discord,disagree,opposite FROM reti_confronti where similaritScore>70;
  18:   
  19:          //SELECT reti_user.userid as id, username as label, anno as Year, genere as Gender, preferito, tag, count(reti_voti.voto) as nvoti
  20:          //FROM reti_user inner join reti_voti
  21:          //on reti_user.userid = reti_voti.userid
  22:          //where reti_voti.voto!=0
  23:          //group by reti_user.userid
  24:          //having nvoti>5
  25:   
  26:          // Query completa per selezionare le persone piu' vicine a noi ...    
  27:          //  SELECT idA, idB, reti_confronti.comparisonID,similaritScore, reti_suggerimenti.list, reti_suggerimenti.userID_to
  28:          //  FROM reti_confronti
  29:          //  inner join
  30:          //  reti_suggerimenti on
  31:          //  reti_confronti.comparisonID=reti_suggerimenti.comparisonID
  32:          //  where (idA= 10 or idB = 10) and similaritScore>60 
  33:          //  having userID_to = 10
  34:          //  order by similaritScore desc
  35:          //  limit 10    
  36:   
  37:          //Query selezionando solo 2 campi ....
  38:          //        SELECT
  39:          //            similaritScore,
  40:          //            reti_suggerimenti.list,
  41:          //            reti_suggerimenti.userID_to
  42:          //        FROM
  43:          //            reti_confronti
  44:          //                INNER JOIN
  45:          //            reti_suggerimenti ON reti_confronti.comparisonID = reti_suggerimenti.comparisonID
  46:          //        WHERE
  47:          //            (idA = 10 OR idB = 10)
  48:          //                AND similaritScore > 60
  49:          //        HAVING reti_suggerimenti.userID_to = 10
  50:          //        ORDER BY similaritScore DESC
  51:          //        LIMIT 10
  52:   
  53:   
  54:          public static class Utility {
  55:              public static string MySQLConString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=sql960766_3;UID=root;PASSWORD=admin;OPTION=3;";
  56:          }
  57:   
  58:          List<User> users = new List<User>();
  59:          public int row = 1;
  60:          int nRegion = 8;
  61:   
  62:          Stopwatch stopwatch = new Stopwatch();
  63:   
  64:          public Form2() {
  65:              InitializeComponent();
  66:          }
  67:   
  68:          private void Form2_Load(object sender, EventArgs e) {
  69:   
  70:          }
  71:   
  72:          private void btnStart_Click(object sender, EventArgs e) {
  73:   
  74:              stopwatch.Start();
  75:              console.Text = "Loading all users...";
  76:              console.Text = LoadAllUser();
  77:              console.AppendText(Environment.NewLine);
  78:              console.AppendText("Abbiamo " + users.Count + " utenti da analizzare." + Environment.NewLine);
  79:              console.AppendText("Stopwatch è ad alta definizione? " + Stopwatch.IsHighResolution + Environment.NewLine);
  80:   
  81:              int totalLink = 0;
  82:              int linkFF = 0;
  83:              int linkMF = 0;
  84:              int linkMM = 0;
  85:              int totNodi = 0;
  86:              int nodiF = 0;
  87:              int nodiM = 0;
  88:   
  89:              bool firstTimeUser = true;
  90:              char genereUtente = ' ';
  91:   
  92:              // Ciclo sui confronti su tutti gli utenti
  93:              // partendo nel ciclo interno dall'utente successivo a quello che sto analizzando
  94:              for (int i = 0; i < users.Count - 1; i++) {
  95:   
  96:                  int User1ID = users[i].userid;
  97:                  Dictionary<int, int> user1Vote = GetUserVote(User1ID); // Prendo i voti dell'utente 1
  98:   
  99:                  genereUtente = Convert.ToChar(users[i].genere);
 100:                  firstTimeUser = true;
 101:   
 102:                  for (int x = i + 1; x < users.Count; x++) {
 103:   
 104:                      int User2ID = users[x].userid;
 105:                      Dictionary<int, int> user2Vote = GetUserVote(User2ID); // Prendo i voti dell'utente 2
 106:   
 107:                      // Unisco i voti dell'utente A a B in un unica lista
 108:                      // (poichè il confronto tra A e B è uguale a quello di B con A)                   
 109:                      Dictionary<int, SimilarityFilmsData> _tempMash = MashSimilarityFilmsUser(user1Vote, user2Vote);
 110:   
 111:                      // Confronto i valori degli utenti
 112:                      SimilarityResoult _temp = UserSimilaryty(_tempMash);
 113:   
 114:                      // Controllo se ho almeno 5 voti in comune per poter fare un vero confronto ...
 115:                      // Se si salvo nel database il risultato, se no nn è rilavante e quindi continuo con i confronti  
 116:                      int nVote = _temp.countvote_disagree + _temp.countvote_discord + _temp.countvote_opposite + _temp.countvote_same + _temp.countvote_similar;
 117:                      if (nVote >= 5) {
 118:                          //SaveInMysql(User1ID, User2ID, _temp);
 119:                          row++;
 120:   
 121:                          if (_temp.similaritScore > 70) {
 122:                              if ((users[i].genere == "M") && (users[x].genere == "M")) {
 123:                                  linkMM++;
 124:                              }
 125:                              else {
 126:                                  if ((users[i].genere == "F") && (users[x].genere == "F")) {
 127:                                      linkFF++;
 128:                                  }
 129:                                  else {
 130:                                      linkMF++;
 131:                                  }
 132:                              }
 133:                              totalLink++;
 134:   
 135:                              if (firstTimeUser == true) {
 136:                                  firstTimeUser = false;
 137:                                  if (genereUtente == 'M') {
 138:                                      nodiM++;
 139:                                  }
 140:                                  else {
 141:                                      nodiF++;
 142:                                  }
 143:                                  totNodi++;
 144:                              }
 145:                          }
 146:   
 147:                      }
 148:   
 149:   
 150:   
 151:   
 152:                  }
 153:              }
 154:              console.AppendText("Time elapsed: " + stopwatch.Elapsed + Environment.NewLine + Environment.NewLine);
 155:   
 156:              stopwatch.Stop();
 157:   
 158:              console.AppendText("Ci sono    totalLink= " + totalLink + Environment.NewLine);
 159:              console.AppendText("Ci sono      linkFF=" + linkFF + Environment.NewLine);
 160:              console.AppendText("Ci sono      linkMF=" + linkMF + Environment.NewLine);
 161:              console.AppendText("Ci sono      linkMM=" + linkMM + Environment.NewLine);
 162:              console.AppendText("Ci sono nodi totali =" + totNodi + Environment.NewLine);
 163:              console.AppendText("Ci sono nodi Femmina=" + nodiF + Environment.NewLine);
 164:              console.AppendText("Ci sono nodi Maschi=" + nodiM + Environment.NewLine);
 165:          }
 166:   
 167:          public class User {
 168:   
 169:              public int userid;
 170:              public string username;
 171:              public int anno;
 172:              public string genere;
 173:              public int preferito;
 174:              public int tag;
 175:              public int nvoti;
 176:   
 177:              public List<User> myFriends = new List<User>();
 178:              public List<int> suggestMovie = new List<int>();
 179:   
 180:              public User(int _userid, string _username, int _anno, string _genere, int _preferito, int _tag, int _nvoti) {
 181:                  this.userid = _userid;
 182:                  this.username = _username;
 183:                  this.anno = _anno;
 184:                  this.genere = _genere;
 185:                  this.preferito = _preferito;
 186:                  this.tag = _tag;
 187:                  this.nvoti = _nvoti;
 188:              }
 189:   
 190:              public void AddaFriend(User _friend) {
 191:                  this.myFriends.Add(_friend);
 192:              }
 193:   
 194:              public void AddSuggestMovie(int FilmID) {
 195:                  suggestMovie.Add(FilmID);
 196:              }
 197:   
 198:          }
 199:   
 200:          public string LoadAllUser() {
 201:   
 202:              string sql = @"SELECT reti_user.userid, username, anno, genere, preferito, tag, count(reti_voti.voto) as nvoti
 203:                             FROM reti_user inner join reti_voti
 204:                             on reti_user.userid = reti_voti.userid 
 205:                             where reti_voti.voto!=0
 206:                             group by reti_user.userid
 207:                             having nvoti>5";
 208:   
 209:              OdbcConnection MyConnection = new OdbcConnection(Utility.MySQLConString);
 210:              OdbcCommand com = null;
 211:              OdbcDataReader reader = null;
 212:   
 213:              try {
 214:                  MyConnection.Open();
 215:                  using (com = new OdbcCommand(sql, MyConnection)) {
 216:                      using (reader = com.ExecuteReader()) {
 217:                          while (reader.Read()) {
 218:                              int userid = reader.GetInt32(0);
 219:                              string username = reader.GetString(1);
 220:                              int anno = reader.GetInt32(2);
 221:                              string genere = reader.GetString(3);
 222:                              int preferito = reader.GetInt32(4);
 223:                              int tag = reader.GetInt32(5);
 224:                              int nvoti = reader.GetInt32(6);
 225:                              users.Add(new User(userid, username, anno, genere, preferito, tag, nvoti));
 226:                          }
 227:                      }
 228:                  }
 229:              }
 230:              catch (Exception e) {
 231:                  return "(LoadAllUser) > Mysql > " + e.Message;
 232:              }
 233:              finally {
 234:                  // close reader
 235:                  if (reader != null) reader.Close();
 236:                  // close connection
 237:                  if (MyConnection != null) MyConnection.Close();
 238:              }
 239:   
 240:              return "";
 241:   
 242:          }
 243:   
 244:          public Dictionary<int, int> GetUserVote(int userID) {
 245:   
 246:              Dictionary<int, int> userVote = new Dictionary<int, int>();
 247:   
 248:              string sql = "SELECT filmid, voto FROM reti_voti where userid=?";
 249:   
 250:              OdbcConnection MyConnection = new OdbcConnection(Utility.MySQLConString);
 251:              OdbcCommand com = null;
 252:              OdbcDataReader reader = null;
 253:   
 254:              try {
 255:                  MyConnection.Open();
 256:                  using (com = new OdbcCommand(sql, MyConnection)) {
 257:   
 258:                      OdbcParameter param1 = new OdbcParameter();
 259:                      param1.DbType = DbType.Int32;
 260:                      param1.Value = userID;
 261:                      com.Parameters.Add(param1);
 262:   
 263:                      using (reader = com.ExecuteReader()) {
 264:                          while (reader.Read()) {
 265:                              int filmid = reader.GetInt32(0);
 266:                              int voto = reader.GetInt32(1);
 267:                              userVote.Add(filmid, voto);
 268:                          }
 269:                      }
 270:                  }
 271:              }
 272:              catch (Exception e) {
 273:                  string err = "(GetUserVote) > Mysql > " + e.Message;
 274:              }
 275:              finally {
 276:                  // close reader
 277:                  if (reader != null) reader.Close();
 278:                  // close connection
 279:                  if (MyConnection != null) MyConnection.Close();
 280:              }
 281:   
 282:              return userVote;
 283:   
 284:          }
 285:   
 286:          public class Film {
 287:              public int voto;
 288:              public int ID;
 289:              public Film(int _id, int _voto) {
 290:                  this.ID = _id;
 291:                  this.voto = _voto;
 292:              }
 293:          }
 294:   
 295:          public SimilarityResoult UserSimilaryty(Dictionary<int, SimilarityFilmsData> _similarityDictionary) {
 296:   
 297:              int total = 0; // film visti in comune
 298:   
 299:              //double VotePoints_opposite = 0;
 300:              double VotePoints_disagree = 0.3;
 301:              double VotePoints_discord = 0.5;
 302:              double VotePoints_similar = 0.8;
 303:              //double VotePoints_same = 1;
 304:   
 305:              int countvote_opposite = 0; // Ogni volta che i voti sono mooolto contrastanti
 306:              int countvote_disagree = 0; // Molto in disaccordo ma nn l'opposto un pò in disaccordo es io 2 tu 3 
 307:              int countvote_discord = 0; // Voto un pò in disaccordo es io 2 tu 3 
 308:              int countvote_similar = 0; // Voto simile es io 4 tu 5, io 2 e tu 1
 309:              int countvote_same = 0;  // Voto uguale tutti e 2 votiamo 3 
 310:   
 311:              List<int> listSuggestAtoB = new List<int>(); // Film che A suggerisce ad B
 312:              List<int> listSuggestBtoA = new List<int>(); // Film che B suggerisce ad A
 313:   
 314:              foreach (KeyValuePair<int, SimilarityFilmsData> row in _similarityDictionary) {
 315:   
 316:                  int filmID = row.Value.filmID;
 317:                  int votoUserA = row.Value.UserAVote;
 318:                  int votoUserB = row.Value.UserBVote;
 319:   
 320:                  // Film che A e B non hanno visto e che consiglia all'altro
 321:                  if ((votoUserA == 0) && ((votoUserB == 5) || (votoUserB == 4))) {
 322:                      listSuggestBtoA.Add(filmID);
 323:                      continue;
 324:                  }
 325:                  if ((votoUserB == 0) && ((votoUserA == 5) || (votoUserA == 4))) {
 326:                      listSuggestAtoB.Add(filmID);
 327:                      continue;
 328:                  }
 329:   
 330:                  // il film  A o B non lo hanno visto, quindi inutile continuare il cofronto, esco dal ciclo
 331:                  if ((votoUserA == 0) || (votoUserB == 0)) {
 332:                      continue;
 333:                  }
 334:   
 335:                  // il Film è stato visto da entrambi ed entrambi, ha senso fare il confronto
 336:   
 337:                  total++; // Incremento il contatore
 338:   
 339:                  // Stesso voto = maggior "legame"
 340:                  if ((votoUserA == votoUserB) && (votoUserA != 0)) {
 341:                      //score += VotePoints_same;
 342:                      countvote_same++;
 343:                      continue;
 344:                  }
 345:   
 346:                  // Voto congiunto che mi permette di dare un Grado di "amicizia" in base hai 2 voti
 347:                  // Es A vota 3 e B vota 2
 348:                  // _choose = (3*10)+2 = 32;
 349:                  int _choose = (votoUserA * 10) + votoUserB;
 350:   
 351:                  switch (_choose) {
 352:                      // Molto simili 1 e 2 oppure 4 o 5
 353:                      case 12:
 354:                      case 21:
 355:                      case 45:
 356:                      case 54:
 357:                          //score += VotePoints_similar;
 358:                          countvote_similar++;
 359:                          break;
 360:                      // Non uguali ma qualcosa in comune ...
 361:                      case 34:
 362:                      case 43:
 363:                          //score += VotePoints_disagree;
 364:                          countvote_disagree++;
 365:                          break;
 366:                      // In contrasto ma non agli antipodi ...
 367:                      case 13:
 368:                      case 23:
 369:                      case 32:
 370:                      case 53:
 371:                          //score += VotePoints_discord;
 372:                          countvote_discord++;
 373:                          break;
 374:                      // Pareri piu' discordanti possibile ...
 375:                      case 14:
 376:                      case 15:
 377:                      case 24:
 378:                      case 25:
 379:                      case 31:
 380:                      case 35:
 381:                      case 41:
 382:                      case 42:
 383:                      case 51:
 384:                      case 52:
 385:                          //score += VotePoints_opposite;
 386:                          countvote_opposite++;
 387:                          break;
 388:                      default:
 389:                          string x = "errore";
 390:                          break;
 391:                  }
 392:              }
 393:   
 394:              int similarity = 0;
 395:              if (total != 0) {
 396:                  double temp = ((countvote_disagree * VotePoints_disagree) + (VotePoints_discord * countvote_discord) + (VotePoints_similar * countvote_similar) + (countvote_same));
 397:                  temp = temp / total;
 398:                  similarity = Convert.ToInt32(temp * 100);
 399:              }
 400:   
 401:              return new SimilarityResoult(similarity, listSuggestBtoA, listSuggestAtoB, countvote_opposite, countvote_disagree, countvote_disagree, countvote_similar, countvote_same);
 402:   
 403:          }
 404:   
 405:          public Dictionary<int, SimilarityFilmsData> MashSimilarityFilmsUser(Dictionary<int, int> user1_voteList, Dictionary<int, int> user2_voteList) {
 406:   
 407:              Dictionary<int, SimilarityFilmsData> _mash = new Dictionary<int, SimilarityFilmsData>();
 408:   
 409:              // Aggiungo i voti di utenteA 
 410:              foreach (KeyValuePair<int, int> us1 in user1_voteList) {
 411:                  // Se anche B ha visto il film
 412:                  if (user2_voteList.ContainsKey(us1.Key) == true) {
 413:                      _mash.Add(us1.Key, new SimilarityFilmsData(us1.Key, user1_voteList[us1.Key], user2_voteList[us1.Key]));
 414:                  }
 415:                  else { // Altrimenti mezzo 0 come voto....
 416:                      _mash.Add(us1.Key, new SimilarityFilmsData(us1.Key, user1_voteList[us1.Key], 0));
 417:                  }
 418:              }
 419:   
 420:              // Aggiungo anche i film che ha visto B ma nn A
 421:              foreach (KeyValuePair<int, int> us2 in user2_voteList) {
 422:                  // Se anche B ha visto il film
 423:                  if (_mash.ContainsKey(us2.Key) == false) {
 424:                      _mash.Add(us2.Key, new SimilarityFilmsData(us2.Key, 0, user2_voteList[us2.Key]));
 425:                  }
 426:              }
 427:   
 428:   
 429:              return _mash;
 430:   
 431:          }
 432:   
 433:          public class SimilarityFilmsData {
 434:              public int filmID;
 435:              public int UserAVote;
 436:              public int UserBVote;
 437:              public SimilarityFilmsData(int _filmID, int _AVote, int _Bvote) {
 438:                  this.filmID = _filmID;
 439:                  this.UserAVote = _AVote;
 440:                  this.UserBVote = _Bvote;
 441:              }
 442:          }
 443:   
 444:          public class SimilarityResoult {
 445:   
 446:              public List<int> suggetMovieListAtoB = new List<int>();
 447:              public List<int> suggetMovieListBtoA = new List<int>();
 448:   
 449:              public int similaritScore;
 450:   
 451:              public int countvote_opposite = 0; // Ogni volta che i voti sono mooolto contrastanti aggiungo 1
 452:              public int countvote_disagree = 0; // Molto in disaccordo ma nn l'opposto un pò in disaccordo es io 2 tu 3 
 453:              public int countvote_discord = 0; // Voto un pò in disaccordo es io 2 tu 3 
 454:              public int countvote_similar = 0; // Voto simile es io 4 tu 5, io 2 e tu 1
 455:              public int countvote_same = 0; // Voto uguale tutti e 2 votiamo 3 
 456:   
 457:              public SimilarityResoult(int _score, List<int> _listAtoB, List<int> _listBtoA, int _opposite, int _disagree, int _discord, int _similar, int _same) {
 458:                  this.similaritScore = _score;
 459:                  this.suggetMovieListAtoB = _listAtoB;
 460:                  this.suggetMovieListBtoA = _listBtoA;
 461:                  this.countvote_opposite = _opposite;
 462:                  this.countvote_disagree = _disagree;
 463:                  this.countvote_discord = _discord;
 464:                  this.countvote_similar = _similar;
 465:                  this.countvote_same = _same;
 466:              }
 467:          }
 468:   
 469:   
 470:          public static void SaveInMysql(int idA, int idB, SimilarityResoult mySimilarityResoult) {
 471:   
 472:              string listMovieAtoB = String.Join(";", mySimilarityResoult.suggetMovieListAtoB);
 473:              string listMovieBtoA = String.Join(";", mySimilarityResoult.suggetMovieListBtoA);
 474:   
 475:              // Creo una stringa unica che identifica la relazione tra A e B
 476:              string comparisonId = (idA < idB) ? idA + "." + idB : idB + "." + idA;
 477:   
 478:              string sql = @"INSERT INTO `reti_confronti` (`idA`, `idB`,`comparisonId`, `same`, `similar`, `discord`, `disagree`, `opposite`, `similaritScore`) 
 479:                  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
 480:   
 481:              OdbcConnection MyConnection = new OdbcConnection(Utility.MySQLConString);
 482:   
 483:              OdbcCommand com = null;
 484:              OdbcDataReader reader = null;
 485:   
 486:              try {
 487:   
 488:                  MyConnection.Open();
 489:   
 490:                  using (com = new OdbcCommand(sql, MyConnection)) {
 491:   
 492:                      OdbcParameter param1 = new OdbcParameter();
 493:                      param1.DbType = DbType.Int32;
 494:                      param1.Value = idA;
 495:                      com.Parameters.Add(param1);
 496:   
 497:                      OdbcParameter param2 = new OdbcParameter();
 498:                      param2.DbType = DbType.Int32;
 499:                      param2.Value = idB;
 500:                      com.Parameters.Add(param2);
 501:   
 502:                      OdbcParameter param2A = new OdbcParameter();
 503:                      param2A.DbType = DbType.String;
 504:                      param2A.Value = comparisonId;
 505:                      com.Parameters.Add(param2A);
 506:   
 507:                      OdbcParameter param3 = new OdbcParameter();
 508:                      param3.DbType = DbType.Int32;
 509:                      param3.Value = mySimilarityResoult.countvote_same;
 510:                      com.Parameters.Add(param3);
 511:   
 512:                      OdbcParameter param4 = new OdbcParameter();
 513:                      param4.DbType = DbType.Int32;
 514:                      param4.Value = mySimilarityResoult.countvote_similar;
 515:                      com.Parameters.Add(param4);
 516:   
 517:                      OdbcParameter param5 = new OdbcParameter();
 518:                      param5.DbType = DbType.Int32;
 519:                      param5.Value = mySimilarityResoult.countvote_discord;
 520:                      com.Parameters.Add(param5);
 521:   
 522:                      OdbcParameter param6 = new OdbcParameter();
 523:                      param6.DbType = DbType.Int32;
 524:                      param6.Value = mySimilarityResoult.countvote_disagree;
 525:                      com.Parameters.Add(param6);
 526:   
 527:                      OdbcParameter param7 = new OdbcParameter();
 528:                      param7.DbType = DbType.Int32;
 529:                      param7.Value = mySimilarityResoult.countvote_opposite;
 530:                      com.Parameters.Add(param7);
 531:   
 532:                      OdbcParameter param8 = new OdbcParameter();
 533:                      param8.DbType = DbType.Int32;
 534:                      param8.Value = mySimilarityResoult.similaritScore;
 535:                      com.Parameters.Add(param8);
 536:   
 537:                      com.ExecuteNonQuery();
 538:   
 539:                  }
 540:   
 541:                  if (String.IsNullOrEmpty(listMovieBtoA) == false) {
 542:   
 543:                      sql = "INSERT INTO `reti_suggerimenti` (`userID_from`, `userID_to`, `list`, `comparisonId`) VALUES (?, ?, ?, ?);";
 544:   
 545:                      using (com = new OdbcCommand(sql, MyConnection)) {
 546:   
 547:                          OdbcParameter param1 = new OdbcParameter();
 548:                          param1.DbType = DbType.Int32;
 549:                          param1.Value = idA;
 550:                          com.Parameters.Add(param1);
 551:   
 552:                          OdbcParameter param2 = new OdbcParameter();
 553:                          param2.DbType = DbType.Int32;
 554:                          param2.Value = idB;
 555:                          com.Parameters.Add(param2);
 556:   
 557:                          OdbcParameter param3 = new OdbcParameter();
 558:                          param3.DbType = DbType.String;
 559:                          param3.Value = listMovieBtoA;
 560:                          com.Parameters.Add(param3);
 561:   
 562:                          OdbcParameter param4 = new OdbcParameter();
 563:                          param4.DbType = DbType.String;
 564:                          param4.Value = comparisonId;
 565:                          com.Parameters.Add(param4);
 566:   
 567:                          com.ExecuteNonQuery();
 568:                      }
 569:                  }
 570:   
 571:   
 572:                  if (String.IsNullOrEmpty(listMovieAtoB) == false) {
 573:   
 574:                      sql = "INSERT INTO `reti_suggerimenti` (`userID_from`, `userID_to`, `list`, `comparisonId`) VALUES (?, ?, ?, ?);";
 575:   
 576:                      using (com = new OdbcCommand(sql, MyConnection)) {
 577:   
 578:                          OdbcParameter param1 = new OdbcParameter();
 579:                          param1.DbType = DbType.Int32;
 580:                          param1.Value = idB;
 581:                          com.Parameters.Add(param1);
 582:   
 583:                          OdbcParameter param2 = new OdbcParameter();
 584:                          param2.DbType = DbType.Int32;
 585:                          param2.Value = idA;
 586:                          com.Parameters.Add(param2);
 587:   
 588:                          OdbcParameter param3 = new OdbcParameter();
 589:                          param3.DbType = DbType.String;
 590:                          param3.Value = listMovieAtoB;
 591:                          com.Parameters.Add(param3);
 592:   
 593:                          OdbcParameter param4 = new OdbcParameter();
 594:                          param4.DbType = DbType.String;
 595:                          param4.Value = comparisonId;
 596:                          com.Parameters.Add(param4);
 597:   
 598:                          com.ExecuteNonQuery();
 599:   
 600:                      }
 601:                  }
 602:   
 603:   
 604:              }
 605:              catch (Exception e) {
 606:                  string catchError = "(SaveSimilarityResoult) > Mysql > " + e.Message;
 607:              }
 608:              finally {
 609:                  // close reader
 610:                  if (reader != null) reader.Close();
 611:                  // close connection
 612:                  if (MyConnection != null) MyConnection.Close();
 613:              }
 614:   
 615:          }
 616:   
 617:          public class Worker {
 618:   
 619:              int idA; int idB;
 620:              SimilarityResoult mySimilarityResoult;
 621:   
 622:              public Worker(int _idA, int _idB, SimilarityResoult _resoult) {
 623:                  this.idA = _idA;
 624:                  this.idB = _idB;
 625:                  this.mySimilarityResoult = _resoult;
 626:              }
 627:   
 628:              // This method will be called when the thread is started.
 629:              public void DoWork() {
 630:                  SaveSimilarityResoult();
 631:              }
 632:   
 633:              public void RequestStop() {
 634:                  _shouldStop = true;
 635:              }
 636:   
 637:              // Volatile is used as hint to the compiler that this data member will be accessed by multiple threads.
 638:              private volatile bool _shouldStop;
 639:   
 640:              public string SaveSimilarityResoult() {
 641:   
 642:                  string listMovieAtoB = String.Join(";", mySimilarityResoult.suggetMovieListAtoB);
 643:                  string listMovieBtoA = String.Join(";", mySimilarityResoult.suggetMovieListBtoA);
 644:   
 645:                  // Creo una stringa unica che identifica la relazione tra A e B
 646:                  string comparisonId = (idA < idB) ? idA + "." + idB : idB + "." + idA;
 647:   
 648:                  string sql = @"INSERT INTO `reti_confronti` (`idA`, `idB`,`comparisonId`, `same`, `similar`, `discord`, `disagree`, `opposite`, `similaritScore`) 
 649:                  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
 650:   
 651:                  OdbcConnection MyConnection = new OdbcConnection(Utility.MySQLConString);
 652:   
 653:                  OdbcCommand com = null;
 654:                  OdbcDataReader reader = null;
 655:   
 656:                  try {
 657:   
 658:                      MyConnection.Open();
 659:   
 660:                      using (com = new OdbcCommand(sql, MyConnection)) {
 661:   
 662:                          OdbcParameter param1 = new OdbcParameter();
 663:                          param1.DbType = DbType.Int32;
 664:                          param1.Value = idA;
 665:                          com.Parameters.Add(param1);
 666:   
 667:                          OdbcParameter param2 = new OdbcParameter();
 668:                          param2.DbType = DbType.Int32;
 669:                          param2.Value = idB;
 670:                          com.Parameters.Add(param2);
 671:   
 672:                          OdbcParameter param2A = new OdbcParameter();
 673:                          param2A.DbType = DbType.String;
 674:                          param2A.Value = comparisonId;
 675:                          com.Parameters.Add(param2A);
 676:   
 677:                          OdbcParameter param3 = new OdbcParameter();
 678:                          param3.DbType = DbType.Int32;
 679:                          param3.Value = mySimilarityResoult.countvote_same;
 680:                          com.Parameters.Add(param3);
 681:   
 682:                          OdbcParameter param4 = new OdbcParameter();
 683:                          param4.DbType = DbType.Int32;
 684:                          param4.Value = mySimilarityResoult.countvote_similar;
 685:                          com.Parameters.Add(param4);
 686:   
 687:                          OdbcParameter param5 = new OdbcParameter();
 688:                          param5.DbType = DbType.Int32;
 689:                          param5.Value = mySimilarityResoult.countvote_discord;
 690:                          com.Parameters.Add(param5);
 691:   
 692:                          OdbcParameter param6 = new OdbcParameter();
 693:                          param6.DbType = DbType.Int32;
 694:                          param6.Value = mySimilarityResoult.countvote_disagree;
 695:                          com.Parameters.Add(param6);
 696:   
 697:                          OdbcParameter param7 = new OdbcParameter();
 698:                          param7.DbType = DbType.Int32;
 699:                          param7.Value = mySimilarityResoult.countvote_opposite;
 700:                          com.Parameters.Add(param7);
 701:   
 702:                          OdbcParameter param8 = new OdbcParameter();
 703:                          param8.DbType = DbType.Int32;
 704:                          param8.Value = mySimilarityResoult.similaritScore;
 705:                          com.Parameters.Add(param8);
 706:   
 707:                          com.ExecuteNonQuery();
 708:   
 709:                      }
 710:   
 711:                      if (String.IsNullOrEmpty(listMovieBtoA) == false) {
 712:   
 713:                          sql = "INSERT INTO `reti_suggerimenti` (`userID_from`, `userID_to`, `list`) VALUES (?, ?, ?);";
 714:   
 715:                          using (com = new OdbcCommand(sql, MyConnection)) {
 716:   
 717:                              OdbcParameter param1 = new OdbcParameter();
 718:                              param1.DbType = DbType.Int32;
 719:                              param1.Value = idA;
 720:                              com.Parameters.Add(param1);
 721:   
 722:                              OdbcParameter param2 = new OdbcParameter();
 723:                              param2.DbType = DbType.Int32;
 724:                              param2.Value = idB;
 725:                              com.Parameters.Add(param2);
 726:   
 727:                              OdbcParameter param3 = new OdbcParameter();
 728:                              param3.DbType = DbType.String;
 729:                              param3.Value = listMovieBtoA;
 730:                              com.Parameters.Add(param3);
 731:   
 732:                              com.ExecuteNonQuery();
 733:                          }
 734:                      }
 735:   
 736:   
 737:                      if (String.IsNullOrEmpty(listMovieAtoB) == false) {
 738:   
 739:                          sql = "INSERT INTO `reti_suggerimenti` (`userID_from`, `userID_to`, `list`) VALUES (?, ?, ?);";
 740:   
 741:                          using (com = new OdbcCommand(sql, MyConnection)) {
 742:   
 743:                              OdbcParameter param1 = new OdbcParameter();
 744:                              param1.DbType = DbType.Int32;
 745:                              param1.Value = idB;
 746:                              com.Parameters.Add(param1);
 747:   
 748:                              OdbcParameter param2 = new OdbcParameter();
 749:                              param2.DbType = DbType.Int32;
 750:                              param2.Value = idA;
 751:                              com.Parameters.Add(param2);
 752:   
 753:                              OdbcParameter param3 = new OdbcParameter();
 754:                              param3.DbType = DbType.String;
 755:                              param3.Value = listMovieAtoB;
 756:                              com.Parameters.Add(param3);
 757:   
 758:                              com.ExecuteNonQuery();
 759:   
 760:                          }
 761:                      }
 762:   
 763:   
 764:                  }
 765:                  catch (Exception e) {
 766:                      return "(SaveSimilarityResoult) > Mysql > " + e.Message;
 767:                  }
 768:                  finally {
 769:                      // close reader
 770:                      if (reader != null) reader.Close();
 771:                      // close connection
 772:                      if (MyConnection != null) MyConnection.Close();
 773:                  }
 774:   
 775:                  return "";
 776:   
 777:   
 778:              }
 779:   
 780:   
 781:          }
 782:   
 783:          private void cmdSetup_Click(object sender, EventArgs e) {
 784:   
 785:              if (users.Count == 0) {
 786:                  stopwatch.Start();
 787:                  console.Text = "loading...";
 788:                  console.Text = LoadAllUser();
 789:                  console.AppendText(Environment.NewLine);
 790:                  console.AppendText("Abbiamo " + users.Count + " utenti da analizzare." + Environment.NewLine);
 791:                  console.AppendText("stopwatch " + Stopwatch.IsHighResolution + Environment.NewLine);
 792:              }
 793:   
 794:              // Raccomandazione "Grezza", conto quanti film suggeriti uguali ci sono tra i miei amici ...
 795:              for (int i = 0; i < users.Count - 1; i++) {
 796:   
 797:                  int UserID = users[i].userid;
 798:                  List<SuggestResoult> temp = GetSuggestedOtherUser(UserID);
 799:   
 800:                  Dictionary<int, int> SuggestedList = new Dictionary<int, int>();
 801:   
 802:                  for (int x = 0; x < temp.Count; x++) {
 803:                      int[] MovieSuggested = temp[x].list.Split(';').Select(int.Parse).ToArray();
 804:                      foreach (int fildid in MovieSuggested) {
 805:                          if (SuggestedList.ContainsKey(fildid) == true) {
 806:                              SuggestedList[fildid] = SuggestedList[fildid] + 1;
 807:                          }
 808:                          else {
 809:                              SuggestedList.Add(fildid, 0);
 810:                          }
 811:                      }
 812:                  }
 813:   
 814:                  int k = 0;
 815:                  foreach (var item in SuggestedList.OrderByDescending(key => key.Value)) {
 816:                      insertSuggestionRow(UserID, item.Key);
 817:                      if (k == 2) break;
 818:                      k++;
 819:                  }
 820:   
 821:              }
 822:   
 823:              console.AppendText("Time elapsed: " + stopwatch.Elapsed + Environment.NewLine + Environment.NewLine);
 824:   
 825:              // Stop.
 826:              stopwatch.Stop();
 827:   
 828:          }
 829:   
 830:          private void insertSuggestionRow(int userID, int filmId) {
 831:   
 832:              List<SuggestResoult> mylist = new List<SuggestResoult>();
 833:   
 834:              string sql = @"INSERT INTO `reti_raccomandati` (`userID`, `filmId`) VALUES (?, ?);";
 835:   
 836:              OdbcConnection MyConnection = new OdbcConnection(Utility.MySQLConString);
 837:              OdbcCommand com = null;
 838:              OdbcDataReader reader = null;
 839:   
 840:              try {
 841:                  MyConnection.Open();
 842:   
 843:                  using (com = new OdbcCommand(sql, MyConnection)) {
 844:   
 845:                      OdbcParameter param1 = new OdbcParameter();
 846:                      param1.DbType = DbType.Int32;
 847:                      param1.Value = userID;
 848:                      com.Parameters.Add(param1);
 849:   
 850:                      OdbcParameter param2 = new OdbcParameter();
 851:                      param2.DbType = DbType.Int32;
 852:                      param2.Value = filmId;
 853:                      com.Parameters.Add(param2);
 854:   
 855:                      com.ExecuteNonQuery();
 856:                  }
 857:              }
 858:              catch (Exception e) {
 859:                  string err = "(Get Suggested Other User) > Mysql > " + e.Message;
 860:              }
 861:              finally {
 862:                  // close reader
 863:                  if (reader != null) reader.Close();
 864:                  // close connection
 865:                  if (MyConnection != null) MyConnection.Close();
 866:              }
 867:   
 868:   
 869:   
 870:   
 871:   
 872:   
 873:          }
 874:   
 875:          private List<SuggestResoult> GetSuggestedOtherUser(int user) {
 876:   
 877:              List<SuggestResoult> mylist = new List<SuggestResoult>();
 878:   
 879:              string sql = @"SELECT 
 880:                                  similaritScore,
 881:                                  reti_suggerimenti.list,
 882:                                  reti_suggerimenti.userID_to
 883:                              FROM
 884:                                  reti_confronti
 885:                                      INNER JOIN
 886:                                  reti_suggerimenti ON reti_confronti.comparisonID = reti_suggerimenti.comparisonID
 887:                              WHERE
 888:                                  (idA = ? OR idB = ?)
 889:                                      AND similaritScore > 60
 890:                              HAVING reti_suggerimenti.userID_to = ?
 891:                              ORDER BY similaritScore DESC
 892:                              LIMIT 10";
 893:   
 894:              OdbcConnection MyConnection = new OdbcConnection(Utility.MySQLConString);
 895:              OdbcCommand com = null;
 896:              OdbcDataReader reader = null;
 897:   
 898:              try {
 899:                  MyConnection.Open();
 900:   
 901:                  using (com = new OdbcCommand(sql, MyConnection)) {
 902:   
 903:                      OdbcParameter param1 = new OdbcParameter();
 904:                      param1.DbType = DbType.Int32;
 905:                      param1.Value = user;
 906:                      com.Parameters.Add(param1);
 907:   
 908:                      OdbcParameter param2 = new OdbcParameter();
 909:                      param2.DbType = DbType.Int32;
 910:                      param2.Value = user;
 911:                      com.Parameters.Add(param2);
 912:   
 913:                      OdbcParameter param3 = new OdbcParameter();
 914:                      param3.DbType = DbType.Int32;
 915:                      param3.Value = user;
 916:                      com.Parameters.Add(param3);
 917:   
 918:                      using (reader = com.ExecuteReader()) {
 919:                          while (reader.Read()) {
 920:                              int similaritScore = reader.GetInt32(0);
 921:                              string list = reader.GetString(1);
 922:                              mylist.Add(new SuggestResoult(similaritScore, list));
 923:                          }
 924:                      }
 925:                  }
 926:              }
 927:              catch (Exception e) {
 928:                  string err = "(Get Suggested Other User) > Mysql > " + e.Message;
 929:              }
 930:              finally {
 931:                  // close reader
 932:                  if (reader != null) reader.Close();
 933:                  // close connection
 934:                  if (MyConnection != null) MyConnection.Close();
 935:              }
 936:   
 937:              return mylist;
 938:   
 939:          }
 940:   
 941:          public class SuggestResoult {
 942:              public string list;
 943:              public int similaritScore;
 944:              public SuggestResoult(int _similaritScore, string _list) {
 945:                  this.similaritScore = _similaritScore;
 946:                  this.list = _list;
 947:              }
 948:          }
 949:   
 950:          private void cmdPredizione_Click(object sender, EventArgs e) {
 951:   
 952:              stopwatch.Start();
 953:   
 954:              console.Text = "inizio predizione ...";
 955:   
 956:              // Query per vedere se ho preso i valori giusti
 957:              // SELECT * FROM sql960766_3.reti_voti where userid IN(172, 36, 161, 122, 12, 76, 115, 22, 10, 1, 67, 164, 61) and filmid = 1;
 958:   
 959:              List<User> LeaderUser = loadSuperUserV6();
 960:              List<Dictionary<int, int>> LeaderUserVote = new List<Dictionary<int, int>>();
 961:   
 962:              for (int i = 0; i < LeaderUser.Count; i++) {
 963:                  LeaderUserVote.Add(GetUserVote(LeaderUser[i].userid));
 964:              }
 965:   
 966:              double totalScore = 0;
 967:              double countTotalMatch = 0;
 968:              int countUserNotDone = 0;
 969:   
 970:              console.Text = LoadAllUser();
 971:              console.AppendText(Environment.NewLine);
 972:              console.AppendText("Abbiamo " + users.Count + " utenti da analizzare." + Environment.NewLine);
 973:              console.AppendText("stopwatch " + Stopwatch.IsHighResolution + Environment.NewLine);
 974:   
 975:              for (int countUser = 0; countUser < users.Count; countUser++) {
 976:   
 977:                  int userIDBestFriend = GetUserIDBestFriend(users[countUser].userid);
 978:   
 979:                  //LeaderUser.Add(new User(userIDBestFriend, "UserIDBestFriend", 0, "Custom", 0, 1, 0));
 980:                  //LeaderUserVote.Add(GetUserVote(userIDBestFriend));
 981:   
 982:                  Dictionary<int, int> user = GetUserVote(users[countUser].userid);
 983:   
 984:                  /*
 985:                  Faccio un setup di 10 voti dove vedo l'utente quando è vicino agli altri utenti tipo....
 986:                  poi inizio la predizione e vedo quante volte sbaglio
 987:                  */
 988:   
 989:                  int[] matrixScore = new int[nRegion];
 990:   
 991:                  int nSetupMach = 10; // Numero di giri prima che l'algoritmo di predizione inizi a funzionare ... 
 992:   
 993:                  double nMatch = 0;
 994:                  double RightPrediction = 0;
 995:   
 996:                  for (int idfilm = 1; idfilm <= 50; idfilm++) {
 997:   
 998:                      // Carico nella matrice i voti dei vari gruppi ...
 999:                      int[] matrixUserScore = SetMatrixValue(LeaderUserVote, idfilm);
1000:   
1001:                      if (user.ContainsKey(idfilm) == true) {
1002:   
1003:                          int UserVote = user[idfilm];
1004:   
1005:                          // Provo ad indovinare se riesco a "predirre" cosa abbia votato l'utente
1006:                          // Ovvero guardo l'utente "leader" che ha piu' punti e suggerisco il suo voto
1007:                          if ((nSetupMach <= 0) && (UserVote != 0)) {
1008:   
1009:                              int voteSuggest = GetVoteSuggestBySuperUser(matrixUserScore, matrixScore);
1010:                              if (UserVote == voteSuggest) {
1011:                                  RightPrediction++;
1012:                              }
1013:                              //else {
1014:                              //    double potenza = 2;
1015:                              //    double delta = UserVote - voteSuggest;
1016:                              //    double deltaPower = Math.Pow(delta, potenza);
1017:                              //    if (deltaPower == 1) {
1018:                              //        RightPrediction = RightPrediction + 0.5;
1019:                              //    }
1020:                              //}
1021:                              nMatch++;
1022:                              //console.AppendText(users[countUser].userid + "." +idfilm + ") UserVote" + UserVote + " Vs voteSuggest:" + voteSuggest + ". Per adesso " + RightPrediction + "/" + nMatch + " successi" + Environment.NewLine);
1023:                          }
1024:                          else {
1025:                              nSetupMach += -1;
1026:                          }
1027:   
1028:                          if (UserVote != 0) {
1029:                              // Confronto il voto dell'utente con tutti i vari "leader" user
1030:                              // Aggiungo il punto a chi ha il punteggio uguale
1031:                              for (int i = 0; i < matrixUserScore.Length; i++) {
1032:                                  if (UserVote == matrixUserScore[i]) {
1033:                                      matrixScore[i] = matrixScore[i] + 1;
1034:                                  }
1035:                              }
1036:                          }
1037:                      }
1038:                  }
1039:   
1040:                  double score = (RightPrediction / nMatch) * 100;
1041:                  if (double.IsNaN(RightPrediction / nMatch) == false) {
1042:                      double not100 = (RightPrediction / nMatch);
1043:                      if (not100 != 1) {
1044:                          totalScore += (RightPrediction / nMatch);
1045:                          countTotalMatch++;
1046:                      }
1047:                      else {
1048:                          string s = "100";
1049:                      }
1050:   
1051:   
1052:   
1053:                  }
1054:                  else {
1055:                      countUserNotDone++;
1056:                  }
1057:   
1058:                  //LeaderUser.RemoveAt(LeaderUser.Count - 1);
1059:                  //LeaderUserVote.RemoveAt(LeaderUserVote.Count - 1);
1060:   
1061:              }
1062:   
1063:   
1064:              double mathTotalScore = (totalScore / countTotalMatch) * 100;
1065:              console.AppendText("Il risultato complessivo dell'algoritmo è stato del " + mathTotalScore + "%. Gli utente non analizzati sono stati " + countUserNotDone + Environment.NewLine);
1066:              console.AppendText("Time elapsed: " + stopwatch.Elapsed + Environment.NewLine + Environment.NewLine);
1067:          }
1068:   
1069:          private int[] SetMatrixValue(List<Dictionary<int, int>> LeaderUserVote, int idfilm) {
1070:   
1071:              int[] matrix = new int[nRegion];
1072:   
1073:              matrix[0] = LeaderUserVote[0][idfilm]; // Rossa
1074:   
1075:              // Tutti gli altri colori ....
1076:              // La i mi indica la posizione nella griglia
1077:              // Se il valore del voto nn lo prendo dalla prima persona (che è in posizione i*2 - 1)  
1078:              // lo prendo dalla seconda (che è in posizione 2i)
1079:              // se nemmeno la seconda ha il voto, allora niente metto zero.
1080:              // Le persone sono aggiunte 2 alla volta per colore, tranne la prima che è idbm.com
1081:   
1082:              for (int i = 1; i < 7; i++) {
1083:   
1084:                  if (LeaderUserVote[(2 * i - 1)].ContainsKey(idfilm) == true) {
1085:                      if (LeaderUserVote[(2 * i - 1)][idfilm] != 0) {
1086:                          matrix[i] = LeaderUserVote[(2 * i - 1)][idfilm];
1087:                      }
1088:                  }
1089:                  else {
1090:                      if (LeaderUserVote[(2 * i)].ContainsKey(idfilm) == true) {
1091:                          if (LeaderUserVote[2 * i][idfilm] != 0) {
1092:                              matrix[i] = LeaderUserVote[2 * i][idfilm];
1093:                          }
1094:                      }
1095:                      else {
1096:                          matrix[i] = 0;
1097:                      }
1098:                  }
1099:              }
1100:   
1101:              //try {
1102:              //    matrix[7] = LeaderUserVote[13][idfilm]; // Best Friends
1103:              //}
1104:              //catch {
1105:              //    matrix[7] = 0; //BestFriends
1106:              //}
1107:   
1108:   
1109:   
1110:              return matrix;
1111:   
1112:          }
1113:   
1114:   
1115:          public int GetVoteSuggestBySuperUser(int[] keys, int[] values) {
1116:   
1117:              // Creo e popolo la lista dei superUser [che hanno espresso il voto per questo film]
1118:              List<RecommendationData> listtSuperUser = new List<RecommendationData>();
1119:              for (int i = 0; i < keys.Length; i++) {
1120:                  if (keys[i] != 0) {
1121:                      listtSuperUser.Add(new RecommendationData(i, values[i], keys[i]));
1122:                  }
1123:              }
1124:              // La ordino per ordine di importanza dei super user
1125:              RecommendationData[] SuperUser = listtSuperUser.ToArray();
1126:              Array.Sort(SuperUser); // after this donators is sorted
1127:   
1128:              // Creo una lista dei primi x SuperUser.
1129:              // Ci entra chi ha il punteggio massimo e 
1130:              // Chi ha il punteggio massimo-1
1131:              int maxNmatch = 0;
1132:              double cmd = 0;
1133:              List<RecommendationData> bestUser = new List<RecommendationData>();
1134:              for (int i = SuperUser.Length - 1; i > 0; i--) {
1135:                  if (i == SuperUser.Length - 1) {
1136:                      maxNmatch = SuperUser[i].nRightMatch;
1137:                  }
1138:                  if (SuperUser[i].nRightMatch + 1 >= maxNmatch) {
1139:                      bestUser.Add(SuperUser[i]);
1140:                      cmd += SuperUser[i].nRightMatch;
1141:                  }
1142:                  else {
1143:                      break;
1144:                  }
1145:              }
1146:              // Inserisco nell'arrey dei voti, il voto pesato di ogni superUser
1147:              double[] arrResult = new double[6];
1148:              for (int i = 0; i < bestUser.Count; i++) {
1149:                  arrResult[bestUser[i].vote] += (bestUser[i].nRightMatch) / cmd;
1150:              }
1151:   
1152:              //Ritorno il voto che ha preso il maggior numero di consensi...
1153:              double maxValue = arrResult.Max();
1154:              return arrResult.ToList().IndexOf(maxValue);
1155:   
1156:          }
1157:   
1158:   
1159:          public int GetUserIDBestFriend(int userID) {
1160:   
1161:              int idA = 0;
1162:              int idB = 0;
1163:   
1164:              string sql = @"SELECT idA,idB, similaritScore FROM sql960766_3.reti_confronti 
1165:                              where idA = ? or idB = ?
1166:                              order by same desc
1167:                              limit 1";
1168:   
1169:              OdbcConnection MyConnection = new OdbcConnection(Utility.MySQLConString);
1170:              OdbcCommand com = null;
1171:              OdbcDataReader reader = null;
1172:   
1173:              try {
1174:                  MyConnection.Open();
1175:   
1176:                  using (com = new OdbcCommand(sql, MyConnection)) {
1177:   
1178:                      OdbcParameter param1 = new OdbcParameter();
1179:                      param1.DbType = DbType.Int32;
1180:                      param1.Value = userID;
1181:                      com.Parameters.Add(param1);
1182:   
1183:                      OdbcParameter param2 = new OdbcParameter();
1184:                      param2.DbType = DbType.Int32;
1185:                      param2.Value = userID;
1186:                      com.Parameters.Add(param2);
1187:   
1188:   
1189:                      using (reader = com.ExecuteReader()) {
1190:                          while (reader.Read()) {
1191:                              idA = reader.GetInt32(0);
1192:                              idB = reader.GetInt32(1);
1193:                          }
1194:                      }
1195:                  }
1196:              }
1197:              catch (Exception e) {
1198:                  string err = "(Get Suggested Other User) > Mysql > " + e.Message;
1199:              }
1200:              finally {
1201:                  // close reader
1202:                  if (reader != null) reader.Close();
1203:                  // close connection
1204:                  if (MyConnection != null) MyConnection.Close();
1205:              }
1206:   
1207:              if (idA == userID) return idB;
1208:              return idA;
1209:   
1210:          }
1211:   
1212:   
1213:   
1214:   
1215:          /// <summary>
1216:          /// Ritorna un int[]
1217:          /// int[0] = il primo valore è il voto suggetito
1218:          /// int[1] = è chi ha suggetito il voto
1219:          /// int[2] = 1 se si è dovuto spostare il voto ad un altro SuperUtente...
1220:          /// </summary>
1221:          /// <param name="matrix">Arrey voti degli utenti</param>
1222:          /// <param name="matrixScore">Matrice utente/similitudine, maggior valore maggior è uguale all'utente</param>
1223:          /// <returns></returns>
1224:          //public void oldtest() {
1225:   
1226:          //    int[] result = new int[3];
1227:   
1228:          //    Array.Sort(keys, values);
1229:          //    int arreySize = keys.Length;
1230:   
1231:          //    for (int i = arreySize - 1; i > 0; i--) {
1232:   
1233:          //        if (values[i] != 0) {
1234:          //            result[0] = values[i];
1235:          //            result[1] = keys[i];
1236:          //            result[2] = (i == keys.Length) ? 0 : 1;
1237:          //            break;
1238:          //        }
1239:   
1240:          //    }
1241:   
1242:          //    return result;
1243:   
1244:          //}
1245:   
1246:          public class RecommendationData : IComparable {
1247:   
1248:              public int superUserID;
1249:              public int nRightMatch;
1250:              public int vote;
1251:   
1252:              public RecommendationData(int _id, int _nRightMatch, int _vote) {
1253:                  this.superUserID = _id;
1254:                  this.nRightMatch = _nRightMatch;
1255:                  this.vote = _vote;
1256:              }
1257:   
1258:              public int CompareTo(object obj) {
1259:                  // throws invalid cast exception if not of type Donator
1260:                  RecommendationData otherDonator = (RecommendationData)obj;
1261:                  return this.nRightMatch.CompareTo(otherDonator.nRightMatch);
1262:                  throw new NotImplementedException();
1263:              }
1264:   
1265:          }
1266:   
1267:   
1268:          public List<User> loadSuperUserV1() {
1269:   
1270:              //Regione Viola pageRank
1271:   
1272:              List<User> LeaderUser = new List<User>();
1273:   
1274:              LeaderUser.Add(new User(2, "imdb.com", 0, "Rossa", 0, 1, 0));
1275:   
1276:              LeaderUser.Add(new User(144, "lauraw", 0, "Blu", 0, 2, 0));
1277:              LeaderUser.Add(new User(29, "fertad69", 0, "Blu", 0, 2, 0));
1278:   
1279:              LeaderUser.Add(new User(137, "Zac", 0, "Verde", 0, 3, 0));
1280:              LeaderUser.Add(new User(9, "danikoch ", 0, "Verde", 0, 3, 0));
1281:   
1282:              LeaderUser.Add(new User(122, "Bo", 0, "Giallo", 0, 4, 0));
1283:              LeaderUser.Add(new User(43, "matte1", 0, "Giallo", 0, 4, 0));
1284:   
1285:              LeaderUser.Add(new User(12, "Cocc", 0, "Marrone", 0, 5, 0));
1286:              LeaderUser.Add(new User(3, "Baby", 0, "Marrone", 0, 5, 0));
1287:   
1288:              LeaderUser.Add(new User(59, "Francesco", 0, "Arancione", 0, 6, 0));
1289:              LeaderUser.Add(new User(56, "nonsonostatoio", 0, "Arancione", 0, 6, 0));
1290:   
1291:              LeaderUser.Add(new User(41, "la chiamavano jeeg robot", 0, "Giallo", 0, 7, 0));
1292:              LeaderUser.Add(new User(111, "rIO_sK", 0, "Giallo", 0, 7, 0));
1293:   
1294:              return LeaderUser;
1295:   
1296:          }
1297:   
1298:   
1299:          public List<User> loadSuperUserV2() {
1300:   
1301:              //Regione Viola deegree
1302:   
1303:              List<User> LeaderUser = new List<User>();
1304:   
1305:              LeaderUser.Add(new User(2, "imdb.com", 0, "Rossa", 0, 1, 0));
1306:   
1307:              LeaderUser.Add(new User(163, "lauraw", 0, "Blu", 0, 2, 0));
1308:              LeaderUser.Add(new User(144, "fertad69", 0, "Blu", 0, 2, 0));
1309:   
1310:              LeaderUser.Add(new User(122, "Zac", 0, "Verde", 0, 3, 0));
1311:              LeaderUser.Add(new User(13, "danikoch ", 0, "Verde", 0, 3, 0));
1312:   
1313:              LeaderUser.Add(new User(139, "Bo", 0, "Giallo", 0, 4, 0));
1314:              LeaderUser.Add(new User(53, "matte1", 0, "Giallo", 0, 4, 0));
1315:   
1316:              LeaderUser.Add(new User(116, "Cocc", 0, "Marrone", 0, 5, 0));
1317:              LeaderUser.Add(new User(137, "Baby", 0, "Marrone", 0, 5, 0));
1318:   
1319:              LeaderUser.Add(new User(12, "Francesco", 0, "Arancione", 0, 6, 0));
1320:              LeaderUser.Add(new User(35, "nonsonostatoio", 0, "Arancione", 0, 6, 0));
1321:   
1322:              LeaderUser.Add(new User(20, "la chiamavano jeeg robot", 0, "Giallo", 0, 7, 0));
1323:              LeaderUser.Add(new User(59, "rIO_sK", 0, "Giallo", 0, 7, 0));
1324:   
1325:              return LeaderUser;
1326:   
1327:          }
1328:   
1329:   
1330:   
1331:          public List<User> loadSuperUserV3() {
1332:   
1333:              //Regione Azzurro deegree
1334:   
1335:              List<User> LeaderUser = new List<User>();
1336:   
1337:              LeaderUser.Add(new User(1, "imdb.com", 0, "Rossa", 0, 1, 0));
1338:   
1339:              LeaderUser.Add(new User(47, "lauraw", 0, "Blu", 0, 2, 0));
1340:              LeaderUser.Add(new User(52, "fertad69", 0, "Blu", 0, 2, 0));
1341:   
1342:              LeaderUser.Add(new User(51, "Zac", 0, "Verde", 0, 3, 0));
1343:              LeaderUser.Add(new User(70, "danikoch ", 0, "Verde", 0, 3, 0));
1344:   
1345:              LeaderUser.Add(new User(48, "Bo", 0, "Giallo", 0, 4, 0));
1346:              LeaderUser.Add(new User(83, "matte1", 0, "Giallo", 0, 4, 0));
1347:   
1348:              LeaderUser.Add(new User(60, "Cocc", 0, "Marrone", 0, 5, 0));
1349:              LeaderUser.Add(new User(176, "Baby", 0, "Marrone", 0, 5, 0));
1350:   
1351:              LeaderUser.Add(new User(77, "Francesco", 0, "Arancione", 0, 6, 0));
1352:              LeaderUser.Add(new User(81, "nonsonostatoio", 0, "Arancione", 0, 6, 0));
1353:   
1354:              LeaderUser.Add(new User(138, "la chiamavano jeeg robot", 0, "Giallo", 0, 7, 0));
1355:              LeaderUser.Add(new User(89, "rIO_sK", 0, "Giallo", 0, 7, 0));
1356:   
1357:              return LeaderUser;
1358:   
1359:          }
1360:   
1361:   
1362:          public List<User> loadSuperUserV4() {
1363:   
1364:              //Regione Azzurro pagerack
1365:   
1366:              List<User> LeaderUser = new List<User>();
1367:   
1368:              LeaderUser.Add(new User(1, "imdb.com", 0, "Rossa", 0, 1, 0));
1369:   
1370:              LeaderUser.Add(new User(47, "lauraw", 0, "Blu", 0, 2, 0));
1371:              LeaderUser.Add(new User(81, "fertad69", 0, "Blu", 0, 2, 0));
1372:   
1373:              LeaderUser.Add(new User(51, "Zac", 0, "Verde", 0, 3, 0));
1374:              LeaderUser.Add(new User(176, "danikoch ", 0, "Verde", 0, 3, 0));
1375:   
1376:              LeaderUser.Add(new User(48, "Bo", 0, "Giallo", 0, 4, 0));
1377:              LeaderUser.Add(new User(83, "matte1", 0, "Giallo", 0, 4, 0));
1378:   
1379:              LeaderUser.Add(new User(77, "Cocc", 0, "Marrone", 0, 5, 0));
1380:              LeaderUser.Add(new User(70, "Baby", 0, "Marrone", 0, 5, 0));
1381:   
1382:              LeaderUser.Add(new User(138, "Francesco", 0, "Arancione", 0, 6, 0));
1383:              LeaderUser.Add(new User(52, "nonsonostatoio", 0, "Arancione", 0, 6, 0));
1384:   
1385:              LeaderUser.Add(new User(89, "la chiamavano jeeg robot", 0, "Giallo", 0, 7, 0));
1386:              LeaderUser.Add(new User(5, "rIO_sK", 0, "Giallo", 0, 7, 0));
1387:   
1388:              return LeaderUser;
1389:   
1390:          }
1391:   
1392:   
1393:          public List<User> loadSuperUserV5() {
1394:   
1395:              //Regione Arancione Degree
1396:   
1397:              List<User> LeaderUser = new List<User>();
1398:   
1399:              LeaderUser.Add(new User(172, "imdb.com", 0, "Rossa", 0, 1, 0));
1400:   
1401:              LeaderUser.Add(new User(126, "lauraw", 0, "Blu", 0, 2, 0));
1402:              LeaderUser.Add(new User(175, "fertad69", 0, "Blu", 0, 2, 0));
1403:   
1404:              LeaderUser.Add(new User(150, "Zac", 0, "Verde", 0, 3, 0));
1405:              LeaderUser.Add(new User(14, "danikoch ", 0, "Verde", 0, 3, 0));
1406:   
1407:              LeaderUser.Add(new User(71, "Bo", 0, "Giallo", 0, 4, 0));
1408:              LeaderUser.Add(new User(145, "matte1", 0, "Giallo", 0, 4, 0));
1409:   
1410:              LeaderUser.Add(new User(15, "Cocc", 0, "Marrone", 0, 5, 0));
1411:              LeaderUser.Add(new User(58, "Baby", 0, "Marrone", 0, 5, 0));
1412:   
1413:              LeaderUser.Add(new User(151, "Francesco", 0, "Arancione", 0, 6, 0));
1414:              LeaderUser.Add(new User(130, "nonsonostatoio", 0, "Arancione", 0, 6, 0));
1415:   
1416:              LeaderUser.Add(new User(100, "la chiamavano jeeg robot", 0, "Giallo", 0, 7, 0));
1417:              LeaderUser.Add(new User(98, "rIO_sK", 0, "Giallo", 0, 7, 0));
1418:   
1419:              return LeaderUser;
1420:   
1421:          }
1422:   
1423:          public List<User> loadSuperUserV6() {
1424:   
1425:              //Regione Verde pagerack
1426:   
1427:              List<User> LeaderUser = new List<User>();
1428:   
1429:              LeaderUser.Add(new User(172, "imdb.com", 0, "Rossa", 0, 1, 0));
1430:   
1431:              LeaderUser.Add(new User(150, "lauraw", 0, "Blu", 0, 2, 0));
1432:              LeaderUser.Add(new User(100, "fertad69", 0, "Blu", 0, 2, 0));
1433:   
1434:              LeaderUser.Add(new User(126, "Zac", 0, "Verde", 0, 3, 0));
1435:              LeaderUser.Add(new User(14, "danikoch ", 0, "Verde", 0, 3, 0));
1436:   
1437:              LeaderUser.Add(new User(71, "Bo", 0, "Giallo", 0, 4, 0));
1438:              LeaderUser.Add(new User(175, "matte1", 0, "Giallo", 0, 4, 0));
1439:   
1440:              LeaderUser.Add(new User(15, "Cocc", 0, "Marrone", 0, 5, 0));
1441:              LeaderUser.Add(new User(58, "Baby", 0, "Marrone", 0, 5, 0));
1442:   
1443:              LeaderUser.Add(new User(151, "Francesco", 0, "Arancione", 0, 6, 0));
1444:              LeaderUser.Add(new User(130, "nonsonostatoio", 0, "Arancione", 0, 6, 0));
1445:   
1446:              LeaderUser.Add(new User(145, "la chiamavano jeeg robot", 0, "Giallo", 0, 7, 0));
1447:              LeaderUser.Add(new User(98, "rIO_sK", 0, "Giallo", 0, 7, 0));
1448:   
1449:              return LeaderUser;
1450:   
1451:          }
1452:   
1453:   
1454:   
1455:   
1456:          public List<User> loadSuperUserV0() {
1457:   
1458:              List<User> LeaderUser = new List<User>();
1459:   
1460:              LeaderUser.Add(new User(172, "imdb.com", 0, "Rossa", 0, 1, 0));
1461:   
1462:              LeaderUser.Add(new User(36, "lauraw", 0, "Blu", 0, 2, 0));
1463:              LeaderUser.Add(new User(161, "fertad69", 0, "Blu", 0, 2, 0));
1464:   
1465:              LeaderUser.Add(new User(122, "Zac", 0, "Verde", 0, 3, 0));
1466:              LeaderUser.Add(new User(12, "danikoch ", 0, "Verde", 0, 3, 0));
1467:   
1468:              LeaderUser.Add(new User(76, "Bo", 0, "Giallo", 0, 4, 0));
1469:              LeaderUser.Add(new User(115, "matte1", 0, "Giallo", 0, 4, 0));
1470:   
1471:              LeaderUser.Add(new User(10, "Cocc", 0, "Marrone", 0, 5, 0));
1472:              LeaderUser.Add(new User(22, "Baby", 0, "Marrone", 0, 5, 0));
1473:   
1474:              LeaderUser.Add(new User(1, "Francesco", 0, "Arancione", 0, 6, 0));
1475:              LeaderUser.Add(new User(67, "nonsonostatoio", 0, "Arancione", 0, 6, 0));
1476:   
1477:              LeaderUser.Add(new User(164, "la chiamavano jeeg robot", 0, "Giallo", 0, 7, 0));
1478:              LeaderUser.Add(new User(61, "rIO_sK", 0, "Giallo", 0, 7, 0));
1479:   
1480:              return LeaderUser;
1481:   
1482:          }
1483:   
1484:   
1485:      }
1486:   
1487:   
1488:   
1489:   
1490:  }
1491:   
1492: