Skip to content

Step 7: Compile the ModelDesign file with UA-ModelCompiler

Last updated on September 1, 2020


Compile the ModelDesign file to UANodeSet and auxiliar files.

This Post is part of the OPC UA Information Model Tutorial.


Compile ModelDesign

Use my provided example ModelDesign file to test your compiler setup. Download this file:

https://github.com/Pro/opcua-modeling-tutorial/blob/master/FooFltModel.xml

Now, depending if you are on windows, you need to have your own compiled Version of the UA-Model Compiler (see this Post).
On Linux you can use the precompiled Docker Container to compile the ModelDesign file, as described in this Post.

The resulting files are also available on Github in the master-published branch:

https://github.com/Pro/opcua-modeling-tutorial/tree/master-published/Published/FooFlt

Model Compile Instructions for Windows

First compile the UA-Model compiler as described here.

Then use PowerShell (or any other console) to compile the model:

# Windows PowerShell
# Copyright (C) Microsoft Corporation. All rights reserved.
# 
# PS X:\>
cd C:\FLT_SW\UA-ModelCompiler\Bin\Release

# PS C:\FLT_SW\UA-ModelCompiler\Bin\Release>
.\Opc.Ua.ModelCompiler.exe `
-d2 C:\FLT_SW\Wiki\OpcUa\documents\FooFltModel.xml `
-cg C:\FLT_SW\Wiki\OpcUa\documents\FooFltModel\FooFltModel.csv `
-o2 C:\FLT_SW\Wiki\OpcUa\documents\FooFltModel `
-console

# PS C:\FLT_SW\UA-ModelCompiler\Bin\Release>
dir C:\FLT_SW\Wiki\OpcUa\documents\FooFltModel\

This will give you the following files:

    Directory: C:\FLT_SW\Wiki\OpcUa\documents\FooFltModel


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       12.03.2020     14:30          36368 FooFlt.Classes.cs
-a----       12.03.2020     14:30          24740 FooFlt.Constants.cs
-a----       12.03.2020     14:30           8651 FooFlt.DataTypes.cs
-a----       12.03.2020     14:30          65702 FooFlt.NodeSet.xml
-a----       12.03.2020     14:30          21616 FooFlt.NodeSet2.xml
-a----       12.03.2020     14:30           5009 FooFlt.PredefinedNodes.uanodes
-a----       12.03.2020     14:30          35953 FooFlt.PredefinedNodes.xml
-a----       12.03.2020     14:30            701 FooFlt.Types.bsd
-a----       12.03.2020     14:30           1117 FooFlt.Types.xsd

Model Compile Instructions for Linux

On Linux you can either use the precompiled Docker Container or compile the UA-ModelCompiler on your own. More details are described here.
The following instruction is for the simpler Docker Container usage:

cd opcua-modeling-tutorial
docker run \
	  --mount type=bind,source=$(pwd),target=/model/src \
	  --entrypoint "/app/PublishModel.sh" \
	  sailavid/ua-modelcompiler \
	   /model/src/FooFltModel FooFlt /model/src/Published

The same command is also used to automatically build the ModelDesign on Github. Therefore your resulting files should be identical to these:

https://github.com/Pro/opcua-modeling-tutorial/tree/master-published/Published/FooFlt

At this point, you have successfully compiled a ModelDesign to an UANodeSet.

Files such as Types.bsd and Nodes.csv are auxiliar files used by nodeset_compiler.py as indicated by https://open62541.org/doc/current/nodeset_compiler.html

Remember that in Step 2 we chose branch v1.04 of UA-ModelCompiler instead of master, because v1.04 works out of the box. AnalogItemType will not compile with v1.04, because v1.04 was released in March 2018 and UA-ModelCompiler received amendments and reworks in November and December 2019 to support AnalogItemType. Relevant commit: https://github.com/OPCFoundation/UA-Nodeset/commit/48549b80249f8468656660a21bdafb14d25ac4ad

To make branch master of UA-ModelCompiler work, see below.

Optional: Use UANodeSet editors to review the UANodeSet files derived from ModelDesign files.

You can use UAModeler to check consistency.

Note: UAModeler can not open UANodeSet files by default. You have to create a new “modeling only” project and include FooZebraCompression.xml

Recommended: Make UA-ModelCompiler master branch work to compile AnalogItemType

When trying to compile a ModelDesign with an AnalogItemTyp it will fail as follows:

