This case study shows how Ascoos OS can execute macros based on AI predictions, combining logistic regression with a DSL (Domain-Specific Language). It uses the classes TArtificialIntelligenceHandler, AbstractDslAstBuilder and AstMacroTranslator to train a model, parse rules into an AST, translate them into macros and execute them dynamically.
- Train a model using
trainLogisticRegression() - Build an AST from DSL with
AbstractDslAstBuilder - Translate the AST into executable macros with
AstMacroTranslator - Execute macros when predictions meet a condition
-
TArtificialIntelligenceHandler
Train and predict logistic regression models -
AbstractDslAstBuilder
Parse DSL statements into an Abstract Syntax Tree -
AstMacroTranslator
Convert AST nodes into a container of callbacks for each macro
This case study is implemented in a single PHP script:
macro_decision_engine.php
Contains data loading, model training, DSL parsing, translation and macro execution.
- PHP ≥ 8.2
- Ascoos OS installed. If you’re using ASCOOS Web Extended Studio (AWES) 26, it’s pre-installed.
- Adjust your training data (
$X,$y) as needed. - Run the script via your web server:
https://localhost/aos/examples/case-studies/ai/macro_decision_engine/macro_decision_engine.php
WHEN predict(user.features) > 0.5 THEN
LOG "User is eligible"
ENABLE MODULE "AdvancedAnalytics"TArtificialIntelligenceHandlertrains a logistic regression model with$Xand$y.AbstractDslAstBuildertransforms the DSL script into AST nodes.AstMacroTranslatormaps each node to a callback:LOG→ print a messageENABLE MODULE→ enable a specific modulepredict→ callpredictLogisticRegression()
TMacroHandlerexecutes commands only if the prediction exceeds the threshold (> 0.5).
// Train the model
$ai = new TArtificialIntelligenceHandler();
$model = $ai->trainLogisticRegression($X, $y);
// Define the DSL
$dsl = <<<DSL
WHEN predict(user.features) > 0.5 THEN
LOG "User is eligible"
ENABLE MODULE "AdvancedAnalytics"
DSL;
// Build AST & translate
$astBuilder = new class extends AbstractDslAstBuilder {};
$ast = $astBuilder->buildAst($dsl);
$translator = new class([...]) extends AstMacroTranslator {};
$macroContainer = $translator->translateAst($ast);
// Execute based on user features
$user = ['features' => [1, 1, 0]];
$macroContainer->executeIfTrue($user);If the prediction predict([1,1,0]) > 0.5:
User is eligible
Module enabled: AdvancedAnalytics
Want to contribute to this case study? Fork the repo, add new macros or DSL enhancements in macro_decision_engine.php and submit a pull request. See CONTRIBUTING.md for guidelines.
This case study is covered by the Ascoos General License (AGL). See LICENSE.