Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OV JS] Expose Model.setFriendlyName and Model.getFriendlyName methods to Node.js api #23743

Merged
merged 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/bindings/js/node/include/model_wrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ class ModelWrap : public Napi::ObjectWrap<ModelWrap> {
*/
Napi::Value get_outputs(const Napi::CallbackInfo& info);

/**
* @brief Sets a friendly name for a model.
* @param info Contains information about the environment and passed arguments
* this method accepts only one argument of type string ,
vishniakov-nikolai marked this conversation as resolved.
Show resolved Hide resolved
* throws ov::Exception if more than 1 arguments are provided or the provided argument is not of type String
vishniakov-nikolai marked this conversation as resolved.
Show resolved Hide resolved
* @return Null
vishniakov-nikolai marked this conversation as resolved.
Show resolved Hide resolved
*/
Napi::Value set_friendly_name(const Napi::CallbackInfo& info);

/**
* @brief Gets the friendly name for a model , if not set , gets the unique name
vishniakov-nikolai marked this conversation as resolved.
Show resolved Hide resolved
* @param info Contains information about the environment and passed arguments
* this method does not accept any arguments. If arguments are provided it throws ov::Exception.
* @return Napi::String containing friendly name
*/
Napi::Value get_friendly_name(const Napi::CallbackInfo& info);

private:
std::shared_ptr<ov::Model> _model;
ov::Core _core;
Expand Down
2 changes: 2 additions & 0 deletions src/bindings/js/node/lib/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ interface Model {
output(nameOrId?: string | number): Output;
input(nameOrId?: string | number): Output;
getName(): string;
setFriendlyName(name: string): void;
getFriendlyName(): string;
}

interface CompiledModel {
Expand Down
29 changes: 29 additions & 0 deletions src/bindings/js/node/src/model_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Napi::Function ModelWrap::get_class(Napi::Env env) {
{InstanceMethod("getName", &ModelWrap::get_name),
InstanceMethod("output", &ModelWrap::get_output),
InstanceMethod("input", &ModelWrap::get_input),
InstanceMethod("setFriendlyName", &ModelWrap::set_friendly_name),
InstanceMethod("getFriendlyName", &ModelWrap::get_friendly_name),
InstanceAccessor<&ModelWrap::get_inputs>("inputs"),
InstanceAccessor<&ModelWrap::get_outputs>("outputs")});
}
Expand Down Expand Up @@ -117,3 +119,30 @@ Napi::Value ModelWrap::get_outputs(const Napi::CallbackInfo& info) {

return js_outputs;
}

Napi::Value ModelWrap::set_friendly_name(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() != 1 || !info[0].IsString()) {
reportError(env , "Expected a single string argument for the friendly name" );
vishniakov-nikolai marked this conversation as resolved.
Show resolved Hide resolved
return env.Undefined();
}
const auto name = info[0].As<Napi::String>().Utf8Value();
try {
_model->set_friendly_name(name);
} catch (const std::exception& e) {
reportError(env , e.what());
return env.Undefined();
vishniakov-nikolai marked this conversation as resolved.
Show resolved Hide resolved
}
return env.Undefined();
}

Napi::Value ModelWrap::get_friendly_name(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() > 0) {
reportError(env , "getFriendlyName() does not take any arguments");
return env.Undefined();
}
const auto friendly_name = _model->get_friendly_name();
return Napi::String::New(env , friendly_name);
}

55 changes: 55 additions & 0 deletions src/bindings/js/node/tests/model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// -*- coding: utf-8 -*-
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

const { addon: ov } = require('..');
const assert = require('assert');
const { describe, it } = require('node:test');
const { getModelPath } = require('./utils.js');
const testXml = getModelPath().xml;
const core = new ov.Core();
const model = core.readModelSync(testXml);

describe('Node.js getFriendlyName() / setFriendlyName()', () => {

describe('getFriendlyName()', () => {
it('returns the unique name of the model if no friendly name is set', () => {
const expectedName = 'test_model';
assert.strictEqual(model.getFriendlyName(), expectedName);
});
it('throws an error when called with arguments', () => {
assert.throws(() => model.getFriendlyName('unexpected argument'), /getFriendlyName\(\) does not take any arguments/);
});
});
describe('setFriendlyName()', () => {
it('sets a friendly name for the model', () => {
assert.doesNotThrow(() => model.setFriendlyName('MyFriendlyName'));
});

it('throws an error when called without a string argument', () => {
assert.throws(() => model.setFriendlyName(), /Expected a single string argument for the friendly name/);
assert.throws(() => model.setFriendlyName(123), /Expected a single string argument for the friendly name/);
});

it('throws an error when called with multiple arguments', () => {
assert.throws(() => model.setFriendlyName('Name1', 'Name2'), /Expected a single string argument for the friendly name/);
});

it('returns the set friendly name of the model', () => {
const friendlyName = 'MyFriendlyModel';
model.setFriendlyName(friendlyName);
assert.strictEqual(model.getFriendlyName(), friendlyName);
});

it('retains the last set friendly name when set multiple times', () => {
model.setFriendlyName('InitialName');
model.setFriendlyName('FinalName');
assert.strictEqual(model.getFriendlyName(), 'FinalName');
});

it('handles setting an empty string as a friendly name', () => {
assert.doesNotThrow(() => model.setFriendlyName(''));
assert.strictEqual(model.getFriendlyName(), 'Model1');
});
});
});
Loading