Loading...
Searching...
No Matches
InterruptGUI.java
1package com.gams.examples.interrupt;
2
3import java.awt.ComponentOrientation;
4import java.awt.Dimension;
5import java.awt.Font;
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.io.File;
12import java.io.IOException;
13import java.io.OutputStream;
14import java.io.PrintStream;
15
16import javax.swing.JButton;
17import javax.swing.JFrame;
18import javax.swing.JLabel;
19import javax.swing.JScrollPane;
20import javax.swing.JTextArea;
21import javax.swing.SwingUtilities;
22
23import com.gams.api.GAMSJob;
24import com.gams.api.GAMSOptions;
27
35public class InterruptGUI extends JFrame implements ActionListener {
36 private final GridBagConstraints constraints;
37 private final JLabel headsLabel;
38 private final JTextArea logTextArea;
39 private final JScrollPane logScrollPanel;
40 private final JButton runButton, stopButton, exitButton;
41 private final GAMSWorkspace ws;
42 private final GAMSJob job;
43 private final GAMSOptions opt;
44 private final PrintStream printStream;
45 private Worker worker;
46
47 public static void main(String[] args) {
48 SwingUtilities.invokeLater(new Runnable() {
49 public void run() {
50 new InterruptGUI();
51 }
52 });
53 }
54
55 public InterruptGUI() {
56 super("GAMS Java API - Interrupt GUI Example");
57 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
58
59 getContentPane().setLayout(new GridBagLayout());
60 getContentPane().setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
61
62 constraints = new GridBagConstraints();
63 constraints.insets = new Insets(5, 10, 5, 10);
64 constraints.anchor = GridBagConstraints.NORTHWEST;
65 constraints.weightx = 1;
66
67 // Create a label
68 headsLabel = new JLabel("GAMS Log -- running [circpack] model from GAMS Model Library");
69 constraints.fill = GridBagConstraints.HORIZONTAL;
70 constraints.gridx = 0;
71 constraints.gridy = 0;
72 getContentPane().add(headsLabel, constraints);
73
74 // Create a text area to display running job's log
75 logTextArea = new JTextArea(40, 140);
76 logTextArea.setLineWrap(true);
77 logTextArea.setWrapStyleWord(true);
78 logTextArea.setEditable(false);
79 Font font = new Font( "Monospaced", Font.PLAIN, 12 );
80 logTextArea.setFont( font );
81
82 // Create a scrollable Text Panel to output the running job's log
83 printStream = new PrintStream(new LogOutputStream(logTextArea));
84 System.setOut(printStream);
85 System.setErr(printStream);
86
87 logScrollPanel = new JScrollPane(logTextArea);
88 logScrollPanel.setMinimumSize(new Dimension(800, 500));
89 logScrollPanel.setAutoscrolls(true);
90 constraints.fill = GridBagConstraints.HORIZONTAL;
91 constraints.gridx = 0;
92 constraints.gridy = 1;
93 constraints.gridwidth = 4;
94 getContentPane().add(logScrollPanel, constraints);
95
96 // Create buttons
97 runButton = makeButton("Run");
98 constraints.fill = GridBagConstraints.HORIZONTAL;
99 constraints.gridx = 0;
100 constraints.gridy = 2;
101 constraints.gridwidth = 1;
102 getContentPane().add(runButton, constraints);
103
104 stopButton = makeButton("Stop");
105 stopButton.setEnabled(false);
106 constraints.fill = GridBagConstraints.HORIZONTAL;
107 constraints.gridx = 2;
108 constraints.gridy = 2;
109 constraints.gridwidth = 1;
110 getContentPane().add(stopButton, constraints);
111
112 exitButton = makeButton("Exit");
113 constraints.fill = GridBagConstraints.HORIZONTAL;
114 constraints.gridx = 3;
115 constraints.gridy = 2;
116 constraints.gridwidth = 1;
117 getContentPane().add(exitButton, constraints);
118
119 // initialize GAMS Workspace and Job
121 File workingDirectory = new File(System.getProperty("user.dir"), "InterruptGUI");
122 workingDirectory.mkdir();
123 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
124 ws = new GAMSWorkspace(wsInfo);
125
126 // Use a MIP that needs some time to solve
127 job = ws.addJobFromGamsLib("circpack");
128 opt = ws.addOptions();
129 opt.setAllModelTypes("scip");
130
131 // Display the window
132 pack();
133 setVisible(true);
134 }
135
136 private JButton makeButton(String caption) {
137 JButton b = new JButton(caption);
138 b.setActionCommand(caption);
139 b.addActionListener(this);
140 getContentPane().add(b, constraints);
141 return b;
142 }
143
144 @Override
145 public void actionPerformed(ActionEvent e) {
146
147 if ("Run" == e.getActionCommand()) {
148 runButton.setEnabled(false);
149 stopButton.setEnabled(true);
150 if ((worker == null) || (worker.getState()==Thread.State.TERMINATED))
151 worker = new Worker(job, opt, printStream, this);
152 worker.start();
153 } else if ("Stop" == e.getActionCommand()) {
154 runButton.setEnabled(true);
155 stopButton.setEnabled(false);
156 worker.interrupt();
157 } else if ("Exit" == e.getActionCommand()) {
158 System.out.println("Closing Interrupt GUI Example...");
159 if (worker != null) {
160 if (!worker.interrupted || (worker.getState() != Thread.State.TERMINATED))
161 System.exit(-1);
162 }
163 System.exit(0);
164 }
165 }
166
167 private void reportJobTerminated() {
168 runButton.setEnabled(true);
169 stopButton.setEnabled(false);
170 }
171
173 static class Worker extends Thread {
174 GAMSJob job;
175 GAMSOptions option;
176 PrintStream output;
177 boolean interrupted;
178 InterruptGUI caller;
179
185 public Worker(GAMSJob jb, GAMSOptions opt, PrintStream out, InterruptGUI c) {
186 interrupted = false;
187 job = jb;
188 option = opt;
189 output = out;
190 caller = c;
191 }
192
194 @Override
195 public void run() {
196 try {
197 job.run(option, output);
198 } catch(Exception e) {
199 e.printStackTrace();
200 } finally {
201 caller.reportJobTerminated();
202 }
203 }
204
206 public void interrupt() {
207 if (getState() != Thread.State.TERMINATED)
208 interrupted = job.interrupt();
209 }
210 }
211}
212
214class LogOutputStream extends OutputStream {
215 private JTextArea textArea;
216
217 public LogOutputStream(JTextArea textArea) {
218 this.textArea = textArea;
219 }
220
221 @Override
222 public void write(int b) throws IOException {
223 textArea.append(String.valueOf((char)b));
224 textArea.setCaretPosition(textArea.getDocument().getLength());
225 }
226}
void setAllModelTypes(String value)
void setWorkingDirectory(String directory)
GAMSJob addJobFromGamsLib(String modelName)
This small example demonstrates how to run a GAMS model in a graphical user interface.
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).