22/12/2013

Test d'intégration de Apache Jena -Sparql dans Processing.org pour visualiser les rdf

Voir le tutoriel plus récent : http://smag0.blogspot.fr/2014/03/creer-une-interface-pour-visualiser-les.html


import com.hp.hpl.jena.query.* ;

int nodeCount;
Node[] nodes = new Node[10];
HashMap nodeTable = new HashMap();

int edgeCount;
Edge[] edges = new Edge[5];

static final color nodeColor   = #F0C070;
static final color selectColor = #FF3030;
static final color fixedColor  = #FF8080;
static final color edgeColor   = #000000;

PFont font;

void setup() {
  size(600, 600);
  // loadData();
  // println(edgeCount);
  font = createFont("SansSerif", 10);


  // File file = new File();
  Rdf rdftest = new Rdf();
}

void addEdge(String fromLabel, String toLabel) {
  // Filter out unnecessary words
  if (ignoreWord(fromLabel) || ignoreWord(toLabel)) return;

  Node from = findNode(fromLabel);
  Node to = findNode(toLabel);
  from.increment();
  to.increment();

  for (int i = 0; i < edgeCount; i++) {
    if (edges[i].from == from && edges[i].to == to) {
      edges[i].increment();
      return;
    }
  }

  Edge e = new Edge(from, to);
  e.increment();
  if (edgeCount == edges.length) {
    edges = (Edge[]) expand(edges);
  }
  edges[edgeCount++] = e;
}


String[] ignore = {
  "a", "of", "the", "i", "it", "you", "and", "to"
};

boolean ignoreWord(String what) {
  for (int i = 0; i < ignore.length; i++) {
    if (what.equals(ignore[i])) {
      return true;
    }
  }
  return false;
}


Node findNode(String label) {
  label = label.toLowerCase();
  Node n = (Node) nodeTable.get(label);
  if (n == null) {
    return addNode(label);
  }
  return n;
}


Node addNode(String label) {
  Node n = new Node(label);
  if (nodeCount == nodes.length) {
    nodes = (Node[]) expand(nodes);
  }
  nodeTable.put(label, n);
  nodes[nodeCount++] = n;
  return n;
}

void draw() {
  if (record) {
    beginRecord(PDF, "output.pdf");
  }

  background(255);
  textFont(font);
  smooth();

  for (int i = 0 ; i < edgeCount ; i++) {
    edges[i].relax();
  }
  for (int i = 0; i < nodeCount; i++) {
    nodes[i].relax();
  }
  for (int i = 0; i < nodeCount; i++) {
    nodes[i].update();
  }
  for (int i = 0 ; i < edgeCount ; i++) {
    edges[i].draw();
  }
  for (int i = 0 ; i < nodeCount ; i++) {
    nodes[i].draw();
  }

  if (record) {
    endRecord();
    record = false;
  }
}
boolean record;

void keyPressed() {
  if (key == 'r') {
    record = true;
  }
}


Node selection;


void mousePressed() {
  // Ignore anything greater than this distance
  float closest = 20;
  for (int i = 0; i < nodeCount; i++) {
    Node n = nodes[i];
    float d = dist(mouseX, mouseY, n.x, n.y);
    if (d < closest) {
      selection = n;
      closest = d;
    }
  }
  if (selection != null) {
    if (mouseButton == LEFT) {
      selection.fixed = true;
    } else if (mouseButton == RIGHT) {
      selection.fixed = false;
    }
  }
}


void mouseDragged() {
  if (selection != null) {
    selection.x = mouseX;
    selection.y = mouseY;
  }
}


void mouseReleased() {
  selection = null;
}

// EDGE
// Code from Visualizing Data, First Edition, Copyright 2008 Ben Fry.
// Based on the GraphLayout example by Sun Microsystems.


class Edge {
  Node from;
  Node to;
  float len;
  int count;



  Edge(Node from, Node to) {
    this.from = from;
    this.to = to;
    this.len = 50;
  }


  void increment() {
    count++;
  }


  void relax() {
    float vx = to.x - from.x;
    float vy = to.y - from.y;
    float d = mag(vx, vy);
    if (d > 0) {
      float f = (len - d) / (d * 3);
      float dx = f * vx;
      float dy = f * vy;
      to.dx += dx;
      to.dy += dy;
      from.dx -= dx;
      from.dy -= dy;
    }
  }


  void draw() {
    stroke(edgeColor);
    strokeWeight(0.35);
    line(from.x, from.y, to.x, to.y);
  }
}


//NODE

// Code from Visualizing Data, First Edition, Copyright 2008 Ben Fry.
// Based on the GraphLayout example by Sun Microsystems.


class Node {
  float x, y;
  float dx, dy;
  boolean fixed;
  String label;
  int count;
  int taille;


  Node(String label) {
    this.label = label;
    x = width/2;
    y = height/2;
  }


  void increment() {
    count++;
  }


