Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / helpers / xml_net_builder.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <utility>
6 #include <xml_net_builder.hpp>
7 #include <details/ie_exception.hpp>
8 #include <algorithm>
9
10 using namespace ::testing;
11
12 size_t  IDManager::getNextLayerID() {
13     return layerID++;
14 }
15
16 size_t  IDManager::getNextPortID() {
17     return portID++;
18 }
19
20 void IDManager::reset() {
21     portID = layerID = 0;
22 }
23
24 LayerDesc::LayerDesc(std::string type, InOutShapes& shapes, IDManager &id_manager) : _type(std::move(type)) {
25     _layerID = id_manager.getNextLayerID();
26     auto inDims = shapes.inDims;
27     auto outDims = shapes.outDims;
28     for (const auto& inDim : inDims) {
29         _inPortsID.emplace_back(id_manager.getNextPortID(), inDim);
30     }
31     for (const auto& outDim : outDims) {
32         _outPortsID.emplace_back(id_manager.getNextPortID(), outDim);
33     }
34 }
35
36 void LayerDesc::resetPortIDs() {
37     _currentInPort = _currentOutPort = 0;
38 }
39
40 LayerDesc::LayerPortData LayerDesc::getNextInData() {
41     if (_currentInPort == _inPortsID.size())
42         THROW_IE_EXCEPTION << "Failed to get next input port: reached the last one";
43     return _inPortsID[_currentInPort++];
44 }
45
46 LayerDesc::LayerPortData LayerDesc::getNextOutData() {
47     if (_currentOutPort == _outPortsID.size())
48         THROW_IE_EXCEPTION << "Failed to get next output port: reached the last one";
49     return _outPortsID[_currentOutPort++];
50 }
51
52 size_t  LayerDesc::getLayerID() const {
53     return _layerID;
54 }
55
56 size_t LayerDesc::getInputsSize() const {
57     return _inPortsID.size();
58 }
59
60 size_t LayerDesc::getOutputsSize() const {
61     return _outPortsID.size();
62 }
63
64 std::string LayerDesc::getLayerName() const {
65     return _type + std::to_string(getLayerID());
66 }
67
68
69 EdgesBuilder& EdgesBuilder::connect(size_t  layer1, size_t  layer2) {
70     auto found1 = std::find_if(layersDesc.begin(), layersDesc.end(), [&layer1](const LayerDesc::Ptr& desc) {
71         return desc->getLayerID() == layer1;
72     });
73     auto found2 = std::find_if(layersDesc.begin(), layersDesc.end(), [&layer2](const LayerDesc::Ptr& desc) {
74         return desc->getLayerID() == layer2;
75     });
76     if (found1 == layersDesc.end() || found2 == layersDesc.end())
77         THROW_IE_EXCEPTION << "Failed to find layers with index: " << layer1 << " and " << layer2;
78
79     nodeEdges.node("edge")
80             .attr("from-layer", (*found1)->getLayerID())
81             .attr("from-port", (*found1)->getNextOutData().portID)
82             .attr("to-layer", (*found2)->getLayerID())
83             .attr("to-port", (*found2)->getNextInData().portID).close();
84     return *this;
85 }
86
87 std::string EdgesBuilder::finish() {
88     auto& exp = nodeEdges.close();
89     return exp;
90 }