我有各种类型的 OPC UA 的 XML 文档和添加到其中的一些自定义对象类型。例如((1)具有年龄属性的 StudentType,出生地(2)具有 Name 属性的 TeacherType,专业知识))
此外,我还有另一个文本输入,上面写着 (XX -- FolderType, AA -- StudentType, BB --TeacherType) 简而言之,它提到了实例名称和实例的类型。
当然,这个完整的东西可以用 UAModeller 工具构建,但是我们计划编写一个 java 程序,它会在提供输入时自动为我们做这件事。
首先,我获取了 types.xml 文件,并对其进行了解析。我创建了一个 UAObjectTypes、UAVariableTypes、StudentTypes、TeacherTypes 等列表。
我给自己做了一个列表,将实例的输入列表映射到它们的 ObjectType。但我无法按预期建立参考。例如:
OPC UA 中的每个节点都通过引用连接到其他节点。例如:类是“OrganisedBy”对象。班级“组织”学生和教师。学生“组织”罗山等等。
我正在努力通过代码建立此地址空间的动态生成。我有该工具生成的示例地址。但是java程序不会产生相同的结果。
File xmlFile = new File("studteachobjmodel.xml"); //this is the types.xml
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(UANodeSet.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
UANodeSet xmlentries = (UANodeSet) jaxbUnmarshaller.unmarshal(xmlFile);
entries.add(xmlentries);
//This has to be well known in prior
UANodeSet newUANodeSetObject = new UANodeSet();
List<UAObjectType> objectTypeList = xmlentries.getUAObjectType();
HashMap<String, UAObjectType> mapWithExtractedObjectTypes = new HashMap<String, UAObjectType>();
for(String name: newUniqueList) {
for(UAObjectType o : objectTypeList) {
if(o.getDisplayName().equals(name))
mapWithExtractedObjectTypes.put(o.getDisplayName(), o); //mapping types with their objectTypes
}
}
System.out.println("----------MAPPING DONE---------------------");
List<UAObject> objectList = xmlentries.getUAObject();
List<UAObject> newObjectList = new ArrayList<UAObject>();
UAObject uaObject = null;
List<String> newNameSpaceUris = new ArrayList<String>();
newNameSpaceUris.add("http://yourorganisation.org/trial1/");
newUANodeSetObject.setNamespaceUris(newNameSpaceUris);
List<UAVariable> variableList = new ArrayList<UAVariable>();
for(Map.Entry mapElement:inputMap.entrySet()) {
String key = (String) mapElement.getKey();
String type = inputMap.get(key);
String value = mapWithExtractedObjectTypes.get(type).getNodeId();
UAObjectType tempType =mapWithExtractedObjectTypes.get(type);
// String value = opcuaInternalMap.get(type);
for(UAObjectType o : objectTypeList) {
if (o.getNodeId().equals(value))
{
// System.out.println(o.getReferences());
uaObject = new UAObject();
List<Reference> referencesForObject = new ArrayList<Reference>();
uaObject.setNodeId("ns=1;s="+key);//String.valueOf(min+random.nextInt(upperBound)));
uaObject.setBrowsename("1:"+key);
uaObject.setDisplayName(key);
List<Reference>tempRef=tempType.getReferences();
Reference ref_01= new Reference();
ref_01.setReferenceType("HasTypeDefinition");
ref_01.setReference(value);
//
////////////////////////NEED GUIDANCE HERE//////////////////////////////////
// Reference ref_02= new Reference();
// ref_02.setReferenceType("Organizes");
// ref_02.setReference("i=85");
// referencesForObject.add(ref_01);
// referencesForObject.add(ref_02);
tempRef.add(ref_01);
uaObject.setReferences(tempRef);
break;
}
}
if(uaObject!=null) {
newObjectList.add(uaObject);
}
newUANodeSetObject.setUAObject(newObjectList);
}
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(newUANodeSetObject, new File("Instances.xml"));
}catch (JAXBException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请给我一些指导。到现在完全一无所知。