public string ToXml(bool bAddTypeDefintions) { XmlDocument doc = DamlContainer.BuildDamlDocumentTemplate(true); XmlNode root = doc.DocumentElement; // Add DAML Process body // Add this process' details name etc., if this process is complex // there is additional data we must add // Given a document (by ref) and an array of RDF properties // we repeat the same basic steps // Add Inputs (if any) if (this.m_arrInputs.Count > 0) { AddRdfProperties(ref doc, (RdfProperty[])this.m_arrInputs.ToArray(typeof(RdfProperty)), enuIOPEType.Input); } // Add Outputs (if any) if (this.m_arrOutputs.Count > 0) { AddRdfProperties(ref doc, (RdfProperty[])this.m_arrOutputs.ToArray(typeof(RdfProperty)), enuIOPEType.Output); } // Add Preconditions (if any) if (this.m_arrPreconditions.Count > 0) { AddRdfProperties(ref doc, (RdfProperty[])this.m_arrPreconditions.ToArray(typeof(RdfProperty)), enuIOPEType.Precondition); } // Add Effects (if any) if (this.m_arrEffects.Count > 0) { AddRdfProperties(ref doc, (RdfProperty[])this.m_arrEffects.ToArray(typeof(RdfProperty)), enuIOPEType.Effect); } // Add CoConditions (if any) if (this.m_arrCoConditions.Count > 0) { AddRdfProperties(ref doc, (RdfProperty[])this.m_arrCoConditions.ToArray(typeof(RdfProperty)), enuIOPEType.CoCondition); } // Add ConditionalOutputs (if any) if (this.m_arrConditionalOutputs.Count > 0) { AddRdfProperties(ref doc, (RdfProperty[])this.m_arrConditionalOutputs.ToArray(typeof(RdfProperty)), enuIOPEType.ConditionalOutput); } // Add CoOutputs (if any) if (this.m_arrCoOutputs.Count > 0) { AddRdfProperties(ref doc, (RdfProperty[])this.m_arrCoOutputs.ToArray(typeof(RdfProperty)), enuIOPEType.CoOutput); } // Add Parameters (if any) if (this.m_arrParameters.Count > 0) { AddRdfProperties(ref doc, (RdfProperty[])this.m_arrParameters.ToArray(typeof(RdfProperty)), enuIOPEType.Parameter); } // Create process node XmlNode processNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_CLASS, DamlConstants.DAML_NS_URI); // Create process attribute XmlAttribute processAtt = doc.CreateAttribute(DamlConstants.RDF_ID, DamlConstants.RDF_NS_URI); // Set attribute value (process name) processAtt.Value = this.m_strName; // Add attribute to node processNode.Attributes.Append(processAtt); // Specify what type of process this is - this will be a child of processNode // Create process type node (rdfs:subClassOf) XmlNode processTypeNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.RDFS_SUBCLASSOF, DamlConstants.RDFS_NS_URI); // Create process type node attribute XmlAttribute processTypeAtt = doc.CreateAttribute(DamlConstants.RDF_RESOURCE, DamlConstants.RDF_NS_URI); // Set the type of process switch (this.ProcessType) { case enuProcessType.AtomicProcess: processTypeAtt.Value = DamlConstants.ATOMIC_PROCESS_URI; break; case enuProcessType.SimpleProcess: processTypeAtt.Value = DamlConstants.SIMPLE_PROCESS_URI; break; case enuProcessType.CompositeProcess: processTypeAtt.Value = DamlConstants.COMPOSITE_PROCESS_URI; break; default: throw new ArgumentException("Unknown process type"); } // Add attribute to process type node processTypeNode.Attributes.Append(processTypeAtt); // Add the processType node as a child of the process node processNode.AppendChild(processTypeNode); // Add restrictions to process node if any DamlRestriction[] arrRestrictions = this.GetRestrictions(enuIOPEType.Input); for (int j = 0; j < arrRestrictions.Length; j++) { if (arrRestrictions[j].Owner == this.Name) { // Create subClassOf node XmlNode subClassOfNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.RDFS_SUBCLASSOF, DamlConstants.RDFS_NS_URI); // Create a node for each restriction (child of subClassOfNode) XmlNode restrictionNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_RESTRICTION, DamlConstants.DAML_NS_URI); // Fill in restrictionNode data // Add cardinality attribute if value has been set if (arrRestrictions[j].Cardinality != DamlRestriction.NO_CARDINALITY) { // Create attribute XmlAttribute cardinalityAtt = doc.CreateAttribute(DamlConstants.DAML_CARDINALITY, DamlConstants.DAML_NS_URI); // Set attribute value cardinalityAtt.Value = arrRestrictions[j].Cardinality.ToString(); // Add attribute to node restrictionNode.Attributes.Append(cardinalityAtt); } // Create onPropertyNode XmlNode onPropertyNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_ON_PROPERTY, DamlConstants.DAML_NS_URI); // Create onProperty attribute XmlAttribute onPropertyAtt = doc.CreateAttribute(DamlConstants.RDF_RESOURCE, DamlConstants.RDF_NS_URI); // Set attribute value onPropertyAtt.Value = arrRestrictions[j].OnProperty; // Add attribute to node onPropertyNode.Attributes.Append(onPropertyAtt); // Add onPropertyNode to restrictionNode restrictionNode.AppendChild(onPropertyNode); // If this instance is a composite process add extra nodes // to the (composedOf) restriction node if (this.ProcessType == enuProcessType.CompositeProcess && arrRestrictions[j].OnProperty == DamlConstants.PROCESS_COMPOSED_OF_URI) { XmlNode toClassNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_TO_CLASS, DamlConstants.DAML_NS_URI); XmlNode classNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_CLASS, DamlConstants.DAML_NS_URI); // Create intersection of node XmlNode intersectionOfNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_INTERSECTION_OF, DamlConstants.DAML_NS_URI); // Create intersectionOf node attribute XmlAttribute intersectionOfAtt = doc.CreateAttribute(DamlConstants.RDF_PARSE_TYPE, DamlConstants.RDF_NS_URI); // Set attribute value intersectionOfAtt.Value = DamlConstants.DAML_COLLECTION; // Add attribute to intersecionOfNode intersectionOfNode.Attributes.Append(intersectionOfAtt); // Add a Daml class node and another restriction node // to the intersectionOfNode // Create a node to store the type of sub task "list" we have // one of enuProcessSubTaskType XmlNode subTaskTypeNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_CLASS, DamlConstants.DAML_NS_URI); // Create an attribute for the subTaskType node XmlAttribute subTaskTypeAtt = doc.CreateAttribute(DamlConstants.RDF_ABOUT, DamlConstants.RDF_NS_URI); // Set the atribute value switch (this.SubTaskType) { case enuProcessSubTaskType.Choice: subTaskTypeAtt.Value = DamlConstants.PROCESS_CHOICE; break; case enuProcessSubTaskType.Sequence: subTaskTypeAtt.Value = DamlConstants.PROCESS_SEQUENCE; break; default: throw new Exception("Unknown process sub-task type"); } // Add subTaskTypeAtt attribute to subTaskType node subTaskTypeNode.Attributes.Append(subTaskTypeAtt); // Add a restriction to the intersectionOf node // this is where we list the names of the subprocesses XmlNode subProcessRestrictionNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_RESTRICTION, DamlConstants.DAML_NS_URI); XmlNode subProcessRestrictionOnPropertyNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_ON_PROPERTY, DamlConstants.DAML_NS_URI); // Add attribute XmlAttribute subProcessRestrictionOnPropertyAtt = doc.CreateAttribute(DamlConstants.RDF_RESOURCE, DamlConstants.RDF_NS_URI); // Set attribute value subProcessRestrictionOnPropertyAtt.Value = DamlConstants.PROCESS_COMPONENTS_URI; // Add attribute to node subProcessRestrictionOnPropertyNode.Attributes.Append(subProcessRestrictionOnPropertyAtt); // last daml:toClass/daml:Class construct added to the subProcessRestrictionNode XmlNode processListToClassNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_TO_CLASS, DamlConstants.DAML_NS_URI); XmlNode processListClassNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_CLASS, DamlConstants.DAML_NS_URI); XmlNode processListOfInstancesNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_LIST_OF_INSTANCES_OF, DamlConstants.DAML_NS_URI); // Add attribute XmlAttribute processListOfInstancesAtt = doc.CreateAttribute(DamlConstants.RDF_PARSE_TYPE, DamlConstants.RDF_NS_URI); // Set attribute value processListOfInstancesAtt.Value = DamlConstants.DAML_COLLECTION; // Add attribute to node processListOfInstancesNode.Attributes.Append(processListOfInstancesAtt); for (int i = 0; i < this.m_arrSubProcesses.Count; i++) { // Create process name node XmlNode processNameNode = doc.CreateNode(XmlNodeType.Element, DamlConstants.DAML_CLASS, DamlConstants.DAML_NS_URI); // Create process name attribute XmlAttribute processNameAtt = doc.CreateAttribute(DamlConstants.RDF_ABOUT, DamlConstants.RDF_NS_URI); // Set processNameAtt value processNameAtt.Value = "#" + ((DamlProcess)this.m_arrSubProcesses[i]).Name; // Add attribute to node processNameNode.Attributes.Append(processNameAtt); // Add to list of instances node processListOfInstancesNode.AppendChild(processNameNode); } processListClassNode.AppendChild(processListOfInstancesNode); processListToClassNode.AppendChild(processListClassNode); subProcessRestrictionNode.AppendChild(subProcessRestrictionOnPropertyNode); subProcessRestrictionNode.AppendChild(processListToClassNode); intersectionOfNode.AppendChild(subTaskTypeNode); intersectionOfNode.AppendChild(subProcessRestrictionNode); classNode.AppendChild(intersectionOfNode); toClassNode.AppendChild(classNode); restrictionNode.AppendChild(toClassNode); } // Add restrictionNode to subClassOfNode subClassOfNode.AppendChild(restrictionNode); //Add subClassOfNode to root processNode.AppendChild(subClassOfNode); } } // Add process node to root root.AppendChild(processNode); if (bAddTypeDefintions) { // Add data type definitions IDictionaryEnumerator it = this.m_dataTypeDefinitionMap.GetEnumerator(); while (it.MoveNext()) { // Ask each type definition to ToXml() itself // this gives us the nodes to add to our document DamlTypeDefinition definition = (DamlTypeDefinition)it.Value; XmlDocument typeDoc = new XmlDocument(); // Load the xml of the type definition typeDoc.LoadXml(definition.ToXml()); // Get the document element XmlNode typeDocRoot = typeDoc.DocumentElement; // Import the first child of the root into the damlProcess xml document // being created XmlNode temp = doc.ImportNode(typeDocRoot.FirstChild, true); // Append that node to our current document root root.AppendChild(temp); } } return(root.OuterXml); }
public string ToXml() { // Get a document template XmlDocument doc = DamlContainer.BuildDamlDocumentTemplate(true); // Get the document element (document root) XmlNode root = doc.DocumentElement; // DamlProcessWriter may have to control the datatypes // being written out by each DamlProcess, multiple processes in // a proces model may share data types, we only need one // data type definition IDictionaryEnumerator it = this.m_processMap.GetEnumerator(); while (it.MoveNext()) { DamlProcess proc = (DamlProcess)it.Value; XmlDocument processDoc = new XmlDocument(); // DO NOT add type defintions to xml document we create // the ProcessModel should keep track of all the data types // in each process to ensure each type definition gets written // only once since multiple process in a process model may share // data types an as such could cause duplicate definitions // to be written out in the process model xml document processDoc.LoadXml(proc.ToXml(false)); XmlNode processDocRoot = processDoc.DocumentElement; XmlNodeList lstChildren = processDocRoot.ChildNodes; foreach (XmlNode child in lstChildren) { // Add every child to our document root except the ontology imports // since we already have these from the DamlDocumentTemplate if (child.Name != DamlConstants.DAML_ONTOLOGY) { XmlNode temp = doc.ImportNode(child, true); root.AppendChild(temp); } } // Get the data types we have not seen as yet DamlTypeDefinition[] arrDefinitions = proc.TypeDefinitions; for (int i = 0; i < arrDefinitions.Length; i++) { if (!this.m_dataTypeDefinitionMap.ContainsKey(arrDefinitions[i].Name)) { this.m_dataTypeDefinitionMap.Add(arrDefinitions[i].Name, arrDefinitions[i]); } } } // Write data type map last IDictionaryEnumerator typeIt = this.m_dataTypeDefinitionMap.GetEnumerator(); while (typeIt.MoveNext()) { DamlTypeDefinition definition = (DamlTypeDefinition)typeIt.Value; XmlDocument typeDoc = new XmlDocument(); // Load the xml of the type definition typeDoc.LoadXml(definition.ToXml()); // Get the document element XmlNode typeDocRoot = typeDoc.DocumentElement; // Import the first child of the root into the damlProcess xml document // being created XmlNode temp = doc.ImportNode(typeDocRoot.FirstChild, true); // Append that node to our current document root root.AppendChild(temp); } return(doc.OuterXml); }