  void relax() {
    float ddx = 0;
    float ddy = 0;

    for (int j = 0; j < nodeCount; j++) {
      Node n = nodes[j];
      if (n != this) {
        float vx = x - n.x;
        float vy = y - n.y;
        float lensq = vx * vx + vy * vy;
        if (lensq == 0) {
          ddx += random(1);
          ddy += random(1);
        }
        else if (lensq < 100*100) {
          ddx += vx / lensq;
          ddy += vy / lensq;
        }
      }
    }
    float dlen = mag(ddx, ddy) / 2;
    if (dlen > 0.00000000000001) {
      dx += ddx / dlen;
      dy += ddy / dlen;
    }
  }


  void update() {
    if (!fixed) {    
      x += constrain(dx, -10, 10);
      y += constrain(dy, -10, 10);

      x = constrain(x, 0, width);
      y = constrain(y, 0, height);
    }
    dx /= 2;
    dy /= 2;
  }


  void draw() {
    fill(nodeColor);
    stroke(0);
    strokeWeight(0.5);
    taille =count;
    taille = constrain(taille, 20, 200);
    ellipse(x, y, taille, taille);
    float w = textWidth(label);

  // if (taille > w+2) {
      fill(0);
      textAlign(CENTER, CENTER);
      text(label, x, y);
   //}
  }
}

// RDF

import com.hp.hpl.jena.query.* ;
import com.hp.hpl.jena.rdf.model.Resource;

class Rdf {
Model model;
  Rdf() {

    test();
    lister();
    afficheConsole();
  }
  void test() {
    // some definitions
    String personURI    = "http://somewhere/JohnSmith";
    String givenName    = "John";
    String familyName   = "Smith";
    String fullName     = givenName + " " + familyName;
    String personURIMe    = "http://fada-range.appspot.com/DavidFaveris";
    String givenNameMe    = "David";
    String familyNameMe   = "Faveris";
    String fullNameMe     = givenNameMe + " " + familyNameMe;
    // create an empty Model
    model = ModelFactory.createDefaultModel();

    // create the resource
    Resource johnSmith =  model.createResource(personURI);
    Resource davidFaveris =  model.createResource(personURIMe);

    // add the property
    johnSmith.addProperty(VCARD.FN, fullName)
      .addProperty(VCARD.N, model.createResource()
        .addProperty(VCARD.Given, givenName)
        .addProperty(VCARD.Family, familyName));
    ;
    davidFaveris.addProperty(VCARD.FN, fullNameMe)
      .addProperty(VCARD.N, model.createResource()
        .addProperty(VCARD.Given, givenNameMe)
        .addProperty(VCARD.Family, familyNameMe));

    println(johnSmith);
    println(davidFaveris);
    addNode("johnSmith");
    addNode("davidFaveris");

  }


void afficheConsole(){
  model.write(System.out);
}

  void lister() {
    // list the statements in the Model
    StmtIterator iter = model.listStatements();

    // print out the predicate, subject and object of each statement
    while (iter.hasNext ()) {
      Statement stmt      = iter.nextStatement();  // get next statement
      Resource  subject   = stmt.getSubject();     // get the subject
      Property  predicate = stmt.getPredicate();   // get the predicate
      RDFNode   object    = stmt.getObject();      // get the object
addEdge(subject.toString(), object.toString());
        System.out.print("Sujet : "+subject.toString());
      System.out.print(" "+"\tPredicat : " + predicate.toString() + " ");
      if (object instanceof Resource) {
        System.out.print("\tObjet : "+object.toString());
      } 
      else {
        // object is a literal
        System.out.print(" \"" + object.toString() + "\"");
      }

      System.out.println(" .");
    }
    
  }
}

//File

class File {
  File() {
    String lines[] = loadStrings("vcard.rdf");
    println("there are " + lines.length + " lines");
for (int i = 0 ; i < lines.length; i++) {

      lines[i] = lines[i].replaceAll("  ", "");

      //PREFIXES
      lines[i] = lines[i].replaceAll("http://www.semanticweb.org/fada/ontologies/2013/10/smag/monde#", "");
      lines[i] = lines[i].replaceAll("http://www.semanticweb.org/fada/ontologies/2013/10/smag/monde#", "");
      lines[i] = lines[i].replaceAll("http://www.w3.org/2002/07/owl#", "");
      println(lines[i]);
    }
    for (int i = 0; i < lines.length; i++) {
      // Make this phrase lowercase
      //  String line = lines[i].toLowerCase();
      String line = lines[i];
      // Split each phrase into individual words at one or more spaces
      String[] words = splitTokens(line, "|");
 /*     for (int w = 0; w < words.length-1; w=+3) {
        words[w]=words[w].replaceAll(" ", "");
        words[w+2]=words[w+2].replaceAll(" ", "");
        if ((words[w+2]=="<Class>") || (words[w]=="<Class>")) {
          print ("classe");
        }
        else {
          addEdge(words[w], words[w+2]);
          print (words[w+1]+" entre "+words[w]+ " et " + words[w+2]+"\n");
        }
      }
   */ }
  }
}


Aucun commentaire:

Enregistrer un commentaire