Пример #1
0
  public synchronized void load() throws SQLException {
    // And calculate
    Connection connection = getDb();
    Statement stmt = connection.createStatement();
    ResultSet scoreSet = stmt.executeQuery("SELECT hold,division,land from klubber");
    while (scoreSet.next()) {
      // Fetch info
      String land = scoreSet.getString("land");
      String hold = scoreSet.getString("hold");
      int division = scoreSet.getInt("division");
      double rating = Setup.pointStart / (Math.pow(Setup.divisionsforskel, division));
      Team team = new Team(hold, land, division, rating, Setup.matchesToStore);
      teams.addTeam(team);
    }
    stmt.close();
    // Load the matches
    stmt = connection.createStatement();
    String matchesSQL = "SELECT * FROM kampe order by dato asc";
    ResultSet r = stmt.executeQuery(matchesSQL);
    while (r.next()) {
      try {
        // Fetch info
        String country = r.getString("land");
        Team away = teams.get(country, r.getString("udehold"));
        int division = r.getInt("division");
        int goalsAway = r.getInt("udemaal");
        int goalsHome = r.getInt("hjemmemaal");
        String seasonStartYear = r.getString("season_start_year");
        Team home = teams.get(country, r.getString("hjemmehold"));
        int round = r.getInt("runde");
        Kamp match = new Kamp(home, away, goalsHome, goalsAway, round, seasonStartYear);
        // Find result
        Udfald y = null;
        if (goalsHome > goalsAway) y = Udfald.HJEMME;
        else if (goalsHome == goalsAway) y = Udfald.UAFGJORT;
        else y = Udfald.UDE;
        double[] features = getKamp(home, away, country);
        DataEntry matchInfo = new DataEntry(features, y);
        matchInfo.addExtra("season_start_year", seasonStartYear);
        String key = getDataKey(country, division + "");
        synchronized (this) {
          LinkedList<DataEntry> matchData = dataset.get(key);
          if (matchData == null) {
            matchData = new LinkedList<DataEntry>();
            dataset.put(key, matchData);
          }
          // If enough matches and so on, update
          if (home.totalplayed >= Setup.TRAINING_ROUNDS
              && away.totalplayed >= Setup.TRAINING_ROUNDS // Total played at least
              && round >= Setup.antalRunder // Total played in start of each season
          ) {
            matchData.add(matchInfo);
          }
        }

        // And update a lot of information
        // Skal altid ske. Til træning af klubbernes point
        teams.opdaterPoint(home, away, goalsHome, goalsAway, division);

        // The rest
        home.addLatestHomeMatch(match);
        home.addLatestMatch(match);
        away.addLatestAwayMatch(match);
        away.addLatestMatch(match);
        home.addInternal(match);
        away.addInternal(match);
        home.totalplayed++;
        away.totalplayed++;
      } catch (Exception e) {
        Log.e(e);
      }
    }
    r.close();
    stmt.close();

    // Filter out noise if needed
    if (Setup.FILTER_DATA) {
      for (String key : dataset.keySet()) {
        LinkedList<DataEntry> matches = dataset.get(key);
        Setup.FILTER_HANDLER.applyFilters(matches);
      }
    }
    // Prepare normalizer
    for (String key : dataset.keySet()) {
      Normalizer normalizer = new Normalizer(dataset.get(key), Normalizer.Method.NORMALIZATION);
      normalizers.put(key, normalizer);
      if (Setup.NORMALIZE_DATA) normalizer.apply(dataset.get(key));
    }
  }