-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8efe36b
commit 211bdaa
Showing
6 changed files
with
195 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
pub mod opennars; | ||
|
||
// ONA | ||
// TODO: 具体实现 | ||
pub mod ona; | ||
|
||
// NARS-Python | ||
// TODO: 具体实现 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//! ONA运行时的启动器 | ||
//! * 🎯允许ONA对原先运行时特别配置功能,同时也支持为ONA定制配置 | ||
//! * 🚩只憎加「启动器」类型,而不增加「运行时」类型 | ||
//! * ✨不同启动器可以启动到相同运行时 | ||
use super::{input_translate, output_translate}; | ||
use crate::runtime::{CommandVm, CommandVmRuntime}; | ||
use navm::vm::VmLauncher; | ||
use std::{path::PathBuf, process::Command}; | ||
|
||
/// ONA Shell启动的默认指令参数 | ||
/// * 🎯默认预置指令:`[.exe文件路径] shell` | ||
const COMMAND_ARGS_ONA: [&str; 1] = ["shell"]; | ||
|
||
/// ONA运行时启动器 | ||
/// * 🎯配置ONA专有的东西 | ||
/// * 🎯以Java运行时专有形式启动ONA | ||
/// * 🚩基于exe文件启动ONA Shell | ||
/// * 默认预置指令:`[.exe文件路径] shell` | ||
/// * 📌【2024-03-25 08:41:16】目前跟随Rust命名规则,仅首字母大写 | ||
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Ona { | ||
/// exe文件路径 | ||
exe_path: PathBuf, | ||
/// ONA Shell | ||
default_volume: Option<usize>, | ||
} | ||
|
||
impl Ona { | ||
pub fn new(exe_path: impl Into<PathBuf>) -> Self { | ||
Self { | ||
// 转换为路径 | ||
exe_path: exe_path.into(), | ||
// 其它全是`None` | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
/// 启动到「命令行运行时」 | ||
impl VmLauncher<CommandVmRuntime> for Ona { | ||
fn launch(self) -> CommandVmRuntime { | ||
// 构造指令 | ||
let mut command = Command::new(self.exe_path); | ||
// * 📝这里的`args`、`arg都返回的可变借用。。 | ||
command.args(COMMAND_ARGS_ONA); | ||
|
||
// 构造并启动虚拟机 | ||
CommandVm::from_io_process(command.into()) | ||
// * 🚩固定的「输入输出转换器」 | ||
.input_translator(input_translate) | ||
.output_translator(output_translate) | ||
// 🔥启动 | ||
.launch() | ||
} | ||
} | ||
|
||
// ! 单元测试见[`super`] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//! 「非公理虚拟机」的ONA运行时 | ||
//! * 🚩只提供「一行启动」的功能封装 | ||
//! * 🎯无需自行配置「输入输出转译器」 | ||
// 转译器 | ||
util::mod_and_pub_use! { | ||
// 转译器 | ||
translators | ||
// 启动器 | ||
launcher | ||
} | ||
|
||
/// 单元测试 | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::runtime::test::{_test_ona, EXE_PATH_ONA}; | ||
use navm::vm::VmLauncher; | ||
|
||
#[test] | ||
fn test() { | ||
// 从别的地方获取jar路径 | ||
let jar_path = EXE_PATH_ONA; | ||
// 一行代码启动ONA | ||
let vm = Ona::new(jar_path).launch(); | ||
// 直接复用之前对ONA的测试 | ||
_test_ona(vm) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//! ONA在「命令行运行时」的转译器 | ||
//! * 🎯维护与ONA Shell的交互 | ||
//! * 📌基于命令行输入输出的字符串读写 | ||
//! * ✨NAVM指令→字符串 | ||
//! * ✨字符串→NAVM输出 | ||
use navm::{ | ||
cmd::Cmd, | ||
output::{Operation, Output}, | ||
}; | ||
use util::ResultS; | ||
|
||
/// ONA的「输入转译」函数 | ||
/// * 🎯用于将统一的「NAVM指令」转译为「ONA Shell输入」 | ||
pub fn input_translate(cmd: Cmd) -> ResultS<String> { | ||
let content = match cmd { | ||
// 直接使用「末尾」,此时将自动格式化任务(可兼容「空预算」的形式) | ||
Cmd::NSE(..) => cmd.tail(), | ||
// CYC指令:运行指定周期数 | ||
// ! ONA Shell同样是自动步进的 | ||
Cmd::CYC(n) => n.to_string(), | ||
// VOL指令:调整音量 | ||
Cmd::VOL(n) => format!("*volume={n}"), | ||
// 其它类型 | ||
// * 📌【2024-03-24 22:57:18】基本足够支持 | ||
_ => return Err(format!("该指令类型暂不支持:{cmd:?}")), | ||
}; | ||
// 转译 | ||
Ok(content) | ||
} | ||
|
||
/// ONA的「输出转译」函数 | ||
/// * 🎯用于将ONA Shell的输出(字符串)转译为「NAVM输出」 | ||
/// * 🚩直接根据选取的「头部」进行匹配 | ||
pub fn output_translate(content: String) -> ResultS<Output> { | ||
// 根据冒号分隔一次,然后得到「头部」 | ||
let head = content.split_once(':').unwrap_or(("", "")).0.to_lowercase(); | ||
// 根据「头部」生成输出 | ||
let output = match &*head { | ||
"answer" => Output::ANSWER { | ||
content_raw: content, | ||
// TODO: 有待捕获转译 | ||
narsese: None, | ||
}, | ||
"derived" => Output::OUT { | ||
content_raw: content, | ||
// TODO: 有待捕获转译 | ||
narsese: None, | ||
}, | ||
"input" => Output::IN { content }, | ||
"anticipate" => Output::ANTICIPATE { | ||
content_raw: content, | ||
// TODO: 有待捕获转译 | ||
narsese: None, | ||
}, | ||
"exe" => Output::EXE { | ||
content_raw: content, | ||
// TODO: 有待捕获转译 | ||
operation: Operation::new("UNKNOWN", [].into_iter()), | ||
}, | ||
"err" | "error" => Output::ERROR { | ||
description: content, | ||
}, | ||
_ => Output::OTHER { content }, | ||
}; | ||
// 返回 | ||
Ok(output) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters