15/09/2014

serveur Fuseki accessible depuis un smartphone Android grâce à Apde

Développé sur mon staraddict 4 avec Apde ( voir sur Google play). Les librairies que j'ai pu trouver ne sont pas compatibles avec Android ou Apde , j'ai donc utilisé http de apache ( androjena, rdfonthego, microjena)
Nota : le code est brut et pas nettoyé, avec tous les essais.
Nota 2 : pour insérer une information, modifiez la requête update , par exemple la ligne commençant par : " update+="smag:Robokaze"

Nota 3: pour en savoir plus sur Rdf et la manière de stocker les informations :
https://plus.google.com/114521704339515532783/posts/jcwovt7vTsZ


Pour enregistrer des infos sur le serveur :



import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;



import android.view.inputmethod.InputMethodManager;

import android.content.Context;

//public class RequestParamExample {
//public static void main(String[] args) {

// }
String message;
String update;
 HttpClient httpclient;


void setup(){
  background(0);

  try {
  update="PREFIX rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ";
update+="PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>" ;
update+="PREFIX smag:   <http://smag0.blogspot.fr/NS/>";
update+="PREFIX zoo:   <http://example.org/zoo/>";
update+="PREFIX owl: <http://www.w3.org/2002/07/owl#>";
update+="PREFIX ex: <http://example.org/>";

update+="INSERT DATA {";
update+="GRAPH <http://smag0.blogspot.fr/GraphTest>{";

update+="smag:Robokaze    rdf:type         smag:Site .";
update+="smag:Robokaze   smag:objectif         'un site de vente revente piece detachees , pour robots, et objets connectes' .";
update+="ex:cat     rdfs:subClassOf  ex:animal .";
update+="zoo:host   rdfs:range       ex:animal .";
update+="ex:zoo1    zoo:host         ex:cat2 .";
update+="ex:cat3    owl:sameAs       ex:cat2 .";

update+="}}";

 // message="";
 //String message;
  //HttpParameterPost();



message=new String("Votre ecran peut vous apparaitre tout noir,\n a la premiere connexion au serveur, \n merci de patienter quelques dizaines de secondes au plus \n");

//private static void HttpParameterPost() {
  httpclient = new DefaultHttpClient();
 
    HttpPost httpPost=new HttpPost("http://fuseki-smag0.rhcloud.com/ds/update?");
  // HttpPost httpPost = new HttpPost("http://192.168.1.52:3030/ds/update?");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
  //  nameValuePairs.add(new BasicNameValuePair("update", "INSERT {<bola> <bolb> <bolc>} WHERE {?s ?p ?o}" ));
   nameValuePairs.add(new BasicNameValuePair("update", update));
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    System.out.println("executing request " + httpPost.getRequestLine());
    HttpResponse response = httpclient.execute(httpPost);
    HttpEntity resEntity = response.getEntity();
    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (resEntity != null) {
      System.out.println("Response content length: " + resEntity.getContentLength());
      System.out.println("Chunked?: " + resEntity.isChunked());
      String responseBody = EntityUtils.toString(resEntity);
      System.out.println("Data: " + responseBody);
     message+= "Response content length: " + resEntity.getContentLength()+"\n";
      message+="Chunked?: " + resEntity.isChunked()+"\n";
    //  String responseBody = EntityUtils.toString(resEntity);
      message+="Data: " + responseBody+"\n";
  // message="45" ;
    }
   // EntityUtils.consume(resEntity);
  }
  catch (Exception e) {
    System.out.println(e);
   message+=e;
  }
  finally {
    // When HttpClient instance is no longer needed,
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
  }
}



void draw(){
  text(message,100,100);

}





 void showVirtualKeyboard()

{

  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

  imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);



}

 void hideVirtualKeyboard()

{

  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

  imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

}




pour lire des infos sur le serveur : 


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.util.Scanner;

//arqoid

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Literal;
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.Statement;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
//import com.hp.hpl.jena.update.*;
import com.hp.hpl.jena.sparql.core.Prologue;



String err="";
String url="";
//UpdateRequest update;
DefaultHttpClient httpClient;
HttpEntity   entity;
HttpResponse response;
String reponse;
PImage img;
String url_img="";
HttpEntity   entity_img;
HttpResponse response_img;
String reponse_img;


