Browse Source

Export DFT to Cytoscape JSON format

tempestpy_adaptions
Matthias Volk 8 years ago
parent
commit
743ce2b023
  1. 12
      src/storm-dft-cli/storm-dyftee.cpp
  2. 12
      src/storm-dft/settings/modules/DFTSettings.cpp
  3. 15
      src/storm-dft/settings/modules/DFTSettings.h
  4. 2
      src/storm-dft/storage/dft/DFTElementType.h
  5. 122
      src/storm-dft/storage/dft/DftJsonExporter.cpp
  6. 47
      src/storm-dft/storage/dft/DftJsonExporter.h

12
src/storm-dft-cli/storm-dyftee.cpp

@ -19,7 +19,7 @@
#include "storm-dft/modelchecker/dft/DFTModelChecker.h"
#include "storm-dft/modelchecker/dft/DFTASFChecker.h"
#include "storm-dft/transformations/DftToGspnTransformator.h"
#include "storm-dft/storage/dft/DftJsonExporter.h"
#include "storm-dft/settings/modules/DFTSettings.h"
@ -129,6 +129,16 @@ int main(const int argc, const char** argv) {
STORM_LOG_THROW(false, storm::exceptions::InvalidSettingsException, "No input model.");
}
if (dftSettings.isExportToJson()) {
STORM_LOG_THROW(dftSettings.isDftFileSet(), storm::exceptions::InvalidSettingsException, "No input model in Galileo format given.");
storm::parser::DFTGalileoParser<double> parser;
storm::storage::DFT<double> dft = parser.parseDFT(dftSettings.getDftFilename());
// Export to json
storm::storage::DftJsonExporter<double>::toFile(dft, dftSettings.getExportJsonFilename());
storm::utility::cleanUp();
return 0;
}
if (dftSettings.isTransformToGspn()) {
std::shared_ptr<storm::storage::DFT<double>> dft;

12
src/storm-dft/settings/modules/DFTSettings.cpp

@ -32,6 +32,7 @@ namespace storm {
const std::string DFTSettings::minValueOptionName = "min";
const std::string DFTSettings::maxValueOptionName = "max";
const std::string DFTSettings::transformToGspnOptionName = "gspn";
const std::string DFTSettings::exportToJsonOptionName = "export-json";
#ifdef STORM_HAVE_Z3
const std::string DFTSettings::solveWithSmtOptionName = "smt";
#endif
@ -55,8 +56,9 @@ namespace storm {
this->addOption(storm::settings::OptionBuilder(moduleName, solveWithSmtOptionName, true, "Solve the DFT with SMT.").build());
#endif
this->addOption(storm::settings::OptionBuilder(moduleName, transformToGspnOptionName, false, "Transform DFT to GSPN.").build());
this->addOption(storm::settings::OptionBuilder(moduleName, exportToJsonOptionName, false, "Export the model to the Cytoscape JSON format.").addArgument(storm::settings::ArgumentBuilder::createStringArgument("filename", "The name of the JSON file to export to.").build()).build());
}
bool DFTSettings::isDftFileSet() const {
return this->getOption(dftFileOptionName).getHasOptionBeenSet();
}
@ -141,6 +143,14 @@ namespace storm {
return this->getOption(transformToGspnOptionName).getHasOptionBeenSet();
}
bool DFTSettings::isExportToJson() const {
return this->getOption(exportToJsonOptionName).getHasOptionBeenSet();
}
std::string DFTSettings::getExportJsonFilename() const {
return this->getOption(exportToJsonOptionName).getArgumentByName("filename").getValueAsString();
}
void DFTSettings::finalize() {
}

15
src/storm-dft/settings/modules/DFTSettings.h

@ -138,6 +138,20 @@ namespace storm {
* @return True iff the option was set.
*/
bool isTransformToGspn() const;
/*!
* Retrieves whether the export to Json file option was set.
*
* @return True if the export to json file option was set.
*/
bool isExportToJson() const;
/*!
* Retrieves the name of the json file to export to.
*
* @return The name of the json file to export to.
*/
std::string getExportJsonFilename() const;
#ifdef STORM_HAVE_Z3
/*!
@ -177,6 +191,7 @@ namespace storm {
static const std::string solveWithSmtOptionName;
#endif
static const std::string transformToGspnOptionName;
static const std::string exportToJsonOptionName;
};

2
src/storm-dft/storage/dft/DFTElementType.h

@ -75,7 +75,7 @@ namespace storm {
}
inline std::ostream& operator<<(std::ostream& os, DFTElementType const& tp) {
return os << toString(tp) << std::endl;
return os << toString(tp);
}

122
src/storm-dft/storage/dft/DftJsonExporter.cpp

@ -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
}
}

47
src/storm-dft/storage/dft/DftJsonExporter.h

@ -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);
};
}
}
Loading…
Cancel
Save