-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1975 from DISSINET/1288-check-circularity-in-asym…
…etric-relations 1288 check circularity in asymetric relations
- Loading branch information
Showing
4 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import "ts-jest"; | ||
import Superclass from "./superclass"; | ||
import Path from "./path"; | ||
import { RelationEnums } from "@shared/enums"; | ||
import { IRelationModel } from "./relation"; | ||
import Synonym from "./synonym"; | ||
|
||
describe("test Path for superclasses", () => { | ||
let pathInstance = new Path(RelationEnums.Type.Superclass); | ||
let entries: IRelationModel[] = []; | ||
|
||
beforeAll(async () => { | ||
const ab = new Superclass({ entityIds: ["A", "B"] }) | ||
const bc = new Superclass({ entityIds: ["B", "C"] }); | ||
const cd = new Superclass({ entityIds: ["C", "D"] }); | ||
entries = [ab, bc, cd]; | ||
}) | ||
|
||
test("test Path.build", async () => { | ||
pathInstance.build(entries); | ||
expect(Object.keys(pathInstance.trees)).toHaveLength(entries.length); | ||
}) | ||
|
||
test("test existing path from A->D", () => { | ||
expect(pathInstance.pathExists("A", "D")).toBeTruthy(); | ||
}) | ||
|
||
test("test existing path D -> A", () => { | ||
expect(pathInstance.pathExists("D", "A")).toBeFalsy(); | ||
}) | ||
}); | ||
|
||
describe("test Path for synonyms & superclass", () => { | ||
let pathInstance = new Path(RelationEnums.Type.Synonym); | ||
let entries: IRelationModel[] = []; | ||
|
||
beforeAll(async () => { | ||
const ab = new Synonym({ entityIds: ["A", "B"] }) | ||
const bc = new Synonym({ entityIds: ["B", "C"] }); | ||
const cd = new Superclass({ entityIds: ["C", "D"] }); | ||
entries = [ab, bc, cd]; | ||
}) | ||
|
||
test("test Path.build", async () => { | ||
pathInstance.build(entries); | ||
expect(Object.keys(pathInstance.trees)).toHaveLength(2); // only two synonyms | ||
}) | ||
|
||
test("test existing path from A->D", () => { | ||
expect(pathInstance.pathExists("A", "D")).toBeFalsy(); | ||
}) | ||
|
||
test("test existing path D -> A", () => { | ||
expect(pathInstance.pathExists("D", "A")).toBeFalsy(); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { RelationEnums } from "@shared/enums"; | ||
import { IRelationModel } from "./relation"; | ||
|
||
interface PathTree { | ||
mainId: string; | ||
ids: string[]; | ||
} | ||
|
||
export default class Path { | ||
type: RelationEnums.Type; | ||
trees: Record<string, PathTree> | ||
|
||
constructor(type: RelationEnums.Type) { | ||
this.type = type; | ||
this.trees = {}; | ||
} | ||
|
||
|
||
async build(entries: IRelationModel[]) { | ||
this.trees = {}; | ||
for (const entry of entries.filter(e => e.type === this.type)) { | ||
this.trees[entry.entityIds[0]] = { | ||
mainId: entry.entityIds[0], | ||
ids: entry.entityIds, | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Tests if there exists path from entity A -> B through relations | ||
* @param a | ||
* @param b | ||
*/ | ||
pathExists(a: string, b: string): boolean { | ||
if (!this.trees[a]) { | ||
return false; | ||
} | ||
|
||
let start = [this.trees[a]] | ||
while (start.length) { | ||
const nextStart = []; | ||
for (const subtree of start) { | ||
for (const entityId of subtree.ids) { | ||
if (entityId === b) { | ||
return true; | ||
} | ||
|
||
// entry from 'entityId' -> X must exists to be added to nextStat | ||
if (entityId !== subtree.mainId && this.trees[entityId]) { | ||
nextStart.push(this.trees[entityId]); | ||
} | ||
} | ||
} | ||
start = nextStart; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters