/
SessionUtils.java
185 lines (146 loc) · 7.08 KB
/
SessionUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
///////////////////////////////////////////////////////////////////////////////
// For information as to what this class does, see the Javadoc, below. //
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, //
// 2007, 2008, 2009, 2010, 2014, 2015, 2022 by Peter Spirtes, Richard //
// Scheines, Joseph Ramsey, and Clark Glymour. //
// //
// This program 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. //
// //
// This program 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 this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
///////////////////////////////////////////////////////////////////////////////
package edu.cmu.tetradapp.app;
import edu.cmu.tetrad.session.SessionModel;
import edu.cmu.tetrad.util.Parameters;
import edu.cmu.tetradapp.model.AbstractAlgorithmRunner;
import javax.swing.*;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* Some utility methods relevant to sessions.
*
* @author josephramsey
*/
final class SessionUtils {
/**
* Shows a dialog that says "Explanation of [Model type]" followed by a list
* of each parent combination for that model type with buttons that link to
* an explanations of how the model works with each combination of parent
* values.
*
* @param warning If true, shows a warning icon.
* @param onlyModel If true, displays a message indicating that this is the
* only model consistent with the parents.
*/
public static void showPermissibleParentsDialog(Class modelClass,
JComponent centeringComp,
boolean warning,
boolean onlyModel) {
TetradApplicationConfig config = TetradApplicationConfig.getInstance();
SessionNodeConfig nodeConfig = config.getSessionNodeConfig(modelClass);
if (nodeConfig == null) {
throw new NullPointerException("No configuration for model: " + modelClass);
}
SessionNodeModelConfig modelConfig = nodeConfig.getModelConfig(modelClass);
String[][] parentCombinations =
SessionUtils.possibleParentCombinations(modelClass);
StringBuilder b = new StringBuilder();
b.append("<html>");
b.append("The combinations of parent models you can use for ").append(modelConfig.getName()).append(" are:");
for (int i = 0; i < parentCombinations.length; i++) {
String[] parentCombination = parentCombinations[i];
b.append("\n ").append(i + 1).append(". ");
if (parentCombination.length == 0) {
b.append("No inputs");
} else {
for (int j = 0; j < parentCombination.length; j++) {
b.append(parentCombination[j]);
if (j < parentCombination.length - 1) {
b.append(" + ");
}
}
}
}
final int messageType = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(centeringComp, b.toString(),
"Information on \"" + modelConfig.getName() + "\"", messageType);
}
/**
* @return a string listing the combinations of legal parent models for a
* given model class. The item at [i][j] is the jth parent model description
* of the ith parent model combination.
*/
private static String[][] possibleParentCombinations(Class modelClass) {
List<List<String>> parentCombinations = new LinkedList<>();
Constructor[] constructors = modelClass.getConstructors();
boolean foundNull = false;
PARENT_SET:
for (Constructor constructor : constructors) {
List _list = Arrays.asList(constructor.getParameterTypes());
List parameterTypes = new LinkedList(_list);
for (Iterator j = parameterTypes.iterator(); j.hasNext(); ) {
Class parameterType = (Class) j.next();
if (!(SessionModel.class.isAssignableFrom(parameterType) ||
(Parameters.class.isAssignableFrom(parameterType)))) {
continue PARENT_SET;
}
String descrip = SessionUtils.getModelName(parameterType);
if (descrip == null) {
j.remove();
}
}
if (parameterTypes.isEmpty()) {
foundNull = true;
continue;
}
List<String> combination = new LinkedList<>();
for (Object parameterType1 : parameterTypes) {
Class parameterType = (Class) parameterType1;
String descrip = SessionUtils.getModelName(parameterType);
combination.add(descrip);
}
parentCombinations.add(combination);
}
if (foundNull) {
parentCombinations.add(0, new LinkedList<>());
}
String[][] _parentCombinations =
new String[parentCombinations.size()][];
for (int i = 0; i < parentCombinations.size(); i++) {
List<String> combination = parentCombinations.get(i);
_parentCombinations[i] = new String[combination.size()];
for (int j = 0; j < combination.size(); j++) {
_parentCombinations[i][j] = combination.get(j);
}
}
return _parentCombinations;
}
//======================================== Private Methods ==============================//
/**
* @return the name of the given model
*/
private static String getModelName(Class model) {
TetradApplicationConfig tetradConfig = TetradApplicationConfig.getInstance();
SessionNodeConfig config = tetradConfig.getSessionNodeConfig(model);
if (config == null) {
if (AbstractAlgorithmRunner.class.equals(model)) {
return "Algorithm";
}
return null;
}
SessionNodeModelConfig modelConfig = config.getModelConfig(model);
return modelConfig.getName();
}
}