Ejemplo n.º 1
0
        /// <summary>
        /// Apply the filters of the given pattern group to its result table
        /// </summary>
        internal static void ApplyFilters(RDFConstructQuery query, RDFPatternGroup patternGroup)
        {
            if (patternGroup.Patterns.Any() && patternGroup.Filters.Any())
            {
                DataTable   filteredTable = query.PatternGroupResultTables[patternGroup].Clone();
                IEnumerator rowsEnum      = query.PatternGroupResultTables[patternGroup].Rows.GetEnumerator();

                //Iterate the rows of the pattern group's result table
                Boolean keepRow = false;
                while (rowsEnum.MoveNext())
                {
                    //Apply the pattern group's filters on the row
                    keepRow = true;
                    IEnumerator <RDFFilter> filtersEnum = patternGroup.Filters.GetEnumerator();
                    while (keepRow && filtersEnum.MoveNext())
                    {
                        keepRow = filtersEnum.Current.ApplyFilter((DataRow)rowsEnum.Current, false);
                    }

                    //If the row has passed all the filters, keep it in the filtered result table
                    if (keepRow)
                    {
                        DataRow newRow = filteredTable.NewRow();
                        newRow.ItemArray = ((DataRow)rowsEnum.Current).ItemArray;
                        filteredTable.Rows.Add(newRow);
                    }
                }

                //Save the result datatable
                query.PatternGroupResultTables[patternGroup] = filteredTable;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get the result table of the given pattern group
        /// </summary>
        internal static void CombinePatterns(RDFConstructQuery query, RDFPatternGroup patternGroup)
        {
            if (patternGroup.Patterns.Any())
            {
                //Populate pattern group result table
                DataTable patternGroupResultTable = RDFQueryEngine.CombineTables(query.PatternResultTables[patternGroup], false);

                //Add it to the list of pattern group result tables
                query.PatternGroupResultTables.Add(patternGroup, patternGroupResultTable);

                //Populate its metadata
                query.PatternGroupResultTables[patternGroup].TableName = patternGroup.ToString();
                if (!query.PatternGroupResultTables[patternGroup].ExtendedProperties.ContainsKey("IsOptional"))
                {
                    query.PatternGroupResultTables[patternGroup].ExtendedProperties.Add("IsOptional", patternGroup.IsOptional);
                }
                else
                {
                    query.PatternGroupResultTables[patternGroup].ExtendedProperties["IsOptional"] = patternGroup.IsOptional;
                }
                if (!query.PatternGroupResultTables[patternGroup].ExtendedProperties.ContainsKey("JoinAsUnion"))
                {
                    query.PatternGroupResultTables[patternGroup].ExtendedProperties.Add("JoinAsUnion", patternGroup.JoinAsUnion);
                }
                else
                {
                    query.PatternGroupResultTables[patternGroup].ExtendedProperties["JoinAsUnion"] = patternGroup.JoinAsUnion;
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Adds the given pattern group to the query
 /// </summary>
 public RDFDescribeQuery AddPatternGroup(RDFPatternGroup patternGroup)
 {
     if (patternGroup != null)
     {
         if (!this.GetPatternGroups().Any(q => q.Equals(patternGroup)))
         {
             this.QueryMembers.Add(patternGroup);
         }
     }
     return(this);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Adds the given pattern group to the query
 /// </summary>
 public RDFAskQuery AddPatternGroup(RDFPatternGroup patternGroup)
 {
     if (patternGroup != null)
     {
         if (!this.PatternGroups.Exists(pg => pg.PatternGroupName.Equals(patternGroup.PatternGroupName, StringComparison.Ordinal)))
         {
             this.PatternGroups.Add(patternGroup);
         }
     }
     return(this);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the intermediate result tables of the given pattern group
        /// </summary>
        internal static void EvaluatePatterns(RDFConstructQuery query, RDFPatternGroup patternGroup, Object graphOrStore)
        {
            query.PatternResultTables[patternGroup] = new List <DataTable>();

            //Iterate over the patterns of the pattern group
            foreach (RDFPattern pattern in patternGroup.Patterns)
            {
                //Apply the pattern to the graph/store
                DataTable patternResultsTable = graphOrStore is RDFGraph?RDFQueryEngine.ApplyPattern(pattern, (RDFGraph)graphOrStore)
                                                    : RDFQueryEngine.ApplyPattern(pattern, (RDFStore)graphOrStore);

                //Set the name and the optionality metadata of the result datatable
                patternResultsTable.TableName = pattern.ToString();
                patternResultsTable.ExtendedProperties.Add("IsOptional", pattern.IsOptional);
                patternResultsTable.ExtendedProperties.Add("JoinAsUnion", pattern.JoinAsUnion);

                //Save the result datatable
                query.PatternResultTables[patternGroup].Add(patternResultsTable);
            }
        }
Ejemplo n.º 6
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());
        }