Skip to content

Commit 3e0a57c

Browse files
committed
refactored: MainRecorder records Java data, includes vcml_to_fvsolve
1 parent cf33e59 commit 3e0a57c

File tree

7 files changed

+139
-99
lines changed

7 files changed

+139
-99
lines changed

build.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def main() -> None:
3030
run_command(
3131
"java -agentlib:native-image-agent=config-output-dir=target/recording "
3232
"-jar target/vcell-native-1.0-SNAPSHOT.jar "
33-
"src/test/resources/TinySpacialProject_Application0.xml "
33+
"src/test/resources/TinySpatialProject_Application0.xml "
34+
"src/test/resources/TinySpatialProject_Application0.vcml "
35+
"Simulation0 "
3436
"target/sbml-input",
3537
cwd=vcell_native_dir,
3638
)

scripts/local_build_native.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ mvn clean install
3333
# run with native-image-agent to record configuration for native-image
3434
java -agentlib:native-image-agent=config-output-dir=target/recording \
3535
-jar target/vcell-native-1.0-SNAPSHOT.jar \
36-
"$ROOT_DIR/vcell-native/src/test/resources/TinySpacialProject_Application0.xml" \
36+
"$ROOT_DIR/vcell-native/src/test/resources/TinySpatialProject_Application0.xml" \
37+
"$ROOT_DIR/vcell-native/src/test/resources/TinySpatialProject_Application0.vcml" \
38+
"Simulation0" \
3739
"$ROOT_DIR/vcell-native/target/sbml-input"
3840

3941
# build vcell-native as native shared object library

vcell-native/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<maven.compiler.source>${java.specification.version}</maven.compiler.source>
2424
<maven.compiler.target>${java.specification.version}</maven.compiler.target>
2525
<imageName>libvcell</imageName>
26-
<mainClass>org.vcell.libvcell.Entrypoints</mainClass>
26+
<mainClass>org.vcell.libvcell.MainRecorder</mainClass>
2727
</properties>
2828

2929
<dependencies>
Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,19 @@
11
package org.vcell.libvcell;
22

3-
import cbit.util.xml.VCLoggerException;
4-
import cbit.vcell.biomodel.BioModel;
5-
import cbit.vcell.geometry.GeometrySpec;
6-
import cbit.vcell.mapping.MappingException;
7-
import cbit.vcell.mapping.SimulationContext;
8-
import cbit.vcell.mongodb.VCMongoMessage;
9-
import cbit.vcell.parser.ExpressionException;
10-
import cbit.vcell.resource.PropertyLoader;
11-
import cbit.vcell.solver.Simulation;
12-
import cbit.vcell.solver.SolverException;
13-
import cbit.vcell.solver.TimeBounds;
14-
import cbit.vcell.solver.UniformOutputTimeSpec;
15-
import cbit.vcell.xml.XMLSource;
16-
import cbit.vcell.xml.XmlHelper;
17-
import cbit.vcell.xml.XmlParseException;
183
import org.apache.logging.log4j.LogManager;
194
import org.apache.logging.log4j.Logger;
205
import org.graalvm.nativeimage.IsolateThread;
216
import org.graalvm.nativeimage.c.function.CEntryPoint;
227
import org.graalvm.nativeimage.c.type.CCharPointer;
238
import org.graalvm.nativeimage.c.type.CTypeConversion;
249
import org.json.simple.JSONValue;
25-
import org.vcell.sbml.FiniteVolumeRunUtil;
26-
import org.vcell.sbml.vcell.SBMLExporter;
27-
import org.vcell.sbml.vcell.SBMLImporter;
2810

29-
import java.beans.PropertyVetoException;
30-
import java.io.ByteArrayInputStream;
3111
import java.io.File;
32-
import java.io.FileInputStream;
33-
import java.io.InputStream;
3412
import java.util.concurrent.ConcurrentHashMap;
3513

