/* * Class MdpPuzzleGenerator */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.Calendar; /**
Direct Known Subclasses:
MdpPuzzleGenerator.Canvas

* MdpPuzzleGenerator generates MDP-type problem input files to be used by the PosgInputFileParser object * @author Mark Gruman -- UMass RBR Lab
* Computer Science Department
* University of Massachusetts Amherst
* @version 1.0
* Created on June 28, 2004

*/ public class MdpPuzzleGenerator { private Canvas m_canvas; private String file_name; private BufferedWriter m_log_writer; private BufferedReader m_log_reader; private int m_current_screen; private PosgInputFileParser m_parser; private String[] temp_actions; private String[] temp_observs; private String[] temp_states; private int temp_agents; private String[] m_actions; private String[] m_observs; /** The main method creates a new instance of the MdpPuzzleGenerator class. */ public static void main(String[] args) { MdpPuzzleGenerator pg = new MdpPuzzleGenerator(); pg.getCanvas().start(); } /** Constructor creates a new canvas */ private MdpPuzzleGenerator() { m_canvas = new Canvas(); } /** This method returns the associated PosgInputFileParser object * @return the corresponding PosgInputFileParser */ public PosgInputFileParser getParser() { return m_parser; } /** This method returns the associated Canvas object * @return the corresponding Canvas */ private Canvas getCanvas() { return m_canvas; } /** This method writes the contents of a JTextField to the corresponding text file * @param c a set of given components in which the output data is entailed * @param i an index number corresponding to the component that comtains the output text * @param s a textual prefix to be written out with its corresponding output text */ private void outputTextField(Component[] c, int i, String s) { try { m_log_writer.write(s + ((JTextField)c[i+1]).getText()); m_log_writer.newLine(); } catch (IOException ioe) { } } /** This method parses the user's list of given parameters into an array of Strings * @param param a flag differentiating between actions and observations * @param values the list of given parameters as provided by the user */ private void setupParams(String param, String values) { String symbol; if (param.startsWith("ALL A")) symbol = "a"; else symbol = "o"; String res = "-- ALL "; try { int num_params = Integer.parseInt(values); for (int i=0; i 0) ) return null; } String res = "\r\n"; switch (b) { case 1: res += "T: "; break; case 11: res += "O: "; break; case 21: res += "R: "; break; } return probOutputHelper(c, 0, values, res, b, flag); } /** This recursive helper method generates properly-formatted text of probabilities of each agent * @param c a set of given components in which the output data is entailed * @param i the starting index of components to be processed * @param temp a matrix that holds all specified actions and observations of all agents * @param res the resulting String of output text * @param offset an integer specifying the appropriate component containing the output data * @param empty a flag indicating whether all fields have been explicitly specified or not * @return the appropriately-formatted text to be written to a corresponding output file */ private String probOutputHelper(Component[] c, int i, Object[][] temp, String res, int offset, int empty) { if (i == temp.length) { if (empty > 0) res += "\r\n"; return (res + ((JTextArea)c[i+offset]).getText() + "\r\n"); } else if (i < temp.length) { if (((String)temp[i][0]).equals("ALL")) return (probOutputHelper(c, i+1, temp, res + "* ", offset, empty)); else if (!(((String)temp[i][0]).equals("--"))) { String s = ""; for (int j=0; j 0) insert(m_screen3, e_head, x_e + insets.left, y + insets.top, size_s.width, size_s.height); if (x_o > 0) insert(m_screen3, o_head, x_o + insets.left, y + insets.top, size_o.width, size_o.height); insert(m_screen3, p_head, x_p + insets.left, y + insets.top, size_p.width, size_p.height); y_offset += temp_y; } JButton finished = new JButton("Accept"); finished.addActionListener(this); Dimension size_f = finished.getPreferredSize(); insert(m_screen3, finished, tot_size.width/2 - size_f.width/2, tot_size.height, size_f.width, size_f.height); tot_size.height += size_f.height + 25; m_screen3.setPreferredSize(tot_size); } /** This method creates the last screen for the MdpPuzzleGenerator * This screen allows the user to terminate the program or create a new Mdp Puzzle */ private void createEndScreen() { m_screen4 = new JPanel(); m_screen4.setLayout(null); Insets insets = m_screen4.getInsets(); int x_offset = 25; int y_offset = 25; Dimension tot_size = new Dimension(10, 10); JLabel message = new JLabel("Your file has been successfully saved under file name: " + file_name); Dimension message_size = message.getPreferredSize(); insert(m_screen4, message, x_offset, y_offset, message_size.width, message_size.height); y_offset += message_size.height + 10; JButton finish = new JButton("End Program"); finish.addActionListener(this); Dimension f_size = finish.getPreferredSize(); insert(m_screen4, finish, x_offset, y_offset, f_size.width, f_size.height); JButton create = new JButton("Create New Problem"); create.addActionListener(this); Dimension r_size = create.getPreferredSize(); insert(m_screen4, create, x_offset*2 + f_size.width, y_offset, r_size.width, r_size.height); tot_size.height = y_offset + f_size.height + 25; tot_size.width = Math.max(2*x_offset + message_size.width, 3*x_offset + f_size.width + r_size.width); m_screen4.setPreferredSize(tot_size); } /** This is a helper method designed to create and insert JLists into a JPanel * @param s the pre-defined list of values that will be maintained in the JList * @param jp the corresponding JPanel to which the JList will be added * @param x_offset the starting X-coordinate of the JList to be added * @param y_offset the starting Y-coordinate of the JList to be added * @return the coordinates available to the next component */ private Dimension insertList(String[] s, JPanel jp, int x_offset, int y_offset) { Insets insets = jp.getInsets(); JList l = new JList(s); l.setSelectedIndex(0); l.setVisibleRowCount(3); JScrollPane scrollPane = new JScrollPane(l); Dimension size = scrollPane.getPreferredSize(); insert(jp, scrollPane, x_offset + insets.left, y_offset + insets.top, size.width, size.height); return (new Dimension(x_offset + size.width + 25, y_offset + size.height + 10)); } /** This is a helper method designed to insert JComponents into a JPanel * @param jc the pre-defined JComponent that will be added to the JPanel * @param jp the corresponding JPanel to which the JComponent will be added * @param x the starting X-coordinate of the JComponent to be added * @param y the starting Y-coordinate of the JComponent to be added * @param w the width of the JComponent to be added * @param h the height of the JComponent to be added */ private void insert(JPanel jp, JComponent jc, int x, int y, int w, int h) { jc.setBounds(x, y, w, h); jp.add(jc); } /** This method initializes a new thread whose purpose is to display the Canvas */ public void run() { m_frame.repaint(); } } }