Skip to content

Commit 6995f7a

Browse files
committed
feat(core): adds better typing for IDS facets
1 parent 196da1a commit 6995f7a

File tree

13 files changed

+95
-58
lines changed

13 files changed

+95
-58
lines changed

packages/core/src/core/Types/src/data-map.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export class DataMap<K, V> extends Map<K, V> {
4545
}
4646

4747
/**
48-
* Sets the value for the specified key in the map and triggers the appropriate event (onItemSet or onItemUpdated).
48+
* Sets the value for the specified key in the map.
49+
* If the item is new, then onItemSet is triggered.
50+
* If the item is already in the map, then onItemUpdated is triggered.
4951
*
5052
* @param key - The key of the item to set.
5153
* @param value - The value of the item to set.

packages/core/src/openbim/IDSSpecifications/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class IDSSpecifications extends Component {
8282
ifcVersion,
8383
);
8484

85-
if (identifier) specification.identifier = identifier;
85+
if (identifier) (specification.identifier as any) = identifier;
8686
this.list.set(specification.identifier, specification);
8787
return specification;
8888
}

packages/core/src/openbim/IDSSpecifications/src/Specification.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import * as FRAGS from "@thatopen/fragments";
22
import { Components } from "../../../core/Components";
33
import { DataSet } from "../../../core/Types";
4-
import { IDSCheckResult, IfcVersion } from "./types";
4+
import { IDSCheckResult, IDSSpecificationData, IfcVersion } from "./types";
55
import { UUID } from "../../../utils";
66
import { IDSFacet } from "./facets";
7+
import { IDSSpecifications } from "..";
78

89
/**
910
* Represents a single specification from the Information Delivery Specification (IDS) standard.
1011
*
1112
* @remarks This class provides methods for testing a model against the specification,
1213
* as well as serializing the specification into XML format.
1314
*/
14-
export class IDSSpecification {
15+
export class IDSSpecification implements IDSSpecificationData {
1516
name: string;
1617
ifcVersion = new Set<IfcVersion>();
17-
identifier = UUID.create();
18+
readonly identifier = UUID.create();
1819
description?: string;
1920
instructions?: string;
2021
requirementsDescription?: string;
@@ -31,6 +32,19 @@ export class IDSSpecification {
3132
}
3233
}
3334

35+
set(data: Partial<IDSSpecificationData>) {
36+
const _data = data as any;
37+
const _this = this as any;
38+
for (const key in data) {
39+
if (key === "identifier") continue;
40+
const value = _data[key];
41+
if (key in this) _this[key] = value;
42+
}
43+
const manager = this.components.get(IDSSpecifications);
44+
manager.list.set(this.identifier, this);
45+
return this;
46+
}
47+
3448
/**
3549
* Tests the model to test against the specification's requirements.
3650
*

packages/core/src/openbim/IDSSpecifications/src/exporters/parameter.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
IDSFacetParameterName,
3-
IDSFacetParameter,
4-
IDSEnumerationParameter,
5-
IDSPatternParameter,
6-
} from "../types";
1+
import { IDSFacetParameterName, IDSFacetParameter } from "../types";
72

83
export const getParameterXML = (
94
name: IDSFacetParameterName,
@@ -16,14 +11,14 @@ export const getParameterXML = (
1611
}
1712

1813
if (parameter.type === "enumeration") {
19-
const value = parameter.parameter as IDSEnumerationParameter;
14+
const value = parameter.parameter;
2015
parameterXML = `<xs:restriction base="xs:string">
2116
${value.map((v) => `<xs:enumeration value="${v}" />`).join("\r\n")}
2217
</xs:restriction>`;
2318
}
2419

2520
if (parameter.type === "pattern") {
26-
const value = parameter.parameter as IDSPatternParameter;
21+
const value = parameter.parameter;
2722
parameterXML = `<xs:restriction base="xs:string">
2823
<xs:pattern value="${value}" />
2924
</xs:restriction>`;

packages/core/src/openbim/IDSSpecifications/src/facets/Attribute.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getParameterXML } from "../exporters/parameter";
77
// https://github.com/buildingSMART/IDS/blob/development/Documentation/UserManual/attribute-facet.md
88

99
export class IDSAttribute extends IDSFacet {
10+
facetType = "Attribute" as const;
1011
name: IDSFacetParameter;
1112
value?: IDSFacetParameter;
1213

packages/core/src/openbim/IDSSpecifications/src/facets/Classification.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getParameterXML } from "../exporters/parameter";
99
// https://github.com/buildingSMART/IDS/blob/development/Documentation/UserManual/classification-facet.md
1010

1111
export class IDSClassification extends IDSFacet {
12+
facetType = "Classification" as const;
1213
system: IDSFacetParameter;
1314
value?: IDSFacetParameter;
1415
uri?: string;

packages/core/src/openbim/IDSSpecifications/src/facets/Entity.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getParameterXML } from "../exporters/parameter";
88
// https://github.com/buildingSMART/IDS/blob/development/Documentation/UserManual/entity-facet.md
99

1010
export class IDSEntity extends IDSFacet {
11+
facetType = "Entity" as const;
1112
name: IDSFacetParameter;
1213
predefinedType?: IDSFacetParameter;
1314

packages/core/src/openbim/IDSSpecifications/src/facets/Facet.ts

+8-15
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import {
44
IDSFacetParameter,
55
IDSCheckResult,
66
IDSFacetParameterName,
7-
IDSSimpleParameter,
8-
IDSEnumerationParameter,
9-
IDSPatternParameter,
10-
IDSLengthParameter,
11-
IDSBoundsParameter,
127
IDSCheck,
138
IDSConditionalCardinaltiy,
149
IDSSimpleCardinality,
10+
IDSFacetType,
1511
} from "../types";
1612

1713
export abstract class IDSFacet {
14+
abstract facetType: IDSFacetType;
15+
1816
// Used when the facet is a requirement
1917
// On IDSEntity is always required
2018
cardinality: IDSSimpleCardinality | IDSConditionalCardinaltiy = "required";
@@ -54,24 +52,20 @@ export abstract class IDSFacet {
5452
let pass = false;
5553

5654
if (facetParameter.type === "simple") {
57-
const parameter = facetParameter.parameter as IDSSimpleParameter;
58-
pass = value === parameter;
55+
pass = value === facetParameter.parameter;
5956
}
6057

6158
if (facetParameter.type === "enumeration") {
62-
const parameter = facetParameter.parameter as IDSEnumerationParameter;
63-
pass = parameter.includes(value as never);
59+
pass = facetParameter.parameter.includes(value as never);
6460
}
6561

6662
if (facetParameter.type === "pattern") {
67-
const parameter = facetParameter.parameter as IDSPatternParameter;
68-
const regex = new RegExp(parameter);
63+
const regex = new RegExp(facetParameter.parameter);
6964
pass = regex.test(String(value));
7065
}
7166

7267
if (facetParameter.type === "length") {
73-
const parameter = facetParameter.parameter as IDSLengthParameter;
74-
const { min, length, max } = parameter;
68+
const { min, length, max } = facetParameter.parameter;
7569
if (length !== undefined) {
7670
pass = String(value).length === length;
7771
}
@@ -84,8 +78,7 @@ export abstract class IDSFacet {
8478
}
8579

8680
if (facetParameter.type === "bounds" && typeof value === "number") {
87-
const { min, minInclusive, max, maxInclusive } =
88-
facetParameter.parameter as IDSBoundsParameter;
81+
const { min, minInclusive, max, maxInclusive } = facetParameter.parameter;
8982

9083
let minPass = true;
9184
let maxPass = true;

packages/core/src/openbim/IDSSpecifications/src/facets/Material.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IDSFacet } from "./Facet";
55
// https://github.com/buildingSMART/IDS/blob/development/Documentation/UserManual/material-facet.md
66

77
export class IdsMaterialFacet extends IDSFacet {
8+
facetType = "Material" as const;
89
value?: IDSFacetParameter;
910
uri?: string;
1011

packages/core/src/openbim/IDSSpecifications/src/facets/PartOf.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { IDSEntity } from "./Entity";
1212
// https://github.com/buildingSMART/IDS/blob/development/Documentation/UserManual/partof-facet.md
1313

1414
export class IDSPartOf extends IDSFacet {
15+
facetType = "PartOf" as const;
16+
1517
private _entityFacet: IDSEntity;
1618

1719
private _entity: {

packages/core/src/openbim/IDSSpecifications/src/facets/Property.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getParameterXML } from "../exporters/parameter";
99
// https://github.com/buildingSMART/IDS/blob/development/Documentation/UserManual/property-facet.md
1010

1111
export class IDSProperty extends IDSFacet {
12+
facetType = "Property" as const;
1213
propertySet: IDSFacetParameter;
1314
baseName: IDSFacetParameter;
1415
value?: IDSFacetParameter;

packages/core/src/openbim/IDSSpecifications/src/importers/parameter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { IDSFacetParameter } from "../types";
22

33
export const getParameterValue = (property: any) => {
44
if (!property) return undefined;
5-
const result: Partial<IDSFacetParameter> = { type: "simple" };
5+
const result: Partial<IDSFacetParameter> = {};
66
if ("simpleValue" in property) {
7+
result.type = "simple";
78
result.parameter = property.simpleValue;
89
}
910
if ("restriction" in property) {

packages/core/src/openbim/IDSSpecifications/src/types.ts

+54-29
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,62 @@ export type IDSFacetParameterName =
1313
| "Entity"
1414
| "Relation";
1515

16+
export type IDSFacetType =
17+
| "Entity"
18+
| "Attribute"
19+
| "Property"
20+
| "Classification"
21+
| "Material"
22+
| "PartOf";
23+
1624
// required must match
1725
// prohibited mustn't match
1826
// optional passes for matches and nulls
1927
export type IDSSimpleCardinality = "required" | "prohibited";
2028
export type IDSConditionalCardinaltiy = IDSSimpleCardinality | "optional";
2129

22-
export type IDSSimpleParameter = string | number | boolean;
23-
export type IDSEnumerationParameter = string[] | number[] | boolean[];
24-
export type IDSPatternParameter = string;
25-
export type IDSBoundsParameter = {
26-
min?: number;
27-
minInclusive?: boolean;
28-
max?: number;
29-
maxInclusive?: boolean;
30-
};
31-
export type IDSLengthParameter = {
32-
min?: number;
33-
length?: number;
34-
max?: number;
35-
};
30+
export interface IDSSimpleParameter {
31+
type: "simple";
32+
parameter: string | number | boolean;
33+
}
34+
35+
export interface IDSEnumerationParameter {
36+
type: "enumeration";
37+
parameter: string[] | number[] | boolean[];
38+
}
3639

37-
export interface IDSRestrictionParameter {}
40+
export interface IDSPatternParameter {
41+
type: "pattern";
42+
parameter: string;
43+
}
3844

39-
export type IDSFacetParameterType =
40-
| "simple"
41-
| "enumeration"
42-
| "pattern"
43-
| "bounds"
44-
| "length";
45+
export interface IDSBoundsParameter {
46+
type: "bounds";
47+
parameter: {
48+
min?: number;
49+
minInclusive?: boolean;
50+
max?: number;
51+
maxInclusive?: boolean;
52+
};
53+
}
4554

46-
export interface IDSFacetParameter {
47-
type: IDSFacetParameterType;
48-
parameter:
49-
| IDSSimpleParameter
50-
| IDSEnumerationParameter
51-
| IDSPatternParameter
52-
| IDSBoundsParameter
53-
| IDSLengthParameter;
55+
export interface IDSLengthParameter {
56+
type: "length";
57+
parameter: {
58+
min?: number;
59+
length?: number;
60+
max?: number;
61+
};
5462
}
5563

64+
export type IDSRestrictionParameter =
65+
| IDSEnumerationParameter
66+
| IDSPatternParameter
67+
| IDSBoundsParameter
68+
| IDSLengthParameter;
69+
70+
export type IDSFacetParameter = IDSSimpleParameter | IDSRestrictionParameter;
71+
5672
export interface IDSCheck {
5773
parameter: IDSFacetParameterName;
5874
currentValue: string | number | boolean | null;
@@ -81,3 +97,12 @@ export interface IDSInfo {
8197
purpose?: string;
8298
milestone?: string;
8399
}
100+
101+
export interface IDSSpecificationData {
102+
name: string;
103+
ifcVersion: Set<IfcVersion>;
104+
identifier: string;
105+
description?: string;
106+
instructions?: string;
107+
requirementsDescription?: string;
108+
}

0 commit comments

Comments
 (0)