<commit_before>package ecologylab.bigsemantics.tools;

import java.awt.Desktop;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author quyin
 */
public class AssistApp extends WindowAdapter
{

  static Logger logger        = LoggerFactory.getLogger(AssistApp.class);

  static int    LOW_MEM_IN_MB = 64;

  private File  bsWrappersDir;

  private File  bsServiceDir;

  private File  bsJsDir;

  JButton       btnUpdate;

  JTextArea     textArea;

  JFrame        frame;

  AntRunner     antRunner;

  BSService     service;

  public AssistApp(Configuration configs)
  {
    createAndDisplayGUI();
    info("Initializing...");

    while (bsWrappersDir == null)
    {
      bsWrappersDir = PathUtil.checkAndChooseDir(frame,
                                                 configs.getString("bigsemantics_wrappers_dir"),
                                                 "BigSemanticsWrappers directory");
    }

    while (bsServiceDir == null)
    {
      bsServiceDir = PathUtil.checkAndChooseDir(frame,
                                                configs.getString("bigsemantics_service_dir"),
                                                "BigSemanticsService project directory");
    }

    while (bsJsDir == null)
    {
      bsJsDir = PathUtil.checkAndChooseDir(frame,
                                           configs.getString("bigsemantics_javascript_dir"),
                                           "BigSemanticsJavaScript project directory");
    }

    antRunner = new AntRunner();
    service = new BSService(configs);

    btnUpdate.setEnabled(false);
    info("Beginning service startup.");
    Thread thread = new Thread(new Runnable()
    {
      @Override
      public void run()
      {
        updateBackend();
        SwingUtilities.invokeLater(new Runnable()
        {
          @Override
          public void run()
          {
            btnUpdate.setEnabled(true);
          }
        });
      }
    });
    thread.run();

    btnUpdate.setEnabled(true);
  }

  public void createAndDisplayGUI()
  {
    JPanel contentPanel = new JPanel(new GridBagLayout());
    contentPanel.setOpaque(true);

    btnUpdate = new JButton("Update Backend with New Wrappers");
    btnUpdate.setEnabled(false);
    btnUpdate.setVerticalTextPosition(AbstractButton.CENTER);
    btnUpdate.setHorizontalTextPosition(AbstractButton.CENTER);
    btnUpdate.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent event)
      {
        btnUpdate.setEnabled(false);
        Thread thread = new Thread(new Runnable() {
          @Override
          public void run()
          {
            updateBackend();
            SwingUtilities.invokeLater(new Runnable()
            {
              @Override
              public void run()<commit_msg>AssistApp now warns user when low on memory<commit_after>package ecologylab.bigsemantics.tools;

import java.awt.Desktop;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author quyin
 */
public class AssistApp extends WindowAdapter
{

  static Logger logger        = LoggerFactory.getLogger(AssistApp.class);

  static int    LOW_MEM_IN_MB = 300;

  private File  bsWrappersDir;

  private File  bsServiceDir;

  private File  bsJsDir;

  JButton       btnUpdate;

  JTextArea     textArea;

  JFrame        frame;

  AntRunner     antRunner;

  BSService     service;

  public AssistApp(Configuration configs)
  {
    createAndDisplayGUI();
    info("Initializing...");

    while (bsWrappersDir == null)
    {
      bsWrappersDir = PathUtil.checkAndChooseDir(frame,
                                                 configs.getString("bigsemantics_wrappers_dir"),
                                                 "BigSemanticsWrappers directory");
    }

    while (bsServiceDir == null)
    {
      bsServiceDir = PathUtil.checkAndChooseDir(frame,
                                                configs.getString("bigsemantics_service_dir"),
                                                "BigSemanticsService project directory");
    }

    while (bsJsDir == null)
    {
      bsJsDir = PathUtil.checkAndChooseDir(frame,
                                           configs.getString("bigsemantics_javascript_dir"),
                                           "BigSemanticsJavaScript project directory");
    }

    antRunner = new AntRunner();
    service = new BSService(configs);

    btnUpdate.setEnabled(false);
    info("Beginning service startup.");
    Thread thread = new Thread(new Runnable()
    {
      @Override
      public void run()
      {
        updateBackend();
        SwingUtilities.invokeLater(new Runnable()
        {
          @Override
          public void run()
          {
            btnUpdate.setEnabled(true);
          }
        });
      
        while(true){
        	if(MemoryUsageMonitor.get().getEffectiveFreeMem() <= AssistApp.LOW_MEM_IN_MB){
        		Object[] options = {"Close now",
                        "Keep running"};
			    Integer n = JOptionPane.showOptionDialog(frame,
			        "Low memory, close AssistApp before recompiling wrappers",
			        "Memory Warning",
			        JOptionPane.YES_NO_CANCEL_OPTION,
			        JOptionPane.QUESTION_MESSAGE,
			        null,
			        options,
			        options[1]);
			    //System.out.println("Choice Number: " + n.toString());
			    if(n == 0){
			    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
			    	try
			         {
			           stopService();
			         }
			         catch (Exception e)
			         {
			           error("Error stopping BS service.", null, e);
			         }
			    	System.exit(0);
			    }
			    else{
			    	break;
			    }
			        	}
        }
        
      }
    });
    thread.run();

    btnUpdate.setEnabled(true);
  }

  public void createAndDisplayGUI()
  {
    JPanel contentPanel = new JPanel(new GridBagLayout());
    contentPanel.setOpaque(true);

    btnUpdate = new JButton("Update Backend with New Wrappers");
    btnUpdate.setEnabled(false);
    btnUpdate.setVerticalTextPosition(AbstractButton.CENTER);
    btnUpdate.setHorizontalTextPosition(AbstractButton.CENTER);
    btnUpdate.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent event)
      {
        btnUpdate.setEnabled(false);

        MemoryUsageMonitor.get().log("after running for a bit");
        if(MemoryUsageMonitor.get().getEffectiveFreeMem() <= AssistApp.LOW_MEM_IN_MB){
        	JOptionPane.showMessageDialog(frame,
        		    "Not enough memory. Closing down.",
        		    "Memory Warning",
        		    JOptionPane.ERROR_MESSAGE);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	    	try
	         {
	           stopService();
	         }
	         catch (Exception e)
	         {
	           error("Error stopping BS service.", null, e);
	         }
	    	System.exit(0);
        	
        }
        
        Thread thread = new Thread(new Runnable() {
          @Override
          public void run()
          {
            updateBackend();
            SwingUtilities.invokeLater(new Runnable()
            {
              @Override
              public void run()
{'fraction_non_alphanumeric': 0.09038591740013541, 'fraction_numerical': 0.001466937485894832, 'mean_word_length': 2.186983099604459, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2892089', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2373, 'n_words': 465}