public void AskQueryResultFalseTest() { //Arrange var y = new RDFVariable("y"); var askQuery = new RDFAskQuery(); var patternGroup = new RDFPatternGroup("PatternGroup"); patternGroup.AddPattern(new RDFPattern(donaldduck, y, new RDFTypedLiteral("90", RDFModelEnums.RDFDatatypes.XSD_INTEGER))); askQuery.AddPatternGroup(patternGroup); //Act var result = askQuery.ApplyToGraph(graph); //Assert Assert.False(result.AskResult); }
public void SelectQueryResultTest() { //Arrange var x = new RDFVariable("x"); var q = new RDFSelectQuery(); var patternGroup = new RDFPatternGroup("PatternGroup"); patternGroup.AddPattern(new RDFPattern(x, RDFVocabulary.FOAF.AGE, new RDFTypedLiteral("85", RDFModelEnums.RDFDatatypes.XSD_INTEGER))); q.AddPatternGroup(patternGroup); //Act var selectResult = q.ApplyToGraph(graph); //Assert Assert.Equal(2, selectResult.SelectResultsCount); }
/// <summary> /// WORKING WITH SPARQL QUERIES /// </summary> private void WorkingWithQueries(RDFGraph graph, RDFResource donaldduck) { RDFSelectQuery selectQuery = new RDFSelectQuery(); // CREATE VARIABLE var x = new RDFVariable("x"); // ?X var y = new RDFVariable("y"); // ?Y var n = new RDFVariable("n"); // ?N var c = new RDFVariable("c"); // ?C // CREATE PATTERNS var dogOf = new RDFResource(RDFVocabulary.DC.BASE_URI + "dogOf"); var y_dogOf_x = new RDFPattern(y, dogOf, x); // TRIPLE PATTERN var c_y_dogOf_x = new RDFPattern(c, y, dogOf, x); // QUADRUPLE PATTERN // CREATE EMPTY PATTERN GROUP var pg1 = new RDFPatternGroup("PG1"); // CREATE PATTERN GROUP FROM A LIST OF PATTERNS var patterns = new List <RDFPattern>() { y_dogOf_x }; var pg2 = new RDFPatternGroup("PG2", patterns); // ADD PATTERNS TO PATTERN GROUP pg1.AddPattern(y_dogOf_x); pg1.AddPattern(c_y_dogOf_x); // ADD PATTERN GROUPS TO QUERY selectQuery.AddPatternGroup(pg1); selectQuery.AddPatternGroup(pg2); // ADD FILTERS TO PATTERN GROUP pg1.AddFilter(new RDFSameTermFilter(new RDFVariable("character"), donaldduck)); pg1.AddFilter(new RDFLangMatchesFilter(n, "it-IT")); // ADD MODIFIERS TO QUERY selectQuery.AddModifier(new RDFOrderByModifier(n, RDFQueryEnums.RDFOrderByFlavors.ASC)); selectQuery.AddModifier(new RDFDistinctModifier()); selectQuery.AddModifier(new RDFGroupByModifier(new List <RDFVariable> { x })); selectQuery.AddModifier(new RDFLimitModifier(100)); selectQuery.AddModifier(new RDFOffsetModifier(25)); // INITIALIZE PROPERTY PATH (VARIABLE TERMS) var variablePropPath = new RDFPropertyPath(new RDFVariable("START"), new RDFVariable("END")); // INITIALIZE PROPERTY PATH (MIXED TERMS) var mixedPropPath = new RDFPropertyPath(new RDFResource("http://res.org/"), new RDFVariable("END")); //ADD SEQUENCE STEPS TO PROPERTY PATH variablePropPath.AddSequenceStep(new RDFPropertyPathStep(new RDFResource("rdf:P1"))); variablePropPath.AddSequenceStep(new RDFPropertyPathStep(new RDFResource("rdf:P2"))); //ADD ALTERNATIVE STEPS TO PROPERTY PATH var altSteps = new List <RDFPropertyPathStep>(); altSteps.Add(new RDFPropertyPathStep(new RDFResource("rdf:P3"))); altSteps.Add(new RDFPropertyPathStep(new RDFResource("rdf:P7"))); variablePropPath.AddAlternativeSteps(altSteps); // ADD INVERSE SEQUENCE STEP TO PROPERTY PATH: ?START ^rdf:INVP ?END variablePropPath.AddSequenceStep(new RDFPropertyPathStep(new RDFResource("rdf:INVP")).Inverse()); //ADD ALTERNATIVE STEPS (ONE INVERSE) TO PROPERTY PATH: ?START (rdf:P3|^rdf:INVP3) ?END var altSteps2 = new List <RDFPropertyPathStep>(); altSteps2.Add(new RDFPropertyPathStep(new RDFResource("rdf:P3"))); altSteps2.Add(new RDFPropertyPathStep(new RDFResource("rdf:INVP3")).Inverse()); variablePropPath.AddAlternativeSteps(altSteps2); // ADD SUBQUERY TO QUERY RDFSelectQuery mySubQuery = new RDFSelectQuery(); selectQuery.AddSubQuery(mySubQuery); // ADD AGGREGATORS TO GROUPBY MODIFIER RDFGroupByModifier gm = new RDFGroupByModifier(new List <RDFVariable> { x }); gm.AddAggregator(new RDFAvgAggregator(new RDFVariable("age"), new RDFVariable("avg_age"))); gm.AddAggregator(new RDFCountAggregator(new RDFVariable("dept"), new RDFVariable("count_dept"))); gm.AddAggregator(new RDFGroupConcatAggregator(new RDFVariable("name"), new RDFVariable("gc_name"), "-")); gm.AddAggregator(new RDFSampleAggregator(new RDFVariable("name"), new RDFVariable("sample_name"))); gm.AddAggregator(new RDFSumAggregator(new RDFVariable("salary"), new RDFVariable("sum_salary"))); gm.AddAggregator(new RDFMinAggregator(new RDFVariable("age"), new RDFVariable("min_age"), RDFQueryEnums.RDFMinMaxAggregatorFlavors.Numeric)); //?age is expected to have numeric typedliterals gm.AddAggregator(new RDFMinAggregator(new RDFVariable("city"), new RDFVariable("min_city"), RDFQueryEnums.RDFMinMaxAggregatorFlavors.String)); gm.AddAggregator(new RDFMaxAggregator(new RDFVariable("salary"), new RDFVariable("max_salary"), RDFQueryEnums.RDFMinMaxAggregatorFlavors.Numeric)); //?salary is expected to have numeric typedliterals gm.AddAggregator(new RDFMaxAggregator(new RDFVariable("city"), new RDFVariable("min_city"), RDFQueryEnums.RDFMinMaxAggregatorFlavors.String)); // It is possible to filter a group - by partitioned set of SPARQL results by applying the SetHavingClause operator on desired aggregators: // ADD AGGREGATORS TO GROUPBY MODIFIER RDFModelEnums.RDFDatatypes xsdDbl = RDFModelEnums.RDFDatatypes.XSD_DOUBLE; RDFModelEnums.RDFDatatypes xsdInt = RDFModelEnums.RDFDatatypes.XSD_INT; gm.AddAggregator(new RDFAvgAggregator(new RDFVariable("age"), new RDFVariable("avg_age")) .SetHavingClause(RDFQueryEnums.RDFComparisonFlavors.GreaterThan, new RDFTypedLiteral("25.5", xsdDbl)) ); gm.AddAggregator(new RDFCountAggregator(new RDFVariable("dept"), new RDFVariable("count_dept")) .SetHavingClause(RDFQueryEnums.RDFComparisonFlavors.EqualTo, new RDFTypedLiteral("4", xsdInt)) ); //Declare the following SPARQL values: /* * VALUES (?a ?b ?c) { * ("1" "2" "3") * ("2" "4" "6") * ("3" "6" UNDEF) * } */ RDFValues myValues = new RDFValues() .AddColumn(new RDFVariable("a"), new List <RDFPatternMember>() { new RDFPlainLiteral("1"), new RDFPlainLiteral("2"), new RDFPlainLiteral("3") }) .AddColumn(new RDFVariable("b"), new List <RDFPatternMember>() { new RDFPlainLiteral("2"), new RDFPlainLiteral("4"), new RDFPlainLiteral("6") }) .AddColumn(new RDFVariable("c"), new List <RDFPatternMember>() { new RDFPlainLiteral("3"), new RDFPlainLiteral("6"), null //UNDEF }); // ADD PROPERTY PATH TO PATTERN GROUP pg1.AddValues(myValues); // CREATING AND EXECUTING SELECT QUERIES // APPLY SELECT QUERY TO GRAPH RDFSelectQueryResult selectQueryResult = selectQuery.ApplyToGraph(graph); // APPLY SELECT QUERY TO STORE //RDFSelectQueryResult selectQueryResult = selectQuery.ApplyToStore(store); // APPLY SELECT QUERY TO FEDERATION //RDFSelectQueryResult selectQueryResult = selectQuery.ApplyToFederation(federation); // EXPORT SELECT QUERY RESULTS TO SPARQL XML FORMAT (FILE) selectQueryResult.ToSparqlXmlResult(@"C:\TMP\select_results.srq"); // EXPORT SELECT QUERY RESULTS TO SPARQL XML FORMAT (STREAM) //selectQueryResult.ToSparqlXmlResult(myStream); // IMPORT SELECT QUERY RESULTS FROM SPARQL XML FORMAT (FILE) selectQueryResult = RDFSelectQueryResult.FromSparqlXmlResult(@"C:\TMP\select_results.srq"); // IMPORT SELECT QUERY RESULTS FROM SPARQL XML FORMAT (STREAM) //selectQueryResult = RDFSelectQueryResult.FromSparqlXmlResult(myStream); }
public void SelectDogsOrCats_UnionWithNext_Test() { // CREATE SELECT QUERY RDFSelectQuery selectQuery = new RDFSelectQuery(); selectQuery.AddPrefix(RDFNamespaceRegister.GetByPrefix("dc")); selectQuery.AddPrefix(RDFNamespaceRegister.GetByPrefix("foaf")); var x = new RDFVariable("x"); var y = new RDFVariable("y"); RDFPatternMember dogOfPredicate = GraphBuilder2.dogOf; RDFPatternMember catOfPredicate = GraphBuilder2.catOf; var x_dogOf_y = new RDFPattern(x, dogOfPredicate, y); var x_catOf_y = new RDFPattern(x, catOfPredicate, y); //var orFilter = new RDFBooleanOrFilter(x_dogOf_y, x_catOf_y); // CREATE PATTERN GROUP FROM A LIST OF PATTERNS var pg1 = new RDFPatternGroup("PG1"); pg1.AddPattern(x_dogOf_y.UnionWithNext()); pg1.AddPattern(x_catOf_y); selectQuery.AddPatternGroup(pg1); selectQuery.AddProjectionVariable(x); selectQuery.AddProjectionVariable(y); selectQuery.AddModifier(new RDFOrderByModifier(x, RDFQueryEnums.RDFOrderByFlavors.ASC)); var sparqlCommand = selectQuery.ToString(); /* * Generates this sparql command * * PREFIX dc: <http://purl.org/dc/elements/1.1/> * PREFIX foaf: <http://xmlns.com/foaf/0.1/> * * SELECT ?X ?Y * WHERE { * { * { ?X dc:dogOf ?Y } * UNION * { ?X dc:catOf ?Y } * } * } * ORDER BY ASC(?X) */ // APPLY SELECT QUERY TO GRAPH RDFSelectQueryResult selectQueryResult = selectQuery.ApplyToGraph(graph); // EXPORT SELECT QUERY RESULTS TO SPARQL XML FORMAT (FILE) selectQueryResult.ToSparqlXmlResult(@"C:\TEMP\select_results.srq"); Assert.Equal(2, selectQueryResult.SelectResultsCount); //DataRow row = selectQueryResult.SelectResults.Rows[0]; //var dog = row[0].ToString(); //var person = row[1].ToString(); //Assert.Equal(pluto.URI.ToString(), dog); //Assert.Equal(mickeyMouse.URI.ToString(), person); /* * Generates this file content * * <?xml version="1.0" encoding="utf-8"?> * <sparql xmlns="http://www.w3.org/2005/sparql-results#"> * <head> * <variable name="?X" /> * <variable name="?Y" /> * </head> * <results> * <result> * <binding name="?X"> * <uri>https://en.wikipedia.org/wiki/Figaro_(Disney)</uri> * </binding> * <binding name="?Y"> * <uri>https://en.wikipedia.org/wiki/Minnie_Mouse</uri> * </binding> * </result> * <result> * <binding name="?X"> * <uri>https://en.wikipedia.org/wiki/Pluto_(Disney)</uri> * </binding> * <binding name="?Y"> * <uri>https://en.wikipedia.org/wiki/Mickey_Mouse</uri> * </binding> * </result> * </results> * </sparql> */ }
public void Test1() { string filePath = GetPath(@"Files\Test1.ttl"); RDFGraph graph = RDFGraph.FromFile(RDFModelEnums.RDFFormats.Turtle, filePath); RDFSelectQuery selectQuery = new RDFSelectQuery(); // PREFIX foaf: <http://xmlns.com/foaf/0.1/> selectQuery.AddPrefix(RDFNamespaceRegister.GetByPrefix("foaf")); // ?NAME var name = new RDFVariable("name"); // ?FRIEND var friend = new RDFVariable("friend"); // ?PERSON var person = new RDFVariable("person"); // ?PERSON foaf:name ?NAME . var person_foaf_name_name = new RDFPattern(person, RDFVocabulary.FOAF.NAME, name); // ?PERSON foaf:knows ?FRIEND . var person_foaf_knows_friend = new RDFPattern(person, RDFVocabulary.FOAF.KNOWS, friend); // WHERE { // { // ?PERSON foaf:name? NAME . // ?PERSON foaf:knows? FRIEND . // } // } var pg1 = new RDFPatternGroup("PG1"); pg1.AddPattern(person_foaf_name_name); pg1.AddPattern(person_foaf_knows_friend); selectQuery.AddPatternGroup(pg1); // SELECT ?PERSON ?NAME ?FRIEND selectQuery.AddProjectionVariable(person); selectQuery.AddProjectionVariable(name); selectQuery.AddProjectionVariable(friend); var sparqlCommand = selectQuery.ToString(); #region Generated SPARQL command /* * Generates this sparql command * * PREFIX foaf: <http://xmlns.com/foaf/0.1/> * * SELECT ?PERSON ?NAME ?FRIEND * WHERE { * { * ?PERSON foaf:name ?NAME . * ?PERSON foaf:knows ?FRIEND . * } * } * */ #endregion // APPLY SELECT QUERY TO GRAPH RDFSelectQueryResult selectQueryResult = selectQuery.ApplyToGraph(graph); // EXPORT SELECT QUERY RESULTS TO SPARQL XML FORMAT (FILE) //selectQueryResult.ToSparqlXmlResult(@"C:\TEMP\Test1.srq"); #region Generated result file /* * <?xml version="1.0" encoding="utf-8"?> * <sparql xmlns="http://www.w3.org/2005/sparql-results#"> * <head> * <variable name="?PERSON" /> * <variable name="?NAME" /> * <variable name="?FRIEND" /> * </head> * <results> * <result> * <binding name="?PERSON"> * <uri>http://example.org/alice#me</uri> * </binding> * <binding name="?NAME"> * <literal>Alice</literal> * </binding> * <binding name="?FRIEND"> * <uri>http://example.org/bob#me</uri> * </binding> * </result> * <result> * <binding name="?PERSON"> * <uri>http://example.org/alice#me</uri> * </binding> * <binding name="?NAME"> * <literal>Alice</literal> * </binding> * <binding name="?FRIEND"> * <uri>http://example.org/charlie#me</uri> * </binding> * </result> * <result> * <binding name="?PERSON"> * <uri>http://example.org/alice#me</uri> * </binding> * <binding name="?NAME"> * <literal>Alice</literal> * </binding> * <binding name="?FRIEND"> * <uri>http://example.org/snoopy</uri> * </binding> * </result> * <result> * <binding name="?PERSON"> * <uri>http://example.org/bob#me</uri> * </binding> * <binding name="?NAME"> * <literal>Bob</literal> * </binding> * <binding name="?FRIEND"> * <uri>http://example.org/alice#me</uri> * </binding> * </result> * <result> * <binding name="?PERSON"> * <uri>http://example.org/charlie#me</uri> * </binding> * <binding name="?NAME"> * <literal>Charlie</literal> * </binding> * <binding name="?FRIEND"> * <uri>http://example.org/alice#me</uri> * </binding> * </result> * </results> * </sparql> */ #endregion Assert.Equal(5, selectQueryResult.SelectResultsCount); }
public void Test2() { string filePath = GetPath(@"Files\Test1.ttl"); RDFGraph graph = RDFGraph.FromFile(RDFModelEnums.RDFFormats.Turtle, filePath); RDFSelectQuery selectQuery = new RDFSelectQuery(); selectQuery.AddPrefix(RDFNamespaceRegister.GetByPrefix("foaf")); var name = new RDFVariable("name"); var friend = new RDFVariable("friend"); var count = new RDFVariable("count"); var person = new RDFVariable("person"); // ?person foaf:name ?name . var person_foaf_name_name = new RDFPattern(person, RDFVocabulary.FOAF.NAME, name); // ?person foaf:knows ?friend . var person_foaf_knows_friend = new RDFPattern(person, RDFVocabulary.FOAF.KNOWS, friend); var pg1 = new RDFPatternGroup("PG1"); pg1.AddPattern(person_foaf_name_name); pg1.AddPattern(person_foaf_knows_friend); selectQuery.AddPatternGroup(pg1); selectQuery.AddProjectionVariable(name); //selectQuery.AddProjectionVariable(count); // GROUP BY ?PERSON ?NAME // var gm = new RDFGroupByModifier(new List<RDFVariable>() { person, name }); // GROUP BY ?NAME var gm = new RDFGroupByModifier(new List <RDFVariable>() { name }); // (COUNT(?friend) AS ?count) gm.AddAggregator(new RDFCountAggregator(friend, count)); selectQuery.AddModifier(gm); var sparqlCommand = selectQuery.ToString(); #region Target query in the SPARQL 1.1 documentation /* * Target query in the document * * PREFIX foaf: <http://xmlns.com/foaf/0.1/> * SELECT ?name (COUNT(?friend) AS ?count) * WHERE { * ?person foaf:name ?name . * ?person foaf:knows ?friend . * } GROUP BY ?person ?name */ #endregion #region Managed to build this command /* * PREFIX foaf: <http://xmlns.com/foaf/0.1/> * * SELECT ?NAME (COUNT(?FRIEND) AS ?COUNT) * WHERE { * { * ?PERSON foaf:name ?NAME . * ?PERSON foaf:knows ?FRIEND . * } * } * GROUP BY ?NAME */ #endregion #region Managed to build this command /* * Managed to build this command * * PREFIX foaf: <http://xmlns.com/foaf/0.1/> * * SELECT ?PERSON ?NAME (COUNT(?FRIEND) AS ?COUNT) * WHERE { * { * ?PERSON foaf:name ?NAME . * ?PERSON foaf:knows ?FRIEND . * } * } * GROUP BY ?PERSON ?NAME */ #endregion // APPLY SELECT QUERY TO GRAPH RDFSelectQueryResult selectQueryResult = selectQuery.ApplyToGraph(graph); // EXPORT SELECT QUERY RESULTS TO SPARQL XML FORMAT (FILE) //selectQueryResult.ToSparqlXmlResult(@"C:\TEMP\Test2.srq"); #region Got result /* * Got result * * <?xml version="1.0" encoding="utf-8"?> * <sparql xmlns="http://www.w3.org/2005/sparql-results#"> * <head> * <variable name="?NAME" /> * <variable name="?COUNT" /> * </head> * <results> * <result> * <binding name="?NAME"> * <literal>Alice</literal> * </binding> * <binding name="?COUNT"> * <literal datatype="http://www.w3.org/2001/XMLSchema#decimal">3</literal> * </binding> * </result> * <result> * <binding name="?NAME"> * <literal>Bob</literal> * </binding> * <binding name="?COUNT"> * <literal datatype="http://www.w3.org/2001/XMLSchema#decimal">1</literal> * </binding> * </result> * <result> * <binding name="?NAME"> * <literal>Charlie</literal> * </binding> * <binding name="?COUNT"> * <literal datatype="http://www.w3.org/2001/XMLSchema#decimal">1</literal> * </binding> * </result> * </results> * </sparql> * */ #endregion #region Got result /* * * * <?xml version="1.0" encoding="utf-8"?> * <sparql xmlns="http://www.w3.org/2005/sparql-results#"> * <head> * <variable name="?PERSON" /> * <variable name="?NAME" /> * <variable name="?COUNT" /> * </head> * <results> * <result> * <binding name="?PERSON"> * <uri>http://example.org/alice#me</uri> * </binding> * <binding name="?NAME"> * <literal>Alice</literal> * </binding> * <binding name="?COUNT"> * <literal datatype="http://www.w3.org/2001/XMLSchema#decimal">3</literal> * </binding> * </result> * <result> * <binding name="?PERSON"> * <uri>http://example.org/bob#me</uri> * </binding> * <binding name="?NAME"> * <literal>Bob</literal> * </binding> * <binding name="?COUNT"> * <literal datatype="http://www.w3.org/2001/XMLSchema#decimal">1</literal> * </binding> * </result> * <result> * <binding name="?PERSON"> * <uri>http://example.org/charlie#me</uri> * </binding> * <binding name="?NAME"> * <literal>Charlie</literal> * </binding> * <binding name="?COUNT"> * <literal datatype="http://www.w3.org/2001/XMLSchema#decimal">1</literal> * </binding> * </result> * </results> * </sparql> * */ #endregion #region Target result from SPARQL 1.1 documentation /* * Target result * 3 Different query results formats supported by SPARQL 1.1 (XML, JSON, CSV, TSV) * * <?xml version="1.0"?> * <sparql xmlns="http://www.w3.org/2005/sparql-results#"> * <head> * <variable name="name"/> * <variable name="count"/> * </head> * <results> * <result> * <binding name="name"> * <literal>Alice</literal> * </binding> * <binding name="count"> * <literal datatype="http://www.w3.org/2001/XMLSchema#integer">3</literal> * </binding> * </result> * <result> * <binding name="name"> * <literal>Bob</literal> * </binding> * <binding name="count"> * <literal datatype="http://www.w3.org/2001/XMLSchema#integer">1</literal> * </binding> * </result> * <result> * <binding name="name"> * <literal>Charlie</literal> * </binding> * <binding name="count"> * <literal datatype="http://www.w3.org/2001/XMLSchema#integer">1</literal> * </binding> * </result> * </results> * </sparql> */ #endregion Assert.Equal(3, selectQueryResult.SelectResultsCount); }