Ejemplo n.º 1
0
        public static Triple Split(String line, Action<string> errorAction)
        {
            Triple triple = new Triple();
            TRIPLE_TAGS CurrentTag = TRIPLE_TAGS.SUBJECT;

            // <http://en.wikipedia.org/wiki/Tony_Benn> <http://purl.org/dc/elements/1.1/title>     "Tony Benn" .
            // <http://en.wikipedia.org/wiki/Tony_Benn> <http://purl.org/dc/elements/1.1/publisher> "Wikipedia" .
            // Hallo <http://test.de/blubb> "Kawumm"@German^^<http://test.de/string> .

            #region init local vars
            int iCurrent = 0;
            StringBuilder strCurrentTag = new StringBuilder();
            bool bFirst = true;
            bool bMaskChar = false;
            char cMaskChar = '0';

            #endregion

            foreach (char currentChar in line.ToCharArray())
            {
                #region simply read char, in case it is running in masked mode
                if (bMaskChar)
                {
                    strCurrentTag.Append(currentChar);
                    if (currentChar == cMaskChar)
                        bMaskChar = false;
                }
                #endregion

                if (!bMaskChar)  // no else, because criteria might have changes within the if
                {
                    switch (CurrentTag)
                    {
                        #region SUBJECT handling
                        case TRIPLE_TAGS.SUBJECT:
                            {
                                switch (currentChar)
                                {
                                    case ' ':
                                        {
                                            if (!bFirst)
                                            {
                                                triple.Subject = strCurrentTag.ToString();
                                                strCurrentTag = new StringBuilder();
                                                CurrentTag = TRIPLE_TAGS.PREDICATE;
                                                bFirst = true;
                                            }
                                            break;
                                        }
                                    case '<':
                                        {
                                            if (!bFirst)
                                                errorAction("invalid format: Subject may not contain '<' unless its a URI.");
                                            else
                                            {
                                                // strCurrentTag.Append(currentChar);
                                                bFirst = false;
                                                bMaskChar = true;
                                                cMaskChar = '>';
                                            }
                                            break;
                                        }
                                    case '>':
                                        {
                                            triple.Subject = strCurrentTag.ToString()
                                                .Substring(0, strCurrentTag.Length - 1);   // cut >
                                            strCurrentTag = new StringBuilder();
                                            CurrentTag = TRIPLE_TAGS.PREDICATE;
                                            bFirst = true;
                                            break;
                                        }
                                    default:
                                        {
                                            strCurrentTag.Append(currentChar);
                                            bFirst = false;
                                            bMaskChar = true;
                                            cMaskChar = ' ';

                                            break;
                                        }
                                }

                                break;
                            }
                        #endregion

                        #region PREDICATE handling
                        case TRIPLE_TAGS.PREDICATE:
                            {
                                switch (currentChar)
                                {
                                    case ' ':   // ignore leading blanks
                                        {
                                            if (!bFirst)
                                            {
                                                errorAction("no blanks allowed in PREDICATE URI!");
                                                throw new Exception();
                                            }
                                            break;
                                        }
                                    case '<':
                                        {
                                            if (!bFirst)
                                                errorAction("invalid format: Subject may not contain '<' unless its a URI.");
                                            else
                                            {
                                                // strCurrentTag.Append(currentChar);
                                                bFirst = false;
                                                bMaskChar = true;
                                                cMaskChar = '>';
                                            }
                                            break;
                                        }
                                    case '>':
                                        {
                                            if (bFirst)
                                            {
                                                errorAction("PREDICATE has top begin with '<'!");
                                                throw new Exception();
                                            }
                                            else
                                            {
                                                triple.Predicate = strCurrentTag.ToString()
                                                    .Substring(0, strCurrentTag.Length - 1);   // cut >
                                                strCurrentTag = new StringBuilder();
                                                CurrentTag = TRIPLE_TAGS.OBJECT;
                                                bFirst = true;
                                            }
                                            break;
                                        }
                                    default:
                                        {
                                            if (bFirst)
                                            {
                                                errorAction("PREDICATE has top begin with '<'!");
                                                throw new Exception();
                                            }
                                            else strCurrentTag.Append(currentChar);

                                            break;
                                        }
                                }
                                break;
                            }
                        #endregion

                        #region OBJECT handling
                        case TRIPLE_TAGS.OBJECT:
                            {
                                switch (currentChar)
                                {
                                    case ' ':
                                        {
                                            if (!bFirst)
                                            {
                                                errorAction("no blanks allowed in OBJECT (except within '\"')!");
                                                throw new Exception();
                                            }
                                            break;
                                        }
                                    case '"':
                                        {
                                            if (bFirst)
                                            {
                                                // strCurrentTag.Append(currentChar);

                                                bFirst = false;
                                                bMaskChar = true;
                                                cMaskChar = '"';
                                            }
                                            else
                                            {
                                                triple.TripleObject = strCurrentTag.ToString()
                                                    .Substring(0, strCurrentTag.Length - 1); ;   // cut trailing "
                                                strCurrentTag = new StringBuilder();
                                                CurrentTag = TRIPLE_TAGS.POST_OBJECT;
                                                bFirst = true;
                                            }
                                            break;
                                        }
                                    case '<':
                                        {
                                            if (bFirst)
                                            {
                                                // strCurrentTag.Append(currentChar);
                                                bFirst = false;
                                                bMaskChar = true;
                                                cMaskChar = '>';
                                            }
                                            break;
                                        }
                                    case '>':
                                        {
                                            triple.TripleObject = strCurrentTag.ToString()
                                                .Substring(0, strCurrentTag.Length - 1); ;    // ignore >
                                            strCurrentTag = new StringBuilder();
                                            CurrentTag = TRIPLE_TAGS.POST_OBJECT;
                                            bFirst = true;
                                            break;
                                        }
                                    default:
                                        {
                                            if (bFirst)
                                            {
                                                errorAction("OBJECT must either begin with '\"' or '<'!");
                                                throw new Exception();
                                            }
                                            break;
                                        }
                                }
                                break;
                            }
                        #endregion

                        #region optional OBJECT data (language and datatype)
                        case TRIPLE_TAGS.POST_OBJECT:
                            {
                                switch (currentChar)
                                {/*
                                    case ' ':
                                        {
                                            if (!bFirst)
                                            {
                                                errorAction("blank's are not allowed in optional Object data.");
                                                throw new Exception();
                                            }  // otherwise ignore
                                            break;
                                        }*/
                                    case '@': // Language
                                        {
                                            CurrentTag = TRIPLE_TAGS.OBJECT_LANG;
                                            break;
                                        }
                                    case '^':
                                        {
                                            CurrentTag = TRIPLE_TAGS.OBJECT_DATATYPE;
                                            break;
                                        }
                                }
                                break;
                            }
                        #endregion

                        #region OBJECT_LANG
                        case TRIPLE_TAGS.OBJECT_LANG:
                            {
                                switch (currentChar)
                                {
                                    case ' ':
                                    case '^':
                                    case '.':
                                        {
                                            triple.Language = strCurrentTag.ToString();
                                            strCurrentTag = new StringBuilder();
                                            CurrentTag = TRIPLE_TAGS.POST_OBJECT;
                                            break;
                                        }
                                    default:
                                        {
                                            strCurrentTag.Append(currentChar);
                                            break;
                                        }
                                }
                                break;
                            }
                        #endregion

                        #region OBJECT_DATATYPE
                        case TRIPLE_TAGS.OBJECT_DATATYPE:
                            {
                                switch (currentChar)
                                {
                                    case '^':  // ignore this char
                                        {
                                            break;
                                        }
                                    case '<':
                                        {
                                            // strCurrentTag.Append(currentChar);
                                            bMaskChar = true;
                                            cMaskChar = '>';
                                            bFirst = false;
                                            break;
                                        }
                                    case '>':
                                        {
                                            triple.Datatype = strCurrentTag.ToString()
                                                .Substring(0, strCurrentTag.Length - 1);    // remove trailing >
                                            CurrentTag = TRIPLE_TAGS.POST_OBJECT;
                                            break;
                                        }
                                    default:
                                        {
                                            strCurrentTag.Append(currentChar);
                                            break;
                                        }
                                }
                                break;
                            }
                        #endregion
                    }
                }
                iCurrent++;
            }

            return triple;
        }
        private static bool SaveTriple(Triple selectedTriple, Dictionary<string, List<string>> dictExistingNodes, Dictionary<string, uint> dictDirectoriesEntries,
            Dictionary<string, long> dictVertexIDs, Action<String> LogError)
        {
            StreamWriter sw = null;
            try
            {
                if (!dictExistingNodes.ContainsKey(selectedTriple.Subject))
                //if (!File.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir + Path.DirectorySeparatorChar + RemoveEvilCharactersForFile(selectedTriple.Subject)))
                {
                    ushort sDirs = 0;
                    String strPathname = null;
                    DirectoryInfo dirInfo = null;

                    /*
                    foreach (String hashDirName in Directory.EnumerateDirectories(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir))
                    {
                        dirInfo = new DirectoryInfo(hashDirName);
                        if (dirInfo.GetFiles().Length < Properties.Settings.Default.DataDirMaxEntries)
                        {
                            strPathname = Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar
                                + Properties.Settings.Default.DataDir + Path.DirectorySeparatorChar
                                + dirInfo.Name + Path.DirectorySeparatorChar;
                            break;
                        }
                        else strPathname = null;

                        sDirs++;
                    }
                    */
                    foreach (String dirname in dictDirectoriesEntries.Keys)
                    {
                        if (dictDirectoriesEntries[dirname] < Properties.Settings.Default.DataDirMaxEntries)
                        {
                            strPathname = dirname;
                            break;
                        }
                        sDirs++;
                    }

                    if (strPathname == null)
                    {
                        if (Directory.Exists(Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar
                                + Properties.Settings.Default.DataDir + Path.DirectorySeparatorChar
                                + sDirs))
                        {
                            LogError("Directory already exists! " + Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar
                                + Properties.Settings.Default.DataDir + Path.DirectorySeparatorChar
                                + sDirs + Path.DirectorySeparatorChar);
                            return false;
                        }
                        else
                        {
                            strPathname = Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar
                                + Properties.Settings.Default.DataDir + Path.DirectorySeparatorChar
                                + sDirs + Path.DirectorySeparatorChar;
                            Directory.CreateDirectory(strPathname);

                            dictDirectoriesEntries.Add(strPathname, 0);
                        }
                    }
                    sw = File.CreateText(strPathname + Path.DirectorySeparatorChar + RemoveEvilCharactersForFile(selectedTriple.Subject));
                    sw.WriteLine("VertexID=" + dictVertexIDs[selectedTriple.TripleObject].ToString());

                    List<string> lData = new List<string>();
                    lData.Add(strPathname + Path.DirectorySeparatorChar + RemoveEvilCharactersForFile(selectedTriple.Subject));
                    lData.Add(dictVertexIDs[selectedTriple.TripleObject].ToString());
                    lData.Add(selectedTriple.TripleObject);
                    dictExistingNodes.Add(selectedTriple.Subject, lData);
                    dictDirectoriesEntries[strPathname]++;
                    dictVertexIDs[selectedTriple.TripleObject]++;
                }
                else
                {
                    sw = File.AppendText(dictExistingNodes[selectedTriple.Subject][0]);
                    // Properties.Settings.Default.WorkDir + Path.DirectorySeparatorChar + Properties.Settings.Default.DataDir + Path.DirectorySeparatorChar + RemoveEvilCharactersForFile(selectedTriple.Subject));
                }
                sw.WriteLine(selectedTriple.Subject + "=" + selectedTriple.TripleObject);
                sw.Flush();
                sw.Close();
            }
            catch (Exception a)
            {
                LogError("Error writing Node-File to FS: " + selectedTriple.Subject);
                LogError(a.Message);
                LogError(a.StackTrace);
                return false;
            }
            return true;
        }