All files / node-opcua-isa95/lib isa95_address_space_extension_utils.js

88.75% Statements 71/80
68.18% Branches 15/22
91.67% Functions 11/12
88.75% Lines 71/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 2371x 1x 1x     1x   1x   1x     17x 17x 17x     17x 17x 17x                                         17x 17x 17x 17x     17x   17x             17x             17x 17x 17x   17x               1x   1x 1x   1x   1x 1x           1x                                                 1x     1x                           1x   19x 19x                                 19x 12x 12x     12x   19x 13x     19x     19x   23x 6x   17x   17x               1x     10x 6x   4x   4x 4x   4x 4x     4x 4x   4x         4x       1x   6x 6x   6x 6x   6x 6x     6x         6x           6x 6x   6x        
var assert = require("assert");
var path = require("path");
var _ = require("underscore");
 
 
module.exports = function (opcua) {
 
    opcua.ISA95.utils = {};
 
    var BrowseDirection = opcua.browse_service.BrowseDirection;
 
    function _coerceISA95ReferenceType(addressSpace,obj) {
        var _coerced = typeof obj === "string" ? addressSpace.findISA95ReferenceType(obj) : obj;
        assert(_coerced,obj + " should exists in ISA95  addressSpace");
        return _coerced;
    }
    function _coerceISA95ObjectType(addressSpace,obj) {
        var _coerced = typeof obj === "string" ? addressSpace.findISA95ObjectType(obj) : obj;
        assert(_coerced,obj.toString() + " should exists in ISA95  addressSpace");
        return _coerced;
    }
 
    /**
     *
     * @param instance
     * @param FooClassReference
     * @param FooClassType
     * @param type                     must be a subtype Of FooClassType
     * @private
     *
     *
     *     _addDefinedByFooClassReference(instance,"DefinedByEquipmentClass","EquipmentClassType",type);
     *     will:
     *        verify that type is a EquipmentClassType
     *        add to instance, a DefinedByEquipmentClass reference pointing to type
     *        update  instance.definedByEquipmentClass by adding type to it.
     *
     */
    function _addDefinedByFooClassReference(instance,FooClassReference,FooClassType,type) {
 
        assert(type," expecting a type");
        assert(FooClassReference, "expecting a ClassReference");
        assert(FooClassType, "expecting a FooClassType");
        var addressSpace = instance.addressSpace;
 
 
        var definedByEquipmentClass = _coerceISA95ReferenceType(addressSpace,FooClassReference);
 
        var equipmentClassType = _coerceISA95ObjectType(addressSpace,FooClassType);
 
        // istanbul ignore next
        if(!type.isSupertypeOf(equipmentClassType)) {
            throw new Error(type.browseName.toString() + " must be of type "+ equipmentClassType.browseName.toString() );
        }
 
        instance.addReference({
            referenceType: definedByEquipmentClass.nodeId,
            nodeId: type
        });
 
        // TODO : Add a __Getter__ property and use cache instead ...
        // set definedByEquipmentClass
        var attribute = opcua.utils.lowerFirstLetter(FooClassReference);
        Eif (!instance[attribute]) {
            instance[attribute] = [];
        }
        instance[attribute].push(type);
 
 
    }
 
 
    function addDefinedByFooClass(node,definedByFooClass,isa95PropertyType,options) {
 
        var addressSpace = node.addressSpace;
 
        var name = opcua.utils.capitalizeFirstLetter(definedByFooClass);
        var attribute = opcua.utils.lowerFirstLetter(definedByFooClass);
 
        var definedByFooClassReference = addressSpace.findISA95ReferenceType(name);
 
        options[attribute] = options[attribute] || [];
        Iif (!_.isArray(options[attribute])) {
            options[attribute] = [options[attribute]];
        }
 
        function addDefinedByFooClassReference(classType) {
 
            node.addReference({
                referenceType: definedByFooClassReference.nodeId, nodeId: classType
            });
 
            // also duplicate all hasIsa95ClassProperty of classType into HasIsa95Property on type
            var hasISA95ClassProperty = addressSpace.findISA95ReferenceType("HasISA95ClassProperty");
            var hasISA95Property = addressSpace.findISA95ReferenceType("HasISA95ClassProperty");
            var refs = classType.findReferencesExAsObject(hasISA95ClassProperty,BrowseDirection.Forward);
 
            function constructIsa95PropertyFromISA95ClassProperty(reference) {
                var srcProperty = addressSpace.findNode(reference.nodeId);
 
                // clone property
                var property = addressSpace.addISA95Property({
                    ISA95PropertyOf: node,
                    browseName:  srcProperty.browseName,
                    dataType: srcProperty.dataType,
                    value: srcProperty.readValue().value,
                    typeDefinition: isa95PropertyType
                });
            }
            refs.forEach(constructIsa95PropertyFromISA95ClassProperty);
 
            //xx console.log("[==>", refs.map(f => f.toString()).join("\n"));
        }
        options[attribute].forEach(addDefinedByFooClassReference);
 
    }
    opcua.ISA95.utils.addDefinedByFooClass = addDefinedByFooClass;
 
    /**
     *
     * @param params
     * @param params.node                {UAObject} the node to add the DefinedBy...ClassType reference
     * @param params.definedByFooClass   {String|UAObjectType|[]}
     * @param params.fooClassType
     * @param params.fooType
     * @param params.definedByFooClassRef  = the r i.e "DefinedByEquipmentClass"
     *
     * example :
     *
     */
    opcua.ISA95.utils.installDefinedByFooClassReferences = function(params) {
 
        assert(params.node);
        var addressSpace = params.node.addressSpace;
 
        // -------------------------------------------------------------------------------------------------------------
        // Duplicate features defined in definedBy(Equipment|PhysicalAsset)Class
        //
        //    My(Equipment|PhysicalAsset)ClassType                          (Equipment|PhysicalAsset)Type
        //          |                                                            |
        //          |                                               \            +---definedBy(Equipment|PhysicalAsset)Class-->My(Equipment|PhysicalAsset)ClassType
        //          |                                         -------\           |
        //          +-HasISA95ClassProperty-> "Property1"             >          +-HasISA95Property-->"Property1"
        //          |                                         -------/
        //          +- hasSubtypeOf ->                              /
        //                             (Equipment|PhysicalAsset)ClassType
        // -------------------------------------------------------------------------------------------------------------
        // The Object identified by the SourceNode has the same features as that defined by the Object specified by TargetNode.
 
 
        if (typeof params.definedByFooClass === "string") {
            var node = addressSpace.findISA95ObjectType(params.definedByFooClass);
            Iif (!node) {
                throw Error(params.definedByFooClass +" must be a valid ISA95 Class Type");
            }
            params.definedByFooClass = node;
        }
        if (!_.isArray(params.definedByFooClass)) {
            params.definedByFooClass =[params.definedByFooClass];
        }
 
        Iif (typeof params.fooClassType === "string") {
            params.fooClassType = addressSpace.findISA95ObjectType(params.fooClassType);
        }
        params.definedByFooClass.forEach(function(xxxxClass){
            // we need to exclude OptionalPlaceHolder
            if (xxxxClass.modellingRule === "OptionalPlaceholder") {
                return;
            }
            assert(xxxxClass.isSupertypeOf(params.fooClassType),"expecting ");
 
            _addDefinedByFooClassReference(
                params.node,
                params.definedByFooClassRef,  // "DefinedByEquipmentClass"
                params.fooClassType,          // "EquipmentClassType",
                xxxxClass);
        });
    };
 
    opcua.ISA95.utils._transferISA95Attributes = function (instance,classType) {
 
        // we need to exclude OptionalPlaceHolder
        if (classType.modellingRule === "OptionalPlaceholder") {
            return;
        }
        assert(classType.constructor.name == "UAObjectType");
 
        var addressSpace = instance.addressSpace;
        var hasISA95Attribute = addressSpace.findISA95ReferenceType("HasISA95Attribute");
 
        assert(hasISA95Attribute);
        var refs = classType.findReferencesEx(hasISA95Attribute);
 
        function constructIsa95AttributeFromISA95Attribute(reference) {
            var attribute = addressSpace.findNode(reference.nodeId);
            var _clone = attribute.clone();
 
            instance.addReference({
                referenceType: reference.referenceType,
                nodeId: _clone
            });
        }
        refs.forEach(constructIsa95AttributeFromISA95Attribute);
 
    };
 
    opcua.ISA95.utils._addContainedByFooReference = function (node,foo,fooType,madeUpOfFoo) {
 
        var addressSpace = node.addressSpace;
        assert(foo.nodeId instanceof opcua.NodeId);
 
        assert(typeof fooType === "string");
        var fooType = addressSpace.findISA95ObjectType(fooType);
 
        assert(typeof madeUpOfFoo === "string");
        var madeUpOfFoo = addressSpace.findISA95ReferenceType(madeUpOfFoo);
 
        // verify that containedByEquipment is really a equipment !
        var t = foo.typeDefinitionObj;
 
        //xx console.log(t.toString());
        //xx assert(equipmentType.isSupertypeOf(t),"options.containedByEquipment object must be of EquipmentType");
 
        node.addReference({
            referenceType: madeUpOfFoo.nodeId,
            isForward:     false,
            nodeId:        foo.nodeId
        });
 
        var inverseName = madeUpOfFoo.inverseName.text.toString();
        inverseName = opcua.utils.lowerFirstLetter(inverseName);
        // for inestance containedByEquipment
        node[inverseName] = foo;
    }
 
};