Пример #1
0
        private void loadXML(TextReader tr)
        {
            XmlNode xn;

            XmlDocument   doc = new XmlDocument();
            XmlTextReader xtr = new XmlTextReader(tr);

            xtr.WhitespaceHandling = WhitespaceHandling.None;
            // No longer using XmlValidatingReader
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.IgnoreWhitespace = true;
            XmlReader reader = XmlReader.Create(xtr, settings);

            try
            {
                doc.Load(reader);                  // Load the AXL into the DOM (Document Object Model)
            }
            catch (Exception e)
            {
                // report error
                MessageBox.Show(String.Format("Error in AXL format: {0}", e.Message), String.Format("Argumentative"),
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                return;
            }

            xn = (XmlNode)doc.DocumentElement;
            Node n = readArg(xn.ChildNodes);

            n.setComment("Araucaria argument read from " + fileName + System.Environment.NewLine +
                         comment);
            a.setArg(n);
        }
Пример #2
0
        /// <summary>
        /// Import a Rationale file
        /// </summary>
        /// <param name="fileName">File name including path to import.</param>
        /// <returns></returns>
        public Argument importFromRationale(string fileName)
        {
            Argument arg;
            string   line, version, s;
            int      i;

            premise = null;
            try
            {
                infile = new StreamReader(fileName);

                // expecting #Rationale Version 1.09
                line = infile.ReadLine();
                if (line == null)
                {
                    return(null);
                }
                String [] split;
                split = line.Split();                  // splits the text using the default delimiters
                // if(split.Length != 3) { infile.Close(); return null; }  // not what was expected - run away
                if (split[2].Equals("1.09"))
                {
                    version = split[2];
                }
                else if (split[2].Equals("1.08"))
                {
                    MessageBox.Show("Sorry, version 1.08 of Rationale cannot be read");
                    return(null);
                }
                else if (split[2].Equals("130.54") || split[2].Equals("120.53") ||
                         split[2].Equals("140.56") || split[2].Equals("140.58")
                         )
                {
                    version = split[2];
                    return(importFromRationale130(fileName, version));
                }
                else
                {
                    DialogResult d;
                    d = MessageBox.Show(String.Format("I do not know version {0}. Try to load anyway?", split[2]), "Unknown version", MessageBoxButtons.YesNo);                    // TODO Make question
                    if (d == DialogResult.Yes)
                    {
                        return(importFromRationale130(fileName, split[2]));
                    }
                    else
                    {
                        return(null);
                    }
                }

                line = infile.ReadLine();
                if (line == null)
                {
                    return(null);
                }
                // node0 = app.Create("Claim") expected
                if (line.ToLower().IndexOf("claim") < 0)
                {
                    return(null);
                }
                line = infile.ReadLine();
                if (line == null)
                {
                    return(null);
                }
                // app.SetText(node0, "I should go to the beach today.")
                i = line.IndexOf("app.SetText(node0,");                         // should return 0
                if (i != 0)
                {
                    MessageBox.Show(String.Format("unknown entry, app.SetText expected.  Found {0}", line));
                    return(null);
                }
                s       = line.Substring(line.IndexOf("\"") + 1);               // split string at the quote mark
                s       = s.Substring(0, s.IndexOf("\""));                      // remove the last quote and bracket
                premise = new Node(s);                                          // add main premise
                // the next lines declare the top level reasons or objections
                // eg
                // app.CreateChild(node0, "CompoundReason") or app.CreateChild(node0, "CompoundObjection")
                // or app.CreateChild(node0, "Reason") or app.CreateChild(node0, "Objection")

                //  read remaining lines
                lines = new ArrayList();
                line  = infile.ReadLine();
                while (line != null)
                {
                    lines.Add(line);
                    line = infile.ReadLine();
                }
                currentLine = 0;
                // unless the argument has a main premise only, the next lines will define top level
                line = (string)lines[currentLine];
                if (line.IndexOf("CompoundReason") > 0 || line.IndexOf("CompoundObjection") > 0)
                {
                    mode = modeType.analysisMode;
                }
                else if (line.IndexOf("Reason") > 0 || line.IndexOf("Objection") > 0)
                {
                    mode = modeType.reasonMode;
                }
                else
                {
                    MessageBox.Show(String.Format("Expecting to find reasons and or objections.  Found: {0}", line));
                    return(null);
                }
                readTopLevelElement(premise);
            }
            catch (Exception e) { MessageBox.Show(String.Format("Error reading Rationale file. {0}\n{1}", e.Message, e.StackTrace)); }

            infile.Close();
            if (premise == null)
            {
                return(null);
            }
            arg = new Argument();
            arg.setArg(premise);
            return(arg);
        }
Пример #3
0
        private Argument importFromRationale130(string fileName, string version)
        {
            string line;
            string mapref;              // currently created element reference
            float  versionNumber = 0F;

            line = infile.ReadLine();
            if (line == null)
            {
                return(null);
            }

            float.TryParse(version, out versionNumber);
            if (versionNumber >= 130.54F)             // skip prelude on versions higher than 130.54
            {
                while ((line != null) && (!line.Equals("#-- End of Prelude --")))
                {
                    line = infile.ReadLine();
                }
                if (line == null)
                {
                    return(null);
                }
            }

            int      claimsRead = 0, s, e;
            Argument arg = new Argument();
            string   variable, argument, note;
            bool     isNote = false;
            Node     n      = null;

            // line = infile.ReadLine(); // read next line
            while (line != null)
            {
                if (line.StartsWith("map"))                 // standard variable
                {
                    int x = line.IndexOf(" = ");
                    variable = line.Substring(0, x).Trim();
                    if (line.Contains("Create(\"Claim\")"))         // top level argument
                    {
                        if (claimsRead == 0)                        // first tree
                        {
                            head       = new Node();
                            n          = head;
                            n.nodeType = Node.ArgumentNodeType.premise;
                            n.setComment("Import of a Rationale(TM) file (version " + version + ") from " + fileName);
                        }
                        else                          // subsequent trees added as reasons
                        {
                            n          = new Node();
                            n.nodeType = Node.ArgumentNodeType.reason;
                            n.setComment("Additional tree #" + claimsRead);
                            n.setRef(variable);
                            head.addKid(n, true);
                        }
                        n.setRef(variable);
                        claimsRead++;
                    }
                    else if (line.Contains("Create(\"Note\")"))
                    {
                        isNote = true;                          // next text node is a note
                    }
                    else if (line.Contains("CreateChild"))
                    {
                        string toRef, what;

                        // eg
                        // map0_20 = CreateChild(map0_10, "CompoundReason")

                        // get map node that is being created
                        s     = line.IndexOf("CreateChild(");
                        e     = line.IndexOf(",", s);        // locate comma
                        e     = e - s - 12;                  // length of map reference
                        toRef = line.Substring(s + 12, e);

                        // get mapref
                        e      = line.IndexOf("=");                     // locate =
                        mapref = line.Substring(0, e).Trim();

                        // find what to add
                        s    = line.IndexOf("\"");                       // find first quote
                        e    = line.IndexOf("\"", s + 1);                // locate last quote
                        what = line.Substring(s + 1, e - s - 1);         // do not include quote marks

                        if (what.Equals("CompoundReason"))
                        {
                            // A compound reason translates to a reason plus helpers
                            n = new Node("CompoundReason", Node.ArgumentNodeType.reason, variable);
                            // TODO Add compound reason node
                            addNode(mapref, toRef, n);
                        }
                        else if (what.Equals("CompoundObjection"))
                        {
                            // A compound objection translates to a objection plus helpers
                            n = new Node("CompoundObjection", Node.ArgumentNodeType.objection, variable);
                            addNode(mapref, toRef, n);
                        }
                        else if (what.Equals("Reason"))
                        {
                            n          = new Node();
                            n.nodeType = Node.ArgumentNodeType.reason;
                            addNode(mapref, toRef, n);
                        }
                        else if (what.Equals("Objection"))
                        {
                            n          = new Node();
                            n.nodeType = Node.ArgumentNodeType.objection;
                            addNode(mapref, toRef, n);
                        }
                        else if (what.Equals("Claim"))
                        {
                            n          = new Node("Claim: " + what);
                            n.nodeType = Node.ArgumentNodeType.reason;
                            addNode(mapref, toRef, n);
                        }
                        else if (what.Equals("Inference"))
                        {
                            // do not add
                            n          = new Node(what);
                            n.nodeType = Node.ArgumentNodeType.reason;
                            addNode(mapref, toRef, n);
                        }
                        else                          // some other element - add as reason
                        {
                            n          = new Node(what);
                            n.nodeType = Node.ArgumentNodeType.reason;
                            addNode(mapref, toRef, n);
                            // TODO Add as a undefined node - if there was one
                        }
                    }
                }
                else if (line.Contains("SetText("))
                {
                    s        = line.IndexOf("SetText(");
                    e        = line.LastIndexOf("\"");               // last quote mark
                    argument = line.Substring(s + 9, e - s - 9);
                    string w = "\\n";
                    int    i = argument.IndexOf(w);
                    if (i >= 0)
                    {
                        // argument.Replace(w,r); // does not replace a \n
                        string s1 = argument.Substring(0, i);
                        string s2 = argument.Substring(i + w.Length);
                        argument = s1 + System.Environment.NewLine + s2;
                    }

                    while (argument.IndexOf("\\\"") != -1)                              // replace \" with "
                    {
                        argument = argument.Replace("\\\"", "\"");
                    }

                    if (isNote)                                         //
                    {
                        note = argument; isNote = false;
                    }

                    else if (n.EditorText.Equals("CompoundReason") || n.EditorText.Equals("CompoundObjection"))
                    {
                        n.EditorText = argument;
                    }
                    else if (string.IsNullOrEmpty(n.EditorText))
                    {
                        n.EditorText = argument;
                    }
                    else
                    {
                        string ss;
                        Node   nn;
                        ss           = n.getRef();
                        nn           = new Node(argument);
                        n.EditorText = argument;
                        n.addKid(nn);                          // more than one text line for
                        nn.setRef(ss);
                    }
                }
                line = infile.ReadLine();                 // read next line
            }

            infile.Close();
            removeDuplicates(head);
            updateCompoundElements(head);
            Argument newArg2 = new Argument();

            newArg2.setArg(head);
            return(newArg2);
        }
Пример #4
0
        /// <summary>Loads the AXL formatted argument. The source may be a file or StringReader</summary>
        public Argument loadXmlArg(TextReader tr)
        {
            XmlNode xn;
            Node    n = null;

            XmlDocument       doc      = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.IgnoreWhitespace = true;
            settings.IgnoreComments   = true;
            XmlReader reader = XmlReader.Create(tr, settings);


            try
            {
                doc.Load(reader);                  // Load the AXL into the DOM (Document Object Model)
            }
            catch (Exception e)
            {
                // report error
                MessageBox.Show(String.Format("Error in XML format: {0}", e.Message), "Argumentative",
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                return(null);
            }
            // load document into argument
            Argument a = new Argument();

            xn = (XmlNode)doc.DocumentElement;

            if (xn.Name.ToLower().Equals("argument"))
            {
                xn = xn.ChildNodes[0];
            }
            // extract Argument level information
            do
            {
                if (xn.Name.Equals("author"))
                {
                    author = getAnyTag("author", xn);
                }
                if (xn.Name.Equals("created"))
                {
                    getDate("created", xn);
                }
                if (xn.Name.Equals("modified"))
                {
                    getDate("modified", xn);
                }
                if (xn.Name.Equals("premise"))
                {
                    n = getDomNode(xn);
                }
                if (xn.Name.Equals("reason"))
                {
                    n = getDomNode(xn);
                }
                if (xn.Name.Equals("objection"))
                {
                    n = getDomNode(xn);
                }
                if (xn.Name.Equals("helper"))
                {
                    n = getDomNode(xn);
                }
                xn = xn.NextSibling;
            }while(xn != null);
            a.setArg(n);
            a.Author              = author;
            a.CreationDate        = this.creationDate;
            a.CreationDateDefined = this.creationDateDefined;

            return(a);
        }