Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enhance documentation of the rules engine #1736

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 10 additions & 13 deletions apps/showcase/src/app/rules-engine/rules-engine.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AsyncPipe } from '@angular/common';
import { AfterViewInit, ChangeDetectionStrategy, Component, inject, QueryList, ViewChildren, ViewEncapsulation } from '@angular/core';
import { RouterModule } from '@angular/router';
import { NgbNav, NgbNavContent, NgbNavItem, NgbNavLink, NgbNavOutlet } from '@ng-bootstrap/ng-bootstrap';
import { Store } from '@ngrx/store';
import { ApplicationDevtoolsModule } from '@o3r/application';
import { ComponentsDevtoolsModule } from '@o3r/components';
import { ConfigOverrideStoreModule, ConfigurationBaseServiceModule, ConfigurationDevtoolsModule } from '@o3r/configuration';
Expand All @@ -24,8 +23,7 @@ import {
RulesEngineDevtoolsModule,
RulesEngineRunnerModule,
RulesEngineRunnerService,
RulesetsStore,
setRulesetsEntities,
Ruleset,
UnaryOperator
} from '@o3r/rules-engine';
import { firstValueFrom } from 'rxjs';
Expand Down Expand Up @@ -71,7 +69,7 @@ import { duringSummer } from '../../operators/index';
export class RulesEngineComponent implements AfterViewInit {
private readonly inPageNavPresService = inject(InPageNavPresService);
private readonly dynamicContentService = inject(DynamicContentService);
private readonly store = inject(Store<RulesetsStore>);
private readonly rulesEngineService = inject(RulesEngineRunnerService);

public newYorkAvailableRule = '';
public helloNewYorkRule = '';
Expand All @@ -87,12 +85,11 @@ export class RulesEngineComponent implements AfterViewInit {
constructor() {
// We recommend to do the next lines in the AppComponent
// Here we do it for the sake of the example
const rulesEngineService = inject(RulesEngineRunnerService);
rulesEngineService.actionHandlers.add(inject(ConfigurationRulesEngineActionHandler));
rulesEngineService.actionHandlers.add(inject(AssetRulesEngineActionHandler));
rulesEngineService.actionHandlers.add(inject(LocalizationRulesEngineActionHandler));
rulesEngineService.engine.upsertOperators([duringSummer] as UnaryOperator[]);
rulesEngineService.engine.upsertOperators([dateInNextMinutes] as Operator[]);
this.rulesEngineService.actionHandlers.add(inject(ConfigurationRulesEngineActionHandler));
this.rulesEngineService.actionHandlers.add(inject(AssetRulesEngineActionHandler));
this.rulesEngineService.actionHandlers.add(inject(LocalizationRulesEngineActionHandler));
this.rulesEngineService.engine.upsertOperators([duringSummer] as UnaryOperator[]);
this.rulesEngineService.engine.upsertOperators([dateInNextMinutes] as Operator[]);
inject(TripFactsService).register();
const currentTimeFactsService = inject(CurrentTimeFactsService);
currentTimeFactsService.register();
Expand All @@ -115,15 +112,15 @@ export class RulesEngineComponent implements AfterViewInit {
);

const resultCall = await fetch(path);
const result = await resultCall.json();
const result = await resultCall.json() as {rulesets: Ruleset[]};

this.store.dispatch(setRulesetsEntities({ entities: result.rulesets }));
this.rulesEngineService.upsertRulesets(result.rulesets);
const [
newYorkAvailableRule,
helloNewYorkRule,
summerOtterRule,
lateOtterRule
] = result.rulesets[0].rules as Rule[];
] = result.rulesets[0].rules;
this.newYorkAvailableRule = JSON.stringify(this.formatRule(newYorkAvailableRule), null, 2);
this.helloNewYorkRule = JSON.stringify(this.formatRule(helloNewYorkRule), null, 2);
this.summerOtterRule = JSON.stringify(this.formatRule(summerOtterRule), null, 2);
Expand Down
3 changes: 2 additions & 1 deletion apps/showcase/src/assets/locales/en-GB.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"o3r-rules-engine-pres.whenQuestion": "When do you want to go?"
"o3r-rules-engine-pres.whenQuestion": "When do you want to go?",
"o3r-rules-engine-pres.hurry-up-question": "You are leaving soon, where will you go?"
}
104 changes: 96 additions & 8 deletions apps/showcase/src/assets/rules/rulesets.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
"id": "6194b61a-1bf3-4c02-8b7c-20f782d68324",
"name": "rules-engine",
"disabled": false,
"validityRange": {
"from": "09/01/2021",
"to": "11/01/2025"
},

"linkedComponents": {
"or": [{
"library": "showcase",
"name": "RulesEnginePresConfig"
}]
},
"rules": [
{
"id": "5467e501-b9ff-414f-8026-56885d0d7a4b",
Expand Down Expand Up @@ -41,11 +52,19 @@
"library": "showcase",
"property": "destinations",
"value": [
{ "cityCode": "LON", "available": true },
{ "cityCode": "PAR", "available": true },
{ "cityCode": "NYC", "available": true }
{
"cityCode": "LON",
"available": true
},
{
"cityCode": "PAR",
"available": true
},
{
"cityCode": "NYC",
"available": true
}
]

}
],
"failureElements": []
Expand Down Expand Up @@ -79,12 +98,79 @@
"value": "o3r-rules-engine-pres.whenQuestion"
}
],
"failureElements": [
{
"elementType": "RULE_BLOCK",
"blockType": "IF_ELSE",
"condition": {
"any": [
{
"lhs": {
"type": "FACT",
"value": "outboundDate"
},
"rhs": {
"type": "LITERAL",
"value": "2880"
},
"operator": "dateInNextMinutes"
}
]
},
"successElements": [
{
"elementType": "ACTION",
"actionType": "UPDATE_LOCALISATION",
"key": "o3r-rules-engine-pres.question",
"value": "o3r-rules-engine-pres.hurry-up-question"
}
],
"failureElements": []
}
]
}
},
{
"id": "5467e501-b9ff-414f-8026-56885d0d7a4d",
"name": "The otter is on vacation",
"disabled": false,
"outputRuntimeFacts": [],
"inputRuntimeFacts": [],
"rootElement": {
"elementType": "RULE_BLOCK",
"blockType": "IF_ELSE",
"condition": {
"all": [
{
"lhs": {
"type": "FACT",
"value": "outboundDate"
},
"operator": "isDefined"
},
{
"lhs": {
"type": "FACT",
"value": "outboundDate"
},
"operator": "duringSummer"
}
]
},
"successElements": [
{
"elementType": "ACTION",
"actionType": "UPDATE_ASSET",
"asset": "otter.svg",
"value": "otter-summer.svg"
}
],
"failureElements": []
}
},
{
"id": "5467e501-b9ff-414f-8026-56885d0d7a4d",
"name":"The otter is on vacation",
"name": "The otter is on vacation 2",
"disabled": false,
"outputRuntimeFacts": [],
"inputRuntimeFacts": [],
Expand Down Expand Up @@ -116,6 +202,7 @@
"asset": "otter.svg",
"value": "otter-summer.svg"
}

],
"failureElements": []
}
Expand All @@ -137,8 +224,8 @@
"value": "outboundDate"
},
"rhs": {
"type": "LITERAL",
"value": "2880"
"type": "LITERAL",
"value": "2880"
},
"operator": "dateInNextMinutes"
}
Expand All @@ -155,7 +242,8 @@
"failureElements": []
}
}
]
],
"description": ""
},
{
"id": "5945e501-b9ff-414f-8025-56885d0d7a4b-5945e501-b9ff-414f-8025-56885d0d7a4b",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { configSignal, DynamicConfigurableWithSignal, O3rConfig } from '@o3r/con
import { O3rComponent } from '@o3r/core';
import { DynamicContentModule } from '@o3r/dynamic-content';
import { Localization, LocalizationModule, LocalizationService, Translatable } from '@o3r/localization';
import { RulesEngineRunnerModule } from '@o3r/rules-engine';
import {RulesEngineRunnerModule, RulesEngineRunnerService} from '@o3r/rules-engine';
import { Subscription } from 'rxjs';
import { TripFactsService } from '../../../facts/trip/trip.facts';
import { DatePickerInputPresComponent } from '../../utilities';
Expand Down Expand Up @@ -35,7 +35,7 @@ const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
export class RulesEnginePresComponent implements OnDestroy, DynamicConfigurableWithSignal<RulesEnginePresConfig>, Translatable<RulesEnginePresTranslation> {
private readonly tripService = inject(TripFactsService);
private readonly localizationService = inject(LocalizationService);

private readonly rulesService = inject(RulesEngineRunnerService);
/** Localization of the component*/
@Input()
@Localization('./rules-engine-pres.localization.json')
Expand Down Expand Up @@ -108,8 +108,13 @@ export class RulesEnginePresComponent implements OnDestroy, DynamicConfigurableW
private formatDate(dateTime: number) {
return formatDate(dateTime, 'yyyy-MM-dd', 'en-GB');
}
public ngOnInit() {
this.rulesService.enableRuleSetFor(RULES_ENGINE_PRES_CONFIG_ID);

}

public ngOnDestroy() {
this.rulesService.disableRuleSetFor(RULES_ENGINE_PRES_CONFIG_ID);
this.subscription.unsubscribe();
}

Expand Down
Loading
Loading