fix module import

System for importing modules from other psfx files was not working.
Probably wasn't working for quite some time. Now fixed.
This commit is contained in:
Douglas Gillespie 2023-12-17 12:19:18 +00:00
parent 75d349e33c
commit 0039ff7422
8 changed files with 142 additions and 48 deletions

View File

@ -198,6 +198,7 @@ public class PSFXReadWriter {
BinaryHeader bh = new BinaryHeader();
bh.readHeader(dis);
PamSettingsGroup psg = new PamSettingsGroup(bh.getDataDate());
ArrayList<ModuleNameObject> moduleNames = new ArrayList<ModuleNameObject>();
/*
*
dos.writeInt(totalLen);
@ -217,6 +218,7 @@ public class PSFXReadWriter {
dis.read(data);
if (objectId == ModuleNameObject.typeId) {
ModuleNameObject mno = new ModuleNameObject(data);
moduleNames.add(mno);
}
else if (objectId == 2) {
PamControlledUnitSettings pcsu = PamControlledUnitSettings.createFromNamedByteArray(data);

View File

@ -26,7 +26,7 @@ import offlineProcessing.OfflineTaskGroup;
* them, but for some of the batch processing control, we need to be able to load and
* manipulate a second set of modules, which do nothing within the running
* configuration, but are there to allow us to set up a configuration to send to
* other batch processes in viewer mode.
* other batch processes in viewer mode. This is also being used in an improved module import system.
* @author dg50
*
*/

View File

@ -141,6 +141,12 @@ public class PamController implements PamControllerInterface, PamSettings {
// flag used in main() to indicate that pamguard should exit as soon as processing ends.
public static final String AUTOEXIT = "-autoexit";
/**
* Never changed. Needed to identify settings for list of modules in prfx files.
*/
public static final String unitName = "Pamguard Controller";
public static final String unitType = "PamController";
/**
* The pam model.
*/
@ -1838,12 +1844,12 @@ public class PamController implements PamControllerInterface, PamSettings {
@Override
public String getUnitName() {
return "Pamguard Controller";
return unitName;
}
@Override
public String getUnitType() {
return "PamController";
return unitType;
}
@Override

View File

@ -33,9 +33,7 @@ public class PamSettingsGroup implements Comparable<PamSettingsGroup> {
* @param settingsTime settings time in milliseconds.
*/
public PamSettingsGroup(long settingsTime) {
super();
this.settingsTime = settingsTime;
unitSettings = new ArrayList<PamControlledUnitSettings>();
this(settingsTime, new ArrayList<PamControlledUnitSettings>());
}
/**

View File

@ -7,7 +7,8 @@ import PamModel.parametermanager.PamParameterSet;
/**
* Very simple class used in an ArrayList of used modules that
* get's saved between runs.
* get's saved between runs. This forms the core of the settings system
* so don't f*** with it !
* @author Doug
*
*/

View File

@ -15,10 +15,12 @@ import PamController.PamController;
import PamController.PamSettingManager;
import PamController.PamSettings;
import PamController.PamSettingsGroup;
import PamController.UsedModuleInfo;
import PamModel.PamModel;
import PamModel.PamModuleInfo;
import PamModel.SMRUEnable;
import PamView.dialog.PamFileBrowser;
import PamView.dialog.warn.WarnOnce;
/**
* Class to handle the import of settings from other psf files.
@ -180,9 +182,16 @@ public class SettingsImport {
/**
* Now make a new Pamcontrolled unit with the given name ...
*/
// need to find the module information in the PamModel
// PamModel pamModel = PamModel.getPamModel();
// pamModel.
PamModuleInfo moduleInfo = PamModuleInfo.findModuleInfo(importGroup.getUsedModuleInfo().className);
// find the module info for this one
PamModuleInfo moduleInfo = importGroup.getModuleInfo();
// PamModuleInfo moduleInfo = importGroup.getUsedModuleInfo();
if (moduleInfo == null) {
String msg = String.format("Unable to find module information for type %s main class %s in model",
importGroup.getUsedModuleInfo().getUnitType(), importGroup.getUsedModuleInfo().className);
WarnOnce.showWarning("Module creating error!", msg, WarnOnce.WARNING_MESSAGE);
return null;
}
@ -206,6 +215,7 @@ public class SettingsImport {
}
}
loadSubUnitSettings(importGroup, unit.getUnitName());
unit.setupControlledUnit();
return unit;
}
@ -215,45 +225,64 @@ public class SettingsImport {
* @return
*/
ArrayList<SettingsImportGroup> organiseSettingsGroups(ArrayList<PamControlledUnitSettings> settings) {
/**
* this needs rewriting for psfx files which are organised differently. first we need to find
* a list of PAMGuard modules by finding the settings group of the PAMController.
*/
ArrayList<SettingsImportGroup> groupedSettings = new ArrayList<>();
// first pull out the settings for PamControlledNnits.
ArrayList<UsedModuleInfo> usedModules = findPamControllerSettings(settings);
// make the group list based on the list of modules.
for (UsedModuleInfo usedModule : usedModules) {
groupedSettings.add(new SettingsImportGroup(usedModule));
}
// // first pull out the settings for PamControlledNnits.
boolean[] used = new boolean[settings.size()];
for (int i = 0; i < settings.size(); i++) {
PamControlledUnitSettings aSet = settings.get(i);
if (aSet.getOwnerClassName() == null) {
continue;
}
Class ownerClass = null;
try {
ownerClass = Class.forName(aSet.getOwnerClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// this is happening since the ownerclassname is not set correctly in psfx files
// so we have to deserialise the data to find the class.
// ownerClass = getClassFromData(aSet.getSerialisedByteArray());
// ownerClass = PamModuleInfo.findModuleClass(aSet.getUnitType());
}
if (ownerClass == null) {
continue;
}
if (PamControlledUnit.class.isAssignableFrom(ownerClass)) {
PamModuleInfo moduleInfo = PamModuleInfo.findModuleInfo(aSet.getOwnerClassName());
groupedSettings.add(new SettingsImportGroup(aSet, moduleInfo));
used[i] = true;
}
}
// for (int i = 0; i < settings.size(); i++) {
// PamControlledUnitSettings aSet = settings.get(i);
// if (aSet.getOwnerClassName() == null) {
// continue;
// }
// Class ownerClass = null;
// try {
// ownerClass = Class.forName(aSet.getOwnerClassName());
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
//// e.printStackTrace();
// // this is happening since the ownerclassname is not set correctly in psfx files
// // so we have to deserialise the data to find the class.
//// ownerClass = getClassFromData(aSet.getSerialisedByteArray());
//// ownerClass = PamModuleInfo.findModuleClass(aSet.getUnitType());
// }
// if (ownerClass == null) {
// continue;
// }
// if (PamControlledUnit.class.isAssignableFrom(ownerClass)) {
// PamModuleInfo moduleInfo = PamModuleInfo.findModuleInfo(aSet.getOwnerClassName());
// groupedSettings.add(new SettingsImportGroup(moduleInfo));
// used[i] = true;
// }
// }
// now match all the remaining settings into the first set based on ModuleName.
for (int i = 0; i < settings.size(); i++) {
PamControlledUnitSettings aSet = settings.get(i);
if (used[i]) continue;
// if (used[i]) continue;
SettingsImportGroup mainGroup = findGroup(groupedSettings, aSet.getUnitName());
if (mainGroup != null) {
// main settings will have same type as well as same name.
boolean mainType = isMainType(mainGroup, aSet);
if (mainType) {
mainGroup.setMainSettings(aSet);
}
else {
mainGroup.addSubSettings(aSet);
}
used[i] = true;
System.out.println(String.format("Adding %s-%s to %s-%s group", aSet.getUnitType(), aSet.getUnitName(),
mainGroup.getMainSettings().getUnitType(), mainGroup.getMainSettings().getUnitName()));
// System.out.println(String.format("Adding %s-%s to %s-%s group", aSet.getUnitType(), aSet.getUnitName(),
// mainGroup.getMainSettings().getUnitType(), mainGroup.getMainSettings().getUnitName()));
}
}
@ -272,6 +301,34 @@ public class SettingsImport {
return groupedSettings;
}
/**
* IS this the main settings group for this module ? If it is, it should have the same
* type as well as the same name.
* @param mainGroup
* @param aSet
* @return
*/
private boolean isMainType(SettingsImportGroup mainGroup, PamControlledUnitSettings aSet) {
boolean isMain = mainGroup.getUsedModuleInfo().getUnitType().equals(aSet.getUnitType());
return isMain;
}
private ArrayList<UsedModuleInfo> findPamControllerSettings(ArrayList<PamControlledUnitSettings> settings) {
if (settings == null) {
return null;
}
for (PamControlledUnitSettings aSet : settings) {
if (aSet.getUnitName().equals(PamController.unitName) &&
aSet.getUnitType().equals(PamController.unitType)) {
Object sets = aSet.getSettings();
if (sets instanceof ArrayList) {
return (ArrayList<UsedModuleInfo>) sets;
}
}
}
return null;
}
private Class getClassFromData(byte[] data) {
try {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
@ -285,7 +342,7 @@ public class SettingsImport {
private SettingsImportGroup findGroup(ArrayList<SettingsImportGroup> groupedSettings, String unitName) {
for (SettingsImportGroup iG:groupedSettings) {
if (iG.getMainSettings().getUnitName().equals(unitName)) {
if (iG.getUsedModuleInfo().unitName.equals(unitName)) {
return iG;
}
}

View File

@ -17,6 +17,7 @@ import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
@ -66,6 +67,7 @@ public class SettingsImportDialog extends PamDialog {
moduleTable.getColumnModel().getColumn(2).setPreferredWidth(200);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(new TitledBorder("Module import selection"));
JScrollPane scrollPanel = new JScrollPane(moduleTable);
mainPanel.add(BorderLayout.CENTER, scrollPanel);
mainPanel.setPreferredSize(new Dimension(600, 250));

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import PamController.PamControlledUnit;
import PamController.PamControlledUnitSettings;
import PamController.PamController;
import PamController.UsedModuleInfo;
import PamModel.PamModuleInfo;
/**
@ -20,19 +21,26 @@ public class SettingsImportGroup {
private ArrayList<PamControlledUnitSettings> subSettings = new ArrayList<>();
private PamModuleInfo moduleInfo;
private UsedModuleInfo usedModuleInfo;
private ArrayList<ImportChoice> importChoices;
private ImportChoice importChoice;
public SettingsImportGroup(UsedModuleInfo moduleInfo) {
super();
this.usedModuleInfo = moduleInfo;
}
/**
* Constructor takes the main settings
* @param mainSettings
* @param moduleInfo
*/
public SettingsImportGroup(PamControlledUnitSettings mainSettings, PamModuleInfo moduleInfo) {
public SettingsImportGroup(PamControlledUnitSettings mainSettings, UsedModuleInfo moduleInfo) {
this.mainSettings = mainSettings;
this.moduleInfo = moduleInfo;
this.usedModuleInfo = moduleInfo;
}
/**
@ -72,7 +80,7 @@ public class SettingsImportGroup {
importChoices.add(importChoice = new ImportChoice(ImportChoice.DONT_IMPORT, null));
Class ownerClass = null;
try {
ownerClass = Class.forName(mainSettings.getOwnerClassName());
ownerClass = Class.forName(usedModuleInfo.className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@ -80,8 +88,9 @@ public class SettingsImportGroup {
ArrayList<PamControlledUnit> existingModules =
PamController.getInstance().findControlledUnits(ownerClass);
int maxnumber = 1; // this will only ever be used by the Array Manager which doesn't have a moduleInfo.
if (this.moduleInfo != null) {
maxnumber = moduleInfo.getMaxNumber();
PamModuleInfo pamModuleInfo = getPamModuleInfo();
if (pamModuleInfo != null) {
maxnumber = pamModuleInfo.getMaxNumber();
}
if (existingModules != null) {
for (int i = 0; i < existingModules.size(); i++) {
@ -111,10 +120,29 @@ public class SettingsImportGroup {
}
/**
* This is the information from an existing module, which may not
* have the full class name, but does have the type and name of the
* module being imported.
* @return the moduleInfo
*/
public PamModuleInfo getModuleInfo() {
return moduleInfo;
public UsedModuleInfo getUsedModuleInfo() {
return usedModuleInfo;
}
/**
* this is the module information held in the PamModel which
* is used to create a module.
* @return
*/
public PamModuleInfo getPamModuleInfo() {
return PamModuleInfo.findModuleInfo(usedModuleInfo.className);
}
/**
* @param mainSettings the mainSettings to set
*/
public void setMainSettings(PamControlledUnitSettings mainSettings) {
this.mainSettings = mainSettings;
}