Skip to content

Commit

Permalink
feat(schema): add helpers to use schema for stronly-typed ops, ref #149
Browse files Browse the repository at this point in the history
  • Loading branch information
iboB committed Nov 22, 2024
1 parent 2e19204 commit 9e420b3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmake/ac_local_plugin_util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ const std::vector<ac::local::ModelLoaderPtr>& get_@nameSym@_model_loaders() {
FILES "${CMAKE_CURRENT_BINARY_DIR}/${schemaHeader}"
)
endforeach()

target_link_libraries(${baselibTargetName} PUBLIC ac::schema)
target_link_libraries(${plibTargetName} PUBLIC ac::schema)
endif()

# add plugin
Expand Down
18 changes: 17 additions & 1 deletion dummy-plugin/test/t-dummy-schema.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// Copyright (c) Alpaca Core
// SPDX-License-Identifier: MIT
//
#include <aclp-dummy-plib.hpp>
#include <ac/local/Lib.hpp>
#include <ac/local/Model.hpp>
#include <ac/local/Instance.hpp>
#include <ac/local/ModelLoaderRegistry.hpp>

#include <ac/schema/Helpers.hpp>

#include <ac-test-util/JalogFixture.inl>

#include <doctest/doctest.h>

#include <dummy-schema.hpp>
#include <aclp-dummy-plib.hpp>
#include <ac-test-data-dummy-models.h>

struct LoadDummyFixture {
LoadDummyFixture() {
Expand All @@ -19,5 +25,15 @@ struct LoadDummyFixture {
LoadDummyFixture loadDummyFixture;

TEST_CASE("dummy schema") {
auto model = ac::local::Lib::modelLoaderRegistry().createModel({
.inferenceType = "dummy",
.name = "synthetic"
}, {});

REQUIRE(!!model);

using Instance = ac::local::schema::Dummy::InstanceGeneral;
auto instance = Model_createInstance<Instance>(*model, {.cutoff = 2});
auto result = Instance_runOp<Instance::OpRun>(*instance, {.input = {"a", "b", "c"}});
CHECK(result.result == "a one b two c one");
}
1 change: 0 additions & 1 deletion local/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ target_link_libraries(ac-local
PUBLIC
ac::astl
ac::dict
ac::schema
)

configure_file(
Expand Down
1 change: 1 addition & 0 deletions schema/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target_link_libraries(ac-schema INTERFACE
ac::dict
)
target_sources(ac-schema INTERFACE FILE_SET HEADERS FILES
ac/schema/Helpers.hpp
ac/schema/TupleIndexByItemId.hpp
)

Expand Down
20 changes: 20 additions & 0 deletions schema/code/ac/schema/Helpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Alpaca Core
// SPDX-License-Identifier: MIT
//
#pragma once
#include <ac/local/Model.hpp>
#include <ac/local/Instance.hpp>

namespace ac::local {

template <typename Instance>
auto Model_createInstance(ac::local::Model& model, typename Instance::Params p) {
return model.createInstance(Instance::id, p.toDict());
}

template <typename Op>
typename Op::Return Instance_runOp(ac::local::Instance& instance, typename Op::Params p) {
return Op::Return::fromDict(instance.runOp(Op::id, p.toDict()));
}

} // namespace ac::local
4 changes: 4 additions & 0 deletions schema/code/generate-cpp-for-ac-model-schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ def generate_struct(lines, name, data, indent)
lines << "#{indent} return ret;"
lines << "#{indent} }"

lines << "#{indent} static #{name.pascal_case} fromDict(Dict&& dict) {"
lines << "#{indent} return fromDict(dict);"
lines << "#{indent} }"

lines << ''

lines << "#{indent} Dict toDict() {"
Expand Down

0 comments on commit 9e420b3

Please sign in to comment.