pub mod trust_relations {
use crepe::crepe;
crepe! {
@input
pub struct DirectTrust<'a>(&'a str, &'a str);
@output
pub struct TransitiveTrust<'a>(&'a str, &'a str);
TransitiveTrust(a, b) <- DirectTrust(a, b);
TransitiveTrust(a, c) <- DirectTrust(a, b), TransitiveTrust(b, c);
}
}
pub mod access_control {
use crepe::crepe;
crepe! {
@input
pub struct HasRole<'a>(&'a str, &'a str);
@input
pub struct RoleGrants<'a>(&'a str, &'a str);
@output
pub struct HasPermission<'a>(&'a str, &'a str);
HasPermission(user, perm) <- HasRole(user, role), RoleGrants(role, perm);
}
}
pub mod knowledge_graph_inference {
use crepe::crepe;
crepe! {
@input
pub struct IsA<'a>(&'a str, &'a str);
@input
pub struct SubclassOf<'a>(&'a str, &'a str);
@output
pub struct DerivedType<'a>(&'a str, &'a str);
DerivedType(entity, typ) <- IsA(entity, typ);
DerivedType(entity, supertype) <- DerivedType(entity, subtype), SubclassOf(subtype, supertype);
}
}
pub struct BeliefState {
observations: Vec<(String, String, String)>,
}
impl BeliefState {
pub fn new() -> Self {
Self {
observations: Vec::new(),
}
}
pub fn observe(&mut self, entity: &str, property: &str, value: &str) {
self.observations
.push((entity.to_string(), property.to_string(), value.to_string()));
}
pub fn observations(&self) -> &[(String, String, String)] {
&self.observations
}
pub fn clear(&mut self) {
self.observations.clear();
}
}
impl Default for BeliefState {
fn default() -> Self {
Self::new()
}
}
pub struct PolicyEngine {
denied_actions: Vec<(String, String)>, allowed_actions: Vec<(String, String)>,
}
impl PolicyEngine {
pub fn new() -> Self {
Self {
denied_actions: Vec::new(),
allowed_actions: Vec::new(),
}
}
pub fn deny(&mut self, actor: &str, action: &str) {
self.denied_actions
.push((actor.to_string(), action.to_string()));
}
pub fn allow(&mut self, actor: &str, action: &str) {
self.allowed_actions
.push((actor.to_string(), action.to_string()));
}
pub fn is_permitted(&self, actor: &str, action: &str) -> bool {
if self
.denied_actions
.iter()
.any(|(a, act)| a == actor && act == action)
{
return false;
}
self.allowed_actions.is_empty()
|| self
.allowed_actions
.iter()
.any(|(a, act)| a == actor && act == action)
}
}
impl Default for PolicyEngine {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_belief_state() {
let mut state = BeliefState::new();
state.observe("object1", "location", "room_a");
state.observe("object1", "color", "red");
assert_eq!(state.observations().len(), 2);
}
#[test]
fn test_policy_engine() {
let mut engine = PolicyEngine::new();
engine.allow("agent", "read_file");
engine.deny("agent", "delete_system");
assert!(engine.is_permitted("agent", "read_file"));
assert!(!engine.is_permitted("agent", "delete_system"));
}
}