use super::{ChannelIn, ChannelOut, ReasonerChannels};
use crate::{control::Reasoner, global::RC, inference::InferenceEngine, parameters::Parameters};
use anyhow::Result;
use nar_dev_utils::RefCount;
use navm::{
cmd::Cmd,
output::Output,
vm::{VmRuntime, VmStatus},
};
#[derive(Debug)]
pub struct RuntimeAlpha {
pub(super) io_channels: ReasonerChannels,
pub(super) reasoner: Reasoner,
i_channel: RC<ChannelIn>,
o_channel: RC<ChannelOut>,
}
impl RuntimeAlpha {
pub fn new(
name: impl Into<String>,
hyper_parameters: Parameters,
inference_engine: InferenceEngine,
) -> Self {
let reasoner = Reasoner::new(name.into(), hyper_parameters, inference_engine);
let (io_channels, i_channel, o_channel) = default_channels();
Self {
reasoner,
io_channels,
i_channel,
o_channel,
}
}
}
fn default_channels() -> (ReasonerChannels, RC<ChannelIn>, RC<ChannelOut>) {
let mut io_channels = ReasonerChannels::new();
let i_channel = RC::new_(ChannelIn::new());
io_channels.add_input_channel(Box::new(i_channel.clone()));
let o_channel = RC::new_(ChannelOut::new());
io_channels.add_output_channel(Box::new(o_channel.clone()));
(io_channels, i_channel, o_channel)
}
impl VmRuntime for RuntimeAlpha {
fn input_cmd(&mut self, cmd: Cmd) -> Result<()> {
self.i_channel.mut_().put(cmd);
self.handle_io();
Ok(())
}
fn fetch_output(&mut self) -> Result<Output> {
self.o_channel
.mut_()
.fetch()
.ok_or(anyhow::anyhow!("没有输出"))
}
fn try_fetch_output(&mut self) -> Result<Option<Output>> {
Ok(self.o_channel.mut_().fetch())
}
fn status(&self) -> &VmStatus {
&VmStatus::Running
}
fn terminate(&mut self) -> Result<()> {
self.reasoner.reset();
Ok(())
}
}