import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.sparql.util.IndentedWriter;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.reasoner.Reasoner;
import org.mindswap.pellet.jena.PelletReasonerFactory;


public class Jungle2
{
    
    public static final String NL = System.getProperty("line.separator") ;
  
    public static void main( String[] args ) {
        // create the simplest model there is
    	//  
    	String jungle_file = "file:"+args[0];
	    // create Pellet reasoner
        Reasoner reasoner = PelletReasonerFactory.theInstance().create();
        
        // create an empty model
        Model emptyModel = ModelFactory.createDefaultModel( );
        
        // create an inferencing model using Pellet reasoner
        InfModel m = ModelFactory.createInfModel( reasoner, emptyModel );
            
        // read the file
        m.read( jungle_file );
    	
        // Model m = ModelFactory.createDefaultModel();

        // use the file manager to read an RDF document into the model
        // FileManager.get().readModel( m, jungle_file );
        System.out.println( "We have loaded a model with no. statements = " + m.size() );
    	String jungle ="http://www.lirmm.fr/jungle#";
    	String prolog1 = "PREFIX jungle: <"+jungle+">" ;
        String prolog2 = "PREFIX rdf: <"+RDF.getURI()+">" ;
        // Query string.
        String queryString = prolog1 + NL + prolog2 + NL +
            "SELECT ?individu  WHERE {?individu rdf:type jungle:Lion }" ;

        Query query = QueryFactory.create(queryString) ;
        // Print with line numbers
        query.serialize(new IndentedWriter(System.out,true)) ;
        System.out.println() ;
        // Create a single execution of this query, apply to a model
        // which is wrapped up as a Dataset

        QueryExecution qexec = QueryExecutionFactory.create(query, m) ;
        // Or QueryExecutionFactory.create(queryString, model) ;

        System.out.println("Les Lions : ") ;

        try {
            // Assumption: it's a SELECT query.
            ResultSet rs = qexec.execSelect() ;
            // The order of results is undefined.
            for ( ; rs.hasNext() ; )
            {
                QuerySolution rb = rs.nextSolution() ;
                // Get title - variable names do not include the '?' (or '$')                
                RDFNode y = rb.get("individu");
                System.out.print("uri : "+y+"---  ");
                Resource z = (Resource) rb.getResource("individu");
                System.out.println("plus simplement "+z.getLocalName());
            }
        }
        finally
        {
            // QueryExecution objects should be closed to free any system resources
            qexec.close() ;
        }
    	
    
}
    }