PS C:\FLT_SW\UA-ModelCompiler\Bin\Release> .\Opc.Ua.ModelCompiler.exe `
>> -d2 C:\FLT_SW\Wiki\OpcUa\documents\FooFltModel.xml `
>> -cg C:\FLT_SW\Wiki\OpcUa\documents\FooFltModel\FooFltModel.csv `
>> -o2 C:\FLT_SW\Wiki\OpcUa\documents\FooFltModel `
>> -console
Could not load resource 'Opc.Ua.ModelCompiler.Design.BuiltInTypes.xml'.
   at Opc.Ua.Schema.SchemaValidator.LoadResource(Type type, String path, Assembly assembly) in C:\FLT_SW\UA-ModelCompiler\Core\Types\Schemas\SchemaValidator.cs:line 303
   at Opc.Ua.ModelCompiler.ModelCompilerValidator.LoadBuiltInModel() in C:\FLT_SW\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 800
   at Opc.Ua.ModelCompiler.ModelCompilerValidator.LoadBuiltInTypes() in C:\FLT_SW\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 101
   at Opc.Ua.ModelCompiler.ModelCompilerValidator.Validate2(IList`1 designFilePaths, String identifierFilePath, Boolean generateIds) in C:\FLT_SW\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 972
   at Opc.Ua.ModelCompiler.ModelGenerator2.ValidateAndUpdateIds(IList`1 designFilePaths, String identifierFilePath, UInt32 startId, String specificationVersion) in C:\FLT_SW\UA-ModelCompiler\ModelCompiler\ModelGenerator2.cs:line 96
   at Opc.Ua.ModelCompiler.Program.ProcessCommandLine2(List`1 tokens) in C:\FLT_SW\UA-ModelCompiler\ModelCompiler\Program.cs:line 424
PS C:\FLT_SW\UA-ModelCompiler\Bin\Release>

A workaround is suggested in https://github.com/OPCFoundation/UA-ModelCompiler/issues/46 with the following diff:

johndoe@winpc MINGW64 /c/FLT_SW/test/UA-ModelCompiler (master)
$ git diff
diff --git a/ModelCompiler/ModelDesignerValidator.cs b/ModelCompiler/ModelDesignerValidator.cs
index 5d5da75..7da31be 100644
--- a/ModelCompiler/ModelDesignerValidator.cs
+++ b/ModelCompiler/ModelDesignerValidator.cs
@@ -52,7 +52,7 @@ namespace Opc.Ua.ModelCompiler
         {
             m_context = ServiceMessageContext.GlobalContext;
             m_startId = startId;
-            EmbeddedResourcePath = "Opc.Ua.ModelCompiler.Design";
+            EmbeddedResourcePath = "Opc.Ua.ModelCompiler.Design.v104";
         }
         #endregion

After introducing that change, clean and rebuild the UA-ModelCompiler solution with Visual Studio. Opc.Ua.ModelCompiler.exe will now succeed to compile ModelDesign files that use AnalogItemType.

Published inInformation Modeling

6 Comments

  1. Fridolin Fridolin

    Hey there, I get an error when starting the compiler, following your instructions for windows, and using exactly your files: “The TypeDefinition reference for node ApeType_MyVariable3 is not valid: AnalogUnitRangeType.
    at Opc.Ua.ModelCompiler.ModelCompilerValidator.FindNode(XmlQualifiedName sym
    bolicId, Type requiredType, String sourceName, String referenceName) in C:\Users\XKh\Desktop\Fridolin\OPCUA\OPCUA.rocks\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:Zeile 3521. […]”

    Does this have to do with using branch v1.04 and not master?

    • Fridolin Fridolin

      …Is the branch master already used, before entering those two lines you suggest in step 2?:
      “git branch -a
      git checkout v1.04”,
      or is there anything else to do, to use it?

      • Benedict Simlinger Benedict Simlinger

        Hi Fridolin,

        your comment prompted me to revisit
        https://github.com/OPCFoundation/UA-ModelCompiler

        Please note:

        1) The maintainer of UA-ModelCompiler claims that the issue, that made me change to branch ‘v1.04’ initially, is fixed by now.
        https://github.com/OPCFoundation/UA-ModelCompiler/issues/46#issuecomment-615938876
        Consequently, you should be able to work with ‘master’, without the workaround from
        https://github.com/OPCFoundation/UA-ModelCompiler/issues/46#issuecomment-603875272
        which I used in this post.

        2) The UA-Nodeset submodule was removed
        https://github.com/OPCFoundation/UA-ModelCompiler/commit/408bc5f60b0afffd3291f841babe04242cffdb97
        from UA-ModelCompiler and (instead?) the NETStandard submodule was added
        https://github.com/OPCFoundation/UA-ModelCompiler/commit/00c8d5465f4356db7ea6199e933acccf5187c8dd

        Considering those two points, I don’t recommend following my documentation to the letter anymore, since the ‘master’ branch of UA-ModelCompiler has changed in a significant way by now.

        I don’t know when I will find the time to update the documentation accordingly. Until then, you have to rely on your own troubleshooting skills.

        • Benedict Simlinger Benedict Simlinger

          But long story short, you should try with ‘master’. It’s impossible to make it work with branch ‘v1.04’ for reasons mentioned in exactly this post.

        • Fridolin Fridolin

          Hi Benedict,
          thanks a lot for your quick answer and the whole tutorial, which is very helpful.
          So I will test my own troubleshooting skills 🙂 Should I succeed I will let you know ^^
          stay healthy!

          • Benedict Simlinger Benedict Simlinger

            Hi Fridolin,

            your question made me setup the toolchain again and have a go at it myself.

            My target is, to compile FooFltModel.xml from
            https://github.com/Pro/opcua-modeling-tutorial/tree/master
            as instructed in this post.

            Here is what i did (copy the comment to a text editor for better readability):

            [TLDR at the end]

            ==========

            Checkout UA-ModelCompiler, current ‘master’.
            At the time of writing that is commit 3754f9bb141b33d0f2a32cad984795295a060782

            ==========

            I DO NOT apply the fix as recommended in “Recommended: Make UA-ModelCompiler master branch work to compile AnalogItemType” this post,
            because the maintainer claims that this is fixed
            https://github.com/OPCFoundation/UA-ModelCompiler/issues/46#issuecomment-615938876
            (Spoiler: it seems not)

            Build UA-ModelCompiler in Release configuration as instructed in this guide.
            That worked and resulted in C:\UA-ModelCompiler\Bin\Release\Opc.Ua.ModelCompiler.exe

            ==========

            !!! ATTENTION: This is new !!!

            Following the advice from
            https://github.com/OPCFoundation/UA-ModelCompiler/issues/59#issuecomment-695170371
            I run
            git cerry-pick 19c669ec9c63ef05e7b12483ff97a90517a7a63d
            to successfully run

            PS C:\UA-ModelCompiler> .\BuildStandardTypes.bat

            and

            PS C:\UA-ModelCompiler> .\BuildEngineeringUnits.bat

            Without that cherry-pick, execution of .\BuildStandardTypes.bat will throw an error.

            ==========

            Let us be naive/positive and try to build:

            PS C:\UA-ModelCompiler\Bin\Release> .\Opc.Ua.ModelCompiler.exe -d2 C:\opcua-modeling-tutorial\FooFltModel.xml -cg C:\opcua-modeling-tutorial\FooFltModel.csv -o2 C:\opcua-modeling-tutorial\FooFltModel -console
            Could not load resource ‘ModelCompiler.Design.BuiltInTypes.xml’.
            at Opc.Ua.Schema.SchemaValidator.LoadResource(Type type, String path, Assembly assembly) in C:\UA-ModelCompiler\Stack\Stack\Opc.Ua.Core\Types\Schemas\SchemaValidator.cs:line 276
            at ModelCompiler.ModelCompilerValidator.LoadBuiltInModel() in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 802
            at ModelCompiler.ModelCompilerValidator.LoadBuiltInTypes() in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 103
            at ModelCompiler.ModelCompilerValidator.Validate2(IList`1 designFilePaths, String identifierFilePath, Boolean generateIds) in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 974
            at ModelCompiler.ModelGenerator2.ValidateAndUpdateIds(IList`1 designFilePaths, String identifierFilePath, UInt32 startId, String specificationVersion) in C:\UA-ModelCompiler\ModelCompiler\ModelGenerator2.cs:line 95
            at ModelCompiler.Program.ProcessCommandLine2(List`1 tokens) in C:\UA-ModelCompiler\ModelCompiler\Program.cs:line 424

            This error is known and discussed in this post at “Recommended: Make UA-ModelCompiler master branch work to compile AnalogItemType”

            BUT: Instead of applying the fix, I discoverd the new “-version” flag of Opc.Ua.ModelCompiler.exe (that flag did not exist when i wrote this guide)!
            Lets try those first.

            ==========

            using -version v104 fails

            PS C:\UA-ModelCompiler\Bin\Release> .\Opc.Ua.ModelCompiler.exe -d2 C:\opcua-modeling-tutorial\FooFltModel.xml -cg C:\opcua-modeling-tutorial\FooFltModel.csv -o2 C:\opcua-modeling-tutorial\FooFltModel -console -version v104
            Could not find a part of the path ‘C:\opcua-modeling-tutorial\FooFltModel\FooFlt.Constants.cs’.
            at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
            at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
            at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
            at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
            at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
            at System.IO.StreamWriter..ctor(String path, Boolean append)
            at ModelCompiler.ModelGenerator2.WriteTemplate_ConstantsSingleFile(String filePath, List`1 nodes) in C:\UA-ModelCompiler\ModelCompiler\ModelGenerator2.cs:line 1871
            at ModelCompiler.ModelGenerator2.GenerateMultipleFiles(String filePath, Boolean useXmlInitializers, String[] excludedCategories, Boolean includeDisplayNames) in C:\UA-ModelCompiler\ModelCompiler\ModelGenerator2.cs:line 129
            at ModelCompiler.Program.ProcessCommandLine2(List`1 tokens) in C:\UA-ModelCompiler\ModelCompiler\Program.cs:line 449

            ==========

            using -version v103 (for the sake of completeness) fails

            PS C:\UA-ModelCompiler\Bin\Release> .\Opc.Ua.ModelCompiler.exe -d2 C:\opcua-modeling-tutorial\FooFltModel.xml -cg C:\opcua-modeling-tutorial\FooFltModel.csv -o2 C:\opcua-modeling-tutorial\FooFltModel -console -version v103
            The TypeDefinition reference for node ApeType_MyVariable3 is not valid: AnalogUnitRangeType.
            at ModelCompiler.ModelCompilerValidator.FindNode(XmlQualifiedName symbolicId, Type requiredType, String sourceName, String referenceName) in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 3639
            at ModelCompiler.ModelCompilerValidator.ValidateInstance(InstanceDesign instance) in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 3480
            at ModelCompiler.ModelCompilerValidator.Validate(NodeDesign node) in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 3218
            at ModelCompiler.ModelCompilerValidator.Validate(NodeDesign node) in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 3222
            at ModelCompiler.ModelCompilerValidator.ValidateDictionary(ModelDesign dictionary) in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 857
            at ModelCompiler.ModelCompilerValidator.Validate2(IList`1 designFilePaths, String identifierFilePath, Boolean generateIds) in C:\UA-ModelCompiler\ModelCompiler\ModelDesignerValidator.cs:line 1110
            at ModelCompiler.ModelGenerator2.ValidateAndUpdateIds(IList`1 designFilePaths, String identifierFilePath, UInt32 startId, String specificationVersion) in C:\UA-ModelCompiler\ModelCompiler\ModelGenerator2.cs:line 95
            at ModelCompiler.Program.ProcessCommandLine2(List`1 tokens) in C:\UA-ModelCompiler\ModelCompiler\Program.cs:line 424

            Oh! That is the same error message as yours! i recommend you investigate the versions / commit IDs you are using.

            ==========

            I decide to apply the fix
            EmbeddedResourcePath = “Opc.Ua.ModelCompiler.Design.v104”;
            recompile Opc.Ua.ModelCompiler.exe and try again (without version flag)

            PS C:\UA-ModelCompiler\Bin\Release> .\Opc.Ua.ModelCompiler.exe -d2 C:\opcua-modeling-tutorial\FooFltModel.xml -cg C:\opcua-modeling-tutorial\FooFltModel.csv -o2 C:\opcua-modeling-tutorial\FooFltModel -console
            Could not find a part of the path ‘C:\opcua-modeling-tutorial\FooFltModel\FooFlt.Constants.cs’.
            at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
            at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
            at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
            at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
            at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
            at System.IO.StreamWriter..ctor(String path, Boolean append)
            at ModelCompiler.ModelGenerator2.WriteTemplate_ConstantsSingleFile(String filePath, List`1 nodes) in C:\UA-ModelCompiler\ModelCompiler\ModelGenerator2.cs:line 1871
            at ModelCompiler.ModelGenerator2.GenerateMultipleFiles(String filePath, Boolean useXmlInitializers, String[] excludedCategories, Boolean includeDisplayNames) in C:\UA-ModelCompiler\ModelCompiler\ModelGenerator2.cs:line 129
            at ModelCompiler.Program.ProcessCommandLine2(List`1 tokens) in C:\UA-ModelCompiler\ModelCompiler\Program.cs:line 449

            So it seems the fix has the same effect as using -version v104

            ==========

            I undo the fix (in order to not taint any future tests) and pause my investigation.

            ==========

            TLDR:
            * Your error messsage looks like you are using v103 somewhere. Consolidate your versions before proceeding.
            * UA-ModelCompiler ‘master’ changed in a way that broke the functioning workflow that was documented here 🙁

            Hopefully you gain any insight from my tests that help you find a solution.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.