/// <summary> /// Executes the projection producing result's table /// </summary> internal override DataTable ExecuteProjection(List <RDFVariable> partitionVariables) { DataTable projFuncTable = new DataTable(); //Initialization partitionVariables.ForEach(pv => RDFQueryEngine.AddColumn(projFuncTable, pv.VariableName)); RDFQueryEngine.AddColumn(projFuncTable, this.ProjectionVariable.VariableName); //Finalization foreach (string partitionKey in this.AggregatorContext.ExecutionRegistry.Keys) { //Get aggregator value double aggregatorValue = this.AggregatorContext.GetPartitionKeyExecutionResult <double>(partitionKey, 0d); //Get aggregator counter double aggregatorCounter = this.AggregatorContext.GetPartitionKeyExecutionCounter(partitionKey); //In case of non-numeric values, consider partition failed double finalAggregatorValue = double.NaN; if (!aggregatorValue.Equals(double.NaN)) { finalAggregatorValue = aggregatorValue / aggregatorCounter; } //Update aggregator context (sum, count) this.AggregatorContext.UpdatePartitionKeyExecutionResult <double>(partitionKey, finalAggregatorValue); //Update result's table this.UpdateProjectionTable(partitionKey, projFuncTable); } return(projFuncTable); }
/// <summary> /// Gets the datatable representing the SPARQL values /// </summary> internal DataTable GetDataTable() { DataTable result = new DataTable(); result.ExtendedProperties.Add("IsOptional", false); result.ExtendedProperties.Add("JoinAsUnion", false); //Create the columns of the SPARQL values this.Bindings.ToList() .ForEach(b => RDFQueryEngine.AddColumn(result, b.Key)); result.AcceptChanges(); //Create the rows of the SPARQL values result.BeginLoadData(); for (int i = 0; i < this.MaxBindingsLength; i++) { Dictionary <String, String> bindings = new Dictionary <String, String>(); this.Bindings.ToList() .ForEach(b => { RDFPatternMember bindingValue = b.Value.ElementAtOrDefault(i); bindings.Add(b.Key, bindingValue?.ToString()); if (bindingValue == null) { result.ExtendedProperties["IsOptional"] = true; } }); RDFQueryEngine.AddRow(result, bindings); } result.EndLoadData(); return(result); }
/// <summary> /// Helps in finalization step by updating the projection's result table (NUMERIC) /// </summary> private void UpdateProjectionTableNumeric(String partitionKey, DataTable projFuncTable) { //Get bindings from context Dictionary <String, String> bindings = new Dictionary <String, String>(); foreach (String pkValue in partitionKey.Split(new String[] { "§PK§" }, StringSplitOptions.RemoveEmptyEntries)) { String[] pValues = pkValue.Split(new String[] { "§PV§" }, StringSplitOptions.None); bindings.Add(pValues[0], pValues[1]); } //Add aggregator value to bindings Double aggregatorValue = this.AggregatorContext.GetPartitionKeyExecutionResult <Double>(partitionKey, Double.PositiveInfinity); if (aggregatorValue.Equals(Double.NaN)) { bindings.Add(this.ProjectionVariable.VariableName, String.Empty); } else { bindings.Add(this.ProjectionVariable.VariableName, new RDFTypedLiteral(Convert.ToString(aggregatorValue, CultureInfo.InvariantCulture), RDFModelEnums.RDFDatatypes.XSD_DOUBLE).ToString()); } //Add bindings to result's table RDFQueryEngine.AddRow(projFuncTable, bindings); }
/// <summary> /// Applies the query to the given datasource /// </summary> internal RDFConstructQueryResult ApplyToDataSource(RDFDataSource datasource) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString()); if (this.PatternGroups.Any()) { //Iterate the pattern groups of the query var fedPatternResultTables = new Dictionary <RDFPatternGroup, List <DataTable> >(); foreach (var patternGroup in this.PatternGroups) { //Step 1: Get the intermediate result tables of the current pattern group if (datasource.IsFederation()) { #region TrueFederations foreach (var store in (RDFFederation)datasource) { //Step FED.1: Evaluate the patterns of the current pattern group on the current store RDFQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step FED.2: Federate the patterns of the current pattern group on the current store if (!fedPatternResultTables.ContainsKey(patternGroup)) { fedPatternResultTables.Add(patternGroup, this.PatternResultTables[patternGroup]); } else { fedPatternResultTables[patternGroup].ForEach(fprt => fprt.Merge(this.PatternResultTables[patternGroup].Single(prt => prt.TableName.Equals(fprt.TableName, StringComparison.Ordinal)), true, MissingSchemaAction.Add)); } } this.PatternResultTables[patternGroup] = fedPatternResultTables[patternGroup]; #endregion } else { RDFQueryEngine.EvaluatePatterns(this, patternGroup, datasource); } //Step 2: Get the result table of the current pattern group RDFQueryEngine.CombinePatterns(this, patternGroup); //Step 3: Apply the filters of the current pattern group to its result table RDFQueryEngine.ApplyFilters(this, patternGroup); } //Step 4: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList(), false); //Step 5: Fill the templates from the result table DataTable filledResultTable = RDFQueryEngine.FillTemplates(this, queryResultTable); //Step 6: Apply the modifiers of the query to the result table constructResult.ConstructResults = RDFQueryEngine.ApplyModifiers(this, filledResultTable); } return(constructResult); }
/// <summary> /// Applies the query to the given store /// </summary> public RDFAskQueryResult ApplyToStore(RDFStore store) { if (store != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFAskQueryResult askResult = new RDFAskQueryResult(); if (!this.IsEmpty) { //Iterate the pattern groups of the query foreach (RDFPatternGroup patternGroup in this.PatternGroups) { //Step 1: Get the intermediate result tables of the current pattern group RDFAskQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step 2: Get the result table of the current pattern group RDFAskQueryEngine.CombinePatterns(this, patternGroup); //Step 3: Apply the filters of the current pattern group to its result table RDFAskQueryEngine.ApplyFilters(this, patternGroup); } //Step 4: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 5: Transform the result into a boolean response askResult.AskResult = (queryResultTable.Rows.Count > 0); } return(askResult); } throw new RDFQueryException("Cannot execute ASK query because given \"store\" parameter is null."); }
/// <summary> /// Helps in finalization step by updating the projection's result table /// </summary> internal override void UpdateProjectionTable(String partitionKey, DataTable projFuncTable) { //Get bindings from context Dictionary <String, String> bindings = new Dictionary <String, String>(); foreach (String pkValue in partitionKey.Split(new String[] { "§PK§" }, StringSplitOptions.RemoveEmptyEntries)) { String[] pValues = pkValue.Split(new String[] { "§PV§" }, StringSplitOptions.None); bindings.Add(pValues[0], pValues[1]); } //Add aggregator value to bindings Double aggregatorValue = this.AggregatorContext.GetPartitionKeyExecutionResult <Double>(partitionKey, 0d); if (aggregatorValue.Equals(Double.NaN)) { bindings.Add(this.ProjectionVariable.VariableName, String.Empty); } else { bindings.Add(this.ProjectionVariable.VariableName, aggregatorValue.ToString()); } //Add bindings to result's table RDFQueryEngine.AddRow(projFuncTable, bindings); }
/// <summary> /// Applies the query to the given store /// </summary> public RDFSelectQueryResult ApplyToStore(RDFStore store) { if (store != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFSelectQueryResult selectResult = new RDFSelectQueryResult(this.ToString()); if (!this.IsEmpty) { //Iterate the pattern groups of the query foreach (RDFPatternGroup patternGroup in this.PatternGroups) { //Step 1: Get the intermediate result tables of the current pattern group RDFSelectQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step 2: Get the result table of the current pattern group RDFSelectQueryEngine.CombinePatterns(this, patternGroup); //Step 3: Apply the filters of the current pattern group to its result table RDFSelectQueryEngine.ApplyFilters(this, patternGroup); } //Step 4: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 5: Apply the modifiers of the query to the result table selectResult.SelectResults = RDFSelectQueryEngine.ApplyModifiers(this, queryResultTable); } return(selectResult); } throw new RDFQueryException("Cannot execute SELECT query because given \"store\" parameter is null."); }
/// <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; } } }
/// <summary> /// Applies the query to the given federation /// </summary> public RDFConstructQueryResult ApplyToFederation(RDFFederation federation) { if (federation != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString()); if (!this.IsEmpty) { //Iterate the pattern groups of the query var fedPatternResultTables = new Dictionary <RDFPatternGroup, List <DataTable> >(); foreach (RDFPatternGroup patternGroup in this.PatternGroups) { #region TrueFederations foreach (RDFStore store in federation.Stores.Values) { //Step 1: Evaluate the patterns of the current pattern group on the current store RDFConstructQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step 2: Federate the patterns of the current pattern group on the current store if (!fedPatternResultTables.ContainsKey(patternGroup)) { fedPatternResultTables.Add(patternGroup, this.PatternResultTables[patternGroup]); } else { fedPatternResultTables[patternGroup].ForEach(fprt => fprt.Merge(this.PatternResultTables[patternGroup].Single(prt => prt.TableName.Equals(fprt.TableName, StringComparison.Ordinal)), true, MissingSchemaAction.Add)); } } this.PatternResultTables[patternGroup] = fedPatternResultTables[patternGroup]; #endregion //Step 3: Get the result table of the current pattern group RDFConstructQueryEngine.CombinePatterns(this, patternGroup); //Step 4: Apply the filters of the current pattern group to its result table RDFConstructQueryEngine.ApplyFilters(this, patternGroup); } //Step 5: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 6: Fill the templates from the result table DataTable filledResultTable = RDFConstructQueryEngine.FillTemplates(this, queryResultTable); //Step 7: Apply the modifiers of the query to the result table constructResult.ConstructResults = RDFConstructQueryEngine.ApplyModifiers(this, filledResultTable); } return(constructResult); } throw new RDFQueryException("Cannot execute CONSTRUCT query because given \"federation\" parameter is null."); }
/// <summary> /// Executes projection algorythm /// </summary> private DataTable ExecuteProjectionAlgorythm() { List <DataTable> projFuncTables = new List <DataTable>(); this.Aggregators.ForEach(ag => projFuncTables.Add(ag.ExecuteProjection(this.PartitionVariables))); DataTable resultTable = new RDFQueryEngine().CombineTables(projFuncTables, false); return(resultTable); }
/// <summary> /// Applies the query to the given store /// </summary> public RDFSelectQueryResult ApplyToStore(RDFStore store) { if (store != null) { return(RDFQueryEngine.CreateNew().EvaluateSelectQuery(this, store)); } else { return(new RDFSelectQueryResult()); } }
/// <summary> /// Applies the query to the given graph /// </summary> public RDFSelectQueryResult ApplyToGraph(RDFGraph graph) { if (graph != null) { return(RDFQueryEngine.CreateNew().EvaluateSelectQuery(this, graph)); } else { return(new RDFSelectQueryResult()); } }
/// <summary> /// Applies the query to the given federation /// </summary> public RDFConstructQueryResult ApplyToFederation(RDFFederation federation) { if (federation != null) { return(RDFQueryEngine.CreateNew().EvaluateConstructQuery(this, federation)); } else { return(new RDFConstructQueryResult(this.ToString())); } }
/// <summary> /// Applies the query to the given store /// </summary> public RDFConstructQueryResult ApplyToStore(RDFStore store) { if (store != null) { return(RDFQueryEngine.CreateNew().EvaluateConstructQuery(this, store)); } else { return(new RDFConstructQueryResult(this.ToString())); } }
/// <summary> /// Applies the query to the given graph /// </summary> public RDFConstructQueryResult ApplyToGraph(RDFGraph graph) { if (graph != null) { return(RDFQueryEngine.CreateNew().EvaluateConstructQuery(this, graph)); } else { return(new RDFConstructQueryResult(this.ToString())); } }
/// <summary> /// Applies the query to the given federation /// </summary> public RDFSelectQueryResult ApplyToFederation(RDFFederation federation) { if (federation != null) { return(RDFQueryEngine.CreateNew().EvaluateSelectQuery(this, federation)); } else { return(new RDFSelectQueryResult()); } }
/// <summary> /// Applies the query to the given federation /// </summary> public RDFAskQueryResult ApplyToFederation(RDFFederation federation) { if (federation != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFAskQueryResult askResult = new RDFAskQueryResult(); if (!this.IsEmpty) { //Iterate the pattern groups of the query var fedPatternResultTables = new Dictionary <RDFPatternGroup, List <DataTable> >(); foreach (RDFPatternGroup patternGroup in this.PatternGroups) { #region TrueFederations foreach (RDFStore store in federation.Stores.Values) { //Step 1: Evaluate the patterns of the current pattern group on the current store RDFAskQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step 2: Federate the patterns of the current pattern group on the current store if (!fedPatternResultTables.ContainsKey(patternGroup)) { fedPatternResultTables.Add(patternGroup, this.PatternResultTables[patternGroup]); } else { fedPatternResultTables[patternGroup].ForEach(fprt => fprt.Merge(this.PatternResultTables[patternGroup].Single(prt => prt.TableName.Equals(fprt.TableName, StringComparison.Ordinal)), true, MissingSchemaAction.Add)); } } this.PatternResultTables[patternGroup] = fedPatternResultTables[patternGroup]; #endregion //Step 3: Get the result table of the current pattern group RDFAskQueryEngine.CombinePatterns(this, patternGroup); //Step 4: Apply the filters of the current pattern group to its result table RDFAskQueryEngine.ApplyFilters(this, patternGroup); } //Step 5: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 6: Transform the result into a boolean response askResult.AskResult = (queryResultTable.Rows.Count > 0); } return(askResult); } throw new RDFQueryException("Cannot execute ASK query because given \"federation\" parameter is null."); }
/// <summary> /// Applies the query to the given store /// </summary> public RDFDescribeQueryResult ApplyToStore(RDFStore store) { if (store != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFDescribeQueryResult describeResult = new RDFDescribeQueryResult(this.ToString()); if (!this.IsEmpty) { //Iterate the pattern groups of the query foreach (RDFPatternGroup patternGroup in this.PatternGroups) { //Step 1: Get the intermediate result tables of the current pattern group RDFDescribeQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step 2: Get the result table of the current pattern group RDFDescribeQueryEngine.CombinePatterns(this, patternGroup); //Step 3: Apply the filters of the current pattern group to its result table RDFDescribeQueryEngine.ApplyFilters(this, patternGroup); } //Step 4: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 5: Describe the terms from the result table DataTable describeResultTable = RDFDescribeQueryEngine.DescribeTerms(this, store, queryResultTable); //Step 6: Apply the modifiers of the query to the result table describeResult.DescribeResults = RDFDescribeQueryEngine.ApplyModifiers(this, describeResultTable); } else { //In this case the only chance to proceed is to have resources in the describe terms, //which will be used to search for S-P-O data. Variables are omitted in this scenario. if (this.DescribeTerms.Any(dt => dt is RDFResource)) { //Step 1: Describe the terms from the result table DataTable describeResultTable = RDFDescribeQueryEngine.DescribeTerms(this, store, new DataTable()); //Step 2: Apply the modifiers of the query to the result table describeResult.DescribeResults = RDFDescribeQueryEngine.ApplyModifiers(this, describeResultTable); } } return(describeResult); } throw new RDFQueryException("Cannot execute DESCRIBE query because given \"store\" parameter is null."); }
/// <summary> /// Executes the projection producing result's table /// </summary> internal override DataTable ExecuteProjection(List <RDFVariable> partitionVariables) { DataTable projFuncTable = new DataTable(); //Initialization partitionVariables.ForEach(pv => RDFQueryEngine.AddColumn(projFuncTable, pv.VariableName)); RDFQueryEngine.AddColumn(projFuncTable, this.ProjectionVariable.VariableName); //Finalization foreach (string partitionKey in this.AggregatorContext.ExecutionRegistry.Keys) { //Update result's table this.UpdateProjectionTable(partitionKey, projFuncTable); } return(projFuncTable); }
/// <summary> /// Helps in finalization step by updating the projection's result table /// </summary> internal override void UpdateProjectionTable(string partitionKey, DataTable projFuncTable) { //Get bindings from context Dictionary <string, string> bindings = new Dictionary <string, string>(); foreach (string pkValue in partitionKey.Split(new string[] { "§PK§" }, StringSplitOptions.RemoveEmptyEntries)) { string[] pValues = pkValue.Split(new string[] { "§PV§" }, StringSplitOptions.None); bindings.Add(pValues[0], pValues[1]); } //Add aggregator value to bindings double aggregatorValue = this.AggregatorContext.GetPartitionKeyExecutionResult <double>(partitionKey, 0d); bindings.Add(this.ProjectionVariable.VariableName, new RDFTypedLiteral(Convert.ToString(aggregatorValue, CultureInfo.InvariantCulture), RDFModelEnums.RDFDatatypes.XSD_DECIMAL).ToString()); //Add bindings to result's table RDFQueryEngine.AddRow(projFuncTable, bindings); }
/// <summary> /// Helps in finalization step by updating the projection's result table /// </summary> internal override void UpdateProjectionTable(string partitionKey, DataTable projFuncTable) { //Get bindings from context Dictionary <string, string> bindings = new Dictionary <string, string>(); foreach (string pkValue in partitionKey.Split(new string[] { "§PK§" }, StringSplitOptions.RemoveEmptyEntries)) { string[] pValues = pkValue.Split(new string[] { "§PV§" }, StringSplitOptions.None); bindings.Add(pValues[0], pValues[1]); } //Add aggregator value to bindings string aggregatorValue = this.AggregatorContext.GetPartitionKeyExecutionResult <string>(partitionKey, string.Empty); bindings.Add(this.ProjectionVariable.VariableName, aggregatorValue.TrimEnd(this.Separator)); //Add bindings to result's table RDFQueryEngine.AddRow(projFuncTable, bindings); }
/// <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); } }
/// <summary> /// Applies the modifier on the given datatable /// </summary> internal override DataTable ApplyModifier(DataTable table) { //Perform consistency checks ConsistencyChecks(table); //Execute partition algorythm foreach (DataRow tableRow in table.Rows) { this.Aggregators.ForEach(ag => ag.ExecutePartition(GetPartitionKey(tableRow), tableRow)); } //Execute projection algorythm List <DataTable> projFuncTables = new List <DataTable>(); this.Aggregators.ForEach(ag => projFuncTables.Add(ag.ExecuteProjection(this.PartitionVariables))); //Produce result's table return(RDFQueryEngine.CreateNew().CombineTables(projFuncTables, false)); }
/// <summary> /// Gets the datatable representing the SPARQL values /// </summary> internal DataTable GetDataTable() { DataTable result = new DataTable(); //Create the columns of the SPARQL values this.Bindings.ToList() .ForEach(b => RDFQueryEngine.AddColumn(result, b.Key)); result.AcceptChanges(); //Create the rows of the SPARQL values result.BeginLoadData(); for (int i = 0; i < this.MaxBindingsLength; i++) { Dictionary <String, String> bindings = new Dictionary <String, String>(); this.Bindings.ToList() .ForEach(b => bindings.Add(b.Key, b.Value.ElementAtOrDefault(i)?.ToString())); RDFQueryEngine.AddRow(result, bindings); } result.EndLoadData(); return(result); }
/// <summary> /// Applies the query to the given graph /// </summary> public RDFConstructQueryResult ApplyToGraph(RDFGraph graph) { if (graph != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString()); if (!this.IsEmpty) { //Iterate the pattern groups of the query foreach (RDFPatternGroup patternGroup in this.PatternGroups) { //Step 1: Get the intermediate result tables of the current pattern group RDFConstructQueryEngine.EvaluatePatterns(this, patternGroup, graph); //Step 2: Get the result table of the current pattern group RDFConstructQueryEngine.CombinePatterns(this, patternGroup); //Step 3: Apply the filters of the current pattern group to its result table RDFConstructQueryEngine.ApplyFilters(this, patternGroup); } //Step 4: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 5: Fill the templates from the result table DataTable filledResultTable = RDFConstructQueryEngine.FillTemplates(this, queryResultTable); //Step 6: Apply the modifiers of the query to the result table constructResult.ConstructResults = RDFConstructQueryEngine.ApplyModifiers(this, filledResultTable); } return(constructResult); } throw new RDFQueryException("Cannot execute CONSTRUCT query because given \"graph\" parameter is null."); }
/// <summary> /// Applies the query to the given datasource /// </summary> internal RDFDescribeQueryResult ApplyToDataSource(RDFDataSource datasource) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFDescribeQueryResult describeResult = new RDFDescribeQueryResult(this.ToString()); if (this.PatternGroups.Any()) { //Iterate the pattern groups of the query var fedPatternResultTables = new Dictionary <RDFPatternGroup, List <DataTable> >(); foreach (var patternGroup in this.PatternGroups) { //Step 1: Get the intermediate result tables of the current pattern group if (datasource.IsFederation()) { #region TrueFederations foreach (var store in (RDFFederation)datasource) { //Step FED.1: Evaluate the patterns of the current pattern group on the current store RDFQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step FED.2: Federate the patterns of the current pattern group on the current store if (!fedPatternResultTables.ContainsKey(patternGroup)) { fedPatternResultTables.Add(patternGroup, this.PatternResultTables[patternGroup]); } else { fedPatternResultTables[patternGroup].ForEach(fprt => fprt.Merge(this.PatternResultTables[patternGroup].Single(prt => prt.TableName.Equals(fprt.TableName, StringComparison.Ordinal)), true, MissingSchemaAction.Add)); } } this.PatternResultTables[patternGroup] = fedPatternResultTables[patternGroup]; #endregion } else { RDFQueryEngine.EvaluatePatterns(this, patternGroup, datasource); } //Step 2: Get the result table of the current pattern group RDFQueryEngine.CombinePatterns(this, patternGroup); //Step 3: Apply the filters of the current pattern group to its result table RDFQueryEngine.ApplyFilters(this, patternGroup); } //Step 4: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList(), false); //Step 5: Describe the terms from the result table DataTable describeResultTable = new DataTable(this.ToString()); if (datasource.IsFederation()) { #region TrueFederations foreach (var store in (RDFFederation)datasource) { describeResultTable.Merge(RDFQueryEngine.DescribeTerms(this, store, queryResultTable), true, MissingSchemaAction.Add); } #endregion } else { describeResultTable = RDFQueryEngine.DescribeTerms(this, datasource, queryResultTable); } //Step 6: Apply the modifiers of the query to the result table describeResult.DescribeResults = RDFQueryEngine.ApplyModifiers(this, describeResultTable); } else { //In this case the only chance to proceed is to have resources in the describe terms, //which will be used to search for S-P-O data. Variables are ignored in this scenario. if (this.DescribeTerms.Any(dt => dt is RDFResource)) { //Step 1: Describe the terms from the result table DataTable describeResultTable = new DataTable(this.ToString()); if (datasource.IsFederation()) { #region TrueFederations foreach (var store in (RDFFederation)datasource) { describeResultTable.Merge(RDFQueryEngine.DescribeTerms(this, store, new DataTable()), true, MissingSchemaAction.Add); } #endregion } else { describeResultTable = RDFQueryEngine.DescribeTerms(this, datasource, new DataTable()); } //Step 2: Apply the modifiers of the query to the result table describeResult.DescribeResults = RDFQueryEngine.ApplyModifiers(this, describeResultTable); } } return(describeResult); }
/// <summary> /// Reads the given "SPARQL Query Results XML Format" stream into a SELECT query result /// </summary> public static RDFSelectQueryResult FromSparqlXmlResult(Stream inputStream) { try { #region deserialize RDFSelectQueryResult result = new RDFSelectQueryResult(); using (StreamReader streamReader = new StreamReader(inputStream, Encoding.UTF8)) { using (XmlTextReader xmlReader = new XmlTextReader(streamReader)) { xmlReader.DtdProcessing = DtdProcessing.Parse; xmlReader.Normalization = false; #region document XmlDocument srxDoc = new XmlDocument(); srxDoc.Load(xmlReader); #endregion #region results Boolean foundHead = false; Boolean foundResults = false; var nodesEnum = srxDoc.DocumentElement.ChildNodes.GetEnumerator(); while (nodesEnum != null && nodesEnum.MoveNext()) { XmlNode node = (XmlNode)nodesEnum.Current; #region HEAD if (node.Name.ToUpperInvariant().Equals("HEAD", StringComparison.Ordinal)) { foundHead = true; if (node.HasChildNodes) { var variablesEnum = node.ChildNodes.GetEnumerator(); while (variablesEnum != null && variablesEnum.MoveNext()) { #region VARIABLE XmlNode varNode = (XmlNode)variablesEnum.Current; if (varNode.Name.ToUpperInvariant().Equals("VARIABLE", StringComparison.Ordinal)) { if (varNode.Attributes.Count > 0) { XmlAttribute varAttr = varNode.Attributes["name"]; if (varAttr != null && varAttr.Value != String.Empty) { RDFQueryEngine.AddColumn(result.SelectResults, varAttr.Value); } else { throw new RDFModelException("one \"variable\" node was found without, or with empty, \"name\" attribute."); } } else { throw new RDFModelException("one \"variable\" node was found without attributes."); } } #endregion } } else { throw new RDFModelException("\"head\" node was found without children."); } } #endregion #region RESULTS else if (node.Name.ToUpperInvariant().Equals("RESULTS", StringComparison.Ordinal)) { foundResults = true; if (foundHead) { var resultsEnum = node.ChildNodes.GetEnumerator(); while (resultsEnum != null && resultsEnum.MoveNext()) { XmlNode resNode = (XmlNode)resultsEnum.Current; #region RESULT if (resNode.Name.ToUpperInvariant().Equals("RESULT", StringComparison.Ordinal)) { if (resNode.HasChildNodes) { var results = new Dictionary <String, String>(); var bdgEnum = resNode.ChildNodes.GetEnumerator(); while (bdgEnum != null && bdgEnum.MoveNext()) { XmlNode bdgNode = (XmlNode)bdgEnum.Current; Boolean foundUri = false; Boolean foundLit = false; #region BINDING if (bdgNode.Name.ToUpperInvariant().Equals("BINDING", StringComparison.Ordinal)) { if (bdgNode.Attributes != null && bdgNode.Attributes.Count > 0) { XmlAttribute varAttr = bdgNode.Attributes["name"]; if (varAttr != null && varAttr.Value != String.Empty) { if (bdgNode.HasChildNodes) { #region URI / BNODE if (bdgNode.FirstChild.Name.ToUpperInvariant().Equals("URI", StringComparison.Ordinal) || bdgNode.FirstChild.Name.ToUpperInvariant().Equals("BNODE", StringComparison.Ordinal)) { foundUri = true; if (RDFModelUtilities.GetUriFromString(bdgNode.InnerText) != null) { results.Add(varAttr.Value, bdgNode.InnerText); } else { throw new RDFModelException("one \"uri\" node contained data not corresponding to a valid Uri."); } } #endregion #region LITERAL else if (bdgNode.FirstChild.Name.ToUpperInvariant().Equals("LITERAL", StringComparison.Ordinal)) { foundLit = true; if (bdgNode.FirstChild.Attributes != null && bdgNode.FirstChild.Attributes.Count > 0) { XmlAttribute litAttr = bdgNode.FirstChild.Attributes["datatype"]; if (litAttr != null && litAttr.Value != String.Empty) { results.Add(varAttr.Value, bdgNode.FirstChild.InnerText + "^^" + litAttr.Value); } else { litAttr = bdgNode.FirstChild.Attributes[RDFVocabulary.XML.PREFIX + ":lang"]; if (litAttr != null && litAttr.Value != String.Empty) { results.Add(varAttr.Value, bdgNode.FirstChild.InnerText + "@" + litAttr.Value); } else { throw new RDFModelException("one \"literal\" node was found with attribute different from \"datatype\" or \"xml:lang\"."); } } } else { results.Add(varAttr.Value, bdgNode.InnerText); } } #endregion } else { throw new RDFQueryException("one \"binding\" node was found without children."); } } else { throw new RDFQueryException("one \"binding\" node was found without, or with empty, \"name\" attribute."); } } else { throw new RDFQueryException("one \"binding\" node was found without attributes."); } } #endregion if (!foundUri && !foundLit) { throw new RDFQueryException("one \"binding\" node was found without mandatory child \"uri\" or \"literal\"."); } } RDFQueryEngine.AddRow(result.SelectResults, results); } } #endregion } } else { throw new RDFModelException("\"head\" node was not found, or was after \"results\" node."); } } #endregion } if (!foundHead) { throw new RDFStoreException("mandatory \"head\" node was not found"); } if (!foundResults) { throw new RDFStoreException("mandatory \"results\" node was not found"); } #endregion } } return(result); #endregion } catch (Exception ex) { throw new RDFQueryException("Cannot read given \"SPARQL Query Results XML Format\" source because: " + ex.Message, ex); } }
/// <summary> /// Applies the query to the given federation /// </summary> public RDFDescribeQueryResult ApplyToFederation(RDFFederation federation) { if (federation != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFDescribeQueryResult describeResult = new RDFDescribeQueryResult(this.ToString()); if (!this.IsEmpty) { //Iterate the pattern groups of the query var fedPatternResultTables = new Dictionary <RDFPatternGroup, List <DataTable> >(); foreach (RDFPatternGroup patternGroup in this.PatternGroups) { #region TrueFederations foreach (RDFStore store in federation.Stores.Values) { //Step 1: Evaluate the patterns of the current pattern group on the current store RDFDescribeQueryEngine.EvaluatePatterns(this, patternGroup, store); //Step 2: Federate the patterns of the current pattern group on the current store if (!fedPatternResultTables.ContainsKey(patternGroup)) { fedPatternResultTables.Add(patternGroup, this.PatternResultTables[patternGroup]); } else { fedPatternResultTables[patternGroup].ForEach(fprt => fprt.Merge(this.PatternResultTables[patternGroup].Single(prt => prt.TableName.Equals(fprt.TableName, StringComparison.Ordinal)), true, MissingSchemaAction.Add)); } } this.PatternResultTables[patternGroup] = fedPatternResultTables[patternGroup]; #endregion //Step 3: Get the result table of the current pattern group RDFDescribeQueryEngine.CombinePatterns(this, patternGroup); //Step 4: Apply the filters of the current pattern group to its result table RDFDescribeQueryEngine.ApplyFilters(this, patternGroup); } //Step 5: Get the result table of the query DataTable queryResultTable = RDFQueryEngine.CombineTables(this.PatternGroupResultTables.Values.ToList <DataTable>(), false); //Step 6: Describe the terms on each store and merge them in the federated result table DataTable describeResultTable = new DataTable(this.ToString()); foreach (RDFStore store in federation.Stores.Values) { describeResultTable.Merge(RDFDescribeQueryEngine.DescribeTerms(this, store, queryResultTable), true, MissingSchemaAction.Add); } //Step 7: Apply the modifiers of the query to the result table describeResult.DescribeResults = RDFDescribeQueryEngine.ApplyModifiers(this, describeResultTable); } else { //In this case the only chance to proceed is to have resources in the describe terms, //which will be used to search for S-P-O data. Variables are ignored in this scenario. if (this.DescribeTerms.Any(dt => dt is RDFResource)) { //Step 1: Describe the terms on each store and merge them in the federated result table DataTable describeResultTable = new DataTable(this.ToString()); foreach (RDFStore store in federation.Stores.Values) { describeResultTable.Merge(RDFDescribeQueryEngine.DescribeTerms(this, store, new DataTable()), true, MissingSchemaAction.Add); } //Step 2: Apply the modifiers of the query to the result table describeResult.DescribeResults = RDFDescribeQueryEngine.ApplyModifiers(this, describeResultTable); } } return(describeResult); } throw new RDFQueryException("Cannot execute DESCRIBE query because given \"federation\" parameter is null."); }