Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Method does not exist.
[simgrid.git] / src / instr / instr_paje_types.cpp
1 /* Copyright (c) 2012, 2014-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/instr/instr_private.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_paje_types, instr, "Paje tracing event system (types)");
10
11 static simgrid::instr::Type* rootType = nullptr; /* the root type */
12
13 namespace simgrid {
14 namespace instr {
15
16 Type::Type(std::string name, std::string alias, std::string color, e_entity_types kind, Type* father)
17     : name_(name), color_(color), kind_(kind), father_(father)
18 {
19   if (name.empty() || alias.empty()) {
20     THROWF(tracing_error, 0, "can't create a new type with no name or alias");
21   }
22
23   id_ = std::to_string(instr_new_paje_id());
24
25   if (father != nullptr){
26     father->children_.insert({alias, this});
27     XBT_DEBUG("new type %s, child of %s", name_.c_str(), father->getCname());
28   }
29 }
30
31 Type::~Type()
32 {
33   for (auto elm : values_)
34     delete elm.second;
35   for (auto elm : children_)
36     delete elm.second;
37 }
38
39 Type* Type::byName(std::string name)
40 {
41   Type* ret = nullptr;
42   for (auto elm : children_) {
43     if (elm.second->name_ == name) {
44       if (ret != nullptr) {
45         THROWF (tracing_error, 0, "there are two children types with the same name?");
46       } else {
47         ret = elm.second;
48       }
49     }
50   }
51   if (ret == nullptr)
52     THROWF(tracing_error, 2, "type with name (%s) not found in father type (%s)", name.c_str(), getCname());
53   return ret;
54 }
55
56 void Type::addEntityValue(std::string name)
57 {
58   addEntityValue(name, "");
59 }
60
61 void Type::addEntityValue(std::string name, std::string color)
62 {
63   if (name.empty()) {
64     THROWF(tracing_error, 0, "can't get a value with no name");
65   }
66   if (kind_ == TYPE_VARIABLE)
67     THROWF(tracing_error, 0, "variables can't have different values (%s)", getCname());
68
69   auto it = values_.find(name);
70   if (it == values_.end()) {
71     Value* new_val = new Value(name, color, this);
72     values_.insert({name, new_val});
73     XBT_DEBUG("new value %s, child of %s", name_.c_str(), getCname());
74     new_val->print();
75   }
76 }
77
78 Value* Type::getEntityValue(std::string name)
79 {
80   auto ret = values_.find(name);
81   if (ret == values_.end()) {
82     THROWF(tracing_error, 2, "value with name (%s) not found in father type (%s)", name.c_str(), getCname());
83   }
84   return ret->second;
85 }
86
87 Type* Type::createRootType()
88 {
89   simgrid::instr::Type* ret = new simgrid::instr::Type("0", "0", "", TYPE_CONTAINER, nullptr);
90   rootType                  = ret;
91   return ret;
92 }
93
94 Type* Type::getRootType()
95 {
96   return rootType;
97 }
98
99 Type* Type::getOrCreateContainerType(std::string name)
100 {
101   auto it = children_.find(name);
102   if (it == children_.end()) {
103     Type* ret = new simgrid::instr::Type(name, name, "", TYPE_CONTAINER, this);
104     XBT_DEBUG("ContainerType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), getCname(), getId());
105     ret->logContainerTypeDefinition();
106     return ret;
107   } else
108     return it->second;
109 }
110
111 Type* Type::getOrCreateEventType(std::string name)
112 {
113   auto it = children_.find(name);
114   if (it == children_.end()) {
115     Type* ret = new Type(name, name, "", TYPE_EVENT, this);
116     XBT_DEBUG("EventType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), getCname(), getId());
117     ret->logDefineEventType();
118     return ret;
119   } else {
120     return it->second;
121   }
122 }
123
124 Type* Type::getOrCreateVariableType(std::string name, std::string color)
125 {
126   auto it = children_.find(name);
127   if (it == children_.end()) {
128     Type* ret = new Type(name, name, color.empty() ? "1 1 1" : color, TYPE_VARIABLE, this);
129
130     XBT_DEBUG("VariableType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), getCname(), getId());
131     ret->logVariableTypeDefinition();
132
133     return ret;
134   } else
135     return it->second;
136 }
137
138 Type* Type::getOrCreateLinkType(std::string name, Type* source, Type* dest)
139 {
140   std::string alias = name + "-" + source->id_ + "-" + dest->id_;
141   auto it           = children_.find(alias);
142   if (it == children_.end()) {
143     Type* ret = new Type(name, alias, "", TYPE_LINK, this);
144     XBT_DEBUG("LinkType %s(%s), child of %s(%s)  %s(%s)->%s(%s)", ret->getCname(), ret->getId(), getCname(), getId(),
145               source->getCname(), source->getId(), dest->getCname(), dest->getId());
146     ret->logLinkTypeDefinition(source, dest);
147     return ret;
148   } else
149     return it->second;
150 }
151
152 Type* Type::getOrCreateStateType(std::string name)
153 {
154   auto it = children_.find(name);
155   if (it == children_.end()) {
156     Type* ret = new Type(name, name, "", TYPE_STATE, this);
157     XBT_DEBUG("StateType %s(%s), child of %s(%s)", ret->getCname(), ret->getId(), getCname(), getId());
158     ret->logStateTypeDefinition();
159     return ret;
160   } else
161     return it->second;
162 }
163 }
164 }