14+
import static org.vcell.libvcell.SolverUtils.sbmlToFiniteVolumeInput;
15+
import static org.vcell.libvcell.SolverUtils.vcmlToFiniteVolumeInput;
16+
3617

3718
public class Entrypoints {
3819
private static final Logger logger = LogManager.getLogger(Entrypoints.class);
@@ -123,70 +104,4 @@ public static CCharPointer entrypoint_sbmlToFiniteVolumeInput(
123104
logger.info("Returning from sbmlToFiniteVolumeInput: " + json);
124105
return createString(json);
125106
}
126-
127-
128-
public static void vcmlToFiniteVolumeInput(String vcml_content, String simulation_name, File outputDir) throws XmlParseException, MappingException, SolverException, ExpressionException {
129-
GeometrySpec.avoidAWTImageCreation = true;
130-
VCMongoMessage.enabled = false;
131-
if (vcml_content.substring(0, 300).contains("<sbml xmlns=\"http://www.sbml.org/sbml")) {
132-
throw new IllegalArgumentException("expecting VCML content, not SBML");
133-
}
134-
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(vcml_content));
135-
bioModel.updateAll(false);
136-
Simulation sim = bioModel.getSimulation(simulation_name);
137-
if (sim == null) {
138-
throw new IllegalArgumentException("Simulation not found: " + simulation_name);
139-
}
140-
FiniteVolumeRunUtil.writeInputFilesOnly(outputDir, sim);
141-
}
142-
143-
144-
public static void sbmlToFiniteVolumeInput(String sbml_content, File outputDir) throws MappingException, PropertyVetoException, SolverException, ExpressionException, VCLoggerException {
145-
GeometrySpec.avoidAWTImageCreation = true;
146-
VCMongoMessage.enabled = false;
147-
SBMLExporter.MemoryVCLogger vcl = new SBMLExporter.MemoryVCLogger();
148-
boolean bValidateSBML = true;
149-
// input stream from sbml_content String
150-
if (sbml_content.substring(0, 300).contains("<vcml xmlns=\"http://sourceforge.net/projects/vcell")) {
151-
throw new IllegalArgumentException("expecting SBML content, not VCML");
152-
}
153-
InputStream is = new ByteArrayInputStream(sbml_content.getBytes());
154-
SBMLImporter importer = new SBMLImporter(is, vcl, bValidateSBML);
155-
BioModel bioModel = importer.getBioModel();
156-
bioModel.updateAll(false);
157-
158-
final double duration = 5.0; // endpoint arg
159-
final double time_step = 0.1; // endpoint arg
160-
//final ISize meshSize = new ISize(10, 10, 10); // future endpoint arg
161-
SimulationContext simContext = bioModel.getSimulationContext(0);
162-
Simulation sim = new Simulation(simContext.getMathDescription(), simContext);
163-
sim.getSolverTaskDescription().setTimeBounds(new TimeBounds(0.0, duration));
164-
sim.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec(time_step));
165-
166-
FiniteVolumeRunUtil.writeInputFilesOnly(outputDir, sim);
167-
}
168-
169-
170-
171-
// Input Goes as Follows: SBML Input, Output dir
172-
// "/Users/evalencia/Documents/VCell_Repositories/vcell/vcell-rest/src/test/resources/TinySpacialProject_Application0.xml"
173-
// "/Users/evalencia/Documents/VCell_Repositories/vcell/vcell-nativelib/target/sbml-input"
174-
public static void main(String[] args) {
175-
try {
176-
logger.info("Logger logging");
177-
PropertyLoader.setProperty(PropertyLoader.vcellServerIDProperty, "none");
178-
PropertyLoader.setProperty(PropertyLoader.mongodbDatabase, "none");
179-
File sbml_file = new File(args[0]);
180-
// read sbml_file and create a string object
181-
try (FileInputStream fis = new FileInputStream(sbml_file)) {
182-
byte[] data = fis.readAllBytes();
183-
logger.info("Read " + data.length + " bytes from " + sbml_file);
184-
String sbml_str = new String(data);
185-
sbmlToFiniteVolumeInput(sbml_str, new File(args[1]));
186-
}
187-
} catch (Exception e) {
188-
System.out.println(e.getMessage());
189-
logger.error("Error processing spatial model", e);
190-
}
191-
}
192-
}
107+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.vcell.libvcell;
2+
3+
import cbit.vcell.resource.PropertyLoader;
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
7+
import java.io.File;
8+
import java.io.FileInputStream;
9+
10+
import static org.vcell.libvcell.SolverUtils.sbmlToFiniteVolumeInput;
11+
import static org.vcell.libvcell.SolverUtils.vcmlToFiniteVolumeInput;
12+
13+
14+
public class MainRecorder {
15+
private static final Logger logger = LogManager.getLogger(MainRecorder.class);
16+
17+
public static void main(String[] args) {
18+
try {
19+
File sbml_file = new File(args[0]);
20+
File vcml_file = new File(args[1]);
21+
String vcml_sim_name = args[2];
22+
File output_dir = new File(args[3]);
23+
logger.info("Logger logging");
24+
PropertyLoader.setProperty(PropertyLoader.vcellServerIDProperty, "none");
25+
PropertyLoader.setProperty(PropertyLoader.mongodbDatabase, "none");
26+
27+
28+
try (FileInputStream f_sbml = new FileInputStream(sbml_file)) {
29+
byte[] data = f_sbml.readAllBytes();
30+
logger.info("Read " + data.length + " bytes from " + sbml_file.getAbsolutePath());
31+
String sbml_str = new String(data);
32+
sbmlToFiniteVolumeInput(sbml_str, output_dir);
33+
//vcmlToFiniteVolumeInput(vcml_str, sim_name, new File(args[1]));
34+
}
35+
// read sbml_file and create a string object
36+
try (FileInputStream f_vcml = new FileInputStream(vcml_file)) {
37+
byte[] data = f_vcml.readAllBytes();
38+
logger.info("Read " + data.length + " bytes from " + vcml_file.getAbsolutePath());
39+
String vcml_str = new String(data);
40+
vcmlToFiniteVolumeInput(vcml_str, vcml_sim_name, output_dir);
41+
}
42+
} catch (Exception e) {
43+
System.out.println(e.getMessage());
44+
logger.error("Error processing spatial model", e);
45+
}
46+
}
47+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.vcell.libvcell;
2+
3+
import cbit.util.xml.VCLoggerException;
4+
import cbit.vcell.biomodel.BioModel;
5+
import cbit.vcell.geometry.GeometrySpec;
6+
import cbit.vcell.mapping.MappingException;
7+
import cbit.vcell.mapping.SimulationContext;
8+
import cbit.vcell.mongodb.VCMongoMessage;
9+
import cbit.vcell.parser.ExpressionException;
10+
import cbit.vcell.solver.Simulation;
11+
import cbit.vcell.solver.SolverException;
12+
import cbit.vcell.solver.TimeBounds;
13+
import cbit.vcell.solver.UniformOutputTimeSpec;
14+
import cbit.vcell.xml.XMLSource;
15+
import cbit.vcell.xml.XmlHelper;
16+
import cbit.vcell.xml.XmlParseException;
17+
import org.apache.logging.log4j.LogManager;
18+
import org.apache.logging.log4j.Logger;
19+
import org.vcell.sbml.FiniteVolumeRunUtil;
20+
import org.vcell.sbml.vcell.SBMLExporter;
21+
import org.vcell.sbml.vcell.SBMLImporter;
22+
23+
import java.beans.PropertyVetoException;
24+
import java.io.ByteArrayInputStream;
25+
import java.io.File;
26+
import java.io.InputStream;
27+
28+
29+
public class SolverUtils {
30+
private static final Logger logger = LogManager.getLogger(Entrypoints.class);
31+
32+
public static void vcmlToFiniteVolumeInput(String vcml_content, String simulation_name, File outputDir) throws XmlParseException, MappingException, SolverException, ExpressionException {
33+
GeometrySpec.avoidAWTImageCreation = true;
34+
VCMongoMessage.enabled = false;
35+
if (vcml_content.substring(0, 300).contains("<sbml xmlns=\"http://www.sbml.org/sbml")) {
36+
throw new IllegalArgumentException("expecting VCML content, not SBML");
37+
}
38+
BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(vcml_content));
39+
bioModel.updateAll(false);
40+
Simulation sim = bioModel.getSimulation(simulation_name);
41+
if (sim == null) {
42+
throw new IllegalArgumentException("Simulation not found: " + simulation_name);
43+
}
44+
FiniteVolumeRunUtil.writeInputFilesOnly(outputDir, sim);
45+
}
46+
47+
48+
public static void sbmlToFiniteVolumeInput(String sbml_content, File outputDir) throws MappingException, PropertyVetoException, SolverException, ExpressionException, VCLoggerException {
49+
GeometrySpec.avoidAWTImageCreation = true;
50+
VCMongoMessage.enabled = false;
51+
SBMLExporter.MemoryVCLogger vcl = new SBMLExporter.MemoryVCLogger();
52+
boolean bValidateSBML = true;
53+
// input stream from sbml_content String
54+
if (sbml_content.substring(0, 300).contains("<vcml xmlns=\"http://sourceforge.net/projects/vcell")) {
55+
throw new IllegalArgumentException("expecting SBML content, not VCML");
56+
}
57+
InputStream is = new ByteArrayInputStream(sbml_content.getBytes());
58+
SBMLImporter importer = new SBMLImporter(is, vcl, bValidateSBML);
59+
BioModel bioModel = importer.getBioModel();
60+
bioModel.updateAll(false);
61+
62+
final double duration = 5.0; // endpoint arg
63+
final double time_step = 0.1; // endpoint arg
64+
//final ISize meshSize = new ISize(10, 10, 10); // future endpoint arg
65+
SimulationContext simContext = bioModel.getSimulationContext(0);
66+
Simulation sim = new Simulation(simContext.getMathDescription(), simContext);
67+
sim.getSolverTaskDescription().setTimeBounds(new TimeBounds(0.0, duration));
68+
sim.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec(time_step));
69+
70+
FiniteVolumeRunUtil.writeInputFilesOnly(outputDir, sim);
71+
}
72+
}

