public NodeId CreateObject(
NodeId parentId,
NodeId referenceTypeId,
NodeId nodeId,
QualifiedName browseName,
ObjectAttributes attributes,
ExpandedNodeId typeDefinitionId)
{
try
{
m_lock.Enter();
// validate browse name.
if (QualifiedName.IsNull(browseName))
{
throw ServiceResultException.Create(StatusCodes.BadBrowseNameInvalid, "BrowsName must not be empty.");
}
// check for null node id.
if (NodeId.IsNull(nodeId))
{
nodeId = CreateUniqueNodeId();
}
// check if node id exists.
if (m_nodes.Exists(nodeId))
{
throw ServiceResultException.Create(StatusCodes.BadNodeIdExists, "NodeId '{0}' already exists.", nodeId);
}
// find parent.
ILocalNode parent = null;
if (!NodeId.IsNull(parentId))
{
parent = GetManagerHandle(parentId) as ILocalNode;
if (parent == null)
{
throw ServiceResultException.Create(StatusCodes.BadParentNodeIdInvalid, "Parent node '{0}' does not exist.", parentId);
}
// validate reference.
ValidateReference(parent, referenceTypeId, false, NodeClass.Object);
}
// find type definition.
if (NodeId.IsNull(typeDefinitionId))
{
typeDefinitionId = ObjectTypes.BaseObjectType;
}
IObjectType objectType = GetManagerHandle(typeDefinitionId) as IObjectType;
if (objectType == null)
{
throw ServiceResultException.Create(StatusCodes.BadTypeDefinitionInvalid, "Type definition '{0}' does not exist or is not an ObjectType.", typeDefinitionId);
}
// verify instance declarations.
ILocalNode instanceDeclaration = FindInstanceDeclaration(parent, browseName);
if (instanceDeclaration != null)
{
if (instanceDeclaration.NodeClass != NodeClass.Object)
{
throw ServiceResultException.Create(
StatusCodes.BadNodeClassInvalid,
"The type model requires that node with a browse name of {0} have a NodeClass of {1}.",
browseName,
instanceDeclaration.NodeClass);
}
if (!m_server.TypeTree.IsTypeOf(typeDefinitionId, instanceDeclaration.TypeDefinitionId))
{
throw ServiceResultException.Create(
StatusCodes.BadNodeClassInvalid,
"The type model requires that node have a type definition of {0}.",
instanceDeclaration.TypeDefinitionId);
}
}
// get the variable.
IObject objectd = instanceDeclaration as IObject;
// create node.
ObjectNode node = new ObjectNode();
// set defaults from type definition.
node.NodeId = nodeId;
node.NodeClass = NodeClass.Object;
node.BrowseName = browseName;
node.DisplayName = browseName.Name;
node.Description = null;
node.WriteMask = 0;
node.UserWriteMask = 0;
node.EventNotifier = EventNotifiers.None;
// set defaults from instance declaration.
if (objectd != null)
{
node.DisplayName = objectd.DisplayName;
node.Description = objectd.Description;
node.WriteMask = (uint)objectd.WriteMask;
node.UserWriteMask = (uint)objectd.UserWriteMask;
node.EventNotifier = objectd.EventNotifier;
}
// update with attributes provided.
UpdateAttributes(node, attributes);
// EventNotifier
if (attributes != null && (attributes.SpecifiedAttributes & (uint)NodeAttributesMask.EventNotifier) != 0)
{
node.EventNotifier = attributes.EventNotifier;
}
// add references with parent.
if (parent != null)
{
AddReference(parent, referenceTypeId, false, node, true);
}
// add type definition.
AddReference(node, ReferenceTypeIds.HasTypeDefinition, false, objectType, false);
// add to address space.
AddNode(node);
// apply modelling rules.
NodeFactory factory = new NodeFactory(m_nodes);
IList<ILocalNode> nodesToAdd = factory.ApplyModellingRules(node, objectType.NodeId, ref m_lastId, 1);
// add the nodes.
foreach (Node nodeToAdd in nodesToAdd)
{
AddNode(nodeToAdd);
}
// find the top level parent that must be used to apply the modelling rules.
if (instanceDeclaration != null)
{
ILocalNode toplevelParent = FindTopLevelModelParent(parent);
// add modelling rule.
AddReference(node, ReferenceTypeIds.HasModelParent, false, parent, true);
// update the hierarchy.
nodesToAdd = factory.ApplyModellingRules(toplevelParent, (NodeId)toplevelParent.TypeDefinitionId, ref m_lastId, 1);
// add the nodes.
foreach (Node nodeToAdd in nodesToAdd)
{
AddNode(nodeToAdd);
}
}
// return the new node id.
return node.NodeId;
}
finally
{
m_lock.Exit();
}
}