Skip to content

Commit

Permalink
Added MainFileParser
Browse files Browse the repository at this point in the history
Change-Id: Ie00a4b630f6f94f9cbfed9571ff6ad6108a3dcc0
  • Loading branch information
vmarkovtsev committed Aug 19, 2015
1 parent 34dc85b commit f042796
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Expand Up @@ -26,3 +26,9 @@
[submodule "libVeles/simd"]
path = libVeles/simd
url = https://github.com/Samsung/veles.simd.git
[submodule "libVeles/rapidjson"]
path = libVeles/rapidjson
url = https://github.com/miloyip/rapidjson.git
[submodule "libVeles/variant"]
path = libVeles/variant
url = https://github.com/mapbox/variant.git
8 changes: 5 additions & 3 deletions libVeles/configure.ac
Expand Up @@ -29,6 +29,10 @@ AC_SUBST(INTERFACE_VERSION, [0])
AC_SUBST(REVISION_NUMBER, [$(cd $srcdir && git rev-list HEAD --count)])
AC_SUBST(AGE_NUMBER, [0])

AS_IF([test "x$enable_static" != "xno" -a "x$enable_shared" != "xno" ], [
AC_MSG_ERROR([Building both shared and static libraries is not supported (use --disable-shared or --disable-static)])
])

