Hi Dick,

On 23/08/11 09:53, Dick Murray wrote:
Hello all.

> [..]
I'm using the following code to create a new model containing a
filtered set of statements based on the language tag;

Can I get an iterator over the statements which match a
subject/predicate and language tag or do I need to do my own
filtering..?
The JavaDoc on listStatements is not as clear as it could be. The lang tag is only used in the case that the object is non-null: ie it can be used to select for the particular language version of a given literal (foo@en as opposed to foo@fr), but it can't be used to generically select all @en or all @fr labels. However, all is not lost, I've provided some alternatives in the attached test case.

Hth,
Ian




--
____________________________________________________________
Ian Dickinson                   Epimorphics Ltd, Bristol, UK
mailto:[email protected]        http://www.epimorphics.com
cell: +44-7786-850536              landline: +44-1275-399069
------------------------------------------------------------
Epimorphics Ltd.  is a limited company registered in England
(no. 7016688). Registered address: Court Lodge, 105 High St,
              Portishead, Bristol BS20 6PT, UK

package test;


import java.io.StringReader;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.RDFS;

public class LangTest
{
    private static final Logger log = LoggerFactory.getLogger( LangTest.class );

    private OntModel model;
    private OntResource r;

    @Before
    public void setUp() throws Exception {
        model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );

        String source = "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> ." +
                "<http://www.example.org/ooo/id/operator/example>\n" +
                "                rdfs:label \"No language tag\" ;\n" +
                "                rdfs:label \"@en language tag\"@en ;\n" +
                "                rdfs:label \"Another @en language tag\"@en ;\n" +
                "                rdfs:label \"@fr language tag\"@fr ;\n" +
                "                rdfs:label \"test\"@x ;\n" +
                "                rdfs:label \"test\"@y .";

        model.read( new StringReader( source ), null, "Turtle" );
        r = model.getOntResource( "http://www.example.org/ooo/id/operator/example"; );
    }

    /** List all statements */
    @Test
    public void testListAll() {
        int n = 0;
        for (StmtIterator i = model.listStatements(); i.hasNext(); n++) {
            log.debug( "testListAll: " + i.next() );
        }
        assertEquals( 6, n );
    }

    /** List all statements matching lang - confusingly, the lang only has an effect if the
     * object is non-null */
    @Test
    public void testListAllWithLang() {
        int n = 0;
        for (StmtIterator i = model.listStatements( null, null, null, "en" ); i.hasNext(); n++) {
            log.debug( "testListAllWithLang + null: " + i.next() );
        }
        assertEquals( 6, n );

        n = 0;
        for (StmtIterator i = model.listStatements( null, null, "Another @en language tag", "en" ); i.hasNext(); n++) {
            log.debug( "testListAllWithLang + non-null: " + i.next() );
        }
        assertEquals( 1, n );
    }

    /** List values of rdfs:label property */
    @Test
    public void testListLiterals() {
        int n = 0;
        for (NodeIterator i = r.listPropertyValues( RDFS.label ); i.hasNext(); n++) {
            log.debug( "testListLiterals: " + i.next() );
        }
        assertEquals( 6, n );
    }

    /** List labels of given resource */
    @Test
    public void testListLabels() {
        int n = 0;
        for (ExtendedIterator<RDFNode> i = r.listLabels( null ); i.hasNext(); n++) {
            log.debug( "testListLabels: " + i.next() );
        }
        assertEquals( 6, n );
    }

    /** List English labels */
    @Test
    public void testListEnLabels() {
        int n = 0;
        for (ExtendedIterator<RDFNode> i = r.listLabels( "en" ); i.hasNext(); n++) {
            log.debug( "testListEnLabels: " + i.next() );
        }
        assertEquals( 2, n );
    }

}

Reply via email to