Created
May 16, 2022 02:35
-
-
Save elcritch/326dc3a5e8020847cb4f284e8781377f to your computer and use it in GitHub Desktop.
pararules static example
This file contains hidden or 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
import pararules | |
import std/[times, monotimes, os, strformat, strutils, sequtils, sugar] | |
type | |
Id = enum | |
Global, Player, Derived | |
Attr = enum | |
DeltaTime, | |
TotalTime, | |
X, | |
Y, | |
AllCharacters | |
Characters = ref seq[tuple[id: int, x: float, y: float]] | |
schema Fact(Id, Attr): | |
DeltaTime: float | |
TotalTime: float | |
X: float | |
Y: float | |
AllCharacters: Characters | |
let (initSession, rules) = | |
staticRuleset(Fact, FactMatch): | |
rule printTime(Fact): | |
what: | |
(Global, TotalTime, tt) | |
then: | |
# echo "global:totaltime: " & $tt | |
discard | |
rule printDeltaTime(Fact): | |
what: | |
(Global, DeltaTime, tt) | |
then: | |
# echo "global:DeltaTime: " & $tt | |
discard | |
rule movePlayer(Fact): | |
what: | |
(Global, TotalTime, tt) | |
then: | |
session.insert(Player, X, tt) | |
session.insert(Player, Y, tt*2) | |
# echo fmt"inserting player: {X=} {tt=}" | |
rule movePlayer10(Fact): | |
what: | |
(Global, TotalTime, tt) | |
then: | |
for i in 1..20: | |
session.insert(10+i, X, 0.1+tt) | |
session.insert(10+i, Y, 0.1+tt*2) | |
# echo fmt"inserting player10: {X=} {tt=}" | |
rule getPlayer(Fact): | |
what: | |
(Player, X, x) | |
(Player, Y, y) | |
rule getCharacter(Fact): | |
what: | |
(id, X, x) | |
(id, Y, y) | |
# then: | |
thenFinally: | |
# session.insert(Derived, AllCharacters, chars) | |
var chars: Characters = nil | |
new chars | |
chars[] = session.queryAll(this) | |
# echo "getCharacters: ", fmt" {chars[]=}" | |
session.insert(Derived, AllCharacters, chars) | |
rule printAllCharacters(Fact): | |
what: | |
(Derived, AllCharacters, chars) | |
then: | |
# echo "All characters: ", chars[] | |
discard | |
# create session and add rule | |
var session: Session[Fact, FactMatch] = initSession(autoFire = false) | |
for r in rules.fields: | |
session.add(r) | |
let st = epochTime() | |
let N = 1000 | |
var deltas = newSeq[int64](N) | |
var t0 = epochTime() | |
for i in 1..N: | |
let timeA = getMonoTime() | |
let | |
t1 = epochTime() | |
deltaTime = t1 - t0 | |
totalTime = t1 - st | |
# echo "\n\n====== loop ======" | |
# echo "insert 1" | |
session.insert(Global, DeltaTime, deltaTime) | |
# echo "insert 2" | |
session.insert(Global, TotalTime, totalTime) | |
# echo "fireRules" | |
session.fireRules() | |
# let results = session.queryAll(rule3) | |
# echo "query rule3: ", repr results | |
t0 = t1 | |
let timeB = getMonoTime() | |
let timeDelta = (timeB - timeA) | |
deltas.add timeDelta.inMicroseconds() | |
# let timeCorrect = clamp(int(deltaTime * 1000) - 100, 0, 10) | |
let timeCorrect = 6 | |
# echo fmt"[timeDelta: {timeDelta.inMicroseconds()} {timeCorrect=}]" | |
# os.sleep(20 - timeDelta.inMilliseconds().int - timeCorrect ) | |
# for i in 1..1000000: | |
# let t1 = epochTime() | |
# let deltaTime = t1 - t0 | |
# if deltaTime >= 0.100: | |
# break | |
var dtAvg = 0.0 | |
for dt in deltas: dtAvg += dt.toBiggestFloat() / deltas.len.toFloat | |
echo "AvgDelta: ", dtAvg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment