Skip to content

Commit

Permalink
feat: wire up rule optimization actors
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Apr 3, 2022
1 parent cf6ed81 commit 60e81fa
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 45 deletions.
5 changes: 1 addition & 4 deletions .componentsjs-generator-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
"Headers",
"Readable",
"LRUCache",
"RuleStream",
"IActionContext",
"Algebra.Pattern",
"AsyncIterator"
"RuleStream"
],
"modulePrefix": {
"@comunica/bus-dereference-rule": "cbdrl"
Expand Down
9 changes: 6 additions & 3 deletions engines/config-reasoning/config/optimize-rule/actors.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"@context": [
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/runner/^2.0.0/components/context.jsonld"
],
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/runner/^2.0.0/components/context.jsonld",
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-optimize-rule-remove-false-conclusion/^1.0.0/components/context.jsonld",
"https://linkedsoftwaredependencies.org/bundles/npm/@comunica/actor-optimize-rule-pattern-restriction/^1.0.0/components/context.jsonld"
],
"@id": "urn:comunica:default:Runner",
"@type": "Runner",
"actors": [
Expand All @@ -11,7 +13,8 @@
},
{
"@id": "urn:comunica:default:optimize-rule/actors#pattern-restriction",
"@type": "ActorOptimizeRulePatternRestriction"
"@type": "ActorOptimizeRulePatternRestriction",
"beforeActors": { "@id": "urn:comunica:default:optimize-rule/actors#remove-false-conclusion" }
}
]
}
1 change: 1 addition & 0 deletions engines/config-reasoning/config/reasoning-default.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"import": [
"ccr:config/normalize-rule/mediators.json",
"ccr:config/optimize-rule/mediators.json",
"ccr:config/optimize-rule/actors.json",
"ccr:config/rule-parse/actors.json",
"ccr:config/rule-parse/mediators.json",
"ccr:config/dereference-rule/actors.json",
Expand Down
2 changes: 0 additions & 2 deletions engines/config-reasoning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,5 @@
},
"dependencies": {
"@comunica/config-query-sparql": "^2.0.1"
},
"devDependencies": {
}
}
4 changes: 2 additions & 2 deletions engines/query-sparql-file-reasoning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
],
"license": "MIT",
"bin": {
"comunica-dynamic-sparql": "bin/query-dynamic.js",
"comunica-sparql": "bin/query.js",
"comunica-sparql-http": "bin/http.js",
"comunica-dynamic-sparql": "bin/query-dynamic.js"
"comunica-sparql-http": "bin/http.js"
},
"bugs": {
"url": "https://github.com/comunica/comunica-feature-reasoning/issues"
Expand Down
4 changes: 2 additions & 2 deletions engines/query-sparql-reasoning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
],
"license": "MIT",
"bin": {
"comunica-dynamic-sparql": "bin/query-dynamic.js",
"comunica-sparql": "bin/query.js",
"comunica-sparql-http": "bin/http.js",
"comunica-dynamic-sparql": "bin/query-dynamic.js"
"comunica-sparql-http": "bin/http.js"
},
"bugs": {
"url": "https://github.com/comunica/comunica-feature-reasoning/issues"
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@
"test-ci": "jest --ci --maxWorkers=4 --coverage",
"lint": "eslint . --ext .ts --cache",
"clean": "rm -rf ./node_modules && rm -rf ./packages/*/node_modules",
"build": "npm run build:ts && npm run build:components",
"build": "yarn run build:ts && yarn run build:components",
"build:ts": "tsc",
"build:components": "componentsjs-generator engines/* packages/*",
"build:components": "lerna exec -- npm run build:components --if-present",
"build-watch": "nodemon -e ts --ignore '*.d.ts' --exec yarn run build",
"build-watch:ts": "tsc --watch",
"build-watch:components": "nodemon -e d.ts --exec yarn run build:components",
Expand All @@ -128,9 +128,7 @@
"postinstall": "yarn run build && lerna run prepare",
"version": "manual-git-changelog onversion",
"depcheck": "lerna-script depcheckTask",
"depcheck:fix": "lerna-script depfixTask",
"build:engine": "yarn run build:engine:sparql-reasoning",
"build:engine:sparql-reasoning": "(cd engines && comunica-package -c config-reasoning/config/config-default.json -o query-sparql-reasoning)"
"depcheck:fix": "lerna-script depfixTask"
},
"resolutions": {
"@rdfjs/types": "1.1.0"
Expand Down
3 changes: 1 addition & 2 deletions packages/actor-dereference-rule-parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@
"build": "npm run build:ts && npm run build:components",
"build:ts": "node \"../../node_modules/typescript/bin/tsc\"",
"build:components": "componentsjs-generator"
},
"devDependencies": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export class ActorOptimizeRulePatternRestriction extends ActorOptimizeRule {
}

