VPTissue Reference Manual
SimWrapper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "SimWrapper.h"
21 
22 #include "Sim.h"
23 #include "SimState.h"
24 #include "fileformats/PTreeFile.h"
26 
27 #include <boost/property_tree/ptree.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
29 #include <sstream>
30 
31 namespace SimPT_Sim {
32 
33 using namespace std;
34 using namespace boost::property_tree;
35 using namespace boost::property_tree::xml_parser;
36 using namespace SimPT_Sim::Util;
37 
38 SimWrapper::SimWrapper()
39  : m_sim(make_shared<Sim>())
40 {
41 }
42 
44 {
45  try {
46  return { SUCCESS, string(), m_sim->GetState() };
47  }
48  catch (std::exception & e) {
49  return { FAILURE, e.what(), SimState() };
50  }
51  catch ( ... ) {
52  return { FAILURE, "Unknown exception", SimState() };
53  }
54 }
55 
57 {
58  try {
59  stringstream ss;
60  write_xml(ss, m_sim->ToPtree(), XmlWriterSettings::GetTab());
61  return { SUCCESS, string(), ss.str() };
62  }
63  catch (std::exception & e) {
64  return { FAILURE, e.what(), string() };
65  }
66  catch ( ... ) {
67  return { FAILURE, "Unknown exception", string() };
68  }
69 }
70 
72 {
73  try {
74  m_sim->Initialize(state);
75  return { SUCCESS, string() };
76  }
77  catch (std::exception & e) {
78  return { FAILURE, e.what() };
79  }
80  catch ( ... ) {
81  return { FAILURE, "Unknown exception" };
82  }
83 }
84 
86 {
87  try {
88  ptree pt;
89  PTreeFile::Read(path, pt);
90  m_sim->Initialize(pt);
91  return { SUCCESS, string() };
92  }
93  catch (std::exception & e) {
94  return { FAILURE, e.what() };
95  }
96  catch ( ... ) {
97  return { FAILURE, "Unknown exception" };
98  }
99 }
100 
102 {
103  try {
104  m_sim->TimeStep();
105  return { SUCCESS, string() };
106  }
107  catch (std::exception & e) {
108  return { FAILURE, e.what() };
109  }
110  catch ( ... ) {
111  return { FAILURE, "Unknown exception" };
112  }
113 }
114 
115 } // namespace
STL namespace.
Interface for PTreeFile.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
Interface for SimWrapper.
Specialization of SimWrapperResult template for type void.
Definition: SimWrapper.h:49
SimWrapperResult< std::string > GetXMLState() const
Provide sim state in XML format serialized to string.
Definition: SimWrapper.cpp:56
Namespace for the core simulator.
SimWrapperResult< void > Initialize(SimState state)
Set sim state.
Definition: SimWrapper.cpp:71
Header for SimState.
Sim, the actual simulator.
All exceptions must be dealt with internally, so each method returns an object of this type...
Definition: SimWrapper.h:39
Contains the state of the whole Simulator at a given simulation step.
Definition: SimState.h:33
SimWrapperResult< void > TimeStep()
Let simulator take a time step.
Definition: SimWrapper.cpp:101
SimWrapperResult< SimState > GetState() const
Provide sim state in format suitable for i/o.
Definition: SimWrapper.cpp:43
Xml writer settings class.