Skip to content

Commit

Permalink
WorldPreference: Require signature, DMFSystemState: Import multiple s…
Browse files Browse the repository at this point in the history
…tates from one string
  • Loading branch information
Philip Heltweg committed Jun 30, 2021
1 parent b57bd2b commit 9099f0b
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 96 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rhazn/logic-ts",
"version": "0.4.1",
"version": "0.5.0",
"description": "Handle logic in typescript",
"private": false,
"repository": {
Expand Down
8 changes: 6 additions & 2 deletions src/logic/DMFSystemState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export class DMFSystemState {
public toJson(): string {
return JSON.stringify({
dmfSystem: JSON.parse(this.dmfSystem.toJson()),
beliefSet: [...this.beliefSet],
contextIndex: this.contextIndex,
states: [
{
beliefSet: [...this.beliefSet],
contextIndex: this.contextIndex,
},
],
});
}
}
14 changes: 8 additions & 6 deletions src/logic/WorldPreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { PropositionalWorldParserFactory } from "../serialize/PropositionalWorld
import { SerializableBinary } from "../serialize/interface/SerializeableBinary";
import { SerializableJson } from "../serialize/interface/SerializeableJson";
import { PropositionalWorld } from "./PropositionalWorld";
import { PropositionalSignature } from "./Syntax";

export class WorldPreference implements SerializableJson, SerializableBinary {
constructor(public data: PropositionalWorld[][]) {}
constructor(public signature: PropositionalSignature, public data: PropositionalWorld[][]) {}

public toJson(): string {
return JSON.stringify(this.data.map(rank => rank.map(world => [...world.assignment])));
return JSON.stringify({
signature: [...this.signature],
ranks: this.data.map(rank => rank.map(world => JSON.parse(world.toJson()))),
});
}

public toBinary(): ArrayBuffer {
Expand All @@ -23,8 +27,7 @@ export class WorldPreference implements SerializableJson, SerializableBinary {
return new ArrayBuffer(0);
}

// todo get signaturesize from a concrete world, dont guess 0,0 is filled
const signatureSize = this.data[0][0].signature.size;
const signatureSize = this.signature.size;

const result = new ArrayBuffer(
// Version
Expand Down Expand Up @@ -101,8 +104,7 @@ export class WorldPreference implements SerializableJson, SerializableBinary {
return new ArrayBuffer(0);
}

// todo get signaturesize from a concrete world, dont guess 0,0 is filled
const signatureSize = this.data[0][0].signature.size;
const signatureSize = this.signature.size;
const possibleWorlds = Math.pow(2, signatureSize);

// Array buffers are initialized to 0
Expand Down
11 changes: 5 additions & 6 deletions src/serialize/DMFSystemStateParserFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import { ParserFactoryJson } from "./interface/ParserFactoryJson";
import { DMFSystemState } from "../logic/DMFSystemState";
import { DMFSystemParserFactory } from "./DMFSystemParserFactory";

export class DMFSystemStateParserFactory implements ParserFactoryJson<DMFSystemState> {
export class DMFSystemStateParserFactory implements ParserFactoryJson<DMFSystemState[]> {
constructor(private signature: PropositionalSignature) {}

public fromJson(json: string): DMFSystemState {
public fromJson(json: string): DMFSystemState[] {
const dmfSystemFactory = new DMFSystemParserFactory(this.signature);
const parsed = JSON.parse(json);
const dmfSystem = dmfSystemFactory.fromJson(JSON.stringify(parsed.dmfSystem));

return new DMFSystemState(
dmfSystemFactory.fromJson(JSON.stringify(parsed.dmfSystem)),
new Set(parsed.beliefSet),
parsed.contextIndex,
return parsed.states.map(
stateData => new DMFSystemState(dmfSystem, new Set(stateData.beliefSet), stateData.contextIndex),
);
}
}
31 changes: 26 additions & 5 deletions src/serialize/WorldPreferenceParserFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ export class WorldPreferenceParserFactory

public fromJson(json: string): WorldPreference {
const parsed = JSON.parse(json);
const signature = new Set(parsed.signature) as PropositionalSignature;

if (signature.size !== this.signature.size) {
throw Error(
`Signature size does not match. Found in data: ${signature.size}, defined in parser: ${this.signature.size}.`,
);
}

return new WorldPreference(
parsed.map(rank => rank.map(assignment => new PropositionalWorld(this.signature, new Set(assignment)))),
signature,
parsed.ranks.map(rank => rank.map(assignment => new PropositionalWorld(signature, new Set(assignment)))),
);
}

Expand All @@ -23,7 +31,7 @@ export class WorldPreferenceParserFactory
const view = new DataView(binary);

if (view.byteLength === 0) {
return new WorldPreference(result);
return new WorldPreference(this.signature, result);
}

let offset = 0;
Expand All @@ -33,6 +41,12 @@ export class WorldPreferenceParserFactory

// read signaturesize
const signatureSize = view.getUint32(offset);
if (signatureSize !== this.signature.size) {
throw Error(
`Signature size does not match. Found in data: ${signatureSize}, defined in parser: ${this.signature.size}.`,
);
}

offset += Uint32Array.BYTES_PER_ELEMENT;

const worldView = PropositionalWorldParserFactory.getMinimalViewForSignatureSize(signatureSize, binary);
Expand Down Expand Up @@ -74,15 +88,15 @@ export class WorldPreferenceParserFactory
}
}

return new WorldPreference(result);
return new WorldPreference(this.signature, result);
}

public fromBinaryRanklist(binary: ArrayBuffer): WorldPreference {
const result = [];
const view = new DataView(binary);

if (view.byteLength === 0) {
return new WorldPreference(result);
return new WorldPreference(this.signature, result);
}

let offset = 0;
Expand All @@ -92,6 +106,13 @@ export class WorldPreferenceParserFactory

// read signaturesize
const signatureSize = view.getUint32(offset);

if (signatureSize !== this.signature.size) {
throw Error(
`Signature size does not match. Found in data: ${signatureSize}, defined in parser: ${this.signature.size}.`,
);
}

offset += Uint32Array.BYTES_PER_ELEMENT;

for (let worldIndex = 0; offset < view.byteLength; worldIndex++, offset += Uint32Array.BYTES_PER_ELEMENT) {
Expand All @@ -117,6 +138,6 @@ export class WorldPreferenceParserFactory
}
}

return new WorldPreference(result);
return new WorldPreference(this.signature, result);
}
}
16 changes: 8 additions & 8 deletions src/test/parse-binary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("parse binary", () => {
test.each([
[
new Uint8Array([1, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 4, 2]).buffer,
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
Expand All @@ -40,7 +40,7 @@ describe("parse binary", () => {
],
[
new Uint8Array([1, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 4, 2, 0, 0, 0, 1, 0, 0, 0, 0, 5]).buffer,
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
Expand All @@ -50,7 +50,7 @@ describe("parse binary", () => {
],
[
new Uint8Array([1, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 4, 2, 0, 0, 0, 1, 0, 0, 0, 0, 5]).buffer,
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
Expand All @@ -59,7 +59,7 @@ describe("parse binary", () => {
[new PropositionalWorld(signature, new Set(["a", "c"]))],
]),
],
[new Uint8Array([]).buffer, new WorldPreference([])],
[new Uint8Array([]).buffer, new WorldPreference(signature, [])],
])("parse: %o", (input: ArrayBuffer, expected: WorldPreference) => {
const inputPreference = preferenceParser.fromBinary(input);

Expand All @@ -77,7 +77,7 @@ describe("parse binary", () => {
1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
]).buffer,
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["b"])),
new PropositionalWorld(signature, new Set(["a"])),
Expand All @@ -89,7 +89,7 @@ describe("parse binary", () => {
1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0,
0, 0, 0, 0,
]).buffer,
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["b"])),
new PropositionalWorld(signature, new Set(["a"])),
Expand All @@ -102,7 +102,7 @@ describe("parse binary", () => {
1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0,
0, 0, 0, 0,
]).buffer,
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["b"])),
new PropositionalWorld(signature, new Set(["a"])),
Expand All @@ -111,7 +111,7 @@ describe("parse binary", () => {
[new PropositionalWorld(signature, new Set(["a", "c"]))],
]),
],
[new Uint8Array([]).buffer, new WorldPreference([])],
[new Uint8Array([]).buffer, new WorldPreference(signature, [])],
])("parse: %o", (input: ArrayBuffer, expected: WorldPreference) => {
const inputPreference = preferenceParser.fromBinaryRanklist(input);

Expand Down
82 changes: 44 additions & 38 deletions src/test/parse-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ describe("parsing json", () => {

test.each([
[
`[[["a"], ["b"]]]`,
new WorldPreference([
`{"signature":["a","b","c"],"ranks":[[["a"], ["b"]]]}`,
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
],
]),
],
[
`[[["a"], ["b"]], [["a", "c"]]]`,
new WorldPreference([
`{"signature":["a","b","c"],"ranks":[[["a"], ["b"]], [["a", "c"]]]}`,
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
Expand All @@ -51,8 +51,8 @@ describe("parsing json", () => {
]),
],
[
`[[["a"], ["b"]], [], [["a", "c"]]]`,
new WorldPreference([
`{"signature":["a","b","c"],"ranks":[[["a"], ["b"]], [], [["a", "c"]]]}`,
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
Expand All @@ -61,7 +61,7 @@ describe("parsing json", () => {
[new PropositionalWorld(signature, new Set(["a", "c"]))],
]),
],
[`[]`, new WorldPreference([])],
[`{"signature":["a","b","c"],"ranks":[]}`, new WorldPreference(signature, [])],
])("parse: %j", (input: string, expected: WorldPreference) => {
expect(preferenceParser.fromJson(input).data).toEqual(expected.data);
});
Expand All @@ -73,16 +73,16 @@ describe("parsing json", () => {

test.each([
[
`{"tpos":[[[["a"],["b"]]],[[[],["a","b"]]]],"edges":[{"fromIndex":0,"toIndex":1,"formula":"a"}]}`,
`{"tpos":[{"signature":["a","b"],"ranks":[[["a"],["b"]]]},{"signature":["a","b"],"ranks":[[[],["a","b"]]]}],"edges":[{"fromIndex":0,"toIndex":1,"formula":"a"}]}`,
new DMFSystem(
new Set([
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
],
]),
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set([])),
new PropositionalWorld(signature, new Set(["a", "b"])),
Expand All @@ -104,34 +104,36 @@ describe("parsing json", () => {

test.each([
[
`{"dmfSystem":{"tpos":[[[["a"],["b"]]],[[[],["a","b"]]]],"edges":[{"fromIndex":0,"toIndex":1,"formula":"a"}]},"beliefSet":["a"],"contextIndex":1}`,
new DMFSystemState(
new DMFSystem(
new Set([
new WorldPreference([
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
],
`{"dmfSystem":{"tpos":[{"signature":["a","b"],"ranks":[[["a"],["b"]]]},{"signature":["a","b"],"ranks":[[[],["a","b"]]]}],"edges":[{"fromIndex":0,"toIndex":1,"formula":"a"}]},"states":[{"beliefSet":["a"],"contextIndex":1}]}`,
[
new DMFSystemState(
new DMFSystem(
new Set([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a"])),
new PropositionalWorld(signature, new Set(["b"])),
],
]),
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set([])),
new PropositionalWorld(signature, new Set(["a", "b"])),
],
]),
]),
new WorldPreference([
[
new PropositionalWorld(signature, new Set([])),
new PropositionalWorld(signature, new Set(["a", "b"])),
],
]),
]),
new Set([{ fromIndex: 0, toIndex: 1, formula: "a" }]),
new Set([{ fromIndex: 0, toIndex: 1, formula: "a" }]),
),
new Set("a"),
1,
),
new Set("a"),
1,
),
],
],
[
`{"dmfSystem":{"tpos":[],"edges":[]},"beliefSet":[],"contextIndex":0}`,
new DMFSystemState(new DMFSystem(new Set([]), new Set()), new Set(), 0),
`{"dmfSystem":{"tpos":[],"edges":[]},"states":[{"beliefSet":[],"contextIndex":0}]}`,
[new DMFSystemState(new DMFSystem(new Set([]), new Set()), new Set(), 0)],
],
])("parse: %o", (input: string, expected: DMFSystemState) => {
])("parse: %o", (input: string, expected: DMFSystemState[]) => {
expect(parser.fromJson(input)).toEqual(expected);
});
});
Expand All @@ -142,9 +144,9 @@ describe("parsing json", () => {

test.each([
[
`{"tpoBefore":[[["a", "b"],["b"],["a"],[]]], "tpoAfter": [[["a", "b"],["a"]],[["b"],[]]], "input": "a"}`,
`{"tpoBefore":{"signature":["a","b"],"ranks":[[["a", "b"],["b"],["a"],[]]]}, "tpoAfter": {"signature":["a","b"],"ranks":[[["a", "b"],["a"]],[["b"],[]]]}, "input": "a"}`,
new SingleStepBeliefRevisionInput(
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a", "b"])),
new PropositionalWorld(signature, new Set(["b"])),
Expand All @@ -153,7 +155,7 @@ describe("parsing json", () => {
],
]),
"a",
new WorldPreference([
new WorldPreference(signature, [
[
new PropositionalWorld(signature, new Set(["a", "b"])),
new PropositionalWorld(signature, new Set(["a"])),
Expand All @@ -166,8 +168,12 @@ describe("parsing json", () => {
),
],
[
`{"tpoBefore": [], "tpoAfter": [], "input": ""}`,
new SingleStepBeliefRevisionInput(new WorldPreference([]), "", new WorldPreference([])),
`{"tpoBefore": {"signature":["a","b"],"ranks":[]}, "tpoAfter": {"signature":["a","b"],"ranks":[]}, "input": ""}`,
new SingleStepBeliefRevisionInput(
new WorldPreference(signature, []),
"",
new WorldPreference(signature, []),
),
],
])("parse: %j", (input: string, expected: SingleStepBeliefRevisionInput) => {
expect(parser.fromJson(input)).toEqual(expected);
Expand Down
Loading

0 comments on commit 9099f0b

Please sign in to comment.