Skip to content

Commit

Permalink
refactor: ⬆️ 根据新版NAVM API,调整自身NAVM启动器实现
Browse files Browse the repository at this point in the history
  • Loading branch information
ARCJ137442 committed Apr 13, 2024
1 parent ffaf4b5 commit 751bbe6
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "babel_nar"
version = "0.20.2"
version = "0.20.3"
edition = "2021"
description = """
Implementation and application supports of the NAVM model
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cin_launcher/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const TEST_PATH_PYNARS: (&str, &str) = (PYNARS_ROOT, "pynars.ConsolePlus");
/// 启动并获取NARS
/// * 🚩【2024-03-27 18:55:07】目前就返回一个测试用的运行时
/// * 🎯敏捷开发用
fn get_nars() -> impl VmLauncher<CommandVmRuntime> {
fn get_nars() -> impl VmLauncher<Runtime = CommandVmRuntime> {
// OpenNARS::new(TEST_PATH_OPENNARS)
PyNARS::new(TEST_PATH_PYNARS.0, TEST_PATH_PYNARS.1)
// ONA::new(TEST_PATH_ONA)
Expand Down
3 changes: 2 additions & 1 deletion src/cin_implements/cxin_js/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl CXinJS {
}

/// 启动到「命令行运行时」
impl VmLauncher<CommandVmRuntime> for CXinJS {
impl VmLauncher for CXinJS {
type Runtime = CommandVmRuntime;
fn launch(self) -> Result<CommandVmRuntime> {
// 构造并启动虚拟机
pipe! {
Expand Down
3 changes: 2 additions & 1 deletion src/cin_implements/nars_python/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ impl NARSPython {
}

/// 启动到「命令行运行时」
impl VmLauncher<CommandVmRuntime> for NARSPython {
impl VmLauncher for NARSPython {
type Runtime = CommandVmRuntime;
fn launch(self) -> Result<CommandVmRuntime> {
// 构造指令,并启动虚拟机
manipulate!(
Expand Down
3 changes: 2 additions & 1 deletion src/cin_implements/ona/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl ONA {
}

/// 启动到「命令行运行时」
impl VmLauncher<CommandVmRuntime> for ONA {
impl VmLauncher for ONA {
type Runtime = CommandVmRuntime;
fn launch(self) -> Result<CommandVmRuntime> {
// 构造并启动虚拟机
let mut runtime = pipe! {
Expand Down
3 changes: 2 additions & 1 deletion src/cin_implements/openjunars/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ impl OpenJunars {
}

/// 启动到「命令行运行时」
impl VmLauncher<CommandVmRuntime> for OpenJunars {
impl VmLauncher for OpenJunars {
type Runtime = CommandVmRuntime;
fn launch(self) -> Result<CommandVmRuntime> {
// 构造指令
let command = self.command_generator.generate_command();
Expand Down
27 changes: 25 additions & 2 deletions src/cin_implements/openjunars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,34 @@ mod tests {
.expect("无法输入指令");

// 等待四秒钟,让Junars启动
std::thread::sleep(std::time::Duration::from_secs(1));
std::thread::sleep(std::time::Duration::from_secs(4));

vm.input_cmd(Cmd::NSE(nse_task!(<A --> B>.)))
.expect("无法输入指令");
std::thread::sleep(std::time::Duration::from_secs(6));

std::thread::sleep(std::time::Duration::from_secs(1));

vm.input_cmd(Cmd::CYC(1)).expect("无法输入指令");

std::thread::sleep(std::time::Duration::from_secs(1));

vm.input_cmd(Cmd::NSE(nse_task!(<A --> B>?)))
.expect("无法输入指令");

std::thread::sleep(std::time::Duration::from_secs(1));

vm.input_cmd(Cmd::CYC(1)).expect("无法输入指令");

std::thread::sleep(std::time::Duration::from_secs(3));

// 尝试截获其所有输出
// * 🚩【2024-04-13 16:10:27】目前经由Julia侧`flush(stdout)`,仍然无法捕获
// * 有输出`[ Info: Answer: <A-->B>. %1.0;0.9%`,但无法被程序捕获为文本
while let Ok(Some(output)) = vm.try_fetch_output() {
dbg!(output);
}

std::thread::sleep(std::time::Duration::from_secs(2));

// 终止虚拟机运行时
vm.terminate().expect("无法终止虚拟机");
Expand Down
3 changes: 2 additions & 1 deletion src/cin_implements/opennars/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ impl OpenNARS {
}

/// 启动到「命令行运行时」
impl VmLauncher<CommandVmRuntime> for OpenNARS {
impl VmLauncher for OpenNARS {
type Runtime = CommandVmRuntime;
fn launch(self) -> Result<CommandVmRuntime> {
// 构造指令
// * 🚩细致的Java参数配置,都外包给[`CommandGeneratorJava`]
Expand Down
3 changes: 2 additions & 1 deletion src/cin_implements/pynars/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ impl PyNARS {
}

/// 启动到「命令行运行时」
impl VmLauncher<CommandVmRuntime> for PyNARS {
impl VmLauncher for PyNARS {
type Runtime = CommandVmRuntime;
fn launch(self) -> Result<CommandVmRuntime> {
// 构造指令
let command = self.command_generator.generate_command();
Expand Down
4 changes: 2 additions & 2 deletions src/cli_support/cin_search/impls_path_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn file_name_matches(path: &Path, name: &str) -> bool {

/// 从遍历者中找到匹配的所有启动器
/// * 🎯仅搜索出「可能有效,故构建好」的启动器
pub fn launchers_from_walker<R: VmRuntime, L: VmLauncher<R>>(
pub fn launchers_from_walker<R: VmRuntime, L: VmLauncher>(
path_walker: impl PathWalker,
path_builder: impl CinPathBuilder<Launcher = L, Runtime = R>,
) -> Vec<(L, usize)> {
Expand All @@ -41,7 +41,7 @@ pub fn launchers_from_walker<R: VmRuntime, L: VmLauncher<R>>(
}

/// 类似[`launchers_from_walker`],但根据返回的「匹配度」从高到底排序
pub fn launchers_from_walker_sorted<R: VmRuntime, L: VmLauncher<R>>(
pub fn launchers_from_walker_sorted<R: VmRuntime, L: VmLauncher>(
path_walker: impl PathWalker,
path_builder: impl CinPathBuilder<Launcher = L, Runtime = R>,
) -> Vec<L> {
Expand Down
2 changes: 1 addition & 1 deletion src/cli_support/cin_search/path_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::path::Path;
pub trait CinPathBuilder {
/// 搜索结果的启动器类型
/// * 📌启动后变为[`CinSearch::Runtime`]运行时类型
type Launcher: VmLauncher<Self::Runtime>;
type Launcher: VmLauncher;

/// 搜索结果的运行时类型
type Runtime: VmRuntime;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub mod tests {
PYNARS_MODULE = "pynars.ConsolePlus"
NARS_PYTHON = "./executables/nars-python-main.exe"
CXIN_JS = "./executables/cxin-nars-shell.js"
OPENJUNARS = "./executables/OpenJunars/run.jl"
OPENJUNARS = "./executables/OpenJunars/launch.jl"
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/runtimes/command_vm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ impl VmRuntime for CommandVmRuntime {
}

/// 构建功能:启动命令行虚拟机
impl VmLauncher<CommandVmRuntime> for CommandVm {
impl VmLauncher for CommandVm {
type Runtime = CommandVmRuntime;
fn launch(self) -> Result<CommandVmRuntime> {
Ok(CommandVmRuntime {
// 状态:正在运行
Expand Down

0 comments on commit 751bbe6

Please sign in to comment.