-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv8.cc
More file actions
377 lines (311 loc) · 12.3 KB
/
v8.cc
File metadata and controls
377 lines (311 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
Compile using [g++ v4.cpp -o gui `pkg-config --cflags --libs gtkmm-3.0`]
*/
#include <gtkmm.h>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#include <sstream>
#include <memory>
#include <vector>
#include <algorithm>
using namespace std;
class Node {
public:
int id;
unordered_set<string> characteristics;
Node(int id) : id(id) {}
};
class SocialNetwork {
public:
void addNode(int id, const unordered_set<string>& characteristics) {
nodes.emplace(id, make_shared<Node>(id));
nodes[id]->characteristics = characteristics;
for (const auto& characteristic : characteristics) {
availableCharacteristics.insert(characteristic);
}
}
void addEdge(int id1, int id2) {
adjList[id1].insert(id2);
adjList[id2].insert(id1);
}
vector<pair<int, string>> postMessage(const string& keyword) {
vector<pair<int, string>> result;
unordered_set<int> reachedNodes;
unordered_set<int> notReachedNodes;
for (const auto& pair : nodes) {
const Node& node = *(pair.second);
if (node.characteristics.count(keyword)) {
reachedNodes.insert(node.id);
} else {
notReachedNodes.insert(node.id);
}
}
for (int nodeId : reachedNodes) {
result.push_back({nodeId, "Received"});
}
for (int nodeId : notReachedNodes) {
result.push_back({nodeId, "Not Received"});
}
return result;
}
vector<int> targetAds(const unordered_set<string>& targetCharacteristics) {
vector<int> result;
for (const auto& pair : nodes) {
const Node& node = *(pair.second);
bool matchesTarget = true;
for (const string& target : targetCharacteristics) {
if (node.characteristics.find(target) == node.characteristics.end()) {
matchesTarget = false;
break;
}
}
if (matchesTarget) {
result.push_back(node.id);
}
}
return result;
}
pair<vector<pair<int, vector<int>>>, pair<int, int>> calculateDominanceAndInfluence(const unordered_set<string>& targetCharacteristics = {}) {
vector<pair<int, vector<int>>> dominanceLevels;
unordered_map<int, int> influenceCount;
for (const auto& pair : adjList) {
int node = pair.first;
if (!targetCharacteristics.empty()) {
const Node& nodeObj = *(nodes[node]);
bool matchesTarget = true;
for (const string& target : targetCharacteristics) {
if (nodeObj.characteristics.find(target) == nodeObj.characteristics.end()) {
matchesTarget = false;
break;
}
}
if (!matchesTarget) {
continue;
}
}
vector<int> connections(pair.second.begin(), pair.second.end());
dominanceLevels.push_back({node, connections});
influenceCount[node] = connections.size();
}
sort(dominanceLevels.begin(), dominanceLevels.end(),
[](const pair<int, vector<int>>& a, const pair<int, vector<int>>& b) {
return a.second.size() > b.second.size();
});
int topDominator = dominanceLevels.empty() ? -1 : dominanceLevels.front().first;
int topInfluencer = -1;
if (!influenceCount.empty()) {
topInfluencer = max_element(influenceCount.begin(), influenceCount.end(),
[](const pair<int, int>& a, const pair<int, int>& b) {
return a.second < b.second;
})->first;
}
return {dominanceLevels, {topDominator, topInfluencer}};
}
void readFromFile(const string& filename) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening file: " << filename << endl;
return;
}
string line;
bool readingNodes = true;
int lineNumber = 0;
while (getline(file, line)) {
lineNumber++;
if (line == "edges") {
readingNodes = false;
continue;
}
istringstream iss(line);
if (readingNodes) {
int id;
if (!(iss >> id)) {
cerr << "Error reading node ID at line " << lineNumber << endl;
continue;
}
unordered_set<string> characteristics;
string characteristic;
while (iss >> characteristic) {
characteristics.insert(characteristic);
}
addNode(id, characteristics);
} else {
int id1, id2;
if (!(iss >> id1 >> id2)) {
cerr << "Error reading edge at line " << lineNumber << endl;
continue;
}
addEdge(id1, id2);
}
}
}
const unordered_set<string>& getAvailableCharacteristics() const {
return availableCharacteristics;
}
private:
unordered_map<int, shared_ptr<Node>> nodes;
unordered_map<int, unordered_set<int>> adjList;
unordered_set<string> availableCharacteristics;
};
SocialNetwork network;
class MainWindow : public Gtk::Window {
public:
MainWindow() {
set_title("Social Network");
set_default_size(600, 500);
add(grid);
// Post message section
post_message_label.set_text("Enter keyword for post message:");
grid.attach(post_message_label, 0, 0, 1, 1);
grid.attach(post_message_entry, 1, 0, 1, 1);
post_message_button.set_label("Post Message");
post_message_button.set_name("post_message");
post_message_button.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_post_message_clicked));
grid.attach(post_message_button, 2, 0, 1, 1);
// Target ads section
target_ads_label.set_text("Select or enter target characteristics:");
grid.attach(target_ads_label, 0, 1, 1, 1);
grid.attach(target_ads_combobox, 1, 1, 1, 1);
grid.attach(target_ads_entry, 1, 2, 1, 1);
target_ads_button.set_label("Target Ads");
target_ads_button.set_name("target_ads");
target_ads_button.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_target_ads_clicked));
grid.attach(target_ads_button, 2, 1, 1, 1);
// Populate combobox with available characteristics
for (const auto& characteristic : network.getAvailableCharacteristics()) {
target_ads_combobox.append(characteristic);
}
// Dominance section
dominance_button.set_label("Calculate Dominance and Influence");
dominance_button.set_name("dominance");
dominance_button.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_dominance_clicked));
grid.attach(dominance_button, 0, 3, 3, 1);
// Quit button
quit_button.set_label("Quit");
quit_button.set_name("quit");
quit_button.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_quit_clicked));
grid.attach(quit_button, 0, 4, 3, 1);
// Top dominator and influencer section
top_dominator_label.set_text("Top Dominator:");
grid.attach(top_dominator_label, 0, 5, 1, 1);
grid.attach(top_dominator_value, 1, 5, 2, 1);
top_influencer_label.set_text("Top Influencer:");
grid.attach(top_influencer_label, 0, 6, 1, 1);
grid.attach(top_influencer_value, 1, 6, 2, 1);
// Results section
grid.attach(scrolled_window, 0, 10, 3, 1);
scrolled_window.add(tree_view);
scrolled_window.set_min_content_height(300);
list_store = Gtk::ListStore::create(columns);
tree_view.set_model(list_store);
tree_view.append_column("Node ID", columns.col_id);
tree_view.append_column("Status/Connections", columns.col_status);
apply_css("stl.css");
show_all_children();
}
protected:
class ModelColumns : public Gtk::TreeModel::ColumnRecord {
public:
ModelColumns() {
add(col_id);
add(col_status);
}
Gtk::TreeModelColumn<int> col_id;
Gtk::TreeModelColumn<Glib::ustring> col_status;
};
void on_post_message_clicked() {
string keyword = post_message_entry.get_text();
auto result = network.postMessage(keyword);
list_store->clear();
for (const auto& entry : result) {
Gtk::TreeModel::Row row = *(list_store->append());
row[columns.col_id] = entry.first;
row[columns.col_status] = entry.second;
}
}
void on_target_ads_clicked() {
string characteristicsStr = target_ads_entry.get_text();
string selectedCharacteristic = target_ads_combobox.get_active_text();
unordered_set<string> targetCharacteristics;
if (!characteristicsStr.empty()) {
istringstream iss(characteristicsStr);
string characteristic;
while (iss >> characteristic) {
targetCharacteristics.insert(characteristic);
}
} else if (!selectedCharacteristic.empty()) {
targetCharacteristics.insert(selectedCharacteristic);
}
auto result = network.targetAds(targetCharacteristics);
list_store->clear();
for (int nodeId : result) {
Gtk::TreeModel::Row row = *(list_store->append());
row[columns.col_id] = nodeId;
row[columns.col_status] = "Targeted";
}
}
void on_dominance_clicked() {
string characteristicsStr = target_ads_entry.get_text();
string selectedCharacteristic = target_ads_combobox.get_active_text();
unordered_set<string> targetCharacteristics;
if (!characteristicsStr.empty()) {
istringstream iss(characteristicsStr);
string characteristic;
while (iss >> characteristic) {
targetCharacteristics.insert(characteristic);
}
} else if (!selectedCharacteristic.empty()) {
targetCharacteristics.insert(selectedCharacteristic);
}
auto result = network.calculateDominanceAndInfluence(targetCharacteristics);
list_store->clear();
for (const auto& entry : result.first) {
Gtk::TreeModel::Row row = *(list_store->append());
row[columns.col_id] = entry.first;
stringstream ss;
for (int conn : entry.second) {
ss << conn << " ";
}
row[columns.col_status] = ss.str();
}
top_dominator_value.set_text(to_string(result.second.first));
top_influencer_value.set_text(to_string(result.second.second));
}
void on_quit_clicked() {
hide();
}
void apply_css(const std::string& css_file) {
Glib::RefPtr<Gtk::CssProvider> css_provider = Gtk::CssProvider::create();
css_provider->load_from_path(css_file);
Glib::RefPtr<Gtk::StyleContext> style_context = get_style_context();
style_context->add_provider_for_screen(Gdk::Screen::get_default(), css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
}
private:
Gtk::Grid grid;
Gtk::Label post_message_label;
Gtk::Entry post_message_entry;
Gtk::Button post_message_button;
Gtk::Label target_ads_label;
Gtk::ComboBoxText target_ads_combobox;
Gtk::Entry target_ads_entry;
Gtk::Button target_ads_button;
Gtk::Button dominance_button;
Gtk::Button quit_button;
Gtk::Label top_dominator_label;
Gtk::Label top_dominator_value;
Gtk::Label top_influencer_label;
Gtk::Label top_influencer_value;
Gtk::ScrolledWindow scrolled_window;
Gtk::TreeView tree_view;
Glib::RefPtr<Gtk::ListStore> list_store;
ModelColumns columns;
};
int main(int argc, char* argv[]) {
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
// Read the social network data from the file
network.readFromFile("nodes.txt");
MainWindow window;
return app->run(window);
}