Skip to content

Commit

Permalink
added parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian committed May 2, 2024
1 parent 2cbae37 commit 8263fd5
Show file tree
Hide file tree
Showing 9 changed files with 882 additions and 33 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ SPDX-License-Identifier: CC-BY-NC-ND-4.0

All notable changes to this project will be documented in this file.

## [1.1.0] - 2024-05-05

### Added
- Pest parser

### Changed
-

## [1.0.0] - 2023-12-01

### Added
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ license = "Apache-2.0"
keywords = ["segmentation", "cyber-physical systems", "offline analysis"]

[dependencies]
pest = "2.7.9"
pest_derive = "2.7.9"
csv = "1.2.2"
clap = "2.33.3"
num-format = "0.4.4"
13 changes: 11 additions & 2 deletions src/behaviortree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,19 @@ impl TbtNode {
let indent = " ".repeat(line_shift);
match self {
TbtNode::Leaf(index, formula, name) => {
let print_name = if name.is_empty() {
name.clone()
} else {
format!(" {name}").to_string()
};
if with_children {
format!("{}Leaf({index} {name})[{}]", indent, formula.pretty_print())
format!(
"{}Leaf({index}{print_name})[{}]",
indent,
formula.pretty_print()
)
} else {
format!("{}Leaf({index} {name})", indent)
format!("{}Leaf({index}{print_name})", indent)
}
}
TbtNode::Fallback(index, subtrees) => {
Expand Down
180 changes: 180 additions & 0 deletions src/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#[derive(Clone, Debug)]
#[allow(dead_code)]
/// Func Syntax
pub enum Func {
Acc(usize, Vec<String>),
Number(f32),
Add(Box<Func>, Box<Func>),
Sub(Box<Func>, Box<Func>),
Pow(Box<Func>, Box<Func>),
Mul(Box<Func>, Box<Func>),
Div(Box<Func>, Box<Func>),
Mod(Box<Func>, Box<Func>),
Sqrt(Box<Func>),
Abs(Box<Func>),
RadToDeg(Box<Func>),
DegToRad(Box<Func>),
Cos(Box<Func>),
Sin(Box<Func>),
}

#[allow(dead_code)]
impl Func {
pub fn acc(c: usize, argument_names: Vec<String>) -> Func {
Func::Acc(c, argument_names)
}
pub fn number(f: f32) -> Func {
Func::Number(f)
}
pub fn add(l: Func, r: Func) -> Func {
Func::Add(Box::new(l), Box::new(r))
}
pub fn sub(l: Func, r: Func) -> Func {
Func::Sub(Box::new(l), Box::new(r))
}
pub fn pow(l: Func, r: Func) -> Func {
Func::Pow(Box::new(l), Box::new(r))
}
pub fn mul(l: Func, r: Func) -> Func {
Func::Mul(Box::new(l), Box::new(r))
}
pub fn div(l: Func, r: Func) -> Func {
Func::Div(Box::new(l), Box::new(r))
}
pub fn modulo(l: Func, r: Func) -> Func {
Func::Mod(Box::new(l), Box::new(r))
}
pub fn sqrt(c: Func) -> Func {
Func::Sqrt(Box::new(c))
}
pub fn abs(c: Func) -> Func {
Func::Abs(Box::new(c))
}
pub fn rad_to_deg(c: Func) -> Func {
Func::RadToDeg(Box::new(c))
}
pub fn deg_to_rad(c: Func) -> Func {
Func::DegToRad(Box::new(c))
}
pub fn cos(c: Func) -> Func {
Func::Cos(Box::new(c))
}
pub fn sin(c: Func) -> Func {
Func::Sin(Box::new(c))
}
}

// Func semantics
#[allow(dead_code)]
impl Func {
pub fn eval(&self, values: &[f32]) -> f32 {
match self {
Func::Acc(c, _) => values[*c],
Func::Number(f) => *f,
Func::Add(l, r) => l.eval(values) + r.eval(values),
Func::Sub(l, r) => l.eval(values) - r.eval(values),
Func::Pow(l, r) => l.eval(values).powf(r.eval(values)),
Func::Mul(l, r) => l.eval(values) + r.eval(values),
Func::Div(l, r) => l.eval(values) + r.eval(values),
Func::Mod(l, r) => l.eval(values) + r.eval(values),
Func::Sqrt(c) => f32::sqrt(c.eval(values)),
Func::Abs(c) => f32::abs(c.eval(values)),
Func::RadToDeg(c) => f32::to_degrees(c.eval(values)),
Func::DegToRad(c) => f32::to_radians(c.eval(values)),
Func::Cos(c) => f32::cos(c.eval(values)),
Func::Sin(c) => f32::sin(c.eval(values)),
}
}
}

// toString
#[allow(dead_code)]
impl Func {
pub fn to_string(&self) -> String {
match self {
Func::Acc(c, names) => names[*c].clone(),
Func::Number(f) => f.to_string(),
Func::Add(l, r) => format!(
"({} {} {})",
l.to_string(),
Func::get_string_add(),
r.to_string()
),
Func::Sub(l, r) => format!(
"({} {} {})",
l.to_string(),
Func::get_string_sub(),
r.to_string()
),
Func::Pow(l, r) => format!(
"({} {} {})",
l.to_string(),
Func::get_string_pow(),
r.to_string()
),
Func::Mul(l, r) => format!(
"({} {} {})",
l.to_string(),
Func::get_string_mul(),
r.to_string()
),
Func::Div(l, r) => format!(
"({} {} {})",
l.to_string(),
Func::get_string_div(),
r.to_string()
),
Func::Mod(l, r) => format!(
"({} {} {})",
l.to_string(),
Func::get_string_mod(),
r.to_string()
),
Func::Sqrt(c) => format!("{}({})", Func::get_string_sqrt(), c.to_string()),
Func::Abs(c) => format!("{}({})", Func::get_string_abs(), c.to_string()),
Func::RadToDeg(c) => format!("{}({})", Func::get_string_rad_to_deg(), c.to_string()),
Func::DegToRad(c) => format!("{}({})", Func::get_string_deg_to_rad(), c.to_string()),
Func::Cos(c) => format!("{}({})", Func::get_string_cos(), c.to_string()),
Func::Sin(c) => format!("{}({})", Func::get_string_sin(), c.to_string()),
}
}
}

impl Func {
pub fn get_string_add() -> String {
"+".to_string()
}
pub fn get_string_sub() -> String {
"-".to_string()
}
pub fn get_string_pow() -> String {
"^".to_string()
}
pub fn get_string_mul() -> String {
"*".to_string()
}
pub fn get_string_div() -> String {
"/".to_string()
}
pub fn get_string_mod() -> String {
"%".to_string()
}
pub fn get_string_sqrt() -> String {
"sqrt".to_string()
}
pub fn get_string_abs() -> String {
"abs".to_string()
}
pub fn get_string_rad_to_deg() -> String {
"rad_to_deg".to_string()
}
pub fn get_string_deg_to_rad() -> String {
"deg_to_rad".to_string()
}
pub fn get_string_cos() -> String {
"cos".to_string()
}
pub fn get_string_sin() -> String {
"sin".to_string()
}
}
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,29 @@ mod tree {
pub mod straight_maneuver;
}
}

pub mod behaviortree;
mod command_line_parser;
mod csv_reader;
mod functions;
mod parser;
mod stl;
mod table;
#[cfg(test)]
mod tests;
use behaviortree::print_segmentation;
use behaviortree::tbt_node_reset_count;
use behaviortree::Segmentation;
use behaviortree::Tbt;
use behaviortree::TbtNode;
use command_line_parser::CommandLineArguments;
use command_line_parser::SegmentationSetting;
use csv_reader::get_best_number_skipped;
use num_format::{Locale, ToFormattedString};
use parser::parse;
use std::collections::HashMap;
use std::rc::Rc;
use std::time::SystemTime;
use stl::stl_reset_count;
use stl::Stl;
use table::Table;

Expand Down Expand Up @@ -66,6 +71,18 @@ pub fn parse_command_line() -> CommandLineArguments {
command_line_parser::parse_command_line()
}

pub fn parse_tbt(formula: String, events_per_second: usize) -> Option<TbtNode> {
tbt_node_reset_count();
stl_reset_count();
match parse(formula, events_per_second) {
Ok(tbt) => Some(tbt),
Err(e) => {
println!("{}", e.to_string());
None
}
}
}

/**********************************
* Returns TBT and Trace
**********************************/
Expand Down
74 changes: 44 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,68 @@
// SPDX-FileCopyrightText: 2023 German Aerospace Center (DLR)
// SPDX-License-Identifier: Apache-2.0

use std::time::SystemTime;
use std::{fs, time::SystemTime};
use tbt_segmentation::{
evaluate, get_best_number_skipped_entries, get_tbt_and_trace, parse_command_line,
evaluate, get_best_number_skipped_entries, get_tbt_and_trace, parse_command_line, parse_tbt,
};

fn main() {
let start = SystemTime::now();
/*************
* PARAMETERS
*************/
let arguments = parse_command_line();
// let arguments = parse_command_line();

/**********************************
* Get best number skipped entries
**********************************/
let (number_skipped_entries, delta_rho_skipped) =
get_best_number_skipped_entries(&arguments.logfile, arguments.sub_sampling);

// let (number_skipped_entries, delta_rho_skipped) =
// get_best_number_skipped_entries(&arguments.logfile, arguments.sub_sampling);
let number_skipped_entries = 2;
let frequency = if number_skipped_entries != 0 {
0.005 * number_skipped_entries as f32
} else {
0.005
};
let events_per_second = (1.0 / frequency) as usize;
println!("events: {events_per_second}");
/*******************
* STARTUP ROUTINES
*******************/
let (trace, tbt) = get_tbt_and_trace(
&arguments.logfile,
number_skipped_entries,
arguments.lazy_evaluation,
arguments.sub_sampling,
);
// let formula =
// "? (<2..10> f(a)[ a+ 1], [0..1] f(a)[ a+ 1], f(a)[a+1] IU(2..3) f(b)[ b-1])".to_string();
// let formula = "a(a)[!sin(a) + !degToRad(a)]".to_string();
let formula = fs::read_to_string("shiplanding_formula.txt").unwrap();
parse_tbt(formula, events_per_second);
todo!("Finished");

// let (trace, tbt) = get_tbt_and_trace(
// &arguments.logfile,
// number_skipped_entries,
// arguments.lazy_evaluation,
// arguments.sub_sampling,
// );

/*********************
* Evaluation
*********************/
evaluate(
tbt,
trace,
start,
arguments.sub_sampling,
arguments.lazy_evaluation,
delta_rho_skipped,
arguments.print_leaf_segments_only,
arguments.segmentation_setting,
arguments.debug_console,
);
// evaluate(
// tbt,
// trace,
// start,
// arguments.sub_sampling,
// arguments.lazy_evaluation,
// delta_rho_skipped,
// arguments.print_leaf_segments_only,
// arguments.segmentation_setting,
// arguments.debug_console,
// );

/*********************
* Finish Execution
*********************/
println!(
"Finished after {} seconds.",
start.elapsed().unwrap().as_secs()
);
// /*********************
// * Finish Execution
// *********************/
// println!(
// "Finished after {} seconds.",
// start.elapsed().unwrap().as_secs()
// );
}
Loading

0 comments on commit 8263fd5

Please sign in to comment.