Working parameter output

Only just and not complete, but working.
This commit is contained in:
Douglas Gillespie 2023-08-31 17:06:51 +01:00
parent 07ced6ae6d
commit 3688719e25
9 changed files with 404 additions and 128 deletions

View File

@ -903,6 +903,14 @@ public class PamguardXMLWriter implements PamSettings {
return doc;
}
/**
* Is this element a writable type ? Basically, this means
* that it's a primitive of some sort. Otherwise it's
* probably an object and may even be a list in which case
* it will need treating differently.
* @param clazz
* @return
*/
public static boolean isWritableType(Class<?> clazz)
{
if (clazz.isEnum()) return true;

View File

@ -48,6 +48,7 @@ public class WarnOnce implements PamSettings {
int ans = showWarning(parent, "Warning Messages", "Show all PAMGuard warning messages", WarnOnce.OK_CANCEL_OPTION);
if (ans == WarnOnce.CANCEL_OPTION) return;
singleInstance.warnOnceList.clearList();
singleInstance.showThisSess.clear();
}
@Override

View File

@ -22,7 +22,7 @@ public class RWSpeciesManager extends DataBlockSpeciesManager<RWEDataUnit> {
@Override
public String getSpeciesCode(RWEDataUnit dataUnit) {
return RWSpeciesTypes.onlyType;
return RWSpeciesTypes.defaultName;
}
}

View File

@ -240,6 +240,34 @@ An error will throw an exception.
return true;
}
/**
* Remove a document based on a collection name and a cdocument Id.
* @param collection
* @param docId
* @return
* @throws TethysException
*/
public boolean removeDocument(String collection, String docId) throws TethysException {
try {
Object result = jerseyClient.removeDocument(collection, docId );
/**
* Return from a sucessful delete is something like
*
deployment = getTethysControl().getDeploymentHandler().createDeploymentDocument(freeId++, recordPeriod);
<DELETE>
<ITEM> ['ECoastNARW0'] </ITEM>
</DELETE>
An error will throw an exception.
*/
}
catch (Exception e) {
// System.out.printf("Error deleting %s %s: %s\n", collection, docId, e.getMessage());
String msg = String.format("Error deleting %s:%s", collection, docId);
throw new TethysException(msg, e.getLocalizedMessage());
}
return true;
}
/**
* check the return string from importFiles and if it's an
* error, throw an exception. Otherwise do nothing.

View File

@ -491,71 +491,15 @@ public class DetectionsHandler {
// dataSource.setEnsembleId(""); ToDo
detections.setDataSource(dataSource);
AlgorithmType algorithm = detections.getAlgorithm();
algorithm.setMethod(getMethodString(dataBlock));
algorithm.setSoftware(getSoftwareString(dataBlock));
algorithm.setVersion(getVersionString(dataBlock));
TethysDataProvider dataProvider = dataBlock.getTethysDataProvider();
if (dataProvider != null) {
// Parameters parameters = dataProvider.getAlgorithmParameters();
Parameters parameters = algorithm.getParameters();
if (parameters == null) {
parameters = new Parameters();
algorithm.setParameters(parameters);
}
List<Element> paramList = parameters.getAny();
// algorithm.setParameters(parameters);
// make a really simple parameter or two to see if it works with simpler xml than PG generates.
/**
* Parameters should look something like
*
<Algorithm>
<Method>Analyst detections</Method>
<Software>Triton</Software>
<Version>unknown</Version>
<Parameters>
<LTSA_plot_time_h>0.75</LTSA_plot_time_h>
<LTSA_low_Hz>0.0</LTSA_low_Hz>
<LTSA_high_Hz>5000.0</LTSA_high_Hz>
<LTSA_brightness>30.0</LTSA_brightness>
</Parameters>
</Algorithm>
*/
// this works. Can look at the source to see how it's done.
// may have fun making this work for more complex structures.
try {
Helper helper = new Helper();
helper.AddAnyElement(paramList, "Threshold", "3.5");
/*
* and see Matlab code for dbStruct2DOM for more complex structures
* This looks like it may be possible to rewrite my functions for
* writing structures to XML using the helper.AddAnyElement function as
* an example and I should be able to output my complex structures.
*/
} catch (JAXBException | ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = XMLUtils.createBlankDoc();
// PamguardXMLWriter pamXMLWriter = PamguardXMLWriter.getXMLWriter();
Element dummyEl = doc.createElement("SomeParam");
//// dummyEl.setNodeValue("nothing");
dummyEl.setTextContent("3.0");
/*
* xsl:stylesheet version=\"1.0\" \n"
+ " xmlns:xsl=http://www.w3.org/1999/XSL/Transform\n"
+ " xmlns:ns0=http://mydata.com/H2H/Automation\n"
*/
dummyEl.setAttribute("xmlns:ns0", TethysControl.xmlNameSpace);
// dummyEl.set
// paramList.add(dummyEl);
// Element mainEl = doc.createElement("CONFIG");
// mainEl.appendChild(dummyEl);
// doc.appendChild(mainEl);
// System.out.println(pamXMLWriter.getAsString(doc));
algorithm = dataProvider.getAlgorithm();
// detections.setAlgorithm(algorithm);
}
algorithm.setMethod(getMethodString(dataBlock));
algorithm.setSoftware(getSoftwareString(dataBlock));
algorithm.setVersion(getVersionString(dataBlock));
List<SupportSoftware> supSoft = algorithm.getSupportSoftware();
SupportSoftware supportSoft = new SupportSoftware();

