Samchon Framework for CPP  1.0.0
PackerProcess.hpp
1 #pragma once
2 #include <samchon/templates/distributed/DistributedProcess.hpp>
3 #include <samchon/templates/distributed/base/DistributedSystemArrayBase.hpp>
4 
5 #include <iostream>
6 #include <samchon/examples/packer/Packer.hpp>
7 
8 namespace samchon
9 {
10 namespace examples
11 {
12 namespace interaction
13 {
14  class PackerProcess : public templates::distributed::DistributedProcess
15  {
16  private:
17  typedef templates::distributed::DistributedProcess super;
18 
19  public:
20  PackerProcess(templates::distributed::base::DistributedSystemArrayBase *systemArray)
21  : super(systemArray)
22  {
23  this->name = "packer";
24  };
25  virtual ~PackerProcess() = default;
26 
27  void solve()
28  {
29  using namespace samchon::library;
30 
31  std::shared_ptr<packer::ProductArray> productArray(new packer::ProductArray
32  ({
33  std::make_shared<packer::Product>("Eraser", 500, 10, 70),
34  std::make_shared<packer::Product>("Eraser", 500, 10, 70),
35  std::make_shared<packer::Product>("Pencil", 400, 30, 35),
36  std::make_shared<packer::Product>("Book", 8000, 150, 300),
37  std::make_shared<packer::Product>("Book", 8000, 150, 300),
38  std::make_shared<packer::Product>("Drink", 1000, 75, 250),
39  std::make_shared<packer::Product>("Drink", 1000, 75, 250),
40  std::make_shared<packer::Product>("Drink", 1000, 75, 250),
41  std::make_shared<packer::Product>("Umbrella", 4000, 200, 1000),
42  std::make_shared<packer::Product>("Notebook-PC", 800000, 150, 850),
43  std::make_shared<packer::Product>("Notebook-PC", 800000, 150, 850),
44  std::make_shared<packer::Product>("Tablet-PC", 600000, 120, 450),
45  std::make_shared<packer::Product>("Tablet-PC", 600000, 120, 450)
46  }));
47 
48  std::unique_ptr<packer::Packer> packer(new packer::Packer(productArray));
49  {
50  packer->emplace_back(new packer::Wrapper("Large", 100, 200, 1000));
51  packer->emplace_back(new packer::Wrapper("Medium", 70, 150, 500));
52  packer->emplace_back(new packer::Wrapper("Small", 50, 100, 250));
53  };
54 
55  sendData(std::make_shared<protocol::Invoke>("optimizePacker", packer->toXML()));
56  };
57 
58  virtual void replyData(std::shared_ptr<protocol::Invoke> invoke) override
59  {
60  if (invoke->getListener() == "replyOptimization")
61  printSolution(invoke->front()->getValueAsXML());
62  };
63 
64  private:
65  void printSolution(std::shared_ptr<library::XML> xml)
66  {
67  std::unique_ptr<packer::Packer> packer(new packer::Packer());
68  packer->construct(xml);
69 
70  std::cout << "The best packing solution: $" << packer->calcPrice() << std::endl;
71  };
72  };
73 };
74 };
75 };