/// <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> /// 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> /// Adds the given store to the federation /// </summary> public RDFFederation AddStore(RDFStore store) { if (store != null) { this.DataSources.Add(store); } return(this); }
/// <summary> /// Applies the query to the given store /// </summary> public RDFAskQueryResult ApplyToStore(RDFStore store) { if (store != null) { return(this.ApplyToDataSource(store)); } throw new RDFQueryException("Cannot execute ASK query because given \"store\" parameter is null."); }
/// <summary> /// Applies the query to the given store /// </summary> public RDFDescribeQueryResult ApplyToStore(RDFStore store) { if (store != null) { return(new RDFQueryEngine().EvaluateDescribeQuery(this, store)); } else { return(new RDFDescribeQueryResult(this.ToString())); } }
/// <summary> /// Applies the query to the given store /// </summary> public RDFAskQueryResult ApplyToStore(RDFStore store) { if (store != null) { return(new RDFQueryEngine().EvaluateAskQuery(this, store)); } else { return(new RDFAskQueryResult()); } }
/// <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 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 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> /// Writes the given store to the given file in the given RDF format. /// </summary> public static void WriteRDF(RDFStoreEnums.RDFFormats rdfFormat, RDFStore store, String filepath) { if (store != null) { if (filepath != null) { switch (rdfFormat) { case RDFStoreEnums.RDFFormats.NQuads: RDFNQuads.Serialize(store, filepath); break; } } else { throw new RDFStoreException("Cannot write RDF file because given \"filepath\" parameter is null."); } } else { throw new RDFStoreException("Cannot write RDF file because given \"store\" parameter is null."); } }
/// <summary> /// Applies the query to the given store /// </summary> public RDFConstructQueryResult ApplyToStore(RDFStore store) => store != null ? new RDFQueryEngine().EvaluateConstructQuery(this, store) : new RDFConstructQueryResult(this.ToString());
/// <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> /// Serializes the given store to the given filepath using N-Quads data format. /// </summary> internal static void Serialize(RDFStore store, String filepath) { try { #region serialize using (StreamWriter sw = new StreamWriter(filepath, false, Encoding.ASCII)) { String quadrupleTemplate = String.Empty; foreach (RDFQuadruple q in store.SelectAllQuadruples()) { #region template if (q.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) { quadrupleTemplate = "<{SUBJ}> <{PRED}> <{OBJ}> <{CTX}> ."; } else { if (q.Object is RDFPlainLiteral) { quadrupleTemplate = "<{SUBJ}> <{PRED}> \"{VAL}\"@{LANG} <{CTX}> ."; } else { quadrupleTemplate = "<{SUBJ}> <{PRED}> \"{VAL}\"^^<{DTYPE}> <{CTX}> ."; } } #endregion #region subj if (((RDFResource)q.Subject).IsBlank) { quadrupleTemplate = quadrupleTemplate.Replace("<{SUBJ}>", RDFModelUtilities.Unicode_To_ASCII(q.Subject.ToString()).Replace("bnode:", "_:")); } else { quadrupleTemplate = quadrupleTemplate.Replace("{SUBJ}", RDFModelUtilities.Unicode_To_ASCII(q.Subject.ToString())); } #endregion #region pred quadrupleTemplate = quadrupleTemplate.Replace("{PRED}", RDFModelUtilities.Unicode_To_ASCII(q.Predicate.ToString())); #endregion #region object if (q.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) { if (((RDFResource)q.Object).IsBlank) { quadrupleTemplate = quadrupleTemplate.Replace("<{OBJ}>", RDFModelUtilities.Unicode_To_ASCII(q.Object.ToString())).Replace("bnode:", "_:"); } else { quadrupleTemplate = quadrupleTemplate.Replace("{OBJ}", RDFModelUtilities.Unicode_To_ASCII(q.Object.ToString())); } } #endregion #region literal else { quadrupleTemplate = quadrupleTemplate.Replace("{VAL}", RDFModelUtilities.Unicode_To_ASCII(((RDFLiteral)q.Object).Value.Replace("\"", "\\\""))); quadrupleTemplate = quadrupleTemplate.Replace("\n", "\\n") .Replace("\t", "\\t") .Replace("\r", "\\r"); #region plain literal if (q.Object is RDFPlainLiteral) { if (((RDFPlainLiteral)q.Object).Language != String.Empty) { quadrupleTemplate = quadrupleTemplate.Replace("{LANG}", ((RDFPlainLiteral)q.Object).Language); } else { quadrupleTemplate = quadrupleTemplate.Replace("@{LANG}", String.Empty); } } #endregion #region typed literal else { quadrupleTemplate = quadrupleTemplate.Replace("{DTYPE}", ((RDFTypedLiteral)q.Object).Datatype.ToString()); } #endregion } #endregion #region context quadrupleTemplate = quadrupleTemplate.Replace("{CTX}", RDFModelUtilities.Unicode_To_ASCII(q.Context.ToString())); #endregion sw.WriteLine(quadrupleTemplate); } } #endregion } catch (Exception ex) { throw new RDFStoreException("Cannot serialize N-Quads because: " + ex.Message, ex); } }
/// <summary> /// Asynchronously applies the query to the given store /// </summary> public Task <RDFConstructQueryResult> ApplyToStoreAsync(RDFStore store) => Task.Run(() => this.ApplyToStore(store));
/// <summary> /// Serializes the given store to the given stream using TriX data format. /// </summary> internal static void Serialize(RDFStore store, Stream outputStream) { try { #region serialize using (XmlTextWriter trixWriter = new XmlTextWriter(outputStream, Encoding.UTF8)) { XmlDocument trixDoc = new XmlDocument(); trixWriter.Formatting = Formatting.Indented; #region xmlDecl XmlDeclaration trixDecl = trixDoc.CreateXmlDeclaration("1.0", "UTF-8", null); trixDoc.AppendChild(trixDecl); #endregion #region trixRoot XmlNode trixRoot = trixDoc.CreateNode(XmlNodeType.Element, "TriX", null); XmlAttribute trixRootNS = trixDoc.CreateAttribute("xmlns"); XmlText trixRootNSText = trixDoc.CreateTextNode("http://www.w3.org/2004/03/trix/trix-1/"); trixRootNS.AppendChild(trixRootNSText); trixRoot.Attributes.Append(trixRootNS); #region graphs foreach (var graph in store.ExtractGraphs()) { XmlNode graphElement = trixDoc.CreateNode(XmlNodeType.Element, "graph", null); XmlNode graphUriElement = trixDoc.CreateNode(XmlNodeType.Element, "uri", null); XmlText graphUriElementT = trixDoc.CreateTextNode(graph.ToString()); graphUriElement.AppendChild(graphUriElementT); graphElement.AppendChild(graphUriElement); #region triple foreach(var t in graph) { XmlNode tripleElement = trixDoc.CreateNode(XmlNodeType.Element, "triple", null); #region subj XmlNode subjElement = (((RDFResource)t.Subject).IsBlank ? trixDoc.CreateNode(XmlNodeType.Element, "id", null) : trixDoc.CreateNode(XmlNodeType.Element, "uri", null)); XmlText subjElementText = trixDoc.CreateTextNode(t.Subject.ToString()); subjElement.AppendChild(subjElementText); tripleElement.AppendChild(subjElement); #endregion #region pred XmlNode uriElementP = trixDoc.CreateNode(XmlNodeType.Element, "uri", null); XmlText uriTextP = trixDoc.CreateTextNode(t.Predicate.ToString()); uriElementP.AppendChild(uriTextP); tripleElement.AppendChild(uriElementP); #endregion #region object if (t.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPO) { XmlNode objElement = (((RDFResource)t.Object).IsBlank ? trixDoc.CreateNode(XmlNodeType.Element, "id", null) : trixDoc.CreateNode(XmlNodeType.Element, "uri", null)); XmlText objElementText = trixDoc.CreateTextNode(t.Object.ToString()); objElement.AppendChild(objElementText); tripleElement.AppendChild(objElement); } #endregion #region literal else { #region plain literal if (t.Object is RDFPlainLiteral) { XmlNode plainLiteralElement = trixDoc.CreateNode(XmlNodeType.Element, "plainLiteral", null); if (((RDFPlainLiteral)t.Object).Language != String.Empty) { XmlAttribute xmlLang = trixDoc.CreateAttribute(RDFVocabulary.XML.PREFIX + ":lang", RDFVocabulary.XML.BASE_URI); XmlText xmlLangText = trixDoc.CreateTextNode(((RDFPlainLiteral)t.Object).Language); xmlLang.AppendChild(xmlLangText); plainLiteralElement.Attributes.Append(xmlLang); } XmlText plainLiteralText = trixDoc.CreateTextNode(RDFModelUtilities.EscapeControlCharsForXML(HttpUtility.HtmlDecode(((RDFLiteral)t.Object).Value))); plainLiteralElement.AppendChild(plainLiteralText); tripleElement.AppendChild(plainLiteralElement); } #endregion #region typed literal else { XmlNode typedLiteralElement = trixDoc.CreateNode(XmlNodeType.Element, "typedLiteral", null); XmlAttribute datatype = trixDoc.CreateAttribute("datatype"); XmlText datatypeText = trixDoc.CreateTextNode(RDFModelUtilities.GetDatatypeFromEnum(((RDFTypedLiteral)t.Object).Datatype)); datatype.AppendChild(datatypeText); typedLiteralElement.Attributes.Append(datatype); XmlText typedLiteralText = trixDoc.CreateTextNode(RDFModelUtilities.EscapeControlCharsForXML(HttpUtility.HtmlDecode(((RDFLiteral)t.Object).Value))); typedLiteralElement.AppendChild(typedLiteralText); tripleElement.AppendChild(typedLiteralElement); } #endregion } #endregion graphElement.AppendChild(tripleElement); } #endregion trixRoot.AppendChild(graphElement); } #endregion trixDoc.AppendChild(trixRoot); #endregion trixDoc.Save(trixWriter); } #endregion } catch(Exception ex) { throw new RDFStoreException("Cannot serialize TriX because: " + ex.Message, ex); } }
/// <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> /// Applies the query to the given store /// </summary> public RDFDescribeQueryResult ApplyToStore(RDFStore store) => store != null ? new RDFQueryEngine().EvaluateDescribeQuery(this, store) : new RDFDescribeQueryResult(this.ToString());
/// <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> /// Applies the query to the given store /// </summary> public RDFConstructQueryResult ApplyToStore(RDFStore store) { if (store != null) { this.PatternGroupResultTables.Clear(); this.PatternResultTables.Clear(); RDFConstructQueryResult constructResult = new RDFConstructQueryResult(this.ToString()); if (this.PatternGroups.Any()) { //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 RDFQueryEngine.EvaluatePatterns(this, patternGroup, store); //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; } throw new RDFQueryException("Cannot execute CONSTRUCT query because given \"store\" parameter is null."); }
/// <summary> /// Asynchronously applies the query to the given store /// </summary> public Task <RDFDescribeQueryResult> ApplyToStoreAsync(RDFStore store) => Task.Run(() => this.ApplyToStore(store));
/// <summary> /// Serializes the given store to the given filepath using TriX data format. /// </summary> internal static void Serialize(RDFStore store, String filepath) { Serialize(store, new FileStream(filepath, FileMode.Create)); }
/// <summary> /// Applies the query to the given store /// </summary> public RDFAskQueryResult ApplyToStore(RDFStore store) => store != null ? new RDFQueryEngine().EvaluateAskQuery(this, store) : new RDFAskQueryResult();