Hi Rob,
thanks for your quick reply.
I did not find replace, but perhaps I was not looking in the right
place/package. I'll search again/better.

The quick and dirty solution for me was to write a replace.java
myself (attached to this email... let's see if the attachement
comes through). It works pretty well, but of course it makes my SPARQL
query less portable. So, if REPLACE() is added to SPARQL 1.1, great!

Here is a snippet of my (trivial) implementation:

    private static NodeValue javaReplace(NodeValue nvString, NodeValue nvRegex,
NodeValue nvReplacement)
    {
        try {
            String string = nvString.getString() ;
            String regex = nvRegex.getString() ;
            String replacement = nvReplacement.getString() ;

            return NodeValue.makeString(string.replaceAll(regex, replacement)) ;
        } catch (IndexOutOfBoundsException ex) {
            throw new ExprEvalException("IndexOutOfBounds", ex) ;
        }
    }

Re: answers.semanticweb.com... thanks for the suggestion (even if
archived mailing lists work better for me). Don't get me wrong,
that website is very interesting and useful. There is also a ml on
SPARQL, but my questions are a mix of SPARQL and RDF data modeling.

If I do not find anything better, I'll post there.
Or, if no one complains, I'll post here. :-)

Thanks again Rob.

Paolo

Robert Vesse wrote:
> Hey Paolo
> 
> I believe latest SPARQL 1.1 drafts add a REPLACE() function which is 
> equivalent to the XPath fn:replace() function (bar some differences in 
> handling literal forms as per the SPARQL 1.1 string functions spec) that 
> should be able to do what you want.  AFAIK this should be in the latest ARQ 
> trunk unless I am mistaken.
> 
> For general "How do I do X with SPARQL?" type questions 
> answers.semanticweb.com has plenty of those kinds of questions and people are 
> pretty good at coming up with answers.
> 
> Cheers,
> 
> Rob
> 
> -----Original Message-----
> From: Paolo Castagna [mailto:[email protected]] 
> Sent: Wednesday, November 30, 2011 9:26 AM
> To: [email protected]
> Subject: How to construct new URIs via SPARQL CONSTRUCT?
> 
> Hi,
> I am using SPARQL CONSTRUCT queries to derive a dataset (from Geonames, 
> DBPedia
> and, eventually, other data sources) of all Italian regions, provinces and 
> 'comuni'.
> 
> Italy is divided in 20 regions. A region can be divided in its provinces. 
> There
> are 110 provinces in Italy. Finally, a province can have 'comuni' (or other
> smaller administrative entities). There are 8094 of these in Italy (according 
> to
> Geonames).
> 
> This is a fragment of my SPARQL CONSTRUCT query:
> 
> ----
> PREFIX italy:  <http://example.com/italy/>
> ...
> CONSTRUCT {
>   ?region_uri rdf:type italy:Region .
>   ?region_uri rdfs:label ?region .
>   ?region_uri italy:contains ?p .
> 
>   ?province_uri rdf:type italy:Province .
>   ?province_uri rdfs:label ?province .
>   ?province_uri italy:contains ?comune_uri .
>   ?province_uri italy:isContained ?region_uri .
> 
>   ?comune_uri rdf:type italy:Comune .
>   ?comune_uri rdfs:label ?comune .
>   ?comune_uri italy:isContained ?province_uri .
>   ...
> } WHERE {
>   ...
>   BIND ( iri(concat("http://example.com/italy/";, lcase(?region))) AS 
> ?region_uri )
>   BIND ( iri(concat("http://example.com/italy/";, lcase(?region), '/',
> lcase(?province))) AS ?province_uri )
>   BIND ( iri(concat("http://example.com/italy/";, lcase(?region), '/',
> lcase(?province), '/', lcase(?comune))) AS ?comune_uri )
> }
> ----
> 
> This is a fragment of what the query above generates, for 'Roncone' which is a
> 'comune' in the province of Trento which is a province in the 'Trentino-Alto
> Adige' region:
> 
> ----
> <http://example.com/italy/regione autonoma trentino-alto adige/provincia di
> trento/roncone>
>       a       italy:Comune ;
>       rdfs:label "Roncone" ;
>       italy:isContained <http://example.com/italy/regione autonoma 
> trentino-alto
> adige/provincia di trento> .
> ----
> 
> All this works well, but I have not been able to strip out "regione autonoma"
> and replace ' ' with '_' when I generate my new URIs.
> 
> This, instead, is what I'd like to achieve:
> 
> ----
> <http://example.com/italy/trentino-alto_adige/trento/roncone>
>       a       italy:Comune ;
>       rdfs:label "Roncone" ;
>       italy:isContained <http://example.com/italy/trentino-alto_adige/trento> 
> .
> ----
> 
> Or, if possible (even better) this:
> 
> ----
> italy:trentino-alto_adige/trento/roncone
>       a       italy:Comune ;
>       rdfs:label "Roncone" ;
>       italy:isContained italy:trentino-alto_adige/trento .
> ----
> 
> Is this possible to do this with a SPARQL CONSTRUCT query without writing a 
> new
> SPARQL/ARQ function?
> I searched in ARQ, but I did not find a 'replace' function. Is there one? 
> Would
> it be an useful addition?
> 
> Also, I know that URIs should be opaque, but I though it would help humans to
> follow a pattern {ns} + {region}/{province}/{comune}. What do you think about 
> this?
> 
> Do you have other suggestions (in addition to Geonames, DBPedia and Freebase) 
> of
> good datasets for geographical informations about Europe and/or Italy?
> 
> Last but not least, this message is probably not completely on-topic on the
> jena-users mailing list. Are these type of messages (on SPARQL queries and/or
> data modelling using RDF) fine on jena-users? If not, what would be a better
> mailing list for this sort of questions (I have a few on 'cheese' :-))?
> 
> Thanks,
> Paolo

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kasabi.labs.arq;

import java.util.List;

import com.hp.hpl.jena.query.QueryBuildException;
import com.hp.hpl.jena.sparql.expr.ExprEvalException;
import com.hp.hpl.jena.sparql.expr.ExprList;
import com.hp.hpl.jena.sparql.expr.NodeValue;
import com.hp.hpl.jena.sparql.function.FunctionBase;
import com.hp.hpl.jena.sparql.util.Utils;

public class replace extends FunctionBase {

	public replace() { 
		super() ; 
	}

    @Override 
    public void checkBuild(String uri, ExprList args) {
        if ( args.size() != 3 ) 
        	throw new QueryBuildException("Function '"+Utils.className(this)+"' takes three arguments") ;
    }

    @Override
    public NodeValue exec(List<NodeValue> args) {
        if ( args.size() > 3 )
        	throw new ExprEvalException("replace: Wrong number of arguments: "+args.size()+" : [wanted 3]") ;
        
        NodeValue v1 = args.get(0) ;
        NodeValue v2 = args.get(1) ;
        NodeValue v3 = args.get(2) ;
        
        return javaReplace(v1, v2, v3) ;
    }
    
    private static NodeValue javaReplace(NodeValue nvString, NodeValue nvRegex, NodeValue nvReplacement)
    {
        try {
            String string = nvString.getString() ;
            String regex = nvRegex.getString() ;
            String replacement = nvReplacement.getString() ;
            
            return NodeValue.makeString(string.replaceAll(regex, replacement)) ;
        } catch (IndexOutOfBoundsException ex) {
            throw new ExprEvalException("IndexOutOfBounds", ex) ;
        }
    }
        
}

Reply via email to