GroupOfOccurrences.java

72 lines | 2.124 kB Blame History Raw Download
package br.ufrgs.inf.prosoft.tfcache.metadata;

import br.ufrgs.inf.prosoft.tfcache.Metrics;
import br.ufrgs.inf.prosoft.tfcache.Pareto;
import br.ufrgs.inf.prosoft.tfcache.Simulator;
import br.ufrgs.inf.prosoft.tfcache.configuration.Configuration;

import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

public class GroupOfOccurrences {

  private static final Logger LOGGER = Logger.getLogger(GroupOfOccurrences.class.getName());
  private final List<Occurrence> occurrences;
  private final String parameters;
  private Pareto pareto;

  public GroupOfOccurrences(String parameters, List<Occurrence> occurrences) {
    this.parameters = parameters;
    this.occurrences = occurrences;
  }

  public String getParameters() {
    return this.parameters;
  }

  public Metrics getBestMetrics() {
    if (this.pareto == null) {
      throw new RuntimeException("simulation must be executed");
    }
    return this.pareto.getBestMetrics();
  }

  public Stream<Occurrence> occurrences() {
    return this.occurrences.stream();
  }

  public boolean isChangeable() {
    Iterator<Occurrence> iterator = this.occurrences.iterator();
    Occurrence first = iterator.next();
    while (iterator.hasNext()) {
      Occurrence next = iterator.next();
      if (!Objects.deepEquals(first.getReturnValue(), next.getReturnValue())) {
        return true;
      }
    }
    return false;
  }

  public void calculateMetrics() {
    if (this.pareto != null) {
      LOGGER.log(Level.WARNING, "metrics already calculated");
    }
    if (this.occurrences.size() < 2) {
      throw new RuntimeException("Not reusable input");
    }
    this.occurrences.sort(Comparator.comparingLong(Occurrence::getStartTime));
    this.pareto = new Pareto();
    Simulator.simulate(this.occurrences, this.pareto);
    if (Configuration.getVerbose()) {
      this.pareto.values().forEach(it ->
        System.out.println(Configuration.getInput() + "," + it.getTtl() + "," + it.getSavedTime() + "," + it.getHits() + "," + it.getTimeInCache()));
    }
  }

}