<commit_before>package wcanalysis.heuristic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import gov.nasa.jpf.Config;
import gov.nasa.jpf.JPF;
import gov.nasa.jpf.report.Publisher;
import gov.nasa.jpf.report.Reporter;
import gov.nasa.jpf.report.Statistics;
import gov.nasa.jpf.symbc.numeric.PathCondition;
import wcanalysis.heuristic.model.State;
import wcanalysis.heuristic.util.OmegaConverter;
import wcanalysis.heuristic.util.SMTLibConverter;

/**
 * @author <NAME>
 */
public abstract class ResultsPublisher extends Publisher {

  private Logger logger = JPF.getLogger(ResultsPublisher.class.getName());
  
  private File file;
  private FileOutputStream fos;
  private int cfgInputSize;
  
  private File baseDir;
  private boolean generateSMTLibFormat = false;
  private boolean generateOmegaFormat = false;
  
  public static final String CFG_INPUT_SIZE_CONF = "report.console.wc.cfginputsize";
  
  public static final String SMTLIB_CONF = "report.console.wc.smtlib";
  public static final String OMEGA_CONF = "report.console.wc.omega";
  
  private Config conf;
  public ResultsPublisher(Config conf, Reporter reporter) {
    super(conf, reporter);
    this.conf = conf;
    this.cfgInputSize = conf.getInt(CFG_INPUT_SIZE_CONF, -1);
    String resultsFile = conf.getString("target") + ".csv";

    this.generateSMTLibFormat = conf.getBoolean(SMTLIB_CONF, false);
    this.generateOmegaFormat = conf.getBoolean(OMEGA_CONF, false);
    baseDir = getResultsDir(conf);
    
    file = new File(baseDir, resultsFile);
  }

  @Override
  public String getName() {
    return "console";
  }

  @Override
  protected void openChannel(){

    if(file != null) {
      try {        
        fos = new FileOutputStream(file, true);
        out = new PrintWriter(fos);
      } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
      }
    }
  }

  @Override
  protected void closeChannel() {
    if (fos != null){
      out.close();
    }
  }

  @Override
  public void publishStart() {
    super.publishStart();
  }

  @Override
  public void publishFinished() {
    super.publishFinished();
  }

  public synchronized void printStatistics (PrintWriter pw){

  }

  @Override
  public void publishStatistics() {

  }
  
  private static boolean isFileEmpty(File file) {
    try(BufferedReader br = new BufferedReader(new FileReader(file))) {
        if(br.readLine() == null) {
          return true;
        } else
          return false;
    } catch (IOException e1) {
      e1.printStackTrace();
      return false;
    }
  }
  
  @Override
  public void publishResult() {
    PathListener pathListener = getListener();
    if(pathListener != null) {
      
      Statistics stat = reporter.getStatistics();
      
      State wcState = pathListener.getWcPath().getWCState();
      if(isFileEmpty(file)) {
        //write header
        
        String stateCSVHeader = wcState.getCSVHeader();
        if(!stateCSVHeader.endsWith(","))
          stateCSVHeader += ",";
        String header = "inputSize," + 
            ((this.cfgInputSize >= 0) ? "cfgInputSize," : "") + 
            stateCSVHeader +
            "analysisTime,mem,paths," +
            getCustomCSVColumnHeader() +
            ",wcConstraint";
        
        out.println(header);
      }<commit_msg>Add additional null pointer check<commit_after>package wcanalysis.heuristic;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import gov.nasa.jpf.Config;
import gov.nasa.jpf.JPF;
import gov.nasa.jpf.report.Publisher;
import gov.nasa.jpf.report.Reporter;
import gov.nasa.jpf.report.Statistics;
import gov.nasa.jpf.symbc.numeric.PathCondition;
import wcanalysis.heuristic.model.State;
import wcanalysis.heuristic.util.OmegaConverter;
import wcanalysis.heuristic.util.SMTLibConverter;

/**
 * @author <NAME>
 */
public abstract class ResultsPublisher extends Publisher {

  private Logger logger = JPF.getLogger(ResultsPublisher.class.getName());
  
  private File file;
  private FileOutputStream fos;
  private int cfgInputSize;
  
  private File baseDir;
  private boolean generateSMTLibFormat = false;
  private boolean generateOmegaFormat = false;
  
  public static final String CFG_INPUT_SIZE_CONF = "report.console.wc.cfginputsize";
  
  public static final String SMTLIB_CONF = "report.console.wc.smtlib";
  public static final String OMEGA_CONF = "report.console.wc.omega";
  
  private Config conf;
  public ResultsPublisher(Config conf, Reporter reporter) {
    super(conf, reporter);
    this.conf = conf;
    this.cfgInputSize = conf.getInt(CFG_INPUT_SIZE_CONF, -1);
    String resultsFile = conf.getString("target") + ".csv";

    this.generateSMTLibFormat = conf.getBoolean(SMTLIB_CONF, false);
    this.generateOmegaFormat = conf.getBoolean(OMEGA_CONF, false);
    baseDir = getResultsDir(conf);
    
    file = new File(baseDir, resultsFile);
  }

  @Override
  public String getName() {
    return "console";
  }

  @Override
  protected void openChannel(){

    if(file != null) {
      try {        
        fos = new FileOutputStream(file, true);
        out = new PrintWriter(fos);
      } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
      }
    }
  }

  @Override
  protected void closeChannel() {
    if (fos != null){
      out.close();
    }
  }

  @Override
  public void publishStart() {
    super.publishStart();
  }

  @Override
  public void publishFinished() {
    super.publishFinished();
  }

  public synchronized void printStatistics (PrintWriter pw){

  }

  @Override
  public void publishStatistics() {

  }
  
  private static boolean isFileEmpty(File file) {
    try(BufferedReader br = new BufferedReader(new FileReader(file))) {
        if(br.readLine() == null) {
          return true;
        } else
          return false;
    } catch (IOException e1) {
      e1.printStackTrace();
      return false;
    }
  }
  
  @Override
  public void publishResult() {
    PathListener pathListener = getListener();
    if(pathListener != null) {
      
      Statistics stat = reporter.getStatistics();
      
      WorstCasePath wcPath = pathListener.getWcPath();
      if(wcPath == null)
        return;
      State wcState = wcPath.getWCState();
      if(isFileEmpty(file)) {
        //write header
        
        String stateCSVHeader = wcState.getCSVHeader();
        if(!stateCSVHeader.endsWith(","))
          stateCSVHeader += ",";
        String header = "inputSize," + 
            ((this.cfgInputSize >= 0) ? "cfgInputSize," : "") + 
            stateCSVHeader +
            "analysisTime,mem,paths," +
            getCustomCSVColumnHeader() +
            ",wcConstraint";
        
        out.println(header);
      }
{'fraction_non_alphanumeric': 0.10782389231632081, 'fraction_numerical': 0.0011217049915872126, 'mean_word_length': 3.0597609561752988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6883443', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2145, 'n_words': 470}