use of org.jdom.Namespace in project vcell by virtualcell.
the class XmlHelper method XMLToImage.
static VCImage XMLToImage(String xmlString, boolean printKeys) throws XmlParseException {
Namespace ns = Namespace.getNamespace(XMLTags.VCML_NS);
if (xmlString == null || xmlString.length() == 0) {
throw new XmlParseException("Invalid xml for Image: " + xmlString);
}
// default parser and no validation
Element root = (XmlUtil.stringToXML(xmlString, null)).getRootElement();
Element extentElement = root.getChild(XMLTags.ExtentTag, ns);
Element imageElement = root.getChild(XMLTags.ImageTag, ns);
// Element extentElement = root.getChild(XMLTags.ExtentTag);
// Element imageElement = root.getChild(XMLTags.ImageTag);
XmlReader reader = new XmlReader(printKeys, ns);
Extent extent = reader.getExtent(extentElement);
VCImage vcImage = reader.getVCImage(imageElement, extent);
vcImage.refreshDependencies();
return vcImage;
}
use of org.jdom.Namespace in project vcell by virtualcell.
the class XmlHelper method XMLToDocument.
/**
* Insert the method's description here.
* Creation date: (2/7/2006 4:45:26 PM)
* @return cbit.vcell.document.VCDocument
* @param xmlString java.lang.String
*/
public static VCDocument XMLToDocument(VCLogger vcLogger, String xmlString) throws Exception {
VCDocument doc = null;
XMLSource xmlSource = new XMLSource(xmlString);
// some overhead.
org.jdom.Element rootElement = xmlSource.getXmlDoc().getRootElement();
String xmlType = rootElement.getName();
if (xmlType.equals(XMLTags.VcmlRootNodeTag)) {
// For now, assuming that <vcml> element has only one child (biomodel, mathmodel or geometry).
// Will deal with multiple children of <vcml> Element when we get to model composition.
java.util.List<?> childElementList = rootElement.getChildren();
// assuming first child is the biomodel, mathmodel or geometry.
Element modelElement = (Element) childElementList.get(0);
xmlType = modelElement.getName();
}
if (xmlType.equals(XMLTags.BioModelTag)) {
doc = XmlHelper.XMLToBioModel(xmlSource);
} else if (xmlType.equals(XMLTags.MathModelTag)) {
doc = XmlHelper.XMLToMathModel(xmlSource);
} else if (xmlType.equals(XMLTags.GeometryTag)) {
doc = XmlHelper.XMLToGeometry(xmlSource);
} else if (xmlType.equals(XMLTags.SbmlRootNodeTag)) {
Namespace namespace = rootElement.getNamespace(XMLTags.SBML_SPATIAL_NS_PREFIX);
boolean bIsSpatial = (namespace == null) ? false : true;
doc = XmlHelper.importSBML(vcLogger, xmlSource, bIsSpatial);
} else if (xmlType.equals(XMLTags.CellmlRootNodeTag)) {
doc = XmlHelper.importMathCellML(vcLogger, xmlSource);
} else {
// unknown XML format
throw new RuntimeException("unsupported XML format, first element tag is <" + rootElement.getName() + ">");
}
return doc;
}
use of org.jdom.Namespace in project vcell by virtualcell.
the class XmlHelper method XMLToSim.
public static Simulation XMLToSim(String xmlString) throws XmlParseException {
Simulation sim = null;
Namespace ns = Namespace.getNamespace(XMLTags.VCML_NS);
try {
if (xmlString == null || xmlString.length() == 0) {
throw new XmlParseException("Invalid xml for Simulation: " + xmlString);
}
// default parser and no validation
Element root = (XmlUtil.stringToXML(xmlString, null)).getRootElement();
Element simElement = root.getChild(XMLTags.SimulationTag, ns);
Element mdElement = root.getChild(XMLTags.MathDescriptionTag, ns);
Element geomElement = root.getChild(XMLTags.GeometryTag, ns);
XmlReader reader = new XmlReader(true, ns);
Geometry geom = null;
if (geomElement != null) {
geom = reader.getGeometry(geomElement);
}
MathDescription md = reader.getMathDescription(mdElement, geom);
sim = reader.getSimulation(simElement, md);
} catch (Exception pve) {
pve.printStackTrace();
throw new XmlParseException("Unable to parse simulation string.", pve);
}
sim.refreshDependencies();
return sim;
}
use of org.jdom.Namespace in project vcell by virtualcell.
the class XmlHelper method XMLToSimTask.
public static SimulationTask XMLToSimTask(String xmlString) throws XmlParseException, ExpressionException {
Namespace ns = Namespace.getNamespace(XMLTags.VCML_NS);
try {
if (xmlString == null || xmlString.length() == 0) {
throw new XmlParseException("Invalid xml for Simulation: " + xmlString);
}
// default parser and no validation
Element root = (XmlUtil.stringToXML(xmlString, null)).getRootElement();
if (!root.getName().equals(SimulationTask_tag)) {
throw new RuntimeException("expecting top level element to be " + SimulationTask_tag);
}
int taskId = Integer.parseInt(root.getAttributeValue(TaskId_attr));
int jobIndex = Integer.parseInt(root.getAttributeValue(JobIndex_attr));
String computeResource = root.getChildTextTrim(ComputeResource_tag, ns);
List<?> children = root.getChildren(FieldFunctionIdentifierSpec_tag, ns);
ArrayList<FieldDataIdentifierSpec> fdisArrayList = new ArrayList<FieldDataIdentifierSpec>();
for (Object child : children) {
if (child instanceof Element) {
String fdisText = ((Element) child).getTextTrim();
FieldDataIdentifierSpec fdis = FieldDataIdentifierSpec.fromCSVString(fdisText);
fdisArrayList.add(fdis);
}
}
FieldDataIdentifierSpec[] fdisArray = fdisArrayList.toArray(new FieldDataIdentifierSpec[0]);
Element simElement = root.getChild(XMLTags.SimulationTag, ns);
Element mdElement = root.getChild(XMLTags.MathDescriptionTag, ns);
Element geomElement = root.getChild(XMLTags.GeometryTag, ns);
XmlReader reader = new XmlReader(true, ns);
Geometry geom = null;
if (geomElement != null) {
geom = reader.getGeometry(geomElement);
}
MathDescription md = reader.getMathDescription(mdElement, geom);
Simulation sim = reader.getSimulation(simElement, md);
sim.refreshDependencies();
SimulationJob simJob = new SimulationJob(sim, jobIndex, fdisArray);
SimulationTask simTask = new SimulationTask(simJob, taskId, computeResource);
return simTask;
} catch (Exception pve) {
pve.printStackTrace();
throw new XmlParseException("Unable to parse simulation string.", pve);
}
}
use of org.jdom.Namespace in project vcell by virtualcell.
the class ParameterEstimationTaskXMLPersistence method getParameterEstimationTask.
/**
* Insert the method's description here.
* Creation date: (5/5/2006 4:50:36 PM)
* @return cbit.vcell.modelopt.ParameterEstimationTask
* @param element org.jdom.Element
* @param simContext cbit.vcell.mapping.SimulationContext
*/
public static ParameterEstimationTask getParameterEstimationTask(Element parameterEstimationTaskElement, SimulationContext simContext) throws ExpressionException, MappingException, MathException, java.beans.PropertyVetoException {
Namespace ns = parameterEstimationTaskElement.getNamespace();
ParameterEstimationTask parameterEstimationTask = new ParameterEstimationTask(simContext);
String name = parameterEstimationTaskElement.getAttributeValue(NameAttribute);
parameterEstimationTask.setName(name);
Element annotationElement = parameterEstimationTaskElement.getChild(AnnotationTag, ns);
if (annotationElement != null) {
String annotationText = annotationElement.getText();
parameterEstimationTask.setAnnotation(annotationText);
}
//
// read ParameterMappingSpecs
//
Element parameterMappingSpecListElement = parameterEstimationTaskElement.getChild(ParameterMappingSpecListTag, ns);
if (parameterMappingSpecListElement != null) {
List<Element> parameterMappingSpecElementList = parameterMappingSpecListElement.getChildren(ParameterMappingSpecTag, ns);
for (Element parameterMappingSpecElement : parameterMappingSpecElementList) {
String parameterName = parameterMappingSpecElement.getAttributeValue(ParameterReferenceAttribute);
SymbolTableEntry ste = getSymbolTableEntry(simContext, parameterName);
if (ste instanceof Parameter) {
Parameter parameter = (Parameter) ste;
ParameterMappingSpec parameterMappingSpec = parameterEstimationTask.getModelOptimizationSpec().getParameterMappingSpec(parameter);
if (parameterMappingSpec != null) {
String lowLimitString = parameterMappingSpecElement.getAttributeValue(LowLimitAttribute);
if (lowLimitString != null) {
parameterMappingSpec.setLow(parseDouble(lowLimitString));
}
String highLimitString = parameterMappingSpecElement.getAttributeValue(HighLimitAttribute);
if (highLimitString != null) {
parameterMappingSpec.setHigh(parseDouble(highLimitString));
}
String currentValueString = parameterMappingSpecElement.getAttributeValue(CurrentValueAttribute);
if (currentValueString != null) {
parameterMappingSpec.setCurrent(Double.parseDouble(currentValueString));
}
String selectedString = parameterMappingSpecElement.getAttributeValue(SelectedAttribute);
if (selectedString != null) {
parameterMappingSpec.setSelected(Boolean.valueOf(selectedString).booleanValue());
}
}
} else {
System.out.println("couldn't read parameterMappingSpec '" + parameterName + "', ste=" + ste);
}
}
}
//
// read ReferenceData
//
Element referenceDataElement = parameterEstimationTaskElement.getChild(ReferenceDataTag, ns);
if (referenceDataElement != null) {
String numRowsText = referenceDataElement.getAttributeValue(NumRowsAttribute);
String numColsText = referenceDataElement.getAttributeValue(NumColumnsAttribute);
// int numRows = Integer.parseInt(numRowsText);
int numCols = Integer.parseInt(numColsText);
//
// read columns
//
String[] columnNames = new String[numCols];
double[] columnWeights = new double[numCols];
int columnCounter = 0;
Element dataColumnListElement = referenceDataElement.getChild(DataColumnListTag, ns);
List<Element> dataColumnList = dataColumnListElement.getChildren(DataColumnTag, ns);
for (Element dataColumnElement : dataColumnList) {
columnNames[columnCounter] = dataColumnElement.getAttributeValue(NameAttribute);
columnWeights[columnCounter] = Double.parseDouble(dataColumnElement.getAttributeValue(WeightAttribute));
columnCounter++;
}
//
// read rows
//
Vector<double[]> rowDataVector = new Vector<double[]>();
Element dataRowListElement = referenceDataElement.getChild(DataRowListTag, ns);
List<Element> dataRowList = dataRowListElement.getChildren(DataRowTag, ns);
for (Element dataRowElement : dataRowList) {
String rowText = dataRowElement.getText();
CommentStringTokenizer tokens = new CommentStringTokenizer(rowText);
double[] rowData = new double[numCols];
for (int j = 0; j < numCols; j++) {
if (tokens.hasMoreTokens()) {
String token = tokens.nextToken();
rowData[j] = Double.parseDouble(token);
} else {
throw new RuntimeException("failed to read row data for ReferenceData");
}
}
rowDataVector.add(rowData);
}
ReferenceData referenceData = new SimpleReferenceData(columnNames, columnWeights, rowDataVector);
parameterEstimationTask.getModelOptimizationSpec().setReferenceData(referenceData);
}
//
// read ReferenceDataMappingSpecs
//
Element referenceDataMappingSpecListElement = parameterEstimationTaskElement.getChild(ReferenceDataMappingSpecListTag, ns);
if (referenceDataMappingSpecListElement != null) {
List<Element> referenceDataMappingSpecList = referenceDataMappingSpecListElement.getChildren(ReferenceDataMappingSpecTag, ns);
for (Element referenceDataMappingSpecElement : referenceDataMappingSpecList) {
String referenceDataColumnName = referenceDataMappingSpecElement.getAttributeValue(ReferenceDataColumnNameAttribute);
String referenceDataModelSymbolName = referenceDataMappingSpecElement.getAttributeValue(ReferenceDataModelSymbolAttribute);
ReferenceDataMappingSpec referenceDataMappingSpec = parameterEstimationTask.getModelOptimizationSpec().getReferenceDataMappingSpec(referenceDataColumnName);
SymbolTableEntry modelSymbolTableEntry = null;
if (referenceDataModelSymbolName != null) {
modelSymbolTableEntry = getSymbolTableEntry(simContext, referenceDataModelSymbolName);
if (referenceDataMappingSpec != null && modelSymbolTableEntry != null) {
referenceDataMappingSpec.setModelObject(modelSymbolTableEntry);
}
}
}
}
//
// read OptimizationSolverSpec
//
Element optimizationSolverSpecElement = parameterEstimationTaskElement.getChild(OptimizationSolverSpecTag, ns);
if (optimizationSolverSpecElement != null) {
OptimizationSolverSpec optSolverSpec = null;
String optimizationSolverTypeName = optimizationSolverSpecElement.getAttributeValue(OptimizationSolverTypeAttribute);
// getting parameters
Element optimizationSolverParameterList = optimizationSolverSpecElement.getChild(OptimizationListOfParametersTag, ns);
if (optimizationSolverParameterList != null) {
List<Element> listOfSolverParams = optimizationSolverParameterList.getChildren(OptimizationParameterTag, ns);
CopasiOptimizationMethod copasiOptMethod = null;
if (listOfSolverParams != null && listOfSolverParams.size() > 0) {
List<CopasiOptimizationParameter> copasiSolverParams = new ArrayList<CopasiOptimizationParameter>();
for (Element solverParam : listOfSolverParams) {
String paramName = solverParam.getAttributeValue(OptimizationParameterNameAttribute);
double paramValue = Double.parseDouble(solverParam.getAttributeValue(OptimizationParameterValueAttribute));
CopasiOptimizationParameter copasiParam = new CopasiOptimizationParameter(getCopasiOptimizationParameterTypeByName(paramName), paramValue);
copasiSolverParams.add(copasiParam);
}
copasiOptMethod = new CopasiOptimizationMethod(getCopasiOptimizationMethodTypeByName(optimizationSolverTypeName), copasiSolverParams.toArray(new CopasiOptimizationParameter[copasiSolverParams.size()]));
} else // no parameters
{
copasiOptMethod = new CopasiOptimizationMethod(getCopasiOptimizationMethodTypeByName(optimizationSolverTypeName), new CopasiOptimizationParameter[0]);
}
optSolverSpec = new OptimizationSolverSpec(copasiOptMethod);
// add number of runs attribute
String numOfRunsStr = optimizationSolverSpecElement.getAttributeValue(OptimizationSolverNumOfRunsAttribute);
if (numOfRunsStr != null) {
int numOfRuns = Integer.parseInt(numOfRunsStr);
optSolverSpec.setNumOfRuns(numOfRuns);
}
}
parameterEstimationTask.setOptimizationSolverSpec(optSolverSpec);
}
if (// optimization solver spec is null create a default copasi evolutionary programming
optimizationSolverSpecElement == null || parameterEstimationTask.getOptimizationSolverSpec() == null) {
OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(new CopasiOptimizationMethod(CopasiOptimizationMethodType.EvolutionaryProgram));
parameterEstimationTask.setOptimizationSolverSpec(optSolverSpec);
}
// read optimization solver result set
Element optimizationResultSetElement = parameterEstimationTaskElement.getChild(OptXmlTags.OptimizationResultSet_Tag, ns);
if (optimizationResultSetElement != null) {
OptimizationResultSet optResultSet = null;
// read optsolverResultSet
if (optimizationResultSetElement.getChild(OptXmlTags.bestOptRunResultSet_Tag, ns) != null) {
Element optSolverResultSetElement = optimizationResultSetElement.getChild(OptXmlTags.bestOptRunResultSet_Tag, ns);
OptSolverResultSet optSolverResultSet = null;
// get best parameters, best func value, number of evaluations and construct an optRunResultSet
Element paramListElement = optSolverResultSetElement.getChild(OptimizationListOfParametersTag, ns);
OptRunResultSet optRunResultSet = null;
List<String> paramNames = new ArrayList<String>();
List<Double> paramValues = new ArrayList<Double>();
if (paramListElement != null && !paramListElement.getChildren().isEmpty()) {
List<Element> paramElements = paramListElement.getChildren(OptimizationParameterTag, ns);
if (paramElements != null) {
for (Element paramElement : paramElements) {
String paramName = paramElement.getAttributeValue(OptimizationParameterNameAttribute);
double paramValue = Double.parseDouble(paramElement.getAttributeValue(OptimizationParameterValueAttribute));
paramNames.add(paramName);
paramValues.add(paramValue);
}
}
}
Element bestFuncValueElement = optSolverResultSetElement.getChild(OptXmlTags.ObjectiveFunction_Tag, ns);
double bestFuncValue = Double.parseDouble(bestFuncValueElement.getAttributeValue(OptimizationParameterValueAttribute));
Element numEvaluationsElement = optSolverResultSetElement.getChild(OptXmlTags.OptSolverResultSetFunctionEvaluations_Tag, ns);
long numEvaluations = Long.parseLong(numEvaluationsElement.getAttributeValue(OptimizationParameterValueAttribute));
// change List<Double> to double[]
double[] values = new double[paramValues.size()];
int index = 0;
for (Double value : paramValues) {
values[index++] = value;
}
optRunResultSet = new OptRunResultSet(values, bestFuncValue, numEvaluations, null);
// create optSolverResultSet
optSolverResultSet = new OptSolverResultSet(paramNames.toArray(new String[paramNames.size()]), optRunResultSet);
// create optimization result set
optResultSet = new OptimizationResultSet(optSolverResultSet, null);
}
parameterEstimationTask.setOptimizationResultSet(optResultSet);
}
return parameterEstimationTask;
}
Aggregations