-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rewrite): rewrite it. Make it interal to the language
- Loading branch information
Fernando Corrêa de Oliveira
committed
Aug 28, 2024
1 parent
cdc7a24
commit 835dcf2
Showing
15 changed files
with
559 additions
and
87 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use lib "lib"; | ||
use EEL; | ||
|
||
event fire-risk { | ||
has Rat $.temperature = $<temp><value>; | ||
has Rat $.humidity = $<hum><value>; | ||
has Str $.area = $<temp><area> // $<hum><area>; | ||
|
||
pattern TOP { | ||
[ | ||
| <temp> <hum> | ||
| <hum> <temp> | ||
] 5min | ||
<{ $<temp><area> eq $<hum><area> }> | ||
} | ||
|
||
pattern temp { | ||
<temperature=event(:type<temperature>)> | ||
<{ $<temperature><value> > 40 }> | ||
{ $!area = $<temperature><area> } | ||
} | ||
|
||
pattern hum { | ||
<humidity=event(:type<humidity>)> | ||
<{ $<humidity><value> < 20 }> | ||
{ $!area = $<humidity><area> } | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use lib "lib"; | ||
|
||
role Event is Capture { | ||
has Pair @.pos; | ||
method match(Supply $supply, Str :$rule = "TOP") { | ||
supply { | ||
whenever $supply -> $ev { | ||
self!step($ev, :$rule) | ||
} | ||
} | ||
} | ||
|
||
method !step($ev, Str :$rule) { | ||
my @*pos = @!pos; | ||
} | ||
} | ||
|
||
class fire-risk does Event { | ||
has Rat $.temperature = $<temp><value>; | ||
has Rat $.humidity = $<hum><value>; | ||
has Str $.area = $<temp><area> // $<hum><area>; | ||
|
||
pattern TOP { | ||
[ | ||
| <temp> <hum> | ||
| <hum> <temp> | ||
] 5min | ||
<{ $<temp><area> eq $<hum><area> }> | ||
} | ||
|
||
pattern temp { | ||
<temperature=event(:type<temperature>)> | ||
<{ $<temperature><value> > 40 }> | ||
{ $!area = $<temperature><area> } | ||
} | ||
|
||
pattern hum { | ||
<humidity=event(:type<humidity>)> | ||
<{ $<humidity><value> < 20 }> | ||
{ $!area = $<humidity><area> } | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
use EELParser; | ||
use EventTranslator; | ||
use Event::Runner; | ||
use Event::AST; | ||
unit class EEL; | ||
# use EELParser; | ||
# use EventTranslator; | ||
# use Event::Runner; | ||
# use Event::AST; | ||
# unit class EEL; | ||
# | ||
# has Supply $.input; | ||
# has EELParser $.parser .= new; | ||
# has EventTranslator $.trans; | ||
# has Str $.file; | ||
# has Str $.code; | ||
# has Event::AST:D @.ast = $!file.defined | ||
# ?? $!parser.parse-file: $!file | ||
# !! $!parser.parse: $!code | ||
# ; | ||
# has @.rules = EventTranslator.new.translate: @!ast; | ||
# has Event::Runner $.runner .= new: :$!input, :@!rules; | ||
# has Supply:D $.output handles * = $!runner.run; | ||
# | ||
# proto eel ($, :$code, :$file) is export {*} | ||
# multi eel(@inputs, |c) { nextwith Supply.merge(@inputs), |c } | ||
# multi eel(Supply:D $input, :$code! --> EEL) { | ||
# EEL.new: :$input, :$code | ||
# } | ||
# multi eel(Supply:D $input, :$file! --> EEL) { | ||
# EEL.new: :$input, :$file | ||
# } | ||
|
||
has Supply $.input; | ||
has EELParser $.parser .= new; | ||
has EventTranslator $.trans; | ||
has Str $.file; | ||
has Str $.code; | ||
has Event::AST:D @.ast = $!file.defined | ||
?? $!parser.parse-file: $!file | ||
!! $!parser.parse: $!code | ||
; | ||
has @.rules = EventTranslator.new.translate: @!ast; | ||
has Event::Runner $.runner .= new: :$!input, :@!rules; | ||
has Supply:D $.output handles * = $!runner.run; | ||
use MetamodelX::Event; | ||
|
||
proto eel ($, :$code, :$file) is export {*} | ||
multi eel(@inputs, |c) { nextwith Supply.merge(@inputs), |c } | ||
multi eel(Supply:D $input, :$code! --> EEL) { | ||
EEL.new: :$input, :$code | ||
my package EXPORTHOW { | ||
package DECLARE { | ||
constant event = MetamodelX::Event; | ||
} | ||
} | ||
multi eel(Supply:D $input, :$file! --> EEL) { | ||
EEL.new: :$input, :$file | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use QueryStorage; | ||
unit class Event is Any; | ||
|
||
has Event $.parent; | ||
has UInt $.pos = 0; | ||
has Str $.rule; | ||
has $.event; | ||
has QueryStorage $.storage .= new; | ||
has $.actions; | ||
has $.made; | ||
|
||
method parse(Supply $supply, Str :$rule = "TOP") { | ||
my $parent = self.^run: $rule; | ||
supply { | ||
whenever $supply -> $event { | ||
say $parent; | ||
$parent.^step: $event | ||
} | ||
} | ||
} | ||
|
||
method event(*%pars) { | ||
my multi run(0) { | ||
my %query is Map = %pars.kv.map: -> $key, $value { | ||
do if $value !~~ Associative { | ||
$key => %("==" => $value) | ||
} else { | ||
$key => $value | ||
} | ||
} | ||
note "\$!storage.add: %query<>, {self.gist}"; | ||
$!storage.add: %query, self | ||
} | ||
my multi run($) { | ||
note "run: {self.gist}"; | ||
$.^return | ||
} | ||
|
||
run $.pos; | ||
self | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use Event; | ||
unit class MetamodelX::Event is Metamodel::ClassHOW; | ||
|
||
method new_type(|c) { | ||
my $type = callsame; | ||
$type.^add_parent: Event; | ||
$type | ||
} | ||
|
||
method to-map(\match --> Map()) { | ||
match.^attributes.map(-> $a { | ||
$a.name.substr(2) => .<> with $a.get_value(match) | ||
}) | ||
} | ||
|
||
method clone(\match, *%p) { | ||
match.new: |match.^to-map, |%p | ||
} | ||
|
||
method step(\match, $event) { | ||
my @nexts = match.storage.search: $event; | ||
for @nexts -> $match { | ||
$match.^run-next: $event | ||
} | ||
} | ||
|
||
method return(\match) { | ||
my $rule = match.rule; | ||
."$rule"(match) with match.actions; | ||
note "emit: {match.gist}"; | ||
return emit match unless match.parent; | ||
# Should it emit match.made if it exist? | ||
match.parent.run-next: :parent(match) | ||
} | ||
|
||
multi method store-submatch(\match, Str $rule) { | ||
match.run-next(:hash(%(|match.hash, $rule => match.parent))) | ||
} | ||
|
||
multi method store-submatch(\match) { | ||
match.run-next(:list((|match.list, match.parent))) | ||
} | ||
|
||
method run(\match, Str $rule, UInt $pos = 0, |c) { | ||
match.new(:$rule, :$pos, :parent(match))."$rule"(|c) | ||
} | ||
|
||
method run-next(\match, $event?, *%pars, |c) { | ||
note "run-next: {match.gist}, $event"; | ||
my $rule = match.rule; | ||
match.clone(|(:$event with $event), :pos(match.pos + 1), |%pars)."$rule"(|c) # TODO: validate if pos exist | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,22 @@ | ||
######################## | ||
# Emits a FireRisk event if its too hot and too dry on a given area | ||
######################## | ||
|
||
event Temperature is generic-hash-event { has Int $.area; has Rat $.value } | ||
event Humidity is generic-hash-event { has Int $.area; has Rat $.value } | ||
use lib 'lib'; | ||
use EEL; | ||
|
||
event FireRisk { | ||
has Int $.area; | ||
has Rat $.temparature; | ||
has Rat $.humidity; | ||
|
||
pattern TOP { | ||
[ | ||
[ <hot> & <dry> ] | ||
<?{ $<hot>.area == $<dry>.area }> | ||
{ $!area = $<hot>.area } | ||
] in 5min | ||
{ $!temperature = $<hot>.value } | ||
{ $!humidity = $<dry>.value } | ||
| <hot> <dry($<hot><event><area>)> | ||
| <dry> <hot($<dry><event><area>)> | ||
] 5min | ||
} | ||
|
||
pattern hot { | ||
<Temperature> <?{ $<Temperature>.value > 40 }> | ||
pattern hot($area) { | ||
<event(|(:$area with $area), :type<temperature>, :value{'>' => 40 })> | ||
} | ||
|
||
pattern dry { | ||
<Humidity> <?{ $<Humidity>.value < 20 }> | ||
pattern dry($area) { | ||
<event(|(:$area with $area), :type<humidity>, :value{'<' => 20 })> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.