📄 learningparametersdialog.java
字号:
/* jahmm package - v0.3.1 *//* * Copyright (c) 2004, Jean-Marc Francois. * * This file is part of Jahmm. * Jahmm is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Jahmm is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jahmm; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package be.ac.ulg.montefiore.run.jahmm.apps;import java.text.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import be.ac.ulg.montefiore.run.jahmm.*;/* * Observation type (integer or vector) and dimension. */public class LearningParametersDialog extends JDialog implements ActionListener { final private JPanel mainPanel; final private JButton okButton; final private JButton cancelButton; final private JSpinner nbStatesSpinner; //final private JComboBox algorithmComboBox; private JComboBox opdfComboBox; private int nbStates; private int observationType = -1; private int observationDimension = -1; // Has the dialog been validated (OK button pressed) private boolean isValidated; private String[] opdfFactoryDescription; private OpdfFactory[] opdfFactory; private int selectedFactory; private String[] opdfIntegerFactoryDescription = new String[] { "Discrete distribution" }; private OpdfFactory[] opdfIntegerFactory = null; private String[] opdfVectorFactoryDescription = new String[] { "Multivariate gaussian distribution" }; private OpdfFactory[] opdfVectorFactory = null; /* * Parameters for a learning algorithm */ public class LearningParameters { public final int nbStates; public final OpdfFactory opdfFactory; public LearningParameters(int nbStates, OpdfFactory opdfFactory) { this.nbStates = nbStates; this.opdfFactory = opdfFactory; } } public LearningParametersDialog() { super(new JFrame(), "Select learning parameters", true); nbStatesSpinner = new JSpinner(new SpinnerNumberModel(5, 1, Integer.MAX_VALUE, 1)); JLabel messageLabel = new JLabel("Choose a learning algorithm and its parameters."); opdfComboBox = new JComboBox(); // Dummy combo box JLabel spinnerLabel = new JLabel("Number of HMM states"); spinnerLabel.setHorizontalAlignment(SwingConstants.RIGHT); spinnerLabel.setLabelFor(nbStatesSpinner); JLabel typesLabel = new JLabel("Observations type"); typesLabel.setHorizontalAlignment(SwingConstants.RIGHT); typesLabel.setLabelFor(opdfComboBox); mainPanel = new JPanel(new GridLayout(2,2)); mainPanel.add(typesLabel); mainPanel.add(opdfComboBox); mainPanel.add(spinnerLabel); mainPanel.add(nbStatesSpinner); okButton = new JButton("Ok"); cancelButton = new JButton("Cancel"); okButton.addActionListener(this); cancelButton.addActionListener(this); JPanel buttonsPanel = new JPanel(new GridLayout(1,1)); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); getContentPane().add(messageLabel, BorderLayout.NORTH); getContentPane().add(mainPanel, BorderLayout.CENTER); getContentPane().add(buttonsPanel, BorderLayout.SOUTH); setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); setResizable(false); } public void setupObservationType(int observationType, int observationDimension) { if (this.observationType == observationType && this.observationDimension == observationDimension) return; // Don't change the previous combo box selections this.observationType = observationType; this.observationDimension = observationDimension; int i = Arrays.asList(mainPanel.getComponents()).indexOf(opdfComboBox); mainPanel.remove(i); if (observationType == ObservationSequences.INTEGER) { opdfIntegerFactory = new OpdfFactory[] { new OpdfIntegerFactory(observationDimension) }; opdfComboBox = new JComboBox(opdfIntegerFactoryDescription); opdfFactoryDescription = opdfIntegerFactoryDescription; opdfFactory = opdfIntegerFactory; } else { // Observations are real vectors opdfVectorFactory = new OpdfFactory[] { new OpdfMultiGaussianFactory(observationDimension) }; opdfComboBox = new JComboBox(opdfVectorFactoryDescription); opdfFactoryDescription = opdfVectorFactoryDescription; opdfFactory = opdfVectorFactory; } mainPanel.add(opdfComboBox, i); mainPanel.validate(); } public LearningParameters parameters() { return new LearningParameters(nbStates, opdfFactory[selectedFactory]); } public boolean isValidated() { return isValidated; } /* * Ok/Cancel button pressed. */ public void actionPerformed(ActionEvent e) { if (e.getSource().equals(okButton)) { try { nbStatesSpinner.commitEdit(); } catch(ParseException pe) { throw new RuntimeException("Internal error"); } isValidated = true; nbStates = ((Integer) nbStatesSpinner.getValue()).intValue(); selectedFactory = Arrays.asList(opdfFactoryDescription). indexOf(opdfComboBox.getSelectedItem()); } if (e.getSource().equals(cancelButton)) isValidated = false; setVisible(false); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -