digitalpetri / opc-ua-nodeset-parser

A parser for the OPC UA UANodeSet XML schema
Apache License 2.0
21 stars 10 forks source link

How to Generate Code? #2

Closed 0DennisLee0 closed 4 years ago

0DennisLee0 commented 4 years ago

Hello Kevin, I wanna figur out if this sdk can generate the code corresponding now? I want to generate the code of https://opcfoundation.org/UA/schemas/CNC/1.0/Opc.Ua.CNC.NodeSet.xml Can it work? Many Thanks!

kevinherron commented 4 years ago

This library is used by the code generator but the generator itself not currently open source yet.

I may be able to generate these Nodes for you, though, and if you’re willing to use the dev/0.4 branch of Milo they might be usable.

0DennisLee0 commented 4 years ago

This library is used by the code generator but the generator itself not currently open source yet.

I may be able to generate these Nodes for you, though, and if you’re willing to use the dev/0.4 branch of Milo they might be usable.

3Q for your reply. And I found the import in milo is import org.opcfoundation.**opcua**.binaryschema.TypeDictionary; However the sdk of opcfoundation is named ' import org.opcfoundation.ua ' And has no class named TypeDeictionary and so does the others,

kevinherron commented 4 years ago

Those classes are generated at build time. If you run mvn clean build from the root of your check out they will get generated.

0DennisLee0 commented 4 years ago

i will take some time to make it out, 3Q for your help~

0DennisLee0 commented 4 years ago

Hello Kevin, I have successfully import the NodeSet, and I have define some utils method, I wish it might be helpful for you.

@Slf4j
public class NodeUtils {

    public static void parseNodeSet(UaNodeSet nodeSet, UaNodeContext context, UaNodeManager nodeManager){
        Map<NodeId, NodeAttributes> nodes = nodeSet.getNodes();
        ListMultimap<NodeId, Reference> allReferences = nodeSet.getAllReferences();

        Map<NodeClass, List<NodeAttributes>> collect = nodes.values().stream()
                .collect(groupingBy(NodeAttributes::getNodeClass));

        for (NodeClass nodeClass : collect.keySet()) {
            if (nodeClass.equals(NodeClass.ObjectType)) {
                collect.get(NodeClass.ObjectType).stream()
                        .map(node -> ((ObjectTypeNodeAttributes) node))
                        .map(node -> node.getUaObjectTypeNode(context))
                        .forEach(nodeManager::addNode);
                log.info("ObjectType success");
            } else if (nodeClass.equals(NodeClass.VariableType)) {
                collect.get(NodeClass.VariableType).stream()
                        .map(node -> ((VariableTypeNodeAttributes) node))
                        .map(node -> node.getUaVariableTypeNode(context))
                        .forEach(nodeManager::addNode);
                log.info("VariableType success");
            } else if (nodeClass.equals(NodeClass.Object)) {
                collect.get(NodeClass.Object).stream()
                        .map(node -> ((ObjectNodeAttributes) node))
                        .map(node -> node.getObjectNode(context))
                        .forEach(nodeManager::addNode);
                log.info("Object success");
            } else if (nodeClass.equals(NodeClass.Variable)) {
                collect.get(NodeClass.Variable).stream()
                        .map(node -> ((VariableNodeAttributes) node))
                        .map(node -> node.getUaVariableNode(context))
                        .forEach(nodeManager::addNode);
                log.info("Variable success");
            } else if (nodeClass.equals(NodeClass.Method)) {
                collect.get(NodeClass.Method).stream()
                        .map(node -> ((MethodNodeAttributes) node))
                        .map(node -> node.getUaMethodNode(context))
                        .forEach(nodeManager::addNode);
                log.info("Method success");
            } else if (nodeClass.equals(NodeClass.DataType)) {
                collect.get(NodeClass.DataType).stream()
                        .map(node -> ((DataTypeNodeAttributes) node))
                        .map(node -> node.getDataTypeNode(context))
                        .forEach(nodeManager::addNode);
                log.info("DataType success");
            } else if (nodeClass.equals(NodeClass.View)) {
                collect.get(NodeClass.View).stream()
                        .map(node -> ((ViewNodeAttributes) node))
                        .map(node -> node.getUaViewNode(context))
                        .forEach(nodeManager::addNode);
                log.info("View success");
            } else if (nodeClass.equals(NodeClass.ReferenceType)) {
                collect.get(NodeClass.ReferenceType).stream()
                        .map(node -> ((ReferenceTypeNodeAttributes) node))
                        .map(node -> node.getReferenceTypeNode(context))
                        .forEach(nodeManager::addNode);
                log.info("ReferenceType success");
            }
        }

        allReferences.values()
                .forEach(nodeManager::addReference);
    }
}

and for each type of node, I have added some functions like this:

public class ObjectNodeAttributes extends NodeAttributes {

    /* Ignored parts */

    public UaObjectNode getObjectNode(UaNodeContext context){
        return new UaObjectNode(
                context,
                getNodeId(),
                getBrowseName(),
                getDisplayName(),
                getDescription(),
                getWriteMask(),
                getUserWriteMask(),
                getEventNotifier()
        );
    }
}
0DennisLee0 commented 4 years ago

And I found the ua-server-sdk 0.4.0-SNAPSHOT hasn't been published yet. I'm pretty looking forward to its coming out~^_^

kevinherron commented 4 years ago

@0DennisLee0 I posted some example code of how I'm using this library to build an AddressSpace from a UANodeSet in another issue: https://github.com/digitalpetri/opc-ua-nodeset-parser/issues/7#issuecomment-609451277

0DennisLee0 commented 4 years ago

Thank you for your kindly reply~