LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qn2jsimg.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_IO_QN2JSIMG_H
6#define LINE_IO_QN2JSIMG_H
7
8/**
9 * @file
10 * @ingroup line_io
11 * Port of `matlab/src/io/QN2JSIMG.m`: write a network to a JMT `.jsimg` FILE
12 * and return its path.
13 *
14 * `jmt_write_jsim` (jmt_writer.h) returns the DOCUMENT TEXT, because SolverJMT
15 * stages it in a directory of its own. The reference's `QN2JSIMG` instead
16 * returns `outputFileName`, the path written, and with no path given it writes
17 * `<lineTempName('jsim')>/model.jsim`. This header supplies exactly that
18 * contract on top of the text writer, so there is still one serializer.
19 *
20 * The temporary directory is NOT removed: the caller is handed its path to
21 * open in JMT (`jsimgView`), as in the reference.
22 */
23
24#include <fstream>
25#include <string>
26
27#include "line/io/jmt_writer.h"
30#include "line/util/error.h"
31#include "line/util/tempdir.h"
32
33namespace line {
34namespace io {
35
36/**
37 * Port of `QN2JSIMG(model, outputFileName, options)` on a refreshed struct.
38 *
39 * @param sn the refreshed struct (`model.getStruct(true)`)
40 * @param path the file to write; empty writes `model.jsim` in a fresh temp directory
41 * @param opt the simulation header controls, `JMTIO`'s properties
42 * @return the path written
43 */
44template <class T>
45std::string qn2jsimg(const qn::NetworkStruct<T>& sn, const std::string& path = std::string(),
47 const std::string out = path.empty() ? util::make_temp_dir("jsim") + "/model.jsim" : path;
49 if (o.log_path.empty()) o.log_path = sn.log_path;
50 const std::string text = jmt_write_jsim(sn, o);
51 std::ofstream f(out.c_str(), std::ios::binary);
52 if (!f) throw InputError("QN2JSIMG: cannot open '" + out + "' for writing");
53 f.write(text.data(), static_cast<std::streamsize>(text.size()));
54 f.close();
55 if (!f) throw InputError("QN2JSIMG: failed to write '" + out + "'");
56 return out;
57}
58
59/** Port of `QN2JSIMG(model, outputFileName, options)` on a model: refreshes it first. */
60template <class T>
61std::string qn2jsimg(qn::Network<T>& model, const std::string& path = std::string(),
63 return qn2jsimg(model.get_struct(), path, opt);
64}
65
66} // namespace io
67} // namespace line
68
69#endif // LINE_IO_QN2JSIMG_H
InputError(const std::string &what)
Definition error.h:39
A network plus its refreshed NetworkStruct.
A queueing network under construction.
const NetworkStruct< T > & get_struct()
The refreshed struct, MATLAB's model.getStruct().
The exception types the port throws.
Port of @@JMTIO: a refreshed NetworkStruct written out as a JMT .jsimg simulation model.
std::string jmt_write_jsim(const qn::NetworkStruct< T > &sn, const JmtWriteOptions &opt)
Port of @@JMTIO/writeJSIM.m: serialize sn as a JMT .jsimg document.
std::string qn2jsimg(const qn::NetworkStruct< T > &sn, const std::string &path=std::string(), const JmtWriteOptions &opt=JmtWriteOptions())
Port of QN2JSIMG(model, outputFileName, options) on a refreshed struct.
Definition qn2jsimg.h:45
std::string make_temp_dir(const std::string &prefix, bool mountable=false)
Create a private scratch directory named after its caller.
Definition tempdir.h:84
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:52
The Network constructor API: Queue, Delay, Source, Sink, Router, ClassSwitch, Cache,...
A queueing network and its refreshed NetworkStruct.
The simulation controls the JSIM header carries, MATLAB's JMTIO properties.
Definition jmt_writer.h:68
std::string log_path
model.getLogPath, the logPath attribute
Definition jmt_writer.h:70
A scratch directory for the subprocess wrappers, the port's lineTempName.