vcell-native/src/test/java/org/vcell/libvcell/EntrypointsTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@
1616

1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertThrows;
19+
import static org.vcell.libvcell.SolverUtils.sbmlToFiniteVolumeInput;
20+
import static org.vcell.libvcell.SolverUtils.vcmlToFiniteVolumeInput;
1921

2022
public class EntrypointsTest {
2123

2224
@Test
2325
public void testSbmlToFiniteVolumeInput() throws PropertyVetoException, SolverException, ExpressionException, MappingException, VCLoggerException, IOException {
2426
String sbmlContent = getFileContentsAsString("/TinySpatialProject_Application0.xml");
2527
Path output_dir = Files.createTempDirectory("sbmlToFiniteVolumeInput");
26-
Entrypoints.sbmlToFiniteVolumeInput(sbmlContent, output_dir.toFile());
28+
sbmlToFiniteVolumeInput(sbmlContent, output_dir.toFile());
2729
}
2830

2931
// TODO: better exception handling by JSBML needed
3032
@Test
3133
public void testSbmlToFiniteVolumeInput_not_well_formed() throws IOException {
3234
String sbmlContent = getFileContentsAsString("/TinySpatialProject_Application0.xml").replaceAll("<model ", "<modelXYZ ");
3335
Path output_dir = Files.createTempDirectory("sbmlToFiniteVolumeInput");
34-
ClassCastException exc = assertThrows(ClassCastException.class, () -> Entrypoints.sbmlToFiniteVolumeInput(sbmlContent, output_dir.toFile()));
36+
ClassCastException exc = assertThrows(ClassCastException.class, () -> sbmlToFiniteVolumeInput(sbmlContent, output_dir.toFile()));
3537
assertEquals(
3638
"class org.sbml.jsbml.xml.XMLNode cannot be cast to class org.sbml.jsbml.Annotation " +
3739
"(org.sbml.jsbml.xml.XMLNode and org.sbml.jsbml.Annotation are in unnamed module of loader 'app')",
@@ -42,7 +44,7 @@ public void testSbmlToFiniteVolumeInput_not_well_formed() throws IOException {
4244
public void testSbmlToFiniteVolumeInput_vcml_instead() throws IOException {
4345
String sbmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");
4446
Path output_dir = Files.createTempDirectory("sbmlToFiniteVolumeInput");
45-
IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> Entrypoints.sbmlToFiniteVolumeInput(sbmlContent, output_dir.toFile()));
47+
IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> sbmlToFiniteVolumeInput(sbmlContent, output_dir.toFile()));
4648
assertEquals("expecting SBML content, not VCML", exc.getMessage());
4749
}
4850

@@ -51,7 +53,7 @@ public void testVcmlToFiniteVolumeInput() throws SolverException, ExpressionExce
5153
String vcmlContent = getFileContentsAsString("/TinySpatialProject_Application0.vcml");
5254
Path output_dir = Files.createTempDirectory("vcmlToFiniteVolumeInput");
5355
String simulationName = "Simulation0";
54-
Entrypoints.vcmlToFiniteVolumeInput(vcmlContent, simulationName, output_dir.toFile());
56+
vcmlToFiniteVolumeInput(vcmlContent, simulationName, output_dir.toFile());
5557
}
5658

5759
@Test
@@ -60,7 +62,7 @@ public void testVcmlToFiniteVolumeInput_bad_simname() throws IOException {
6062
Path output_dir = Files.createTempDirectory("vcmlToFiniteVolumeInput");
6163
String simulationName = "wrong_sim_name";
6264
// expect to throw an IllegalArgumentException
63-
IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> Entrypoints.vcmlToFiniteVolumeInput(vcmlContent, simulationName, output_dir.toFile()));
65+
IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> vcmlToFiniteVolumeInput(vcmlContent, simulationName, output_dir.toFile()));
6466
assertEquals("Simulation not found: wrong_sim_name", exc.getMessage());
6567
}
6668

@@ -71,7 +73,7 @@ public void testVcmlToFiniteVolumeInput_not_well_formed() throws IOException {
7173
Path output_dir = Files.createTempDirectory("vcmlToFiniteVolumeInput");
7274
String simulationName = "wrong_sim_name";
7375
// expect to throw an IllegalArgumentException
74-
RuntimeException exc = assertThrows(RuntimeException.class, () -> Entrypoints.vcmlToFiniteVolumeInput(vcmlContent, simulationName, output_dir.toFile()));
76+
RuntimeException exc = assertThrows(RuntimeException.class, () -> vcmlToFiniteVolumeInput(vcmlContent, simulationName, output_dir.toFile()));
7577
assertEquals(
7678
"source document is not well-formed\n" +
7779
"Error on line 25: The element type \"SimpleReactionXYZ\" must be terminated by the matching end-tag \"</SimpleReactionXYZ>\".",
@@ -84,7 +86,7 @@ public void testVcmlToFiniteVolumeInput_sbml_instead() throws IOException {
8486
Path output_dir = Files.createTempDirectory("vcmlToFiniteVolumeInput");
8587
String simulationName = "wrong_sim_name";
8688
// expect to throw an IllegalArgumentException
87-
IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> Entrypoints.vcmlToFiniteVolumeInput(vcmlContent_actually_sbml, simulationName, output_dir.toFile()));
89+
IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> vcmlToFiniteVolumeInput(vcmlContent_actually_sbml, simulationName, output_dir.toFile()));
8890
assertEquals("expecting VCML content, not SBML", exc.getMessage());
8991
}
9092

0 commit comments

Comments
 (0)