public async test(action: IActionOptimizeRule): Promise<IActorTest> {
// Console.log(action)

const { pattern } = action;

if (!pattern) {
Expand Down Expand Up @@ -101,16 +99,15 @@ IPremiseConclusionRule[] {
* @param pattern A pattern - possibly containing variables
* @param quad A quad - possibly containing variables
*/
export function matchPatternMappings(pattern: RDF.Quad | Algebra.Pattern, quad: RDF.Quad): boolean {
export function matchPatternMappings(pattern: RDF.Quad | Algebra.Pattern, quad: Algebra.Pattern | RDF.Quad): boolean {
const mapping: Record<string, RDF.Term> = {};
const res = everyTerms(pattern, (term, key) => {
if (term.termType !== 'Variable') {
if (quad[key].termType === 'Variable')
return true;
if (term.termType !== "Variable")
return term.equals(quad[key]);
}
// eslint-disable-next-line no-return-assign
return quad[key].termType === 'Variable' || (term.value in mapping ?
mapping[term.value].equals(quad[key]) :
(mapping[term.value] = quad[key]) && true);
return (term.value in mapping ? mapping[term.value].equals(quad[key]) : (mapping[term.value] = quad[key]) && true);
});
return res;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
],
"dependencies": {
"@comunica/bus-optimize-rule": "^1.0.0",
"@comunica/reasoning-types": "^1.0.0",
"@comunica/core": "^2.0.1",
"@comunica/reasoning-types": "^1.0.0",
"@rdfjs/types": "*",
"asynciterator": "^3.4.0",
"rdf-terms": "^1.7.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,71 @@ describe('ActorOptimizeRulePatternRestriction', () => {
) });
expect(await rules.toArray()).toHaveLength(2);
});

it('should work with default graph rule', async() => {
const { rules } = await actor.run({ context: new ActionContext(),
rules: fromArray<Rule>([
{
ruleType: 'premise-conclusion',
premise: [ DF.quad(
DF.variable('s'),
DF.namedNode('a'),
DF.variable('o'),
DF.defaultGraph(),
), DF.quad(
DF.variable('o'),
DF.namedNode('subClassOf'),
DF.variable('o2'),
DF.defaultGraph(),
) ],
conclusion: [ DF.quad(
DF.variable('s'),
DF.namedNode('a'),
DF.variable('o2'),
DF.defaultGraph(),
) ],
},
]),
pattern: factory.createPattern(
DF.variable('s'),
DF.variable('p'),
DF.variable('o'),
DF.variable('g'),
) });
expect(await rules.toArray()).toHaveLength(1);
});

