6 changed files with 207 additions and 3 deletions
			
			
		- 
					12src/storm-dft-cli/storm-dyftee.cpp
- 
					12src/storm-dft/settings/modules/DFTSettings.cpp
- 
					15src/storm-dft/settings/modules/DFTSettings.h
- 
					2src/storm-dft/storage/dft/DFTElementType.h
- 
					122src/storm-dft/storage/dft/DftJsonExporter.cpp
- 
					47src/storm-dft/storage/dft/DftJsonExporter.h
| @ -0,0 +1,122 @@ | |||
| #include "DftJsonExporter.h"
 | |||
| 
 | |||
| #include "storm/exceptions/NotImplementedException.h"
 | |||
| #include "storm/exceptions/FileIOException.h"
 | |||
| 
 | |||
| #include <algorithm>
 | |||
| #include <string>
 | |||
| 
 | |||
| namespace storm { | |||
|     namespace storage { | |||
| 
 | |||
|         template<typename ValueType> | |||
|         size_t DftJsonExporter<ValueType>::currentId = 0; | |||
| 
 | |||
|         template<typename ValueType> | |||
|         void DftJsonExporter<ValueType>::toFile(storm::storage::DFT<ValueType> const& dft, std::string const& filepath) { | |||
|             std::ofstream ofs; | |||
|             ofs.open(filepath, std::ofstream::out); | |||
|             if(ofs.is_open()) { | |||
|                 toStream(dft, ofs); | |||
|                 ofs.close(); | |||
|             } else { | |||
|                 STORM_LOG_THROW(false, storm::exceptions::FileIoException, "Cannot open " << filepath); | |||
|             } | |||
|         } | |||
| 
 | |||
|         template<typename ValueType> | |||
|         void DftJsonExporter<ValueType>::toStream(storm::storage::DFT<ValueType> const& dft, std::ostream& os) { | |||
|             os << translate(dft).dump(4) << std::endl; | |||
|         } | |||
| 
 | |||
|         template<typename ValueType> | |||
|         modernjson::json DftJsonExporter<ValueType>::translate(storm::storage::DFT<ValueType> const& dft) { | |||
|             modernjson::json jsonDft; | |||
|             currentId = 0; | |||
| 
 | |||
|             // Nodes
 | |||
|             modernjson::json jsonNodes; | |||
|             for (size_t i = 0; i < dft.nrElements(); ++i) { | |||
|                 modernjson::json jsonNode = translateNode(dft.getElement(i)); | |||
|                 jsonDft.push_back(jsonNode); | |||
|             } | |||
| 
 | |||
|             // Edges
 | |||
|             modernjson::json jsonEdges; | |||
|             for (size_t i = 0; i < dft.nrElements(); ++i) { | |||
|                 if (dft.isGate(i)) { | |||
|                     std::shared_ptr<DFTGate<ValueType> const> gate = dft.getGate(i); | |||
|                     for (size_t index = 0; index < gate->nrChildren(); ++index) { | |||
|                         DFTElementPointer child = gate->children()[index]; | |||
|                         modernjson::json jsonEdge = translateEdge(gate, child, index); | |||
|                         jsonDft.push_back(jsonEdge); | |||
|                     } | |||
|                 } | |||
|             } | |||
| 
 | |||
|             return jsonDft; | |||
|         } | |||
| 
 | |||
|         template<typename ValueType> | |||
|         modernjson::json DftJsonExporter<ValueType>::translateNode(DFTElementCPointer const& element) { | |||
|             modernjson::json nodeData; | |||
|             nodeData["id"] = element->id(); | |||
|             ++currentId; | |||
|             STORM_LOG_ASSERT(element->id() == currentId-1, "Ids do not correspond"); | |||
|             nodeData["name"] = element->name(); | |||
| 
 | |||
|             if (element->isGate()) { | |||
|                 // Set children for gate
 | |||
|                 std::shared_ptr<DFTGate<ValueType> const> gate = std::static_pointer_cast<DFTGate<ValueType> const>(element); | |||
|                 std::vector<size_t> children; | |||
|                 for (DFTElementPointer const& child : gate->children()) { | |||
|                     children.push_back(child->id()); | |||
|                 } | |||
|                 nodeData["children"] = children; | |||
|             } else if (element->isBasicElement()) { | |||
|                 // Set rates for BE
 | |||
|                 std::shared_ptr<DFTBE<ValueType> const> be = std::static_pointer_cast<DFTBE<ValueType> const>(element); | |||
|                 std::stringstream stream; | |||
|                 stream << be->activeFailureRate(); | |||
|                 nodeData["rate"] = stream.str(); | |||
|                 stream.clear(); | |||
|                 stream << (be->passiveFailureRate() / be->activeFailureRate()); | |||
|                 nodeData["dorm"] = stream.str(); | |||
|             } | |||
| 
 | |||
|             modernjson::json jsonNode; | |||
|             jsonNode["data"] = nodeData; | |||
| 
 | |||
|             jsonNode["group"] = "nodes"; | |||
|             std::string type = storm::storage::toString(element->type()); | |||
|             std::transform(type.begin(), type.end(), type.begin(), ::tolower); | |||
|             jsonNode["classes"] = type; | |||
|             return jsonNode; | |||
|         } | |||
| 
 | |||
|         template<typename ValueType> | |||
|         modernjson::json DftJsonExporter<ValueType>::translateEdge(std::shared_ptr<DFTGate<ValueType> const> const& gate, DFTElementCPointer const& child, size_t index) { | |||
|             modernjson::json nodeData; | |||
|             nodeData["id"] = currentId; | |||
|             ++currentId; | |||
|             nodeData["source"] = gate->id(); | |||
|             nodeData["target"] = child->id(); | |||
|             nodeData["index"] = index; | |||
| 
 | |||
|             modernjson::json jsonNode; | |||
|             jsonNode["data"] = nodeData; | |||
| 
 | |||
|             jsonNode["group"] = "edges"; | |||
|             jsonNode["classes"] = ""; | |||
|             return jsonNode; | |||
|         } | |||
| 
 | |||
|         // Explicitly instantiate the class.
 | |||
|         template class DftJsonExporter<double>; | |||
| 
 | |||
| #ifdef STORM_HAVE_CARL
 | |||
|         template class DftJsonExporter<storm::RationalFunction>; | |||
| #endif
 | |||
| 
 | |||
|     } | |||
| } | |||
| @ -0,0 +1,47 @@ | |||
| #pragma  once | |||
| 
 | |||
| #include "storm/utility/macros.h" | |||
| 
 | |||
| #include "storm-dft/storage/dft/DFT.h" | |||
| 
 | |||
| // JSON parser | |||
| #include "json.hpp" | |||
| namespace modernjson { | |||
|     using json = nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double, std::allocator>; | |||
| } | |||
| 
 | |||
| namespace storm { | |||
|     namespace storage { | |||
| 
 | |||
|         /** | |||
|          * Exports a DFT into the JSON format for visualizing it. | |||
|          */ | |||
|         template<typename ValueType> | |||
|         class DftJsonExporter { | |||
| 
 | |||
|             using DFTElementPointer = std::shared_ptr<DFTElement<ValueType>>; | |||
|             using DFTElementCPointer = std::shared_ptr<DFTElement<ValueType> const>; | |||
|             using DFTGatePointer = std::shared_ptr<DFTGate<ValueType>>; | |||
| 
 | |||
|         public: | |||
| 
 | |||
|             static void toFile(storm::storage::DFT<ValueType> const& dft, std::string const& filepath); | |||
| 
 | |||
|             static void toStream(storm::storage::DFT<ValueType> const& dft, std::ostream& os); | |||
| 
 | |||
|             static modernjson::json translate(storm::storage::DFT<ValueType> const& dft); | |||
| 
 | |||
| 
 | |||
|         private: | |||
| 
 | |||
|             static size_t currentId; | |||
| 
 | |||
|             static modernjson::json translateNode(DFTElementCPointer const& element); | |||
| 
 | |||
|             static modernjson::json translateEdge(std::shared_ptr<DFTGate<ValueType> const> const& gate, DFTElementCPointer const& child, size_t index); | |||
| 
 | |||
| 
 | |||
|         }; | |||
|         | |||
|     } | |||
| } | |||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue