Skip to content

Instantly share code, notes, and snippets.

@jeswr
Created May 11, 2022 00:44
Show Gist options
  • Save jeswr/8260c6a753ef4f8bccc3893464f72130 to your computer and use it in GitHub Desktop.
Save jeswr/8260c6a753ef4f8bccc3893464f72130 to your computer and use it in GitHub Desktop.
public async execute({ rules, context }: IActionRdfReasonExecute): Promise<void> {
const nodes: IRuleNode[] = rules.map(rule => ({ rule, next: [] }));
// Creating rule dependencies
for (const n1 of nodes) {
for (const n2 of nodes) {
if (n1.rule.conclusion === false) {
continue;
}
for (const conclusion of n1.rule.conclusion) {
for (let i = 0; i < n2.rule.premise.length; i++) {
const pattern = n2.rule.premise[i];
if (matchPatternMappings(conclusion, pattern)) {
n1.next.push({ rule: n2, index: i });
}
}
}
}
}
const store = context.get(KeysRdfResolveQuadPattern.source) as Store
const nexts = []
function runRule(rule: IRuleNode) {
const next = [];
for (const mapping of applyMappings(rule, store)) {
for (const conclusion of rule.rule.conclusion || []) {
const quad = substituteQuad(conclusion, mapping);
if (store.addQuad(quad) as unknown as boolean) {
next.push(quad);
}
}
}
if (next.length > 0) {
return { rule, quads: next };
}
}
for (const rule of nodes) {
const res = runRule(rule);
if (res)
nexts.push(res)
}
let n;
while ((n = nexts.pop()) !== undefined) {
const { rule, quads } = n;
for (const r of rule.next) {
for (const quad of quads) {
const s = maybeSubstitute(r, quad);
if (s) {
const run = runRule(s);
if (run)
nexts.push(run)
}
}
}
}
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment