LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
tables.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// builder.h is included before the guard: it includes this header at its
6// foot, once Agent and Composition are complete.
8
9#ifndef LINE_LANG_LAL_TABLES_H
10#define LINE_LANG_LAL_TABLES_H
11
12/**
13 * @file
14 * @ingroup line_lang
15 * LINE Agent Language (LAL): label lookups on LAL action tables
16 * (`getAvgAct` / `getActAvg`, `Composition::hide`). Port of
17 * python/line_solver/lal/tables.py.
18 *
19 * An action is named in one of four ways (`ActionSpec`):
20 *
21 * - a label string, exactly as it appears in `getActTable()` (a trailing
22 * `[*]` sums an index family, e.g. `Queue1.DEP[*]`);
23 * - an `ActionRef` of a hand-declared agent (`agent.action("x")`);
24 * - a `NodeRef` from `Composition::action(...)`, whose open class sums the
25 * family over classes;
26 * - `(node, EventType[, class])`, resolved through `Composition::action`
27 * (`ActionSpec::key`).
28 */
29
30#include <optional>
31#include <set>
32#include <string>
33#include <utility>
34#include <vector>
35
36#include "line/lang/lal/core.h"
37
38namespace line {
39namespace lal {
40
41/** One way of naming an action of a composition. */
42struct ActionSpec {
43 enum class Kind { Label, Agent, Node, Key };
44 Kind kind = Kind::Label;
45 std::string label;
46 std::optional<ActionRef> ref;
47 std::optional<NodeRef> node;
48 // Kind::Key
49 std::string node_name;
50 std::size_t node_index = 0;
51 bool by_index = false;
52 lang::EventType event = lang::EventType::LOCAL;
53 std::optional<std::string> jobclass;
54 std::size_t class_index = 0;
55 std::optional<std::string> mode;
56 std::optional<std::string> part;
57
58 ActionSpec(const std::string& l) : kind(Kind::Label), label(l) {}
59 ActionSpec(const char* l) : kind(Kind::Label), label(l) {}
60 ActionSpec(const ActionRef& r) : kind(Kind::Agent), ref(r) {}
61 ActionSpec(const NodeRef& n) : kind(Kind::Node), node(n) {}
62
63 /** `(node, EventType[, class])` by names; `mode` names a Petri-net mode of ENABLE/FIRE. */
64 static ActionSpec key(const std::string& node, lang::EventType event,
65 const std::optional<std::string>& jobclass = std::nullopt,
66 const std::optional<std::string>& mode = std::nullopt,
67 const std::optional<std::string>& part = std::nullopt) {
68 ActionSpec s{std::string()};
69 s.kind = Kind::Key;
70 s.node_name = node;
71 s.event = event;
72 s.jobclass = jobclass;
73 s.mode = mode;
74 s.part = part;
75 return s;
76 }
77 /** `(node, EventType[, class])` by 1-based node and class indices (class 0: every class). */
78 static ActionSpec key(std::size_t node, lang::EventType event, std::size_t jobclass = 0,
79 const std::optional<std::string>& part = std::nullopt) {
80 ActionSpec s{std::string()};
81 s.kind = Kind::Key;
82 s.by_index = true;
83 s.node_index = node;
84 s.event = event;
85 s.class_index = jobclass;
86 s.part = part;
87 return s;
88 }
89};
90
91/** `(label, family)`: an exact label, or a prefix whose index family is summed (`resolve_labels`). */
92inline std::pair<std::string, bool> resolve_labels(const Composition& composition, const ActionSpec& spec) {
93 NodeEvent key;
94 switch (spec.kind) {
95 case ActionSpec::Kind::Label: {
96 const std::string& l = spec.label;
97 if (l.size() >= 3 && l.compare(l.size() - 3, 3, "[*]") == 0) return {l.substr(0, l.size() - 3), true};
98 return {l, false};
99 }
100 case ActionSpec::Kind::Agent: {
101 const ActionRef& r = *spec.ref;
102 std::set<std::string> labels;
103 for (const Transition& t : r.agent.transitions())
104 if (t.action == r.name) labels.insert(transition_label(r.agent.name(), t));
105 if (labels.size() != 1)
106 throw InputError(ErrorKind("Argument"), r.agent.name() + "." + r.name + " has no single event label.");
107 return {*labels.begin(), false};
108 }
109 case ActionSpec::Kind::Node:
110 key = spec.node->key;
111 break;
112 case ActionSpec::Kind::Key:
113 key = spec.by_index ? composition.action(spec.node_index, spec.event, spec.class_index, spec.part).key
114 : composition.action(spec.node_name, spec.event, spec.jobclass, spec.mode, spec.part).key;
115 break;
116 }
117 return {key.label(), !key.index.has_value()};
118}
119
120/** Throughput of one action, or the sum over an open index family (`act_value`). */
121template <class Table>
122double act_value(const Composition& composition, const Table& table, const ActionSpec& spec) {
123 const std::pair<std::string, bool> lf = resolve_labels(composition, spec);
124 const std::string& label = lf.first;
125 double total = 0.0;
126 bool any = false;
127 for (const auto& row : table.rows) {
128 const std::string& a = row.action;
129 const bool hit = lf.second ? (a == label || a.compare(0, label.size() + 1, label + "[") == 0) : a == label;
130 if (hit) {
131 any = true;
132 total += row.throughput;
133 }
134 }
135 if (!any)
136 throw InputError(ErrorKind("Argument"),
137 "no action '" + label + "' in this composition; see getActTable().");
138 return total;
139}
140
141inline Composition& Composition::hide(const ActionSpec& action) {
142 const std::pair<std::string, bool> lf = resolve_labels(*this, action);
143 hidden_.insert(lf.second ? lf.first + "[*]" : lf.first);
144 return *this;
145}
146
147inline Composition& Composition::hide(const std::vector<ActionSpec>& actions) {
148 for (const ActionSpec& a : actions) hide(a);
149 return *this;
150}
151
152/** True when `Composition::hide` removed `label` from the action tables (`is_hidden`). */
153inline bool is_hidden(const Model& snapshot, const std::string& label) {
154 for (const std::string& h : snapshot.hidden) {
155 if (h == label) return true;
156 if (h.size() >= 3 && h.compare(h.size() - 3, 3, "[*]") == 0) {
157 const std::string base = h.substr(0, h.size() - 3);
158 if (label == base || label.compare(0, base.size() + 1, base + "[") == 0) return true;
159 }
160 }
161 return false;
162}
163
164} // namespace lal
165} // namespace line
166
167#endif // LINE_LANG_LAL_TABLES_H
LINE Agent Language (LAL): programmatic construction of finite agents and their composition,...
InputError(const std::string &what)
Definition error.h:105
A collection of agents and embedded LINE models with atomic synchronisations (Composition).
Definition builder.h:655
Composition & hide(const ActionSpec &action)
Make actions internal (hide): they still occur, but leave the action tables.
friend struct ActionSpec
Definition builder.h:974
NodeRef action(const std::string &node, lang::EventType event, const std::optional< std::string > &jobclass=std::nullopt, const std::optional< std::string > &mode=std::nullopt, const std::optional< std::string > &part=std::nullopt) const
A node action of an embedded LINE model (action): node and jobclass are names, mode a Petri-net mode ...
Definition builder.h:776
LINE Agent Language (LAL): the finite-agent model record, its validation, the joint-state event kerne...
Definition ag.h:49
std::string transition_label(const std::string &agent, const Transition &t)
Primary label of a transition (its first label when it carries several).
Definition core.h:453
const std::map< std::string, std::vector< std::string > > & table()
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
ErrorKind(std::string n)
Definition error.h:39
A composition snapshot (core.Model): agents in declaration order plus synchronisations.
Definition core.h:310
Key of an embedded node action: (node, EventType, class | mode).
Definition core.h:153
One labelled local transition source --(action, value)--> target.
Definition core.h:111