AC_CONFIG_SUBDIRS([simd])
SIMD_LIBS="\$(top_builddir)/simd/src/libSimd.la"
SIMD_CFLAGS="-I\$(top_srcdir)/simd/inc"
Expand Down Expand Up @@ -62,9 +66,7 @@ AC_ARG_WITH([built-in-libarchive],
AC_SUBST([LIBARCHIVE_DIR])
AC_SUBST([ZLIB_DIR])

AS_IF([test "x$enable_static" != "xno" -a "x$enable_shared" != "xno" ], [
AC_MSG_ERROR([Building both shared and static libraries is not supported (use --disable-shared or --disable-static)])
])
AM_CPPFLAGS="$AM_CPPFLAGS -I\$(top_srcdir)/rapidjson/include"

# Check whether to use nice Eina logging
AC_ARG_ENABLE([eina-logging],
Expand Down
1 change: 1 addition & 0 deletions libVeles/rapidjson
Submodule rapidjson added at 3ede21
2 changes: 1 addition & 1 deletion libVeles/src/Sources.make
@@ -1,2 +1,2 @@
SOURCES := unit.cc unit_factory.cc workflow.cc workflow_loader.cc logger.cc \
numpy_array_loader.cc
numpy_array_loader.cc main_file_loader.cc
146 changes: 146 additions & 0 deletions libVeles/src/main_file_loader.cc
@@ -0,0 +1,146 @@
/*! @file main_file_loader.cc
* @brief Implementation of MainFileLoader class.
* @author Vadim Markovtsev <v.markovtsev@samsung.com>
* @version 1.0
*
* @section Notes
* This code partially conforms to <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">Google C++ Style Guide</a>.
*
* @section Copyright
* Copyright © 2015 Samsung R&D Institute Russia
*
* @section License
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "src/main_file_loader.h"
#include <algorithm>
#include <sstream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#include <rapidjson/document.h>
#pragma GCC diagnostic pop

namespace veles {

UnitDefinition::UnitDefinition(
const std::string& name, const std::string& uuid)
: name_(name) {
std::stringstream shex;
std::string uuid36(uuid);
uuid36.erase(std::remove(uuid36.begin(), uuid36.end(), '-'), uuid36.end());
shex << std::hex << uuid36;
shex >> uuid_;
}

std::vector<std::string> UnitDefinition::PropertyNames() const noexcept {
std::vector<std::string> keys(props_.size());
for (auto& it : props_) {
keys.push_back(it.first);
}
return keys;
}

void UnitDefinition::Link(std::shared_ptr<UnitDefinition> def) {
links_.insert(def);
}

WorkflowDefinition::WorkflowDefinition(
const std::string& checksum, const std::string& name,
std::shared_ptr<UnitDefinition> start)
: checksum_(checksum), name_(name), start_(start) {
}

namespace {

template <class T>
class InputStreamWrapper {
public:
typedef T Ch;
InputStreamWrapper(std::basic_istream<T>* in) : in_(*in) {}

InputStreamWrapper(const InputStreamWrapper&) = delete;
InputStreamWrapper& operator=(const InputStreamWrapper&) = delete;

T Peek() const {
int c = in_.peek();
return c == std::char_traits<T>::eof()? 0 : static_cast<T>(c);
}

T Take() {
int c = in_.get();
return c == std::char_traits<T>::eof()? 0 : static_cast<T>(c);
}

size_t Tell() const { return static_cast<size_t>(in_.tellg()); }
T* PutBegin() { assert(false); return 0; }
void Put(T) { assert(false); }
void Flush() { assert(false); }
size_t PutEnd(T*) { assert(false); return 0; }

private:
std::istream& in_;
};

}

WorkflowDefinition MainFileLoader::Load(std::istream* src) {
rapidjson::Document doc;
InputStreamWrapper<char> srcwrapper(src);
doc.ParseStream(srcwrapper);
auto& units = doc["units"];
std::vector<std::shared_ptr<UnitDefinition>> udefs(units.Size());
for (int i = static_cast<int>(units.Size()) - 1; i >= 0; i--) {
auto& unit = units[i];
auto udef = std::make_shared<UnitDefinition>(
unit["class"]["name"].GetString(),
unit["class"]["uuid"].GetString());
auto& props = unit["data"];
for (auto pit = props.MemberBegin(); pit != props.MemberEnd(); ++pit) {
switch (pit->value.GetType()) {
case rapidjson::kFalseType:
case rapidjson::kTrueType:
udef->set(pit->name.GetString(), pit->value.GetBool());
break;
case rapidjson::kStringType:
udef->set(pit->name.GetString(), pit->value.GetString());
break;
case rapidjson::kNumberType:
if (pit->value.IsDouble()) {
udef->set(pit->name.GetString(),
static_cast<float>(pit->value.GetDouble()));
} else if (pit->value.IsInt()) {
udef->set(pit->name.GetString(), pit->value.GetInt());
} else {
WRN("Unsupported property type: int64");
}
break;
default:
WRN("Unsupported property type: %d", pit->value.GetType());
break;
}
}
auto& links = unit["links"];
for (rapidjson::SizeType l = 0; l < links.Size(); l++) {
udef->Link(udefs[links[l].GetInt()]);
}
}
return { doc["checksum"].GetString(), doc["workflow"].GetString(), udefs[0] };
}

} // namespace veles
96 changes: 96 additions & 0 deletions libVeles/src/main_file_loader.h
@@ -0,0 +1,96 @@
/*! @file main_file_loader.h
* @brief Declaration of MainFileLoader class.
* @author Vadim Markovtsev <v.markovtsev@samsung.com>
* @version 1.0
*
* @section Notes
* This code partially conforms to <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">Google C++ Style Guide</a>.
*
* @section Copyright
* Copyright 2013 Samsung R&D Institute Russia
*/

#ifndef MAIN_FILE_LOADER_H_
#define MAIN_FILE_LOADER_H_

#include <istream>
#include <memory>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <variant/variant.hpp>
#include <veles/logger.h> // NOLINT(*)

template<typename... Types>
using variant = mapbox::util::variant<Types...>;

namespace veles {

class UnitDefinition {
public:
UnitDefinition(const std::string& name, const std::string& uuid);

std::string name() const noexcept { return name_; }
const uint8_t* uuid() const noexcept { return uuid_; }
const std::unordered_set<std::shared_ptr<UnitDefinition>>&
links() const noexcept {
return links_;
}
template <class T>
const T& operator[](const std::string& key) const;
template <class T>
T& operator[](const std::string& key);
template <class T>
void set(const std::string& key, const T& value);
std::vector<std::string> PropertyNames() const noexcept;
void Link(std::shared_ptr<UnitDefinition> def);

private:
std::string name_;
uint8_t uuid_[16];
std::unordered_set<std::shared_ptr<UnitDefinition>> links_;
std::unordered_map<std::string, variant<bool, int, float, std::string>> props_;
};

class WorkflowDefinition {
public:
WorkflowDefinition(const std::string& checksum, const std::string& name,
std::shared_ptr<UnitDefinition> start);
std::string checksum() const noexcept { return checksum_; }
std::string name() const noexcept { return name_; }
std::shared_ptr<UnitDefinition> start() const noexcept { return start_; }

private:
std::string checksum_;
std::string name_;
std::shared_ptr<UnitDefinition> start_;
};

/// Reads and parses the main file with description of the package (e.g.,
/// "contents.json").
class MainFileLoader : protected DefaultLogger<MainFileLoader,
Logger::COLOR_YELLOW> {
public:
virtual ~MainFileLoader() = default;

WorkflowDefinition Load(std::istream* src);
};

template <class T>
const T& UnitDefinition::operator[](const std::string& key) const {
auto val = props_.find(key)->second;
return mapbox::util::get<T>(val);
}

template <class T>
T& UnitDefinition::operator[](const std::string& key) {
return mapbox::util::get<T>(props_[key]);
}

template <class T>
void UnitDefinition::set(const std::string& key, const T& value) {
props_[key] = value;
}

} // namespace veles
#endif // MAIN_FILE_LOADER_H_
2 changes: 1 addition & 1 deletion libVeles/src/numpy_array_loader.cc
Expand Up @@ -28,7 +28,7 @@
* under the License.
*/

#include <src/numpy_array_loader.h>
#include "src/numpy_array_loader.h"
#include <simd/arithmetic.h>

namespace veles {
Expand Down
8 changes: 0 additions & 8 deletions libVeles/tests/workflow_files/default.yaml

This file was deleted.

Binary file removed libVeles/tests/workflow_files/neural_network.tar.gz
Binary file not shown.
Binary file not shown.
Binary file removed libVeles/tests/workflow_files/test_archive.tar.gz
Binary file not shown.
Binary file removed libVeles/tests/workflow_files/workflow.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions libVeles/variant
Submodule variant added at bf485d

0 comments on commit f042796

Please sign in to comment.