Exemple #1
0
        /// <summary>
        /// Prints the string representation of a pattern group
        /// </summary>
        internal static String PrintPatternGroup(RDFPatternGroup patternGroup, Int32 spaceIndent, Boolean skipOptional, List <RDFNamespace> prefixes)
        {
            String spaces = new StringBuilder().Append(' ', spaceIndent < 0 ? 0 : spaceIndent).ToString();

            #region HEADER
            StringBuilder result = new StringBuilder();
            if (patternGroup.IsOptional && !skipOptional)
            {
                result.Append("  " + spaces + "OPTIONAL {\n");
                spaces = spaces + "  ";
            }
            result.Append(spaces + "  {\n");
            #endregion

            #region MEMBERS
            Boolean printingUnion = false;
            List <RDFPatternGroupMember> evaluablePGMembers = patternGroup.GetEvaluablePatternGroupMembers().ToList();
            RDFPatternGroupMember        lastPGMember       = evaluablePGMembers.LastOrDefault();
            foreach (var pgMember in evaluablePGMembers)
            {
                #region PATTERNS
                if (pgMember is RDFPattern)
                {
                    //Union pattern
                    if (((RDFPattern)pgMember).JoinAsUnion)
                    {
                        if (!pgMember.Equals(lastPGMember))
                        {
                            //Begin a new Union block
                            printingUnion = true;
                            result.Append(spaces + "    { " + PrintPattern((RDFPattern)pgMember, prefixes) + " }\n" + spaces + "    UNION\n");
                        }
                        else
                        {
                            //End the Union block
                            if (printingUnion)
                            {
                                printingUnion = false;
                                result.Append(spaces + "    { " + PrintPattern((RDFPattern)pgMember, prefixes) + " }\n");
                            }
                            else
                            {
                                result.Append(spaces + "    " + PrintPattern((RDFPattern)pgMember, prefixes) + " .\n");
                            }
                        }
                    }

                    //Intersect pattern
                    else
                    {
                        //End the Union block
                        if (printingUnion)
                        {
                            printingUnion = false;
                            result.Append(spaces + "    { " + PrintPattern((RDFPattern)pgMember, prefixes) + " }\n");
                        }
                        else
                        {
                            result.Append(spaces + "    " + PrintPattern((RDFPattern)pgMember, prefixes) + " .\n");
                        }
                    }
                }
                #endregion

                #region PROPERTY PATHS
                else if (pgMember is RDFPropertyPath && pgMember.IsEvaluable)
                {
                    //End the Union block
                    if (printingUnion)
                    {
                        printingUnion = false;
                        result.Append(spaces + "    { " + PrintPropertyPath((RDFPropertyPath)pgMember, prefixes) + " }\n");
                    }
                    else
                    {
                        result.Append(spaces + "    " + PrintPropertyPath((RDFPropertyPath)pgMember, prefixes) + " .\n");
                    }
                }
                #endregion
            }
            #endregion

            #region FILTERS
            patternGroup.GroupMembers.Where(m => m is RDFFilter)
            .ToList()
            .ForEach(f => result.Append(spaces + "    " + ((RDFFilter)f).ToString(prefixes) + " \n"));
            #endregion

            #region FOOTER
            result.Append(spaces + "  }\n");
            if (patternGroup.IsOptional && !skipOptional)
            {
                result.Append(spaces + "}\n");
            }
            #endregion

            return(result.ToString());
        }
        internal String ToString(Int32 spaceIndent, List <RDFNamespace> prefixes)
        {
            String spaces = new StringBuilder().Append(' ', spaceIndent < 0 ? 0 : spaceIndent).ToString();

            #region HEADER
            StringBuilder patternGroup = new StringBuilder();
            if (this.IsOptional)
            {
                patternGroup.Append("  " + spaces + "OPTIONAL\n");
                patternGroup.Append("  " + spaces + "{\n");
                spaces = spaces + "  ";
            }
            patternGroup.Append("  " + spaces + "#" + this.PatternGroupName + "\n");
            patternGroup.Append(spaces + "  {\n");
            #endregion

            #region EVALUABLEMEMBERS
            Boolean printingUnion      = false;
            RDFPatternGroupMember last = this.GetEvaluablePatternGroupMembers().LastOrDefault();
            this.GetEvaluablePatternGroupMembers().ToList().ForEach(pgm =>
            {
                #region PATTERNS
                if (pgm is RDFPattern)
                {
                    //Union pattern
                    if (((RDFPattern)pgm).JoinAsUnion)
                    {
                        if (!pgm.Equals(last))
                        {
                            //Begin a new Union block
                            printingUnion = true;
                            patternGroup.Append(spaces + "    { " + ((RDFPattern)pgm).ToString(prefixes) + " }\n" + spaces + "    UNION\n");
                        }
                        else
                        {
                            //End the Union block
                            if (printingUnion)
                            {
                                printingUnion = false;
                                patternGroup.Append(spaces + "    { " + ((RDFPattern)pgm).ToString(prefixes) + " }\n");
                            }
                            else
                            {
                                patternGroup.Append(spaces + "    " + ((RDFPattern)pgm).ToString(prefixes) + " .\n");
                            }
                        }
                    }

                    //Intersect pattern
                    else
                    {
                        //End the Union block
                        if (printingUnion)
                        {
                            printingUnion = false;
                            patternGroup.Append(spaces + "    { " + ((RDFPattern)pgm).ToString(prefixes) + " }\n");
                        }
                        else
                        {
                            patternGroup.Append(spaces + "    " + ((RDFPattern)pgm).ToString(prefixes) + " .\n");
                        }
                    }
                }
                #endregion

                #region PROPERTY PATHS
                else if (pgm is RDFPropertyPath && pgm.IsEvaluable)
                {
                    //End the Union block
                    if (printingUnion)
                    {
                        printingUnion = false;
                        patternGroup.Append(spaces + "    { " + ((RDFPropertyPath)pgm).ToString(prefixes) + " }\n");
                    }
                    else
                    {
                        patternGroup.Append(spaces + "    " + ((RDFPropertyPath)pgm).ToString(prefixes) + " .\n");
                    }
                }
                #endregion
            });
            #endregion

            #region FILTERS
            this.GroupMembers.Where(m => m is RDFFilter)
            .ToList()
            .ForEach(f => patternGroup.Append(spaces + "    " + ((RDFFilter)f).ToString(prefixes) + " \n"));
            #endregion

            #region FOOTER
            patternGroup.Append(spaces + "  }\n");
            if (this.IsOptional)
            {
                patternGroup.Append(spaces + "}\n");
            }
            #endregion

            return(patternGroup.ToString());
        }