Example #1
0
        private DamlTypeDefinition FindTypeDefintionInRdfProperty(string strTypeName)
        {
            DamlTypeDefinition definition = null;

            try
            {
                // A type defition could be in an rdf:Property or a damlClass
                XmlNode root = this.m_doc.DocumentElement;

                // Search for a matching rdf:Property
                string strXPath = DamlConstants.RDF_PROPERTY + "[@" + DamlConstants.RDF_ID + "='" + strTypeName.TrimStart(new char[] { '#' }) + "'" + "]";

                XmlNode typeDefinitionNode = root.SelectSingleNode(strXPath, m_mgr);

                if (typeDefinitionNode == null)
                {
                    return(null);
                }

                definition = this.GetNodeData(typeDefinitionNode);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            return(definition);
        }
        public void AddDataTypeDefinition(DamlTypeDefinition definition)
        {
            if (definition == null || definition.Name.Length == 0)
            {
                throw new ArgumentException("Invalid DamlTypeDefintion passed, definition either null or has empty name", "definition");
            }

            if (!this.m_dataTypeDefinitionMap.Contains(definition.Name))
            {
                this.m_dataTypeDefinitionMap.Add(definition.Name, definition);
            }
        }
Example #3
0
        private DamlTypeDefinition[] FindTypeDefinitions(RdfProperty data)
        {
            ArrayList lstDefinitions = new ArrayList();

            if (!data.Range.ToLower().StartsWith("http://"))
            {
                DamlTypeDefinition defnRdf = this.FindTypeDefintionInRdfProperty(data.Range.TrimStart(new char[] { '#' }));
                if (defnRdf != null)
                {
                    lstDefinitions.Add(defnRdf);
                }

                DamlTypeDefinition defnClass = this.FindTypeDefinitionInDamlClass(data.Range.TrimStart(new char[] { '#' }));
                if (defnClass != null)
                {
                    lstDefinitions.Add(defnClass);
                }
            }

            if (!data.SameValueAs.ToLower().StartsWith("http://"))
            {
                DamlTypeDefinition defnRdf = this.FindTypeDefintionInRdfProperty(data.SameValueAs.TrimStart(new char[] { '#' }));
                if (defnRdf != null)
                {
                    lstDefinitions.Add(defnRdf);
                }

                DamlTypeDefinition defnClass = this.FindTypeDefinitionInDamlClass(data.SameValueAs.TrimStart(new char[] { '#' }));
                if (defnClass != null)
                {
                    lstDefinitions.Add(defnClass);
                }
            }

            return((DamlTypeDefinition[])lstDefinitions.ToArray(typeof(DamlTypeDefinition)));
        }
Example #4
0
        private DamlTypeDefinition FindTypeDefinitionInDamlClass(string strTypeName)
        {
            DamlTypeDefinition definition = null;

            try
            {
                XmlNode root = this.m_doc.DocumentElement;

                string  strXPath           = DamlConstants.DAML_CLASS + "[@" + DamlConstants.RDF_ID + "='" + strTypeName + "'" + "]";
                XmlNode typeDefinitionNode = root.SelectSingleNode(strXPath, m_mgr);
                if (typeDefinitionNode == null)
                {
                    return(null);
                }

                definition = this.GetDamlClassTypeData(typeDefinitionNode);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            return(definition);
        }
Example #5
0
        private DamlTypeDefinition GetDamlClassTypeData(XmlNode typeDefinitionNode)
        {
            if (typeDefinitionNode == null)
            {
                throw new ArgumentNullException("typeDefinitionNode", "Cannot be null");
            }

            DamlTypeDefinition definition = null;

            // Get the first child of this node, this will determine the "type" we actually have
            XmlNode typeDescriptionNode = typeDefinitionNode.FirstChild;

            if (typeDescriptionNode == null)
            {
                throw new Exception("Invalid/Unexpected typeDefinitionNode passed");
            }

            switch (typeDescriptionNode.Name)
            {
            case DamlConstants.DAML_ONE_OF:
            {
                DamlOneOf oneOf = new DamlOneOf();

                // Get attribute on description node, set the type name
                oneOf.Name = typeDefinitionNode.Attributes[DamlConstants.RDF_ID].Value;
                // Get the parseType on the description node
                string strParseType = typeDescriptionNode.Attributes[DamlConstants.RDF_PARSE_TYPE].Value;

                if (strParseType != DamlConstants.DAML_COLLECTION)
                {
                    throw new Exception("Unexpected parse type on daml:Class node's first child");
                }
                else
                {
                    oneOf.ParseType = enuRdfParseType.damlCollection;
                }

                XmlNodeList lstOptions = typeDescriptionNode.ChildNodes;
                foreach (XmlNode optionNode in lstOptions)
                {
                    oneOf.AddOption(optionNode.Attributes[DamlConstants.RDF_ID].Value);
                }

                definition = oneOf;
            }
            break;

            case DamlConstants.DAML_UNION_OF:
            {
                DamlUnionOf unionOf = new DamlUnionOf();

                // Get attribute on description node, set the type name
                unionOf.Name = typeDefinitionNode.Attributes[DamlConstants.RDF_ID].Value;
                // Get the parseType on the description node
                string strParseType = typeDescriptionNode.Attributes[DamlConstants.RDF_PARSE_TYPE].Value;

                if (strParseType != DamlConstants.DAML_COLLECTION)
                {
                    throw new Exception("Unexpected parse type on daml:Class node's first child");
                }
                else
                {
                    unionOf.ParseType = enuRdfParseType.damlCollection;
                }

                XmlNodeList lstOptions = typeDescriptionNode.ChildNodes;
                foreach (XmlNode optionNode in lstOptions)
                {
                    XmlAttribute att = optionNode.Attributes[DamlConstants.RDF_ABOUT];
                    unionOf.AddOption(att.Value);
                }

                definition = unionOf;
            }
            break;

            case DamlConstants.RDFS_SUBCLASSOF:
            {
                RdfsSubClassOf subClassOf = new RdfsSubClassOf();

                subClassOf.Name  = typeDefinitionNode.Attributes[DamlConstants.RDF_ID].Value;
                subClassOf.Value = typeDescriptionNode.Attributes[DamlConstants.RDF_RESOURCE].Value;

                definition = subClassOf;
            }
            break;

            default: throw new Exception("Unexpected node. Cannot get daml:Class type data.");
            }

            return(definition);
        }
        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);
        }