void setup() {
  background(0);
  String req_insert_prefix="PREFIX+ns:+<http://example.org/ns#>";
  req_insert_prefix+="PREFIX dc: <http://purl.org/dc/elements/1.1/>";
  req_insert_prefix+="PREFIX d: <http://example.com/ns/data#>";
  req_insert_prefix+="PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>";

  String req_insert="INSERT DATA";
  req_insert+="{ GRAPH <http://example/bookStore> { ";
  req_insert+="<http://example/bidule>  ns:price  42 ." ;
  //req_insert+="d:i1 rdfs:label \"one\" .";
  //d:i2 rdfs:label "two" .
  req_insert+=" }}";

  //url="http://fuseki-smag0.rhcloud.com/ds/update?update="+req_insert_prefix+req_insert;
  //text(url,20,200);
  //

  //insert();
  //img = loadImage("image.jpg");
  img=loadImage("http://3.bp.blogspot.com/-MbiBWE4xcD0/Ux90q1F1RsI/AAAAAAAAAS8/hRpHBM5E4PE/s1600/Rover_5421698457340622361.JPG" ) ;
 // img=loadImage("http://88.142.232.252:8090/?action=stream");
  /*
try{
   img=loadImage("http://88.142.232.252:8090/?action=stream");

 
   }
 
   catch( Exception e ) {
   e.printStackTrace();
   err+=e.toString();
   text(e.toString(), 100, 200);
   }
   */
  //String url="http://127.0.0.1:8080" ;
//String url="http://88.142.232.252:3030/ds/query?query=construct+where+%7B%3Fs+%3Fp+%3Fo%7D";
 String url="http://fuseki-smag0.rhcloud.com/ds/query?query=construct+where+%7B%3Fs+%3Fp+%3Fo%7D";
  //  String url="http://fuseki-smag0.rhcloud.com/ds/query?query=select+*+where+%7B%3Fs+%3Fp+%3Fo%7D";
  //  String url = "https://www.google.fr/search?q=test";
  //  https://www.google.fr/search?site=&source=hp&ei=PQX2U5qfMdPY0QXP_IGYAg&q=test&oq=test&gs_l=mobile-gws-hp.3..35i39j0l4.2250.2976.0.3886.6.5.1.1.1.0.168.532.0j4.4.0....0...1c.1.51.mobile-gws-hp..1.5.457.3.BSrYBhzULXU
  text(" Recuperation des informations depuis :",100,100);
  text(url,100,120);

    try {
        httpClient = new DefaultHttpClient();
        HttpGet          httpGet   = new HttpGet( url );
    //  ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(2);
    //    nameValuePairs.add(new BasicNameValuePair("q", "value1"));
    //   nameValuePairs.add(new BasicNameValuePair("param2", "value2"));
    //   httpGet.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // println( "executing request: " + httpPost.getRequestLine() );
       response = httpClient.execute( httpGet );
   
       entity   = response.getEntity();

     if ( entity != null ) {
   
      //   String reponse;
      //  entity.writeTo( reponse );
      // String reponse=new String(entity.getContent());
       reponse = new Scanner(entity.getContent(), "UTF-8").useDelimiter("\\A").next();
    }
      //  if ( entity != null ) entity.consumeContent();

        // When HttpClient instance is no longer needed,
     // shut down the connection manager to ensure
       // immediate deallocation of all system resources
    //   httpClient.getConnectionManager().shutdown();
                // When HttpClient instance is no longer needed,
     // shut down the connection manager to ensure
       // immediate deallocation of all system resources
    //    httpClient.getConnectionManager().shutdown();
       
  }
 

    catch( Exception e ) {
    e.printStackTrace();
    err+=e.toString();
    text(e.toString(), 100, 100);
  }

      // When HttpClient instance is no longer needed,
   // shut down the connection manager to ensure
     // immediate deallocation of all system resources
   httpClient.getConnectionManager().shutdown();
     
    //   exit();
}
void draw() {
  background(0);
  text("Smag", displayWidth-50, 10);
  text(url, 20, 200);
  text(err, 10, 100);
  // text(update.toString(),50,100);

        if ( entity != null ) {

    text("----------------------------------------", 10, 10);
    text( response.getStatusLine().toString(), 10, 20);
    text("----------------------------------------", 10, 30);
   
      //   String reponse;
    //  entity.writeTo( reponse );
    // String reponse=new String(entity.getContent());
    //   try{ String reponse = new Scanner(entity.getContent(), "UTF-8").useDelimiter("\\A").next();

    text(reponse, 10, 100);
    //   }
    //    catch( Exception e ) {
    //  e.printStackTrace();
    //  err=e.toString();
    // text(e.toString(), 100, 100);
    //}

    // Displays the image at its actual size at point (0,0)
    // image(img, 0, 0);
    // Displays the image at point (0, height/2) at half of its size
    image(img, 0, displayHeight-100, img.width/2, img.height/2);
  }
}

void insert() {

  // update = UpdateFactory.create("");
  //  UpdateProcessRemote riStore = (UpdateProcessRemote) UpdateExecutionFactory.createRemote(update, "http://myendpointuri");
  // riStore.execute();
}

// The following short JSON file called "data.json" is parsed // in the code below. It must be in the project's "data" folder. // // { // "id": 0, // "species": "Panthera leo", // "name": "Lion" // } JSONObject json; void setup() { json = loadJSONObject("data.json"); int id = json.getInt("id"); String species = json.getString("species"); String name = json.getString("name"); println(id + ", " + species + ", " + name); } // Sketch prints: // 0, Panthera leo, Lion

01/09/2014

Explorer les données d'un serveur fuseki/rdf

Dans Chrome Web Store, ajouter l'application " OpenLinkDataExplorer", lancer l'appli
ensuite dans la ligne  "Data Source URI", saisissez l'adresse de requete de votre serveur Fuseki sous la forme :
http://fuseki-smag0.rhcloud.com/ds/query?query=select+*+where%7B%3FP+%3FS+%3FO%7D