This is very helpful.  
Thank you, Mona

-----Original Message-----
From: Joshua TAYLOR [mailto:joshuaaa...@gmail.com] 
Sent: Wednesday, January 29, 2014 2:15 PM
To: users@jena.apache.org
Subject: Re: Can I pass a model in a parameter list?

On Wed, Jan 29, 2014 at 5:01 PM, Mona Salem <monasalem...@gmail.com> wrote:
> I am using Eclipse actually.  I am working with a jena model and can 
> print out my triples.
> The problem that I am running into is adding a property to my model in 
> a second class;  most properties are added in the Parser class, except 
> for the annotation labels.  I am calling a third party
> api(?TextAnnotations.java) and wish to load the labels into my model there.
>
> This is a jena question.  I do not know how to pass the model in a 
> parameter list, and have the object returned to Parser.java.

I think you may have a misconception about how properties are represented in 
Jena.  Code like

    //create the properties
    Property extracteddata =
model.createProperty("http://www.mydirectory.com/softwareproduct#extracteddata";);

    Property annotationLabel =
model.createProperty("http://www.mydirectory.com/softwareproduct#softwareannotation";);

doesn't actually do anything to the Model.  (In the case of an OntModel, where 
it takes some triples to "declare" a property, there might be some data added 
to a model.)  All this is doing is using a model to return a Property that you 
could use later on when you're
trying to add property values to things.   Here's some example code
that shows that you can pass Models as parameters, and illustrates that 
creating Properties doesn't change the model.  Adding triples to a model 
changes the model.


import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

public class CreatePropertyExample {
  private final static String NS = "http://example.org/";;

  public static void main(String[] args) {
    // Create a model and show it.
    final Model model = ModelFactory.createDefaultModel();
    System.out.println( "===" );
    model.write( System.out );

    // Create a property from the model, and
    // show the property and the model again (nothing's changed).
    final Property p = model.createProperty( NS+"p" );
    System.out.println( "===" );
    System.out.println( p );
    model.write( System.out );

    // Pass the model as a parameter to a method that
    // will modify the model.  This time there's some
    // new stuff in the model.
    addTripleToModel( model );
    System.out.println( "===" );
    model.write( System.out );

  }

  private static void addTripleToModel( final Model m ) {
    // Create some resources and properties.
    Resource x = m.createResource( NS+"x" );
    Resource y = m.createResource( NS+"y" );
    Resource z = m.createResource( NS+"z" );
    Property p = m.createProperty( NS+"p" );
    Property q = m.createProperty( NS+"q" );

    // Add a triple to the model by adding a property to x.
    x.addProperty( q, y );

    // Add a triple to the model by adding it directly.
    m.add( y, p, z );
  }
}

The output is

===
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; > </rdf:RDF> === 
http://example.org/p <rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; > </rdf:RDF> === 
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    xmlns:j.0="http://example.org/"; >
  <rdf:Description rdf:about="http://example.org/x";>
    <j.0:q rdf:resource="http://example.org/y"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://example.org/y";>
    <j.0:p rdf:resource="http://example.org/z"/>
  </rdf:Description>
</rdf:RDF>
===
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; > </rdf:RDF> === 
http://example.org/p <rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; > </rdf:RDF> === 
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    xmlns:j.0="http://example.org/"; >
  <rdf:Description rdf:about="http://example.org/x";>
    <j.0:q rdf:resource="http://example.org/y"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://example.org/y";>
    <j.0:p rdf:resource="http://example.org/z"/>
  </rdf:Description>
</rdf:RDF>





--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Reply via email to