View File

@ -31,7 +31,12 @@ import tethys.TethysControl;
import tethys.TethysTimeFuncs;
import tethys.output.StreamExportParams;
import tethys.output.TethysExportParams;
import tethys.species.DataBlockSpeciesManager;
import tethys.species.ITISTypes;
import tethys.species.SpeciesMapItem;
import whistleClassifier.WhistleContour;
import javax.xml.bind.JAXBException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
@ -104,10 +109,13 @@ public class AutoTethysProvider implements TethysDataProvider {
// TODO Auto-generated catch block
e.printStackTrace();
}
algorithm.setMethod(this.getAlgorithmMethod());
algorithm.setSoftware("PAMGuard");
algorithm.setVersion(PamguardVersionInfo.version);
//algorithm.setParameters(this.getAlgorithmParameters());
// algorithm.setMethod(this.getAlgorithmMethod());
// algorithm.setSoftware("PAMGuard");
// algorithm.setVersion(PamguardVersionInfo.version);
Parameters algoParameters = this.getAlgorithmParameters();
if (algoParameters != null) {
algorithm.setParameters(algoParameters);
}
return algorithm;
}
@ -120,66 +128,80 @@ public class AutoTethysProvider implements TethysDataProvider {
PamSettings pamSettings = (PamSettings) pamControlledUnit;
Parameters parameters = new Parameters();
List<Element> paramList = parameters.getAny();
Document doc = XMLUtils.createBlankDoc();
PamguardXMLWriter pamXMLWriter = PamguardXMLWriter.getXMLWriter();
Element dummyEl = doc.createElement("MODULES");
doc.appendChild(dummyEl);
PamSettings[] settingsObjs = getSettingsObjects();
if (settingsObjs == null) {
return null;
}
// pamXMLWriter.setStaticNameSpace(TethysControl.xmlNameSpace);
Element settingsEl = pamXMLWriter.writeUnitSettings(doc, dummyEl, pamSettings, settingsObjs);
if (settingsEl == null) {
Object settings = pamSettings.getSettingsReference();
TethysParameterPacker paramPacker = null;
try {
paramPacker = new TethysParameterPacker();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Element> genList = paramPacker.packParameters(settings);
if (genList == null || genList.size() == 0) {
return null;
}
paramList.addAll(genList);
// settingsEl = addNameSpaceToElements(doc, settingsEl, TethysControl.xmlNameSpace);
dummyEl.appendChild(settingsEl);
NodeList childs = settingsEl.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
Node el = childs.item(i);
// System.out.println(el.getNodeName());
if (el instanceof Element) {
paramList.add((Element) el);
}
}
// Document doc = pamXMLWriter.writeOneModule((PamSettings) pamControlledUnit, System.currentTimeMillis());
// String moduleXML = null;
if (doc != null) {
// this string should be XML of all the settings for the module controlling this
// datablock.
// moduleXML = pamXMLWriter.getAsString(doc, true); // change to false to get smaller xml
// System.out.printf("Module settings for datablock %s are:\n", moduleXML);
// System.out.println(moduleXML);
// Element pamguard = doc.get("PAMGUARD");
// Element modules = (Element) pamguard.getElementsByTagName("MODULES");
// doc.get
// NodeList childs = doc.getChildNodes();
// for (int i = 0; i < childs.getLength(); i++) {
// Node el = childs.item(i);
// System.out.println(el.getNodeName());
// if (el instanceof Element) {
// paramList.add((Element) el);
// }
// }
// String moduleXML = pamXMLWriter.getAsString(doc, true); // change to false to get smaller xml
// System.out.printf("Module settings for datablock %s are:\n%s", this.pamDataBlock.getDataName(), moduleXML);
}
// // try the old say
// Document doc2 = pamXMLWriter.writeOneModule((PamSettings) pamControlledUnit, System.currentTimeMillis());
// String moduleXML = null;
// if (doc2 != null) {
// // this string should be XML of all the settings for the module controlling this
// // datablock.
// moduleXML = pamXMLWriter.getAsString(doc2, true); // change to false to get smaller xml
// System.out.printf("Module settings for datablock %s are:\n%s", pamDataBlock.getDataName(),moduleXML);
// }
//
// Document doc = XMLUtils.createBlankDoc();
// PamguardXMLWriter pamXMLWriter = PamguardXMLWriter.getXMLWriter();
// Element dummyEl = doc.createElement("MODULES");
// doc.appendChild(dummyEl);
// PamSettings[] settingsObjs = getSettingsObjects();
// if (settingsObjs == null) {
// return null;
// }
//// pamXMLWriter.setStaticNameSpace(TethysControl.xmlNameSpace);
// Element settingsEl = pamXMLWriter.writeUnitSettings(doc, dummyEl, pamSettings, settingsObjs);
// if (settingsEl == null) {
// return null;
// }
//
//// settingsEl = addNameSpaceToElements(doc, settingsEl, TethysControl.xmlNameSpace);
//
//
// dummyEl.appendChild(settingsEl);
// NodeList childs = settingsEl.getChildNodes();
// for (int i = 0; i < childs.getLength(); i++) {
// Node el = childs.item(i);
// // System.out.println(el.getNodeName());
// if (el instanceof Element) {
// paramList.add((Element) el);
// }
// }
//
// // Document doc = pamXMLWriter.writeOneModule((PamSettings) pamControlledUnit, System.currentTimeMillis());
// // String moduleXML = null;
// if (doc != null) {
// // this string should be XML of all the settings for the module controlling this
// // datablock.
// // moduleXML = pamXMLWriter.getAsString(doc, true); // change to false to get smaller xml
// // System.out.printf("Module settings for datablock %s are:\n", moduleXML);
// // System.out.println(moduleXML);
// // Element pamguard = doc.get("PAMGUARD");
// // Element modules = (Element) pamguard.getElementsByTagName("MODULES");
// // doc.get
// // NodeList childs = doc.getChildNodes();
// // for (int i = 0; i < childs.getLength(); i++) {
// // Node el = childs.item(i);
// // System.out.println(el.getNodeName());
// // if (el instanceof Element) {
// // paramList.add((Element) el);
// // }
// // }
// // String moduleXML = pamXMLWriter.getAsString(doc, true); // change to false to get smaller xml
// // System.out.printf("Module settings for datablock %s are:\n%s", this.pamDataBlock.getDataName(), moduleXML);
// }
//
// // // try the old say
// // Document doc2 = pamXMLWriter.writeOneModule((PamSettings) pamControlledUnit, System.currentTimeMillis());
// // String moduleXML = null;
// // if (doc2 != null) {
// // // this string should be XML of all the settings for the module controlling this
// // // datablock.
// // moduleXML = pamXMLWriter.getAsString(doc2, true); // change to false to get smaller xml
// // System.out.printf("Module settings for datablock %s are:\n%s", pamDataBlock.getDataName(),moduleXML);
// // }
// //
return parameters;
}
@ -275,7 +297,29 @@ public class AutoTethysProvider implements TethysDataProvider {
Detection detection = new Detection();
detection.setStart(TethysTimeFuncs.xmlGregCalFromMillis(dataUnit.getTimeMilliseconds()));
detection.setEnd(TethysTimeFuncs.xmlGregCalFromMillis(dataUnit.getEndTimeInMilliseconds()));
detection.setSpeciesId(getSpeciesIdType());
DataBlockSpeciesManager speciesManager = pamDataBlock.getDatablockSpeciesManager();
SpeciesMapItem speciesItem = null;
if (speciesManager != null) {
speciesItem = speciesManager.getSpeciesItem(dataUnit);
// detection.setSpeciesId(new Species);
// detection.setSpeciesId(getSpeciesIdType());
}
else {
}
SpeciesIDType species = new SpeciesIDType();
List<String> calls = detection.getCall();
if (speciesItem != null) {
species.setValue(BigInteger.valueOf(speciesItem.getItisCode()));
if (speciesItem.getCallType() != null) {
calls.add(speciesItem.getCallType());
}
}
else {
species.setValue(BigInteger.valueOf(ITISTypes.ANTHROPOGENIC));
calls.add("unknown");
}
detection.setSpeciesId(species);
/*
* NOTE: I use channel bitmaps throughout since detections are often made on multiple channels.
*/

View File

@ -0,0 +1,233 @@
package tethys.pamdata;
import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import PamController.settings.output.xml.PamguardXMLWriter;
import PamModel.parametermanager.ManagedParameters;
import PamModel.parametermanager.PamParameterData;
import PamModel.parametermanager.PamParameterSet;
import nilus.MarshalXML;
/**
* Functions to pack up a PAMGuard parameters object into the correct format
* for Tethys. This is very similar to functions in PamguardXMLWriter.writeUnitSettings
* but seems to have enough differences that it needs totally rewriting for Tethys.
* @author dg50
*
*/
public class TethysParameterPacker {
// /**
// * Parameters should look something like below. however, only packing them with a long
// schema name seems to work.
// *
//<Algorithm>
//<Method>Analyst detections</Method>
//<Software>Triton</Software>
//<Version>unknown</Version>
//<Parameters>
//<LTSA_plot_time_h>0.75</LTSA_plot_time_h>
//<LTSA_low_Hz>0.0</LTSA_low_Hz>
//<LTSA_high_Hz>5000.0</LTSA_high_Hz>
//<LTSA_brightness>30.0</LTSA_brightness>
//</Parameters>
//</Algorithm>
/*
*
// // this works. Can look at the source to see how it's done.
// // may have fun making this work for more complex structures.
// try {
// Helper helper = new Helper();
// helper.AddAnyElement(paramList, "Threshold", "3.5");
//
// * and see Matlab code for dbStruct2DOM at C:\Users\dg50\source\repos\TethysMatlab\db
// * for more complex structures
// * This looks like it may be possible to rewrite my functions for
// * writing structures to XML using the helper.AddAnyElement function as
// * an example and I should be able to output my complex structures.
//
// } catch (JAXBException | ParserConfigurationException e) {
// e.printStackTrace();
// }
*/
private MarshalXML marshaller;
/**
* @throws JAXBException
*
*/
public TethysParameterPacker() throws JAXBException {
super();
try {
marshaller = new MarshalXML();
} catch (JAXBException e) {
}
}
public List<Element> packParameters(Object data) {
List<Element> elList = new ArrayList<Element>();
ArrayList<Object> objectHierarchy = new ArrayList<>();
PamParameterSet parameterSet;
if (data instanceof ManagedParameters) {
parameterSet = ((ManagedParameters) data).getParameterSet();
}
else {
int exclusions = 0;
// if (writerSettings.includeConstants == false) {
exclusions = Modifier.STATIC | Modifier.FINAL;
// }
parameterSet = PamParameterSet.autoGenerate(data, exclusions);
}
if (parameterSet == null) {
return null;
}
// Document document = null;
// try {
// document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
// } catch (ParserConfigurationException e1) {
// e1.printStackTrace();
// }
QName qname = new QName(MarshalXML.schema, "parameters", "ty");
JAXBElement<String> jaxel = new JAXBElement<String>(
qname, String.class, parameterSet.getParentObject().getClass().getCanonicalName());
Document doc = null;
try {
doc = marshaller.marshalToDOM(jaxel);
} catch (JAXBException | ParserConfigurationException e1) {
e1.printStackTrace();
}
Element el = doc.getDocumentElement();
for (PamParameterData pamParam : parameterSet.getParameterCollection()) {
try {
Object paramData = pamParam.getData();
boolean ok = createElement(doc, el, paramData, pamParam, objectHierarchy);
// if (newEl != null) {
// elList.add(newEl);
// }
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
elList.add(el);
return elList;
}
private boolean createElement(Document document, Element parentEl, Object paramData, PamParameterData pamParam, ArrayList<Object> objectHierarchy) {
Class<? extends Object> javaClass = paramData.getClass();
if (PamguardXMLWriter.isWritableType(javaClass)) {
String name = pamParam.getFieldName();
String value = paramData.toString();
Element el = document.createElement(name);
// el.setNodeValue(value);
el.setTextContent(value);
parentEl.appendChild(el);
// QName qname = new QName(MarshalXML.schema, name, "ty");
// JAXBElement<String> jaxel = new JAXBElement<String>(
// qname, String.class, value);
//
//
// try {
// jxbm.marshal(jaxel, dom);
// } catch (JAXBException e) {
// e.printStackTrace();
// }
// Document doc = null;
// try {
// doc = marshaller.marshalToDOM(jaxel);
// } catch (JAXBException e) {
// e.printStackTrace();
// } catch (ParserConfigurationException e) {
// e.printStackTrace();
// }
// Element el = doc.getDocumentElement();
// return el;
return true;
}
if (javaClass.isArray()) {
return writeArray(document, parentEl, paramData, pamParam, objectHierarchy);
}
/*
*
if (javaClass.isArray()) {
return writeArray(doc, el, data, pamParam, objectHierarchy);
}
if (List.class.isAssignableFrom(javaClass)){
return writeList(doc, el, data, pamParam, objectHierarchy);
}
if (Map.class.isAssignableFrom(javaClass)){
return writeMap(doc, el, data, pamParam, objectHierarchy);
}
if (File.class.isAssignableFrom(javaClass)) {
return writeFile(doc, el, data, pamParam);
}
else {
Element e = makeElement(doc, pamParam.getFieldName(), data.getClass().getName());
el.appendChild(e);
writeObjectData(doc, e, data, objectHierarchy);
return e;
}
*/
return false;
}
private boolean writeArray(Document document, Element parentEl, Object paramData, PamParameterData pamParam,
ArrayList<Object> objectHierarchy) {
if (paramData.getClass().isArray() == false) {
return false;
}
String name = pamParam.getFieldName();
Element el = document.createElement(name);
parentEl.appendChild(el);
int n = Array.getLength(paramData);
boolean ok = true;
for (int i = 0; i < n; i++) {
Object arrayEl = Array.get(paramData, i);
ok &= createElement(document, el, arrayEl, pamParam, objectHierarchy);
}
// TODO Auto-generated method stub
return ok;
}
// private Element writeArray(PamParameterData pamParam, ArrayList<Object> objectHierarchy) {
// QName qname = new QName(MarshalXML.schema, pamParam.getFieldName(), "ty");
// JAXBElement<String> jaxel = new JAXBElement<String>(
// qname, String.class, value);
//
// Document doc = null;
// try {
// doc = marshaller.marshalToDOM(jaxel);
// } catch (JAXBException e) {
// e.printStackTrace();
// } catch (ParserConfigurationException e) {
// e.printStackTrace();
// }
// return null;
// }
}

View File

@ -72,6 +72,9 @@ abstract public class DataBlockSpeciesManager<T extends PamDataUnit> {
super();
this.dataBlock = dataBlock;
datablockSpeciesMap = SpeciesMapManager.getInstance().getSpeciesMap(dataBlock);
if (datablockSpeciesMap == null) {
datablockSpeciesMap = new DataBlockSpeciesMap();
}
// datablockSpeciesMap.clearMap();
clearMapNulls();
checkMapDefault();
@ -81,6 +84,9 @@ abstract public class DataBlockSpeciesManager<T extends PamDataUnit> {
* Clear up some old maps which have got a null null default.
*/
private void clearMapNulls() {
if (datablockSpeciesMap == null) {
return;
}
SpeciesMapItem nullVal = datablockSpeciesMap.getItem(null);
if (nullVal == null) {
datablockSpeciesMap.removeItem(null);
@ -136,7 +142,7 @@ abstract public class DataBlockSpeciesManager<T extends PamDataUnit> {
// if (defaultItem == null) {
// datablockSpeciesMap.putItem(getDefaultSpeciesCode(), getDefaultDefaultSpecies());
// }
if (defaultDefaultSpecies == null) {
if (defaultDefaultSpecies == null || datablockSpeciesMap == null) {
return;
}
SpeciesMapItem defaultItem = datablockSpeciesMap.getItem(defaultDefaultSpecies.getPamguardName());

View File

@ -16,9 +16,12 @@ import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import PamController.PamController;
import PamView.dialog.PamDialogPanel;
import PamView.dialog.warn.WarnOnce;
import PamView.tables.SwingTableColumnWidths;
import tethys.TethysControl;
import tethys.dbxml.TethysException;
/**
* Table view of a collection of Tethys documents.
@ -118,8 +121,17 @@ public class TethysDocumentTable implements PamDialogPanel {
}
private void deleteDocument(String docName) {
// TODO Auto-generated method stub
int ans = WarnOnce.showNamedWarning("deletedoc"+collectionName, PamController.getMainFrame(), "Delete document",
"Are you sure you want to delete the document " + docName, WarnOnce.OK_CANCEL_OPTION);
if (ans == WarnOnce.OK_OPTION) {
try {
tethysControl.getDbxmlConnect().removeDocument(collectionName, docName);
} catch (TethysException e) {
System.out.println("Failed to delete " + docName);
System.out.println(e.getMessage());
}
}
updateTableData();
}
private class TableModel extends AbstractTableModel {