Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Servlet
- ===============================================================
- package com.jbjares.jvisualrdf.servlet;
- import java.io.IOException;
- import java.io.Writer;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class RdfToJsonServlet
- */
- @WebServlet("/RdfToJsonServlet")
- public class RdfToJsonServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private static final String resultJson = "{\"nodes\": [{\"name\": \"g: visualRDF\", "+
- "\"uri\": \"http: \\\\graves.cl\\/visualRDF\", "+
- "\"type\": \"uri\", "+
- "\"reflexive\": true, "+
- "\"id\": 0 "+
- "}, "+
- "{ "+
- "\"name\": \"http: \\\\alvaro.graves.cl\", "+
- "\"uri\": \"http: \\\\alvaro.graves.cl\", "+
- "\"type\": \"uri\", "+
- "\"reflexive\": false, "+
- "\"id\": 1 "+
- "}, "+
- "{ "+
- "\"name\": \"http: \\\\github.com\\/alangrafu\\/visualRDF\", "+
- "\"uri\": \"http: \\\\github.com\\/alangrafu\\/visualRDF\", "+
- "\"type\": \"uri\", "+
- "\"reflexive\": false, "+
- "\"id\": 2 "+
- "}, "+
- "{ "+
- "\"name\": \"g:visualRDF\\/css\\/bootstrap-responsive.min.css\", "+
- "\"uri\": \"http: \\\\graves.cl\\/visualRDF\\/css\\/bootstrap-responsive.min.css\", "+
- "\"type\": \"uri\", "+
- "\"reflexive\": false, "+
- "\"id\": 3 "+
- "}, "+
- "{ "+
- "\"name\": \"g:visualRDF\\/css\\/bootstrap.min.css\", "+
- "\"uri\": \"http:\\\\graves.cl\\/visualRDF\\/css\\/bootstrap.min.css\", "+
- "\"type\": \"uri\", "+
- "\"reflexive\": false, "+
- "\"id\": 4 "+
- "}, "+
- "{ "+
- "\"name\": \"jbjares\", "+
- "\"uri\": \"http:\\\\jbjares\", "+
- "\"type\": \"uri\", "+
- "\"reflexive\": true, "+
- "\"id\": 5 "+
- "} "+
- "], "+
- "\"links\": [ "+
- "{ "+
- "\"source\": 0, "+
- "\"target\": 1, "+
- "\"name\": \"dc: creator\", "+
- "\"right\": true, "+
- "\"felt\": false, "+
- "\"value\": 10 "+
- "}, "+
- "{ "+
- "\"source\": 0, "+
- "\"target\": 2, "+
- "\"name\": \"dc:source\", "+
- "\"right\": true, "+
- "\"felt\": false, "+
- "\"value\": 10 "+
- "}, "+
- "{ "+
- "\"source\": 0, "+
- "\"target\": 3, "+
- "\"name\": \"xhtml:stylesheet\", "+
- "\"right\": true, "+
- "\"felt\": false, "+
- "\"value\": 10 "+
- "}, "+
- "{ "+
- "\"source\": 0, "+
- "\"target\": 4, "+
- "\"name\": \"xhtml:stylesheet\", "+
- "\"right\": true, "+
- "\"felt\": false, "+
- "\"value\": 10 "+
- "} "+
- "], "+
- "\"literals\": [ "+
- "{ "+
- "\"p\": \"dc:title\", "+
- "\"o\": \"VisualRDF\", "+
- "\"l\": \"\", "+
- "\"d\": \"\" "+
- "}, "+
- "{ "+
- "\"p\": \"dc:modified\", "+
- "\"o\": \"2012-05-18\", "+
- "\"l\": \"\", "+
- "\"d\": \"\" "+
- "} "+
- "] "+
- "} ";
- /**
- * @see HttpServlet#HttpServlet()
- */
- public RdfToJsonServlet() {
- super();
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doExecute(request, response);
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doExecute(request, response);
- }
- /**
- * @see HttpServlet#doExecute(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doExecute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- Writer writer = response.getWriter();
- writer.write(resultJson);
- writer.flush();
- writer.close();
- }
- }
- ==============================================================
- main js of the app: var drag_line = null;
- var force = null;
- // handles to link and node element groups
- var path = null,
- circle = null;
- var loaded = false;
- //var lastNodeId = 4;
- var lastNodeId = null;
- // set up SVG for D3
- var width = 2100,
- height = 950,
- colors = d3.scale.category10();
- var svg = d3.select('body')
- .append('svg')
- .attr('width', width)
- .attr('height', height);
- // set up initial nodes and links
- // - nodes are known by 'id', not by index in array.
- // - reflexive edges are indicated on the node (as a bold black circle).
- // - links are always source < target; edge directions are set by 'left' and 'right'.
- var nodes = [
- {id: 0, reflexive: false},
- {id: 1, reflexive: true },
- {id: 2, reflexive: false}
- ],
- links = [
- {source: nodes[0], target: nodes[1], left: false, right: true },
- {source: nodes[1], target: nodes[2], left: false, right: true }
- ];
- // init D3 force layout
- function initD3ForceLayout(nodes,links){
- force = d3.layout.force()
- .nodes(nodes)
- .links(links)
- .size([width, height])
- .linkDistance(150)
- .charge(-500)
- .on('tick', tick)
- // define arrow markers for graph links
- svg.append('svg:defs').append('svg:marker')
- .attr('id', 'end-arrow')
- .attr('viewBox', '0 -5 10 10')
- .attr('refX', 6)
- .attr('markerWidth', 3)
- .attr('markerHeight', 3)
- .attr('orient', 'auto')
- .append('svg:path')
- .attr('d', 'M0,-5L10,0L0,5')
- .attr('fill', '#000');
- svg.append('svg:defs').append('svg:marker')
- .attr('id', 'start-arrow')
- .attr('viewBox', '0 -5 10 10')
- .attr('refX', 4)
- .attr('markerWidth', 3)
- .attr('markerHeight', 3)
- .attr('orient', 'auto')
- .append('svg:path')
- .attr('d', 'M10,-5L0,0L10,5')
- .attr('fill', '#000');
- // line displayed when dragging new nodes
- drag_line = svg.append('svg:path')
- .attr('class', 'link dragline hidden')
- .attr('d', 'M0,0L0,0');
- // handles to link and node element groups
- path = svg.append('svg:g').selectAll('path'),
- circle = svg.append('svg:g').selectAll('g');
- }
- // mouse event vars
- var selected_node = null,
- selected_link = null,
- mousedown_link = null,
- mousedown_node = null,
- mouseup_node = null;
- function resetMouseVars() {
- mousedown_node = null;
- mouseup_node = null;
- mousedown_link = null;
- }
- // update force layout (called automatically each iteration)
- function tick() {
- // draw directed edges with proper padding from node centers
- path.attr('d', function(d) {
- var deltaX = d.target.x - d.source.x,
- deltaY = d.target.y - d.source.y,
- dist = Math.sqrt(deltaX * deltaX + deltaY * deltaY),
- normX = deltaX / dist,
- normY = deltaY / dist,
- sourcePadding = d.left ? 17 : 12,
- targetPadding = d.right ? 17 : 12,
- sourceX = d.source.x + (sourcePadding * normX),
- sourceY = d.source.y + (sourcePadding * normY),
- targetX = d.target.x - (targetPadding * normX),
- targetY = d.target.y - (targetPadding * normY);
- return 'M' + sourceX + ',' + sourceY + 'L' + targetX + ',' + targetY;
- });
- circle.attr('transform', function(d) {
- for(var key in d) {
- var value = d[key];
- }
- return 'translate(' + d.x + ',' + d.y + ')';
- });
- }
- //function createPredicateFilters(up){
- // updateFilters();
- //}
- // update graph (called when needed)
- function load() {
- if(loaded){
- return;
- }
- // literals = json.literals;
- // for(i in json.links){
- // uniquePredicates[json.links[i].name] = 1;
- // }
- // createPredicateFilters(uniquePredicates);
- console.log("===> restart");
- //d3.json('http://localhost:8080/JVisualRDFApp/RdfToJsonServlet?url='+encodeURIComponent(myUrl), function(json){
- d3.json('http://localhost:8080/JVisualRDFApp/RdfToJsonServlet?url=<abc>', function(json){
- d3.select("#waiting").style("display", "none");
- console.log("===> restart: json content "+json);
- // path (link) group
- console.log("===> init: json.links content "+json.links);
- if(json.links!=null){
- links = json.links;
- }else{
- console.log("===> error: json.links is null");
- }
- if(json.nodes!=null){
- nodes = json.nodes;
- }else{
- console.log("===> error: json.nodes is null");
- }
- if(json.nodes!=null){
- console.log(json.nodes.length);
- lastNodeId = json.nodes.length-1;
- }else{
- console.log("===> error: json.nodes is null");
- }
- initD3ForceLayout(nodes,links);
- restart();
- loaded = true;
- });
- }
- function restart(){
- path = path.data(links);
- console.log("===> init: links content "+links);
- // update existing links
- path.classed('selected', function(d) { return d === selected_link; })
- .style('marker-start', function(d) { return d.left ? 'url(#start-arrow)' : ''; })
- .style('marker-end', function(d) { return d.right ? 'url(#end-arrow)' : ''; });
- // add new links
- path.enter().append('svg:path')
- .attr('class', 'link')
- .classed('selected', function(d) { return d === selected_link; })
- .style('marker-start', function(d) { return d.left ? 'url(#start-arrow)' : ''; })
- .style('marker-end', function(d) { return d.right ? 'url(#end-arrow)' : ''; })
- .style('label', function(d) { return d.right ? 'url(#end-arrow)' : ''; })
- .on('mousedown', function(d) {
- if(d3.event.ctrlKey) return;
- // select link
- mousedown_link = d;
- if(mousedown_link === selected_link) selected_link = null;
- else selected_link = mousedown_link;
- selected_node = null;
- restart();
- });
- // remove old links
- path.exit().remove();
- // circle (node) group
- // NB: the function arg is crucial here! nodes are known by id, not by index!
- circle = circle.data(nodes, function(d) { return d.id; });
- // update existing nodes (reflexive & selected visual states)
- circle.selectAll('circle')
- .style('fill', function(d) { return (d === selected_node) ? d3.rgb(colors(d.id)).brighter().toString() : colors(d.id); })
- .classed('reflexive', function(d) { return d.reflexive; });
- // add new nodes
- var g = circle.enter().append('svg:g');
- g.append('svg:circle')
- .attr('class', 'node')
- .attr('r', 12)
- .style('fill', function(d) { return (d === selected_node) ? d3.rgb(colors(d.id)).brighter().toString() : colors(d.id); })
- .style('stroke', function(d) { return d3.rgb(colors(d.id)).darker().toString(); })
- .classed('reflexive', function(d) { return d.reflexive; })
- .on('mouseover', function(d) {
- if(!mousedown_node || d === mousedown_node) return;
- // enlarge target node
- d3.select(this).attr('transform', 'scale(1.1)');
- })
- .on('mouseout', function(d) {
- if(!mousedown_node || d === mousedown_node) return;
- // unenlarge target node
- d3.select(this).attr('transform', '');
- })
- .on('mousedown', function(d) {
- if(d3.event.ctrlKey) return;
- // select node
- mousedown_node = d;
- if(mousedown_node === selected_node) selected_node = null;
- else selected_node = mousedown_node;
- selected_link = null;
- // reposition drag line
- drag_line
- .style('marker-end', 'url(#end-arrow)')
- .classed('hidden', false)
- .attr('d', 'M' + mousedown_node.x + ',' + mousedown_node.y + 'L' + mousedown_node.x + ',' + mousedown_node.y);
- restart();
- })
- .on('mouseup', function(d) {
- if(!mousedown_node) return;
- // needed by FF
- drag_line
- .classed('hidden', true)
- .style('marker-end', '');
- // check for drag-to-self
- mouseup_node = d;
- if(mouseup_node === mousedown_node) { resetMouseVars(); return; }
- // unenlarge target node
- d3.select(this).attr('transform', '');
- // add link to graph (update if exists)
- // NB: links are strictly source < target; arrows separately specified by booleans
- var source, target, direction;
- if(mousedown_node.id < mouseup_node.id) {
- source = mousedown_node;
- target = mouseup_node;
- direction = 'right';
- } else {
- source = mouseup_node;
- target = mousedown_node;
- direction = 'left';
- }
- var link;
- link = links.filter(function(l) {
- return (l.source === source && l.target === target);
- })[0];
- if(link) {
- link[direction] = true;
- } else {
- link = {source: source, target: target, left: false, right: false};
- link[direction] = true;
- links.push(link);
- }
- // select new link
- selected_link = link;
- selected_node = null;
- restart();
- });
- // show node names
- g.append('svg:text')
- .attr('x', 0)
- .attr('y', 4)
- .attr('class', 'name')
- .text(function(d) { return d.name; });
- // .attr('class', 'id')
- // .text(function(d) { return d.id; });
- // remove old nodes
- circle.exit().remove();
- // set the graph in motion
- force.start();
- }
- function mousedown() {
- // prevent I-bar on drag
- //d3.event.preventDefault();
- // because :active only works in WebKit?
- svg.classed('active', true);
- if(d3.event.ctrlKey || mousedown_node || mousedown_link) return;
- // insert new node at point
- var point = d3.mouse(this),
- node = {id: ++lastNodeId, reflexive: false};
- node.x = point[0];
- node.y = point[1];
- nodes.push(node);
- restart();
- }
- function mousemove() {
- if(!mousedown_node) return;
- // update drag line
- drag_line.attr('d', 'M' + mousedown_node.x + ',' + mousedown_node.y + 'L' + d3.mouse(this)[0] + ',' + d3.mouse(this)[1]);
- restart();
- }
- function mouseup() {
- if(mousedown_node) {
- // hide drag line
- drag_line
- .classed('hidden', true)
- .style('marker-end', '');
- }
- // because :active only works in WebKit?
- svg.classed('active', false);
- // clear mouse event vars
- resetMouseVars();
- }
- function spliceLinksForNode(node) {
- var toSplice = links.filter(function(l) {
- return (l.source === node || l.target === node);
- });
- toSplice.map(function(l) {
- links.splice(links.indexOf(l), 1);
- });
- }
- // only respond once per keydown
- var lastKeyDown = -1;
- function keydown() {
- d3.event.preventDefault();
- if(lastKeyDown !== -1) return;
- lastKeyDown = d3.event.keyCode;
- // ctrl
- if(d3.event.keyCode === 17) {
- circle.call(force.drag);
- svg.classed('ctrl', true);
- }
- if(!selected_node && !selected_link) return;
- switch(d3.event.keyCode) {
- case 8: // backspace
- case 46: // delete
- if(selected_node) {
- nodes.splice(nodes.indexOf(selected_node), 1);
- spliceLinksForNode(selected_node);
- } else if(selected_link) {
- links.splice(links.indexOf(selected_link), 1);
- }
- selected_link = null;
- selected_node = null;
- restart();
- break;
- case 66: // B
- if(selected_link) {
- // set link direction to both left and right
- selected_link.left = true;
- selected_link.right = true;
- }
- restart();
- break;
- case 76: // L
- if(selected_link) {
- // set link direction to left only
- selected_link.left = true;
- selected_link.right = false;
- }
- restart();
- break;
- case 82: // R
- if(selected_node) {
- // toggle node reflexivity
- selected_node.reflexive = !selected_node.reflexive;
- } else if(selected_link) {
- // set link direction to right only
- selected_link.left = false;
- selected_link.right = true;
- }
- restart();
- break;
- }
- }
- function keyup() {
- lastKeyDown = -1;
- // ctrl
- if(d3.event.keyCode === 17) {
- circle
- .on('mousedown.drag', null)
- .on('touchstart.drag', null);
- svg.classed('ctrl', false);
- }
- }
- // app starts here
- svg.on('mousedown', mousedown)
- .on('mousemove', mousemove)
- .on('mouseup', mouseup);
- d3.select(window)
- .on('keydown', keydown)
- .on('keyup', keyup);
- load();
- restart();
- ================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement