Skip to content

Commit

Permalink
improve getBasicDataType
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Oct 1, 2023
1 parent d3f410c commit 9f65a03
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface InstantiateVariableOptions extends InstantiateOptions {
nodeId?: NodeIdLike;
minimumSamplingInterval?: number;
propertyOf?: NodeIdLike | UAObject | UAObjectType | UAVariable | UAVariableType | UAMethod;
value?: BindVariableOptions;
value?: BindVariableOptions;
/**
* This attribute indicates whether the Value attribute of the Variableis an array and how many dimensions the array has.
* It may have the following values:
Expand All @@ -31,8 +31,8 @@ export interface InstantiateVariableOptions extends InstantiateOptions {
* * Any (−2): The value can be a scalar or an array with any number of dimensions.
* * ScalarOrOneDimension (−3): The value can be a scalar or a one dimensional array.
* * All DataTypes are considered to be scalar, even if they have array-like semantics like ByteString and String.
*
*
*
*
* Note: the valueRank of the instantiated variable must be compatible with the valueRank of the VariableType.
*/
valueRank?: number;
Expand All @@ -50,12 +50,13 @@ export declare class UAVariableType extends BaseNode implements VariableAttribut

public isAbstract: boolean;

public isSubtypeOf(type: UAVariableType | NodeIdLike): boolean;
public isSubtypeOf(type: UAVariableType | NodeIdLike): boolean;

/** @deprecated - use isSubtypeOf instead */
public isSupertypeOf(type: UAVariableType | NodeIdLike): boolean;
public isSupertypeOf(type: UAVariableType | NodeIdLike): boolean;

public instantiate(options: InstantiateVariableOptions): UAVariable;
public getBasicDataType(): DataType;
}

export interface UAVariableTypeT<T, DT extends DataType> extends UAVariableType {
Expand Down
32 changes: 32 additions & 0 deletions packages/node-opcua-address-space/src/get_basic_datatype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IAddressSpace } from "node-opcua-address-space-base";
import { DataType } from "node-opcua-basic-types";
import { NodeClass } from "node-opcua-data-model";
import { NodeId } from "node-opcua-nodeid";

export interface IBaseNodeVariableOrVariableType {
addressSpace: IAddressSpace;
dataType: NodeId;
}
interface IBaseNodeVariableOrVariableTypeEx extends IBaseNodeVariableOrVariableType {
_basicDataType: DataType;
}
export function _getBasicDataType(uaNode: IBaseNodeVariableOrVariableType): DataType {
const _uaNode = uaNode as IBaseNodeVariableOrVariableTypeEx;
if (_uaNode._basicDataType) {
return _uaNode._basicDataType;
}
if (_uaNode.dataType.namespace === 0 && _uaNode.dataType.value === 0) {
return DataType.Null;
}
const addressSpace = _uaNode.addressSpace;
if (!addressSpace) {
// may be node has been deleted already
return DataType.Null;
}
const dataTypeNode = addressSpace.findDataType(_uaNode.dataType)!;
const basicDataType =
dataTypeNode && dataTypeNode.nodeClass === NodeClass.DataType ? dataTypeNode.getBasicDataType() : DataType.Null;
// const basicDataType = addressSpace.findCorrespondingBasicDataType(uaNode.dataType);
_uaNode._basicDataType = basicDataType;
return basicDataType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ function _constructNamespaceDependency(
if (uaVariable.getBasicDataType() !== DataType.ExtensionObject) {
return;
}
if (!uaVariable.$dataValue) return;
const variant = uaVariable.$dataValue.value;
if (!variant) return;
const value: any | any[] = variant.value;
if (!value) return;
if (Array.isArray(value)) {
Expand Down
19 changes: 2 additions & 17 deletions packages/node-opcua-address-space/src/ua_variable_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import {
_touchValue
} from "./ua_variable_impl_ext_obj";
import { adjustDataValueStatusCode } from "./data_access/adjust_datavalue_status_code";
import { _getBasicDataType } from "./get_basic_datatype";

const debugLog = make_debugLog(__filename);
const warningLog = make_warningLog(__filename);
Expand Down Expand Up @@ -710,23 +711,7 @@ export class UAVariableImpl extends BaseNodeImpl implements UAVariable {
}

public getBasicDataType(): DataType {
if (this._basicDataType) {
return this._basicDataType;
}
if (this.dataType.namespace === 0 && this.dataType.value === 0) {
return DataType.Null;
}
const addressSpace = this.addressSpace;
if (!addressSpace) {
// may be node has been deleted already
return DataType.Null;
}
const dataTypeNode = addressSpace.findDataType(this.dataType)!;
const basicDataType =
dataTypeNode && dataTypeNode.nodeClass === NodeClass.DataType ? dataTypeNode.getBasicDataType() : DataType.Null;
// const basicDataType = addressSpace.findCorrespondingBasicDataType(this.dataType);
this._basicDataType = basicDataType;
return basicDataType;
return _getBasicDataType(this);
}
public adjustVariant(variant: Variant): Variant {
return adjustVariant(variant, this.valueRank, this.getBasicDataType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import {
ModellingRuleType,
INamespace,
UAVariable,
UAVariableType,
UAVariableType
} from "node-opcua-address-space-base";


import { coerceQualifiedName, NodeClass, QualifiedName, BrowseDirection, AttributeIds } from "node-opcua-data-model";
import { DataValue, DataValueLike } from "node-opcua-data-value";
import { checkDebugFlag, make_debugLog, make_warningLog, make_errorLog } from "node-opcua-debug";
Expand All @@ -36,6 +35,7 @@ import * as tools from "./tool_isSubtypeOf";
import { get_subtypeOfObj } from "./tool_isSubtypeOf";
import { get_subtypeOf } from "./tool_isSubtypeOf";
import { checkValueRankCompatibility } from "./check_value_rank_compatibility";
import { _getBasicDataType } from "./get_basic_datatype";

const debugLog = make_debugLog(__filename);
const doDebug = checkDebugFlag(__filename);
Expand Down Expand Up @@ -306,9 +306,11 @@ export class UAVariableTypeImpl extends BaseNodeImpl implements UAVariableType {

return instance;
}
public getBasicDataType(): DataType {
return _getBasicDataType(this);
}
}


/**
* @method hasChildWithBrowseName
* returns true if the parent object has a child with the provided browseName
Expand Down Expand Up @@ -365,4 +367,3 @@ export function assertUnusedChildBrowseName(addressSpace: AddressSpacePrivate, o
);
}
}

33 changes: 33 additions & 0 deletions packages/node-opcua-address-space/test/test_datatype_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,36 @@ describe("testing UADataType - Attribute", () => {
//xx debugLog(dataTypeDefinition.toString());
});
});

describe("testing UADataType - Attribute", () => {
let addressSpace: AddressSpace;
before(async () => {
addressSpace = AddressSpace.create();
const nodesetFilename = [nodesets.standard];
await generateAddressSpace(addressSpace, nodesetFilename);
addressSpace.registerNamespace("PRIVATE");
});
after(async () => {
addressSpace.dispose();
});

it("DTX5 should extract Definition from DataType structure", async () => {
const ns = addressSpace.getOwnNamespace();


const dataType = ns.createDataType({

browseName: "MyDataType",
isAbstract: true,
subtypeOf: "Structure",
partialDefinition: [
{
dataType: "Double",
description: "the list of values",
name: "Values",
valueRank: 1
}
]
});
});
});

0 comments on commit 9f65a03

Please sign in to comment.