Skip to content

Commit

Permalink
Support reading controller list from commands sto file
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanhab committed Sep 2, 2024
1 parent 86654fa commit 2675146
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class EditCosnstraintTermJPanel extends javax.swing.JPanel {
private String[] availableComponentNames;
private ConstraintTermModel constraintTermModel;
private NumberFormat numFormat = NumberFormat.getInstance();

private String trackedDataDir, initialGuessDir, osimxFile;

/**
* Creates new form EditJointTaskJPanel
Expand All @@ -41,10 +41,14 @@ public EditCosnstraintTermJPanel() {
}

EditCosnstraintTermJPanel(OpenSimObject constraintTerm,
TreatmentOptimizationToolModel.Mode mode) {
TreatmentOptimizationToolModel.Mode mode,
String trackedDataDir, String initialGuessDir, String osimxFile) {
constraintTerm2Edit = constraintTerm;
initializing = true;
this.mode = mode;
this.trackedDataDir = trackedDataDir;
this.initialGuessDir = initialGuessDir;
this.osimxFile = osimxFile;
constraintTermModel = new ConstraintTermModel(constraintTerm2Edit, mode);
AbstractProperty typeProp = constraintTerm2Edit.getPropertyByName("type");
String saveType = PropertyHelper.getValueString(typeProp);
Expand Down Expand Up @@ -274,7 +278,8 @@ private void jEnabledCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//

private void editComonentListButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editComonentListButtonActionPerformed
// TODO add your handling code here:
String[] names = RCNLCostTermsInfo.getAvailableNamesForComponentType(constraintTermModel.getComponentType(), constraintTermModel.getModel());
String[] names = RCNLCostTermsInfo.getAvailableNamesForComponentType(constraintTermModel.getComponentType(), constraintTermModel.getModel(),
this.trackedDataDir, this.initialGuessDir, this.osimxFile);
PropertyStringList componentListProperty = constraintTermModel.getPropertyComponentList();
ComponentTableModel ctm = new ComponentTableModel(componentListProperty, names);
SelectQuantitiesFromListJPanel selectionPanel = new SelectQuantitiesFromListJPanel(ctm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class EditCostTermJPanel extends javax.swing.JPanel {
private String[] availableComponentNames;
private CostTermModel costTermModel;
private NumberFormat numFormat = NumberFormat.getInstance();
private String trackedDataDir, initialGuessDir, osimxFile;

/**
* Creates new form EditJointTaskJPanel
Expand All @@ -47,10 +48,14 @@ public EditCostTermJPanel() {
initComponents();
}

EditCostTermJPanel(OpenSimObject rcnlCostTerm, TreatmentOptimizationToolModel.Mode mode) {
EditCostTermJPanel(OpenSimObject rcnlCostTerm, TreatmentOptimizationToolModel.Mode mode,
String trackedDataDir, String initialGuessDir, String osimxFile) {
costTerm2Edit = rcnlCostTerm;
initializing = true;
this.mode = mode;
this.trackedDataDir = trackedDataDir;
this.initialGuessDir = initialGuessDir;
this.osimxFile = osimxFile;
costTermModel = new CostTermModel(costTerm2Edit, mode);
AbstractProperty typeProp = costTerm2Edit.getPropertyByName("type");
String saveType = PropertyHelper.getValueString(typeProp);
Expand Down Expand Up @@ -284,7 +289,8 @@ private void jEnabledCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//
private void editComonentListButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editComonentListButtonActionPerformed
// TODO add your handling code here:
// get Component list from costTermModel as well as the corresponding property
String[] names = RCNLCostTermsInfo.getAvailableNamesForComponentType(costTermModel.getComponentType(), costTermModel.getModel());
String[] names = RCNLCostTermsInfo.getAvailableNamesForComponentType(costTermModel.getComponentType(), costTermModel.getModel(),
this.trackedDataDir, this.initialGuessDir, this.osimxFile);
PropertyStringList componentListProperty = costTermModel.getPropertyComponentList();
ComponentTableModel ctm = new ComponentTableModel(componentListProperty, names);
SelectQuantitiesFromListJPanel selectionPanel = new SelectQuantitiesFromListJPanel(ctm);
Expand Down
62 changes: 61 additions & 1 deletion Gui/opensim/rice_cnl/src/org/opensim/rcnl/RCNLCostTermsInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
*/
package org.opensim.rcnl;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Vector;
import org.openide.util.Exceptions;
import org.opensim.modeling.ArrayBool;
import org.opensim.modeling.ArrayStr;
import org.opensim.modeling.Coordinate;
import org.opensim.modeling.CoordinateSet;
import org.opensim.modeling.Model;
import org.opensim.modeling.Storage;

/**
*
Expand Down Expand Up @@ -60,10 +65,18 @@ public class RCNLCostTermsInfo {
private static String[] muscleList = null;
private static String[] markerList = null;
private static String[] loadList = null;
private static String[] forces_List = null;
private static String[] moments_List = null;
private static String[] controller_List = null;
private static String[] synergy_Groups = null;
private static Model currentModel=null;
private static String trackedDir = "";
private static String initalGuessDir = "";
private static String osimxFile = "";


// Create lists of the proper types to be used in Cost/Constraint create/edit
public static String[] getAvailableNamesForComponentType(String componentType, Model model) {
public static String[] getAvailableNamesForComponentType(String componentType, Model model, String testTrackedDir, String testInitalGuessDir, String testOsimxFile) {
//Will evaluate these lazily for componentTypes not in model
if (currentModel != model){
// fresh or changed model
Expand All @@ -73,6 +86,7 @@ public static String[] getAvailableNamesForComponentType(String componentType, M
markerList = null;
loadList = null;
}

ArrayStr componentNames = new ArrayStr();
String[] availableQuantities;

Expand Down Expand Up @@ -119,10 +133,56 @@ public static String[] getAvailableNamesForComponentType(String componentType, M
loadNames.copyInto(loadList);
}
return loadList;
case "controller":
if (trackedDir.equalsIgnoreCase(testTrackedDir) && initalGuessDir.equalsIgnoreCase(testInitalGuessDir) ){
// No need to search for synergyCommands.sto or torqueControls.sto to populate controller_List or synergy_Groups
if (controller_List == null){
populateControllersList();
}
return controller_List;
}
else {
trackedDir = testTrackedDir;
initalGuessDir = testInitalGuessDir;
populateControllersList();
}
return controller_List;

}
return new String[]{};
}

private static void populateControllersList() {
String[] availableQuantities;
boolean found = false;
String[] candidateFolders = new String[]{trackedDir, initalGuessDir};
for (String folder : candidateFolders){
File dir = new File(folder);
File [] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("_synergyCommands.sto");
}
});

for (File commandFile : files) {
try {
Storage commandSto = new Storage(commandFile.getAbsolutePath(), true);
// Successful find, get labels to populate controller_List, then break
found = true;
ArrayStr controllerNames = commandSto.getColumnLabels();
controllerNames.remove(0); // time needs to be removed
availableQuantities = new String[controllerNames.getSize()];
controllerNames.toVector().copyInto(availableQuantities);
controller_List= availableQuantities;
break;
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
if (found)
break;
}
}
public static String[] getCostTermTypes(TreatmentOptimizationToolModel.Mode mode) {
switch(mode){
case TrackingOptimization:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,10 @@ private void editConstraintTermButtonActionPerformed(java.awt.event.ActionEvent
int idx = sels[0];
OpenSimObject currentTerm = (OpenSimObject)constraintTermListModel.get(idx);
OpenSimObject termToEdit = currentTerm.clone();
EditCosnstraintTermJPanel ejtPanel = new EditCosnstraintTermJPanel(termToEdit, mode);
EditCosnstraintTermJPanel ejtPanel = new EditCosnstraintTermJPanel(termToEdit, mode,
treatmentOptimizationToolModel.getTrackedQuantitiesDir(),
treatmentOptimizationToolModel.getInitialGuessDir(),
treatmentOptimizationToolModel.getInputOsimxFile());
DialogDescriptor dlg = new DialogDescriptor(ejtPanel, "Create/Edit One Constraint Term ");
Dialog d = DialogDisplayer.getDefault().createDialog(dlg);
d.setVisible(true);
Expand All @@ -775,7 +778,10 @@ private void editConstraintTermButtonActionPerformed(java.awt.event.ActionEvent
private void addConstraintTermButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addConstraintTermButtonActionPerformed
// TODO add your handling code here:
OpenSimObject constraintTerm = OpenSimObject.newInstanceOfType("RCNLConstraintTerm");
EditCosnstraintTermJPanel ejtPanel = new EditCosnstraintTermJPanel(constraintTerm, mode);
EditCosnstraintTermJPanel ejtPanel = new EditCosnstraintTermJPanel(constraintTerm, mode,
treatmentOptimizationToolModel.getTrackedQuantitiesDir(),
treatmentOptimizationToolModel.getInitialGuessDir(),
treatmentOptimizationToolModel.getInputOsimxFile());
DialogDescriptor dlg = new DialogDescriptor(ejtPanel, "Create/Edit One Constraint Term ");
Dialog d = DialogDisplayer.getDefault().createDialog(dlg);
d.setVisible(true);
Expand Down Expand Up @@ -818,7 +824,10 @@ private void editCostTermButtonActionPerformed(java.awt.event.ActionEvent evt) {
int idx = sels[0];
OpenSimObject currentTerm = (OpenSimObject)costTermListModel.get(idx);
OpenSimObject termToEdit = currentTerm.clone();
EditCostTermJPanel ejtPanel = new EditCostTermJPanel(termToEdit, mode);
EditCostTermJPanel ejtPanel = new EditCostTermJPanel(termToEdit, mode,
treatmentOptimizationToolModel.getTrackedQuantitiesDir(),
treatmentOptimizationToolModel.getInitialGuessDir(),
treatmentOptimizationToolModel.getInputOsimxFile());
DialogDescriptor dlg = new DialogDescriptor(ejtPanel, "Create/Edit One Cost Term ");
Dialog d = DialogDisplayer.getDefault().createDialog(dlg);
d.setVisible(true);
Expand All @@ -832,7 +841,10 @@ private void editCostTermButtonActionPerformed(java.awt.event.ActionEvent evt) {
private void addCostTermButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCostTermButtonActionPerformed
// TODO add your handling code here:
OpenSimObject costTerm = OpenSimObject.newInstanceOfType("RCNLCostTerm");
EditCostTermJPanel ejtPanel = new EditCostTermJPanel(costTerm, mode);
EditCostTermJPanel ejtPanel = new EditCostTermJPanel(costTerm, mode,
treatmentOptimizationToolModel.getTrackedQuantitiesDir(),
treatmentOptimizationToolModel.getInitialGuessDir(),
treatmentOptimizationToolModel.getInputOsimxFile());
DialogDescriptor dlg = new DialogDescriptor(ejtPanel, "Create/Edit One Cost Term ");
Dialog d = DialogDisplayer.getDefault().createDialog(dlg);
d.setVisible(true);
Expand Down

0 comments on commit 2675146

Please sign in to comment.