From: Tomasz P. <tom...@gm...> - 2012-09-24 20:33:03
|
Hi Rob The QueryBuilder starts very promising. Way better than the Algebra API. However I'm not convinced with the extension methods way. What I could suggest is a specialized builder class which holds the query inside and the user can retrieve it whenever he is done with building. Something along the below: public class QueryBuilder : IQueryBuilder { private SparqlQuery _query = new SparqlQuery(); // such constructor only as a suggestion // could also create factory methods for the query types public QueryBuilder(SparqlQueryType queryType) { _query.QueryType = queryType; } // Now all methods are simmilar but not extension // and will return self. For example: public IQueryBuilder Where(IEnumerable<Triple> ts) { foreach (Triple t in ts) { _query = _query.Where(t.Subject, t.Predicate, t.Object); } return this; } public SparqlQuery GetExecutableQuery() { // not sure the Copy() is required // the internal API forbids any changes to the underlying query anyway // but shall that change any time, it could lead to unexpected results return _query.Copy(); } } I'm suggesting this, because currently the SparqlQuery has quite a few public getters and methods and at first it was very obscure what functionality the QueryBuilder actually provides. Also notice the IQueryBuilder interface. I think it would be beneficial to let the users actuall mock the builder and have the calls verified in unit tests. Last but not least, there could be not one but multiple interfaces implemented by the QueryBuilder class, which gives us power to order some of the calls if we ever need such a feature. It's already 10:30PM here so I won't be reworking the code today. Tomorrow maybe, depending on your thoughts on all this :). Tom On Mon, Sep 24, 2012 at 7:20 PM, Rob Vesse <rv...@do...> wrote: > Off the top of my head it should look something like this, assuming you > create the URIs as INode instances first: > > ISparqlAlgebra algebra = new Select( > new Filter( > new Join( > new LeftJoin( > new LeftJoin( > new BGP(), > new BGP(new TriplePattern(new VariablePattern("?item"), new > NodeMatchPattern(rdfType), new NodeMatchPattern(someClass))) > ), > new BGP(new TriplePattern(new VariablePattern("?item"), new > NodeMatchPattern(rdfType), new NodeMatchPattern(someOtherClass))) > ), > new BGP(new TriplePattern(new VariablePattern("?item"), new > NodeMatchPattern(foafName), new VariablePattern("?itemName"))) > ), > new UnaryExpressionFilter(new LangMatchesFunction(new LangFunction(new > VariableTerm("itemName")), new ConstantTerm(en))) > ), > new String[] { "item" }); > > Not the prettiest thing to build, see the QueryBuilder class [1] on the > fluent-query [2] branch for an initial rough cut of a more user friendly > fluent style API, see the unit tests in that commit for some examples of > building queries > > Hopefully that style of API will be much more readable than the above and > much more user friendly > > Rob > > [1] > https://bitbucket.org/dotnetrdf/dotnetrdf/changeset/f0654d04f2612d6e215ff17 > d166ba99c74e781af > [2] > https://bitbucket.org/dotnetrdf/dotnetrdf/changesets/tip/branch(%22fluent-q > uery%22) > > > On 9/24/12 5:40 AM, "Tomasz Pluskiewicz" <tom...@gm...> > wrote: > >>Hello again >> >>I tried to build some algebra programmatically but I failed miserably :) >> >>Could you please as an example help me with expressing the below >>SPARQL in Algebra API? >> >>SELECT ?item >>{ >> OPTIONAL { ?item a ex:someClass . } >> OPTIONAL { ?item a ex:someOtherClass . } >> ?item foaf:name ?itemName >> FILTER( langMatches( lang(?itemName), "en" )) >>} >> >>This should give me enough insight to get up to speed with the API. >> >>Thanks, >>Tom >> >>On Sat, Sep 22, 2012 at 7:02 PM, Tomasz Pluskiewicz >><tom...@gm...> wrote: >>> Also are there any introductory examples on creating SPARQL Algebra. >>> It could be the way to go for my initially. Are there many scenarios >>> when it would be impossible to convert it to a query? >>> >>> Tom >>> >>> On Sat, Sep 22, 2012 at 11:30 AM, Tomasz Pluskiewicz >>> <tom...@gm...> wrote: >>>> Thanks Rob >>>> >>>> I could look into it. Please point me int the right direction in the >>>> code where the internal API is located. >>>> >>>> Tom >>>> >>>> On Fri, Sep 21, 2012 at 10:26 PM, Rob Vesse <rv...@do...> >>>>wrote: >>>>> Hey Tom >>>>> >>>>> Right now it's somewhat limited, you can build the algebra >>>>> programmatically and try and convert it back to a query with the >>>>>AsQuery() >>>>> method but that isn't guaranteed to work (some algebras can't be >>>>>converted >>>>> back into queries). Otherwise you have to generate a string and then >>>>> parse it. >>>>> >>>>> Most of the SparqlQuery API is protected internal/private, I have a >>>>>To Do >>>>> item to add a public fluent style API for building queries that I will >>>>> likely do for the next release if that helps. I/you can start >>>>>building >>>>> that out in a feature branch if that would solve your issue? >>>>> >>>>> Rob >>>>> >>>>> On 9/21/12 11:39 AM, "Tomasz Pluskiewicz" >>>>><tom...@gm...> >>>>> wrote: >>>>> >>>>>>Hi >>>>>> >>>>>>Please tell me what are the options for building SPARQL >>>>>>programatically with dotNetRDF? Is there any such API? >>>>>> >>>>>>Regards, >>>>>>Tom >>>>>> >>>>>>---------------------------------------------------------------------- >>>>>>---- >>>>>>---- >>>>>>Got visibility? >>>>>>Most devs has no idea what their production app looks like. >>>>>>Find out how fast your code is with AppDynamics Lite. >>>>>>http://ad.doubleclick.net/clk;262219671;13503038;y? >>>>>>http://info.appdynamics.com/FreeJavaPerformanceDownload.html >>>>>>_______________________________________________ >>>>>>dotNetRDF-develop mailing list >>>>>>dot...@li... >>>>>>https://lists.sourceforge.net/lists/listinfo/dotnetrdf-develop >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>>----------------------------------------------------------------------- >>>>>------- >>>>> Got visibility? >>>>> Most devs has no idea what their production app looks like. >>>>> Find out how fast your code is with AppDynamics Lite. >>>>> http://ad.doubleclick.net/clk;262219671;13503038;y? >>>>> http://info.appdynamics.com/FreeJavaPerformanceDownload.html >>>>> _______________________________________________ >>>>> dotNetRDF-develop mailing list >>>>> dot...@li... >>>>> https://lists.sourceforge.net/lists/listinfo/dotnetrdf-develop >> >>-------------------------------------------------------------------------- >>---- >>Live Security Virtual Conference >>Exclusive live event will cover all the ways today's security and >>threat landscape has changed and how IT managers can respond. Discussions >>will include endpoint security, mobile security and the latest in malware >>threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ >>_______________________________________________ >>dotNetRDF-develop mailing list >>dot...@li... >>https://lists.sourceforge.net/lists/listinfo/dotnetrdf-develop > > > > > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > dotNetRDF-develop mailing list > dot...@li... > https://lists.sourceforge.net/lists/listinfo/dotnetrdf-develop |