use of ij.macro.Interpreter in project imagej1 by imagej.
the class Startup method runMacro.
private boolean runMacro(String macro) {
Interpreter interp = new Interpreter();
interp.run(macro, null);
if (interp.wasError())
return false;
else
return true;
}
use of ij.macro.Interpreter in project astroimagej by keastrid.
the class GenericDialog method getNextNumber.
/**
* Returns the contents of the next numeric field,
* or NaN if the field does not contain a number.
*/
public double getNextNumber() {
if (numberField == null)
return -1.0;
TextField tf = (TextField) numberField.elementAt(nfIndex);
String theText = tf.getText();
String label = null;
if (macro) {
label = (String) labels.get((Object) tf);
theText = Macro.getValue(macroOptions, label, theText);
}
String originalText = (String) defaultText.elementAt(nfIndex);
double defaultValue = ((Double) (defaultValues.elementAt(nfIndex))).doubleValue();
double value;
boolean skipRecording = false;
if (theText.equals(originalText)) {
value = defaultValue;
if (smartRecording)
skipRecording = true;
} else {
Double d = getValue(theText);
if (d != null)
value = d.doubleValue();
else {
// Is the value a macro variable?
if (theText.startsWith("&"))
theText = theText.substring(1);
Interpreter interp = Interpreter.getInstance();
value = interp != null ? interp.getVariable2(theText) : Double.NaN;
if (Double.isNaN(value)) {
invalidNumber = true;
errorMessage = "\"" + theText + "\" is an invalid number";
value = Double.NaN;
if (macro) {
IJ.error("Macro Error", "Numeric value expected in run() function\n \n" + " Dialog box title: \"" + getTitle() + "\"\n" + " Key: \"" + label.toLowerCase(Locale.US) + "\"\n" + " Value or variable name: \"" + theText + "\"");
}
}
}
}
if (recorderOn && !skipRecording) {
recordOption(tf, trim(theText));
}
nfIndex++;
return value;
}
use of ij.macro.Interpreter in project astroimagej by keastrid.
the class GenericDialog method getNextChoiceIndex.
/**
* Returns the index of the selected item in the next popup menu.
*/
public int getNextChoiceIndex() {
if (choice == null)
return -1;
Choice thisChoice = (Choice) (choice.elementAt(choiceIndex));
int index = thisChoice.getSelectedIndex();
if (macro) {
String label = (String) labels.get((Object) thisChoice);
String oldItem = thisChoice.getSelectedItem();
int oldIndex = thisChoice.getSelectedIndex();
String item = Macro.getValue(macroOptions, label, oldItem);
if (// value is macro variable
item != null && item.startsWith("&"))
item = getChoiceVariable(item);
thisChoice.select(item);
index = thisChoice.getSelectedIndex();
if (index == oldIndex && !item.equals(oldItem)) {
// is value a macro variable?
Interpreter interp = Interpreter.getInstance();
String s = interp != null ? interp.getStringVariable(item) : null;
if (s == null)
IJ.error(getTitle(), "\"" + item + "\" is not a valid choice for \"" + label + "\"");
else
item = s;
}
}
if (recorderOn) {
int defaultIndex = ((Integer) (defaultChoiceIndexes.elementAt(choiceIndex))).intValue();
if (!(smartRecording && index == defaultIndex)) {
String item = thisChoice.getSelectedItem();
if (!(item.equals("*None*") && getTitle().equals("Merge Channels")))
recordOption(thisChoice, thisChoice.getSelectedItem());
}
}
choiceIndex++;
return index;
}
use of ij.macro.Interpreter in project astroimagej by keastrid.
the class ResultsTable method applyMacro.
/**
* Applies a macro to each row of the table; the columns are assigned variable names
* as given by getHeadingsAsVaribleNames(). New variables starting with an uppercase letter
* create a new column with this name.
* The variable 'row' (the row index) is pre-defined.
* Except for the row label (if existing), currently only supports numeric values, no Strings.
* @return false in case of a macro error
*/
public boolean applyMacro(String macro) {
String[] columnHeadings = getHeadings();
// same as variable names
String[] columnNames = getHeadingsAsVariableNames(columnHeadings);
// corresponding column index; <0 for rowLabels
int[] columnIndices = new int[columnHeadings.length];
for (int i = 0; i < columnHeadings.length; i++) columnIndices[i] = getColumnIndex(columnHeadings[i]);
Program pgm = (new Tokenizer()).tokenize(macro);
StringBuilder sb = new StringBuilder(1000);
sb.append("var ");
for (int i = 0; i < columnNames.length; i++) {
// create 'var' statement with 'real' data values, so errors are less likely
sb.append(columnNames[i]);
sb.append('=');
if (columnIndices[i] < 0)
sb.append(rowLabels[0] == null ? "\"\"" : '"' + rowLabels[0] + '"');
else
// avoid negative values since minus would be extra token
sb.append(Math.abs(getValueAsDouble(columnIndices[i], 0)));
sb.append(',');
}
sb.append("row;\n");
sb.append("function dummy() {}\n");
sb.append(macro);
sb.append(";\n");
String code = sb.toString();
// 'macro' code starts at this token number
int PCStart = 9 + 4 * columnNames.length;
Interpreter interp = new Interpreter();
interp.setApplyMacroTable(this);
try {
// first test run
interp.run(code, null);
} catch (Exception e) {
}
if (interp.wasError())
return false;
boolean[] columnInUse = new boolean[columnNames.length];
ArrayList<String> newColumnList = new ArrayList<String>();
String[] variables = interp.getVariableNames();
for (String variable : variables) {
// check for variables that make a new Column
int columnNumber = indexOf(columnNames, variable);
if (// variable is a know column
columnNumber >= 0)
columnInUse[columnNumber] = macro.indexOf(variable) >= 0;
else if (Character.isUpperCase(variable.charAt(0))) {
// create new column
getFreeColumn(variable);
newColumnList.add(variable);
}
}
String[] newColumns = newColumnList.toArray(new String[0]);
int[] newColumnIndices = new int[newColumns.length];
for (int i = 0; i < newColumns.length; i++) newColumnIndices[i] = getColumnIndex(newColumns[i]);
for (int row = 0; row < counter; row++) {
// apply macro to each row
for (int col = 0; col < columnHeadings.length; col++) {
if (columnInUse[col]) {
// set variable values for used columns
if (columnIndices[col] < 0) {
String str = rowLabels[row];
interp.setVariable(columnNames[col], str);
} else {
double v = getValueAsDouble(columnIndices[col], row);
interp.setVariable(columnNames[col], v);
}
}
}
interp.setVariable("row", row);
interp.run(PCStart);
if (interp.wasError())
return false;
for (int col = 0; col < columnNames.length; col++) {
if (columnInUse[col]) {
// set new values for previous columns
if (columnIndices[col] < 0) {
String str = interp.getVariableAsString(columnNames[col]);
rowLabels[row] = str;
} else {
double v = interp.getVariable(columnNames[col]);
setValue(columnIndices[col], row, v);
}
}
}
for (int i = 0; i < newColumns.length; i++) {
// set new values for newly-created columns
double v = interp.getVariable(newColumns[i]);
setValue(newColumnIndices[i], row, v);
}
}
return true;
}
use of ij.macro.Interpreter in project astroimagej by keastrid.
the class OpenDialog method lookupPathVariable.
public static String lookupPathVariable(String path) {
if (path != null && path.indexOf(".") == -1 && !((new File(path)).exists())) {
if (path.startsWith("&"))
path = path.substring(1);
Interpreter interp = Interpreter.getInstance();
String path2 = interp != null ? interp.getStringVariable(path) : null;
if (path2 != null)
path = path2;
}
return path;
}
Aggregations