it('should work with default graph rule and using restricted pattern', async() => {
const { rules } = await actor.run({ context: new ActionContext(),
rules: fromArray<Rule>([
{
ruleType: 'premise-conclusion',
premise: [ DF.quad(
DF.variable('s'),
DF.namedNode('a'),
DF.variable('o'),
DF.defaultGraph(),
), DF.quad(
DF.variable('o'),
DF.namedNode('subClassOf'),
DF.variable('o2'),
DF.defaultGraph(),
) ],
conclusion: [ DF.quad(
DF.variable('s'),
DF.namedNode('a'),
DF.variable('o2'),
DF.defaultGraph(),
) ],
},
]),
pattern: factory.createPattern(
DF.namedNode('Jesse'),
DF.namedNode('a'),
DF.variable('o'),
DF.variable('g'),
) });
expect(await rules.toArray()).toHaveLength(1);
});
});
});
2 changes: 1 addition & 1 deletion packages/actor-rdf-reason-rule-restriction/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
],
"dependencies": {
"@comunica/bus-rdf-reason": "^1.0.0",
"@comunica/reasoning-context-entries": "^1.0.0",
"@comunica/core": "^2.0.0",
"@comunica/reasoning-context-entries": "^1.0.0",
"@comunica/reasoning-types": "^1.0.0",
"@rdfjs/types": "*",
"asynciterator": "3.4.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/actor-rule-parse-hylar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@
"build": "npm run build:ts && npm run build:components",
"build:ts": "node \"../../node_modules/typescript/bin/tsc\"",
"build:components": "componentsjs-generator"
},
"devDependencies": {}
}
}
4 changes: 2 additions & 2 deletions packages/actor-rule-resolve-hypermedia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
],
"dependencies": {
"@comunica/bus-dereference-rule": "^1.0.0",
"@comunica/reasoning-types": "^1.0.0",
"@comunica/bus-http-invalidate": "^2.0.0",
"@comunica/bus-rule-resolve": "^1.0.0",
"@comunica/core": "^2.0.0",
"@comunica/types": "^2.0.0",
"@comunica/reasoning-context-entries": "^1.0.0",
"@comunica/reasoning-types": "^1.0.0",
"@comunica/types": "^2.0.0",
"@types/lru-cache": "^5.1.0",
"asynciterator": "^3.4.0",
"lru-cache": "^6.0.0"
Expand Down
7 changes: 3 additions & 4 deletions packages/bus-rdf-reason/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
],
"dependencies": {
"@comunica/bus-optimize-rule": "^1.0.0",
"@comunica/reasoning-types": "^1.0.0",
"@comunica/reasoning-context-entries": "^1.0.0",
"@comunica/bus-rdf-resolve-quad-pattern": "^2.0.0",
"@comunica/bus-rdf-update-quads": "^2.0.0",
"@comunica/bus-rule-resolve": "^1.0.0",
"@comunica/context-entries": "^2.0.0",
"@comunica/core": "^2.0.0",
"@comunica/reasoning-context-entries": "^1.0.0",
"@comunica/reasoning-types": "^1.0.0",
"@comunica/types": "^2.0.0",
"@rdfjs/types": "*",
"asynciterator": "^3.4.0",
Expand All @@ -47,6 +47,5 @@
"build": "npm run build:ts && npm run build:components",
"build:ts": "node \"../../node_modules/typescript/bin/tsc\"",
"build:components": "componentsjs-generator"
},
"devDependencies": {}
}
}
6 changes: 2 additions & 4 deletions packages/reasoning-mocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@
"@comunica/bus-rdf-parse": "^2.0.1",
"@comunica/bus-rdf-reason": "^1.0.0",
"@comunica/context-entries": "^2.0.1",
"@comunica/reasoning-context-entries": "^1.0.0",
"@comunica/core": "^2.0.1",
"@comunica/reasoning-context-entries": "^1.0.0",
"@comunica/reasoning-types": "^1.0.0",
"@rdfjs/types": "*",
"asynciterator": "^3.4.0",
"n3": "^1.16.0"
},
"scripts": {
"build": "npm run build:ts && npm run build:components",
"build:ts": "node \"../../node_modules/typescript/bin/tsc\"",
"build:components": "componentsjs-generator"
"build": "node \"../../node_modules/typescript/bin/tsc\""
},
"devDependencies": {
"@types/n3": "^1.10.4",
Expand Down
4 changes: 1 addition & 3 deletions packages/reasoning-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
"@rdfjs/types": "*"
},
"scripts": {
"build": "npm run build:ts && npm run build:components",
"build:ts": "node \"../../node_modules/typescript/bin/tsc\"",
"build:components": "componentsjs-generator"
"build": "node \"../../node_modules/typescript/bin/tsc\""
}
}

0 comments on commit 60e81fa

Please sign in to comment.