-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add chain component as alternative forward-chaining engine #104
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import asyncio | ||
| from sqlalchemy import select | ||
| from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession | ||
| from apyds import Chain | ||
| from .orm import insert_or_ignore, Facts, Ideas | ||
| from .utility import str_rule_get_str_idea | ||
|
|
||
|
|
||
| async def main(session: async_sessionmaker[AsyncSession]) -> None: | ||
| try: | ||
| chain = Chain() | ||
| max_fact = -1 | ||
|
|
||
| while True: | ||
| async with session() as sess: | ||
| for i in await sess.scalars(select(Facts).where(Facts.id > max_fact)): | ||
| max_fact = max(max_fact, i.id) | ||
| chain.add(i.data) | ||
| tasks = [] | ||
|
|
||
| def handler(rule): | ||
| ds = str(rule) | ||
| tasks.append(asyncio.create_task(insert_or_ignore(sess, Facts, ds))) | ||
| if idea := str_rule_get_str_idea(ds): | ||
| tasks.append(asyncio.create_task(insert_or_ignore(sess, Ideas, idea))) | ||
| return False | ||
|
|
||
| chain.execute(handler) | ||
| await asyncio.gather(*tasks) | ||
| await sess.commit() | ||
|
|
||
| await asyncio.sleep(0) | ||
| except asyncio.CancelledError: | ||
| pass | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { Op, type Sequelize } from "sequelize"; | ||
| import { Chain } from "atsds"; | ||
| import type { Rule } from "atsds"; | ||
| import { Fact, Idea, insertOrIgnore } from "./orm.ts"; | ||
| import { strRuleGetStrIdea } from "./utility.ts"; | ||
|
|
||
| export async function main(sequelize: Sequelize): Promise<void> { | ||
| const chain = new Chain(); | ||
| let maxFact = -1; | ||
|
|
||
| while (true) { | ||
| const newFacts = await Fact.findAll({ | ||
| where: { id: { [Op.gt]: maxFact } }, | ||
| }); | ||
|
|
||
| for (const fact of newFacts) { | ||
| maxFact = Math.max(maxFact, fact.id); | ||
| chain.add(fact.data); | ||
| } | ||
|
|
||
| const tasks: Promise<void>[] = []; | ||
|
|
||
| const handler = (rule: Rule) => { | ||
| const ds = rule.toString(); | ||
| tasks.push(insertOrIgnore(Fact, ds)); | ||
| const idea = strRuleGetStrIdea(ds); | ||
| if (idea) { | ||
| tasks.push(insertOrIgnore(Idea, idea)); | ||
| } | ||
|
Comment on lines
+24
to
+29
|
||
| return false; | ||
| }; | ||
|
|
||
| chain.execute(handler); | ||
| await Promise.all(tasks); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 0)); | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ddss.chain.maincreatesIdeasrows whenstr_rule_get_str_idea(ds)returns a value, but the newly added Chain tests never assert that ideas are created. Add a test that triggers idea generation and verifies anIdeasrecord is inserted, mirroring the coverage that exists for the Search engine.