Skip to content

packages/engine/scram-node/src/event_tree.h

Event Tree facilities.

Namespaces

Name
scram
scram::mef

Classes

Name
classscram::mef::Sequence <br>Representation of sequences in event trees.
classscram::mef::FunctionalEvent <br>Representation of functional events in event trees.
classscram::mef::Branch <br>The branch representation in event trees.
classscram::mef::NamedBranch <br>Named branches that can be referenced and reused.
classscram::mef::Path <br>Functional-event state paths in event trees.
classscram::mef::Fork <br>Functional event forks.
classscram::mef::EventTree <br>Event Tree representation with MEF constructs.
classscram::mef::InitiatingEvent <br>Event-tree Initiating Event.

Source code

cpp
/*
 * Copyright (C) 2014-2018 Olzhas Rakhimov
 * Copyright (C) 2023 OpenPRA ORG Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#pragma once

#include <memory>
#include <string>
#include <variant>
#include <vector>

#include "element.h"
#include "ext/variant.h"
#include "instruction.h"

namespace scram::mef {

class Sequence : public Element, public Usage {
 public:
  static constexpr const char* kTypeString = "sequence";

  using Element::Element;

  void instructions(std::vector<Instruction*> instructions) {
    instructions_ = std::move(instructions);
  }

  const std::vector<Instruction*>& instructions() const {
    return instructions_;
  }

 private:
  std::vector<Instruction*> instructions_;
};

class EventTree;  // Manages the order assignment to functional events.

class FunctionalEvent : public Element, public Usage {
 public:
  static constexpr const char* kTypeString = "functional event";

  using Element::Element;

  int order() const { return order_; }

  void order(int order) { order_ = order; }

 private:
  int order_ = 0;  
};

class Fork;
class NamedBranch;

class Branch {
 public:
  using Target = std::variant<Sequence*, Fork*, NamedBranch*>;

  void instructions(std::vector<Instruction*> instructions) {
    instructions_ = std::move(instructions);
  }

  const std::vector<Instruction*>& instructions() const {
    return instructions_;
  }

  void target(Target target) { target_ = std::move(target); }

  const Target& target() const {
    assert(ext::as<bool>(target_));
    return target_;
  }

 private:
  std::vector<Instruction*> instructions_;  
  Target target_;  
};

class NamedBranch : public Element,
                    public Branch,
                    public NodeMark,
                    public Usage {
 public:
  static constexpr const char* kTypeString = "branch";  

  using Element::Element;
};

class Path : public Branch {
 public:
  explicit Path(std::string state);

  const std::string& state() const { return state_; }

 private:
  std::string state_;  
};

class Fork {
 public:
  Fork(const FunctionalEvent& functional_event, std::vector<Path> paths);

  const FunctionalEvent& functional_event() const { return functional_event_; }

  const std::vector<Path>& paths() const { return paths_; }
  std::vector<Path>& paths() { return paths_; }

 private:
  const FunctionalEvent& functional_event_;  
  std::vector<Path> paths_;  
};

class EventTree : public Element,
                  public Usage,
                  public Composite<Container<EventTree, Sequence, false>,
                                   Container<EventTree, FunctionalEvent>,
                                   Container<EventTree, NamedBranch>> {
 public:
  static constexpr const char* kTypeString = "event tree";

  using Element::Element;

  const Branch& initial_state() const { return initial_state_; }

  void initial_state(Branch branch) { initial_state_ = std::move(branch); }

  auto sequances() const { return table<Sequence>(); }
  auto functional_events() const { return table<FunctionalEvent>(); }
  auto branches() const { return table<NamedBranch>(); }

  using Composite::Add;
  using Composite::Remove;

  void Add(std::unique_ptr<Fork> fork) { forks_.push_back(std::move(fork)); }

 private:
  Branch initial_state_;  
  std::vector<std::unique_ptr<Fork>> forks_;  
};

class InitiatingEvent : public Element, public Usage {
 public:
  static constexpr const char* kTypeString = "initiating event";

  using Element::Element;

  void event_tree(EventTree* event_tree) {
    assert(!event_tree_ && event_tree && "Resetting or un-setting event tree.");
    event_tree_ = event_tree;
  }

  EventTree* event_tree() const { return event_tree_; }
  EventTree* event_tree() { return event_tree_; }

 private:
  EventTree* event_tree_ = nullptr;  
};

}  // namespace scram::mef

Updated on 2025-11-11 at 16:51:08 +0000