Skip to content

Commit 1c0df8f

Browse files
committed
feat: add browser support
1 parent 1cc8c1b commit 1c0df8f

File tree

8 files changed

+2641
-49
lines changed

8 files changed

+2641
-49
lines changed

package-lock.json

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
{
22
"name": "odrl-evaluator",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "An open implementation of an ODRL Evaluator that evaluates ODRL policies by generating Compliance Reports",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
7+
"browser": {
8+
"./dist/index.js": "./dist/index.browser.js",
9+
"./dist/index.d.ts": "./dist/index.browser.d.ts"
10+
},
711
"files": [
812
"dist"
913
],
1014
"scripts": {
11-
"build": "npm run clean && tsc && npm run copy-files",
15+
"build": "npm run clean && npm run makeRules && tsc && npm run copy-files",
16+
"makeRules": "ts-node scripts/MakeRules.ts ",
1217
"clean": "rm -rf ./dist",
13-
"copy-files":"cp -r ./src/rules/ ./dist/",
18+
"copy-files": "cp -r ./src/rules/ ./dist/",
1419
"demo:test-engine": "ts-node demo/test-n3-engine.ts",
1520
"demo:test-evaluator": "ts-node demo/test-n3-evaluator.ts",
1621
"prepare": "npm run build",
1722
"release": "npm run build && npm publish --access public",
18-
"test":"jest"
23+
"test": "jest"
1924
},
2025
"repository": {
2126
"type": "git",
@@ -47,6 +52,7 @@
4752
"rdf-isomorphic": "^1.3.1",
4853
"rdf-parse": "^3.0.0",
4954
"rdf-store-stream": "^2.0.1",
55+
"rdf-vocabulary": "^1.0.0",
5056
"streamify-string": "^1.0.1",
5157
"tmp": "^0.2.3",
5258
"uuidv4": "^6.2.13"

scripts/MakeRules.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as path from "path"
2+
import * as fs from "fs"
3+
import { combineNotation3Files } from "../src/util/Notation3Util";
4+
5+
// initialize the rules as ts class such that it can be used in the browser
6+
const ruleDir = path.join(path.dirname(__filename), "..", "src", "rules");
7+
const fileNamesRulesFirst = ["built-ins.n3", "constraints.n3", "report.n3", "odrl-voc.ttl", "odrl-voc-inferences.ttl"];
8+
const fileNamesRulesSecond = ["activation-state.n3"];
9+
10+
const firstStepRulePaths = fileNamesRulesFirst.map(fileName => path.join(ruleDir, fileName));
11+
const secondStepRulePaths = fileNamesRulesSecond.map(fileName => path.join(ruleDir, fileName));
12+
13+
const firstStepRules = combineNotation3Files(firstStepRulePaths);
14+
const secondStepRules = combineNotation3Files(secondStepRulePaths);
15+
const rules = `export const RULES: string[] = [\`${firstStepRules}\`, \`${secondStepRules}\`];`
16+
fs.writeFileSync(path.join(ruleDir, "Rules.ts"), rules)

src/evaluator/Engine.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ import { Reasoner } from "../reasoner/Reasoner";
44
import { EyeJsReasoner } from "../reasoner/EyeJsReasoner";
55
import * as path from "path"
66
import * as fs from "fs"
7-
import { combineNotation3, combineNotation3Files } from '../util/Notation3Util';
7+
import { RULES} from '../rules/Rules'
88

99
export interface Engine {
1010
evaluate(input: Quad[]): Promise<Quad[]>;
1111
}
1212

1313
export class ODRLN3Engine implements Engine {
1414
protected readonly reasoner: Reasoner;
15-
protected readonly rules: string;
15+
protected readonly rules: string[];
1616

17-
constructor(reasoner: Reasoner, rules: string) {
17+
constructor(reasoner: Reasoner, rules: string[]) {
1818
this.reasoner = reasoner;
1919
this.rules = rules;
2020
}
2121

2222
public async evaluate(input: Quad[]): Promise<Quad[]> {
23-
const evaluation = await this.reasoner.reason(new Store(input), [this.rules]);
23+
const evaluation = await this.reasoner.reason(new Store(input), this.rules);
2424
return evaluation.getQuads(null, null, null, null);
2525
}
2626
}
@@ -30,48 +30,27 @@ export class ODRLEngine extends ODRLN3Engine {
3030
const ruleDir = path.join(path.dirname(__filename), "..", "rules");
3131
const rulePath = path.join(ruleDir, "simpleRules.n3");
3232
const rules = fs.readFileSync(rulePath, "utf-8");
33-
super(reasoner, rules);
33+
super(reasoner, [rules]);
3434
}
3535
}
3636

37-
export class ODRLEngineMultipleSteps extends ODRLN3Engine {
38-
private readonly firstStepRules: string;
39-
private readonly secondStepRules: string;
40-
41-
constructor(reasoner?: Reasoner) {
42-
reasoner = reasoner ?? new EyeJsReasoner();
43-
const ruleDir = path.join(path.dirname(__filename), "..", "rules");
4437

45-
// note: if there are more steps than two, this MUST be made into a function.
46-
// This also applies to the class variables and evaluate function.
47-
const fileNamesRulesFirst = ["built-ins.n3", "constraints.n3", "report.n3", "odrl-voc.ttl", "odrl-voc-inferences.ttl"];
48-
const fileNamesRulesSecond = ["activation-state.n3"];
49-
50-
const firstStepRulePaths = fileNamesRulesFirst.map(fileName => path.join(ruleDir, fileName));
51-
const secondStepRulePaths = fileNamesRulesSecond.map(fileName => path.join(ruleDir, fileName));
52-
53-
const firstStepRules = combineNotation3Files(firstStepRulePaths);
54-
const secondStepRules = combineNotation3Files(secondStepRulePaths);
55-
56-
super(reasoner, combineNotation3([firstStepRules, secondStepRules]) );
57-
this.firstStepRules = firstStepRules;
58-
this.secondStepRules = secondStepRules;
38+
export class ODRLEngineMultipleSteps extends ODRLN3Engine {
39+
constructor(config?: {reasoner?: Reasoner, rules?: string[]}) {
40+
const reasoner = config?.reasoner ?? new EyeJsReasoner();
41+
const rules = config?.rules ?? RULES;
42+
super(reasoner, rules );
5943
}
6044

6145
public async evaluate(input: Quad[]): Promise<Quad[]> {
6246
const complianceReportQuads: Quad[] = [];
6347
const store = new Store(input);
6448

65-
const firstEvaluation = await this.reasoner.reason(store,[this.firstStepRules]);
66-
67-
// The first evaluation creates a partial report. Now reason again over the same input + partial evaluation report
68-
complianceReportQuads.push(...firstEvaluation.getQuads(null, null, null, null));
69-
store.addQuads(complianceReportQuads)
70-
71-
// might be a problem that store is re-used?
72-
const secondEvaluation = await this.reasoner.reason(store,[this.secondStepRules]);
73-
74-
complianceReportQuads.push(...secondEvaluation.getQuads(null, null, null, null));
49+
for (const rule of this.rules){
50+
const evaluation = await this.reasoner.reason(store, [rule]);
51+
complianceReportQuads.push(...evaluation.getQuads(null, null, null, null));
52+
store.addQuads(complianceReportQuads)
53+
}
7554
return complianceReportQuads;
7655
}
7756
}

src/index.browser.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export * from './evaluator/Engine'
2+
export * from './evaluator/Evaluate'
3+
export * from './evaluator/Validate'
4+
5+
export * from './reasoner/EyeJsReasoner'
6+
// export * from './reasoner/EyeReasoner'
7+
export * from './reasoner/Reasoner'
8+
9+
export * from './util/Notation3Util'
10+
// export * from './util/RDFUtil'
11+
export * from './rules/Rules'

src/reasoner/Reasoner.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { Store } from "n3";
2-
import { storeToString, turtleStringToStore } from "../util/RDFUtil";
3-
1+
import { Store, Parser, Writer } from "n3";
42

53
// copy from https://github.com/eyereasoner/Koreografeye/tree/e2c980c64b230ccbd65a5d0e0bf22cdf0cd5d21c/src/orchestrator/reasoner + my own util functions
64
export abstract class Reasoner {
@@ -26,16 +24,16 @@ export abstract class Reasoner {
2624
*/
2725
public async reason(dataStore: Store, rules: string[]): Promise<Store> {
2826

29-
const n3 = storeToString(dataStore);
27+
const n3 = new Writer().quadsToString(dataStore.getQuads(null, null, null, null));
3028

3129
if (!n3) {
3230
throw new Error(`failed to transform store to turtle`);
3331
}
3432

3533
const result = await this.run([n3], rules);
3634

37-
const resultStore = await turtleStringToStore(result);
38-
35+
const resultStore = new Store(new Parser().parse(result));
36+
3937
return resultStore;
4038
}
4139
}

0 commit comments

Comments
 (0)