示例#1
0
        public static lstory Read(string path)
        {
            lstory lstory = new lstory();

            lstory.FileName = path;

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(path);

            // 1.read locations and tree structure
            XmlNode xmlNode = xmlDoc.SelectSingleNode("Story/Locations");

            lstory.LTree = new LocationNode(-1, "root", "Black", false, null);
            if (xmlNode != null && xmlNode.HasChildNodes)
            {
                foreach (XmlNode locationNode in xmlNode.ChildNodes)
                {
                    ParseXmlNode(locationNode, lstory.LTree);
                }
            }

            // 2.read characters and session table
            lstory.Characters = new List <Character>();
            lstory.segments   = new List <List <segmentread> >();
            HashSet <int> timestamps = new HashSet <int>();

            lstory.sessionCount = 0;
            xmlNode             = xmlDoc.SelectSingleNode("Story/Characters");
            foreach (XmlNode characterNode in xmlNode.ChildNodes)
            {
                string name = characterNode.Attributes["Color"] != null ? characterNode.Attributes["Name"].Value : "";


                string color  = characterNode.Attributes["Color"] != null ? characterNode.Attributes["Color"].Value : "Black";
                double weight = characterNode.Attributes["Weight"] != null?double.Parse(characterNode.Attributes["Weight"].Value) : 1.0;

                Character character = new Character(name, weight, color, lstory.Characters.Count);
                lstory.Characters.Add(character);
                lstory.segments.Add(new List <segmentread>());
                foreach (XmlNode spanNode in characterNode.ChildNodes)
                {
                    int start = int.Parse(spanNode.Attributes["Start"].Value);

                    int end = int.Parse(spanNode.Attributes["End"].Value);

                    timestamps.Add(start);
                    timestamps.Add(end);
                    string[] prevItems = spanNode.Attributes["Prev"].Value.Split(',');
                    var      prev      = prevItems.Select(item => int.Parse(item.Trim())).ToList();
                    int      now       = int.Parse(spanNode.Attributes["Now"].Value);
                    lstory.sessionCount = now > lstory.sessionCount ? now : lstory.sessionCount;
                    string[] nextItems = spanNode.Attributes["Next"].Value.Split(',');
                    var      next      = nextItems.Select(item => int.Parse(item.Trim())).ToList();
                    double   sweight   = double.Parse(spanNode.Attributes["Weight"].Value);
                    lstory.segments.Last().Add(new segmentread(start, end, prev, now, next, sweight));
                }
            }
            lstory.TimeStamps = timestamps.ToArray();
            Array.Sort(lstory.TimeStamps);
            lstory.FC = lstory.TimeStamps.Count() - 1;


            lstory.TimeStampStartDic = new Dictionary <int, int>();
            lstory.TimeStampEndDic   = new Dictionary <int, int>();
            for (int i = 0; i < lstory.TimeStamps.Length - 1; i++)
            {
                lstory.TimeStampStartDic.Add(lstory.TimeStamps[i], i);
                lstory.TimeStampEndDic.Add(lstory.TimeStamps[i + 1], i);
            }
            lstory.sessionCount++;


            return(lstory);
        }
示例#2
0
        public static lstory ReadOriginalXml(string path)
        {
            lstory lstory = new lstory();

            lstory.FileName = path;
            List <string> getdata = new List <string>();
            XmlDocument   xmlDoc  = new XmlDocument();

            xmlDoc.Load(path);

            // 1.read locations and tree structure
            XmlNode xmlNode = xmlDoc.SelectSingleNode("Story/Locations");

            lstory.LTree = new LocationNode(-1, "root", "Black", false, null);
            if (xmlNode != null && xmlNode.HasChildNodes)
            {
                foreach (XmlNode locationnode in xmlNode.ChildNodes)
                {
                    ParseXmlNode(locationnode, lstory.LTree);
                }
            }
            // 2.read characters and session table
            lstory.Characters = new List <Character>();
            lstory.segments   = new List <List <segmentread> >();
            HashSet <int> timestamps = new HashSet <int>();

            lstory.sessionCount = 0;
            xmlNode             = xmlDoc.SelectSingleNode("Story/Characters");
            foreach (XmlNode characterNode in xmlNode.ChildNodes)
            {
                string name = characterNode.Attributes["Color"] != null ? characterNode.Attributes["Name"].Value : "";
                getdata.Add(name);
                string color  = characterNode.Attributes["Color"] != null ? characterNode.Attributes["Color"].Value : "Black";
                double weight = characterNode.Attributes["Weight"] != null?double.Parse(characterNode.Attributes["Weight"].Value) : 1.0;

                Character character = new Character(name, weight, color, lstory.Characters.Count);
                lstory.Characters.Add(character);
                lstory.segments.Add(new List <segmentread>());
                foreach (XmlNode spanNode in characterNode.ChildNodes)
                {
                    int start = int.Parse(spanNode.Attributes["Start"].Value);
                    getdata.Add(Convert.ToString(start));
                    int end = int.Parse(spanNode.Attributes["End"].Value);
                    getdata.Add(Convert.ToString(start));
                    timestamps.Add(start);
                    timestamps.Add(end);
                    int now = int.Parse(spanNode.Attributes["Session"].Value);
                    lstory.sessionCount = now > lstory.sessionCount ? now : lstory.sessionCount;
                    var    prev    = new List <int>();
                    var    next    = new List <int>();
                    double sweight = 1.0;
                    lstory.segments.Last().Add(new segmentread(start, end, prev, now, next, sweight));
                }
            }
            lstory.TimeStamps = timestamps.ToArray();

            for (int i = 0; i < lstory.Characters.Count; i++)
            {
                if (lstory.segments[i].Count == 1)
                {
                    lstory.segments[i][0].Prev.Add(-1);
                    lstory.segments[i][0].Next.Add(-1);
                }
                else
                {
                    #region >=2
                    int j = 0;
                    lstory.segments[i][j].Prev.Add(-1);
                    if (lstory.segments[i][j + 1].Start == lstory.segments[i][j].End)
                    {
                        lstory.segments[i][j].Next.Add(lstory.segments[i][j + 1].Now);
                    }
                    for (j = 1; j < lstory.segments[i].Count - 1; j++)
                    {
                        if (lstory.segments[i][j].Start == lstory.segments[i][j - 1].End)
                        {
                            lstory.segments[i][j].Prev.Add(lstory.segments[i][j - 1].Now);
                        }
                        if (lstory.segments[i][j].End == lstory.segments[i][j + 1].Start)
                        {
                            lstory.segments[i][j].Next.Add(lstory.segments[i][j + 1].Now);
                        }
                    }
                    lstory.segments[i][j].Next.Add(-1);
                    if (lstory.segments[i][j].Start == lstory.segments[i][j - 1].End)
                    {
                        lstory.segments[i][j].Prev.Add(lstory.segments[i][j - 1].Now);
                    }
                    #endregion
                }
            }
            Array.Sort(lstory.TimeStamps);
            lstory.FC = lstory.TimeStamps.Count() - 1;
            getdata.Add(Convert.ToString(lstory.FC));
            lstory.TimeStampStartDic = new Dictionary <int, int>();
            lstory.TimeStampEndDic   = new Dictionary <int, int>();
            for (int i = 0; i < lstory.TimeStamps.Length - 1; i++)
            {
                lstory.TimeStampStartDic.Add(lstory.TimeStamps[i], i);
                lstory.TimeStampEndDic.Add(lstory.TimeStamps[i + 1], i);
            }
            lstory.sessionCount++;
            return(lstory);
        }