Пример #1
0
        public void TripleIntTest()
        {
            TripleInt triple = new TripleInt
            {
                X = 125,
                Y = 1024
            };

            Assert.AreEqual(@"X125 Y1024 Z0", triple.ToString());
        }
Пример #2
0
        public void TripleIntDefaultTest()
        {
            TripleInt triple = new TripleInt();

            Assert.AreEqual(@"X0 Y0 Z0", triple.ToString());
        }
Пример #3
0
        public static IEnumerable <TripleInt> LoadGraph(string datafile)
        {
            int    ntriples = 0;
            string subject  = null;
            Dictionary <string, string> namespaces = new Dictionary <string, string>();
            StreamReader sr    = new StreamReader(datafile);
            int          count = 2000000000;

            for (int i = 0; i < count; i++)
            {
                string line = sr.ReadLine();
                //if (i % 10000 == 0) { Console.Write("{0} ", i / 10000); }
                if (line == null)
                {
                    break;
                }
                if (line == "")
                {
                    continue;
                }
                if (line[0] == '@')
                { // namespace
                    string[] parts = line.Split(' ');
                    if (parts.Length != 4 || parts[0] != "@prefix" || parts[3] != ".")
                    {
                        Console.WriteLine("Err: strange line: " + line);
                        continue;
                    }
                    string pref   = parts[1];
                    string nsname = parts[2];
                    if (nsname.Length < 3 || nsname[0] != '<' || nsname[nsname.Length - 1] != '>')
                    {
                        Console.WriteLine("Err: strange nsname: " + nsname);
                        continue;
                    }
                    nsname = nsname.Substring(1, nsname.Length - 2);
                    namespaces.Add(pref, nsname);
                }
                else if (line[0] != ' ')
                { // Subject
                    line    = line.Trim();
                    subject = GetEntityString(namespaces, line);
                    if (subject == null)
                    {
                        continue;
                    }
                }
                else
                { // Predicate and object
                    string line1       = line.Trim();
                    int    first_blank = line1.IndexOf(' ');
                    if (first_blank == -1)
                    {
                        Console.WriteLine("Err in line: " + line); continue;
                    }
                    string pred_line = line1.Substring(0, first_blank);
                    string predicate = GetEntityString(namespaces, pred_line);
                    string rest_line = line1.Substring(first_blank + 1).Trim();
                    // Уберем последний символ
                    rest_line = rest_line.Substring(0, rest_line.Length - 1).Trim();
                    bool isDatatype = rest_line[0] == '\"';
                    // объект может быть entity или данное, у данного может быть языковый спецификатор или тип
                    string entity   = null;
                    string sdata    = null;
                    string datatype = null;
                    string lang     = null;
                    if (isDatatype)
                    {
                        // Последняя двойная кавычка
                        int lastqu = rest_line.LastIndexOf('\"');

                        // Значение данных
                        sdata = rest_line.Substring(1, lastqu - 1);

                        // Языковый специализатор:
                        int dog = rest_line.LastIndexOf('@');
                        if (dog == lastqu + 1)
                        {
                            lang = rest_line.Substring(dog + 1, rest_line.Length - dog - 1);
                        }

                        int pp = rest_line.IndexOf("^^");
                        if (pp == lastqu + 1)
                        {
                            //  Тип данных
                            string qname = rest_line.Substring(pp + 2);
                            //  тип данных может быть "префиксным" или полным
                            if (qname[0] == '<')
                            {
                                datatype = qname.Substring(1, qname.Length - 2);
                            }
                            else
                            {
                                datatype = GetEntityString(namespaces, qname);
                            }
                        }
                        yield return(new DTripleInt()
                        {
                            subject = TripleInt.Code(subject),
                            predicate = TripleInt.Code(predicate),
                            data = Literal.Create(datatype, sdata, lang)
                        });
                    }
                    else
                    { // entity
                        entity = rest_line[0] == '<' ? rest_line.Substring(1, rest_line.Length - 2) : GetEntityString(namespaces, rest_line);

                        yield return(new OTripleInt()
                        {
                            subject = TripleInt.Code(subject),
                            predicate = TripleInt.Code(predicate),
                            obj = TripleInt.Code(entity)
                        });
                    }
                    ntriples++;
                }
            }
            Console.WriteLine("ntriples={0}", ntriples);
        }