« Back to all blog posts

Information modeling features in the upcoming UA SDK

03.04.2014


Update 20.5.2014: Edited things that have changed recently.

Update 10.7.2014: Edited code-examples to reflect 2.0 release version

The release of the 2.0 version of our Java SDK is just around the corner. In the new version, we are introducing new features that should help you to use information models in your UA applications. In this blog post, I will tell you about how to

  • generate Java classes from your UA models
  • create instances of UA types in your server applications
  • read instances of UA types in your client applications

Code generation

The new SDK has an option to include a code generation tool which basically converts your information models from NodeSet2.xml-format to Java classes. You can create NodeSet2.xml files with UaModeler design tool.

The generator has its own codegen-folder:

codegen\
  lib\
    *.NodeSet2.xml     --  dependent NodeSet2.xml-files
    *.package          --  Java packages of the dependent models
    *.package.clj      --  Used when model has multiple packages
  templates\           --  Mustache templates used in code generation
    common\            --  templates common to both server and client
      *.java.mustache  --  template for each generated class
      partials\
        *.mustache     --  partials included in the templates
    client\            --  client only templates
    server\            --  server only templates
  build.xml            --  Apache Ant task for code generation
  codegen.properties   --  configuration used by build.xml
  codegen-*.jar        --  code generator executable
  Readme.md            --  generic notes and instructions
  ReadmeSample.md      --  an example on how to use the generator  
  set-package.bat      --  script for defining packages (Windows)
  set-package.sh       --  script for defining packages (Linux/Mac)

To initially setup your code generation infrastructure, you have to edit the codegen.properties file:

add path to your source NodeSet2.xml file add Java package for your model or use the set-package scripts select templates you want to generate code from add paths where code should be generated to And that’s it! Now you can just run the Ant build tool and source code will appear in the output folder. By executing the Ant task in your build script, you do not have to worry about code generation afterwards; code is generated each time the source NodeSet2.xml file changes!

Currently, code generation supports the following features:

  • Object types and variable types
    • Getters and setters for child objects and variables
    • Getters and setters for child variable values
  • Method calls and implementations
  • Custom structures and enumerations

Create instances on UA server

To integrate the generated code to your UA server application, follow these steps:

// 1. Register classes on your UaServer object.
// The Information model class is generated for each information model.
server.registerModel(InformationModel.MODEL);

// 2. Load type nodes from the SampleTypes.xml NodeSet2.xml file.
server.getAddressSpace().loadModel(
	this.getClass().getResource("SampleTypes.xml").toURI());

// 3. Now you can create an instance of a ValveType
// with a NodeManagerUaNode:
ValveType valve = manager.createInstance(ValveType.class, "Valve1");

// 4. Use the instance.
// e.g., set the value of Flow variable.
UaNode objNode = server.getAddressSpace().getNode(
	Identifiers.ObjectsFolder);
objNode.addComponent(valve);
double i = 0;
while (true) {
	i = i + 1;
	valve.setFlowValue(i);
	Thread.sleep(1000);
}

Read instances on UA client

Reading instances with UA client is now really simple:

// 1. Register classes on your UaClient object.
client.registerModel(InformationModel.MODEL);

// 2. Get a node from server with an AddressSpace object.
// Give the nodeId of the instance as a parameter.
ValveType valve =
(ValveType) addressSpace.getNode(nodeId);

// 3. Read value of the Flow variable.
valve.getFlowValue();

Additional reading

In the codegen folder, there are Readme.md and ReadmeSample.md files that contain up to date instructions for use.

Java source code generation from OPC UA information models

Eero Laukkanen

Eero Laukkanen

Software Engineer

Expertise and responsibility areas: OPC & OPC UA product development and project work

Tags: Information Models, Java, OPC UA, Development

comments powered by Disqus

About Prosys

Prosys is a leading provider of professional OPC software and services with over 10 years of experience in the field. OPC and OPC UA (Unified Architecture) are communications standards used especially by industrial and high-tech companies.

Read more about us »

Newest blog posts

OPC UA Getting Real and Real-time - Report from OPC Day Finland 2018

OPC UA is getting popular in real-world installations and also going towards real-time with the latest announcements about support for field level.

Getting Started with OPC UA (Videos)

Videos explain you ho to get started with OPC UA end-user products or with development. Watch Jouni Aro, the CTO of Prosys OPC, giving the presentations for the OPC Foundation online.

OPC UA Buffer Overflow Exploit

Security vulnerability

View all blog posts »