Skip to content

Commit

Permalink
fix assigning of multiple dyn nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ttulka committed Dec 7, 2022
1 parent 84e59fa commit 7e025a1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add necessary dependencies:
<dependency>
<groupId>com.connectedcooking.opcua</groupId>
<artifactId>opcua-dynamic-node-manager</artifactId>
<version>0.1.2</version>
<version>0.1.3</version>
</dependency>
<dependency>
<groupId>com.prosysopc.ua</groupId>
Expand Down Expand Up @@ -329,6 +329,13 @@ There are basic micrometer.io metrics included:
## Release Notes
**0.1.3**
- Fix assigning of multiple dynamic nodes
**0.1.2**
- Map `UtcTime` for `ZonedDateTime` implicitly
- Add a new ctor to `RealNodeId`
**0.1.1**
- Update to Prosys 4.10.0-58
Expand Down
2 changes: 1 addition & 1 deletion examples/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>com.connectedcooking.opcua</groupId>
<artifactId>opcua-dynamic-node-manager</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.prosysopc.ua</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.connectedcooking.opcua</groupId>
<artifactId>opcua-dynamic-node-manager</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.1.3-SNAPSHOT</version>

<name>OPC UA Dynamic Node Manager</name>
<description>Java library for implementing a dynamic node manager that responses dynamically based on the user context.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DynNodeManager {

protected final List<DynNode> nodes = new LinkedList<>();

protected final Map<RealNodeId, AssignedChild> assignedChildren = new HashMap<>();
protected final Map<RealNodeId, Set<AssignedChild>> assignedChildren = new HashMap<>();
protected final Map<DynNodeId, DynRequest.Full<Boolean>> canBrowse = new HashMap<>();

/**
Expand Down Expand Up @@ -104,12 +104,15 @@ public void assign(RealNodeId parentId, DynNodeId childNodeId, RealNodeId childR
* See {@link #assign(RealNodeId, DynNodeId, BiFunction)}
*/
public void assignSet(RealNodeId parentId, DynNodeId nodeId, BiFunction<UserContext, DynNodeId, Collection<RealNodeId>> fn) {
assignedChildren.compute(parentId, (k, fns) -> {
if (fns == null) {
fns = new AssignedChild(nodeId);
assignedChildren.compute(parentId, (k, children) -> {
if (children == null) {
children = new HashSet<>();
}
fns.fns.add(fn);
return fns;
var child = new AssignedChild(nodeId);
child.fns.add(fn);

children.add(child);
return children;
});
}

Expand All @@ -125,8 +128,11 @@ public List<DynReference> assignedChildren(RealNodeId parentId, UserContext user
if (assignedChildren != null) {
var assigned = assignedChildren.get(parentId);
if (assigned != null) {
assigned.fns.forEach(fn -> fn.apply(userContext, assigned.childNodeId)
.forEach(rid -> references.add(new DynReference.HasComponentRef(parentId, rid))));
for (var child: assigned) {
child.fns.forEach(fn ->
fn.apply(userContext, child.childNodeId).forEach(rid ->
references.add(new DynReference.HasComponentRef(parentId, rid))));
}
}
}
return Collections.unmodifiableList(references);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public RealNodeId(String parent, String child) {
* @param child the child real node ID
*/
public RealNodeId(RealNodeId parent, RealNodeId child) {
this(parent.namespaceIndex, parent.identifier + "/" + child.identifier);
this(parent.namespaceIndex != null ? parent.namespaceIndex : -1, parent.identifier + "/" + child.identifier);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.connectedcooking.opcua.dynamicnodemanager.core;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
class DynNodeManagerTest {

@Test
void multipleAssignedChildrenSet() {
var parent = new RealNodeId("Parent");
var dynNode1 = new DynNodeId("DynNodeA_<No.>");
var dynNode2 = new DynNodeId("DynNodeB_<No.>");

var dnm = new DynNodeManager();

dnm.assignSet(parent, dynNode1, (ctx, nodeId) -> List.of(
new RealNodeId("DynNodeA_1")));

dnm.assignSet(parent, dynNode2, (ctx, nodeId) -> List.of(
new RealNodeId("DynNodeB_1"), new RealNodeId("DynNodeB_2")));

var references = dnm.assignedChildren(parent, null).stream()
.map(DynReference::targetId)
.map(RealNodeId::identifier)
.collect(Collectors.toList());

assertThat(references).containsExactlyInAnyOrder("DynNodeA_1", "DynNodeB_1", "DynNodeB_2");
}
}

0 comments on commit 7e025a1

Please sign in to comment.