From b8d829dd982e8c4fa170f8bb8b095e8a1be8115e Mon Sep 17 00:00:00 2001 From: nirmal070125 Date: Mon, 13 Jun 2011 07:37:39 +0000 Subject: Another version of layout algorithm and diagram creation git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1135039 13f79535-47bb-0310-9956-ffa450edef68 --- .../output/Calculator_diagram.svg | 1 + .../output/store_diagram.svg | 1 + .../output/supplychain_diagram.svg | 1 + .../tuscany/sca/impl/artifacts/Artifact.java | 18 +++ .../tuscany/sca/impl/artifacts/Component.java | 33 +--- .../tuscany/sca/impl/artifacts/Composite.java | 29 +--- .../tuscany/sca/impl/artifacts/Property.java | 13 -- .../tuscany/sca/impl/artifacts/Reference.java | 29 +--- .../apache/tuscany/sca/impl/artifacts/Service.java | 24 +-- .../apache/tuscany/sca/impl/artifacts/Wire.java | 35 +---- .../tuscany/sca/impl/diagram/DiagramGenerator.java | 174 ++++++++++++++++++++- .../org/apache/tuscany/sca/impl/io/XMLReader.java | 5 - .../org/apache/tuscany/sca/impl/io/XMLWriter.java | 27 ++++ .../org/apache/tuscany/sca/impl/layout/Entity.java | 16 +- .../tuscany/sca/impl/layout/EntityBuilder.java | 148 ++++++++---------- .../java/org/apache/tuscany/sca/main/Main.java | 14 +- 16 files changed, 328 insertions(+), 240 deletions(-) create mode 100755 collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/Calculator_diagram.svg create mode 100755 collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/store_diagram.svg create mode 100755 collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/supplychain_diagram.svg create mode 100755 collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/io/XMLWriter.java (limited to 'collaboration') diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/Calculator_diagram.svg b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/Calculator_diagram.svg new file mode 100755 index 0000000000..66318f9eb9 --- /dev/null +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/Calculator_diagram.svg @@ -0,0 +1 @@ +CalculatorCalculatorServiceComponentaddServicesubtractServicemultiplyServicedivideServiceAddServiceComponentSubtractServiceComponentMultiplyServiceComponentDivideServiceComponent \ No newline at end of file diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/store_diagram.svg b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/store_diagram.svg new file mode 100755 index 0000000000..eb7fece1ac --- /dev/null +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/store_diagram.svg @@ -0,0 +1 @@ +storeStoreWidgetcatalogshoppingCartshoppingTotalCatalogCatalogcurrencyConvertercurrencyCodeShoppingCartCartTotalCurrencyConverter \ No newline at end of file diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/supplychain_diagram.svg b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/supplychain_diagram.svg new file mode 100755 index 0000000000..6a465e70f2 --- /dev/null +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/output/supplychain_diagram.svg @@ -0,0 +1 @@ +supplychainCustomerComponentretailerRetailerComponentwarehouseWarehouseComponentshipperShipperComponentcustomer \ No newline at end of file diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Artifact.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Artifact.java index 9a14ecf929..849d440c2d 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Artifact.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Artifact.java @@ -24,6 +24,8 @@ import org.w3c.dom.Element; public abstract class Artifact { + private String name; + private String containerName; private int height; private int width; private int xCoordinate; @@ -88,5 +90,21 @@ public abstract class Artifact { public static String getRoundCorner() { return ROUND_CORNER; } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setContainerName(String containerName) { + this.containerName = containerName; + } + + public String getContainerName() { + return containerName; + } } diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Component.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Component.java index 8e7d20146e..9254657547 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Component.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Component.java @@ -26,39 +26,8 @@ public class Component extends Artifact{ public static final int DEFAULT_WIDTH = 200 ; public static final int DEFAULT_HEIGHT = 130 ; + public static final int TEXT_SPACING = 20 ; - -// public Element addElement(Document document, -// String svgNs, int x, int y) { -// -// -// //Element g = document.createElementNS(svgNs, "g"); -// //g.setAttributeNS(null, "transform", "translate(100 100)"); -// // Create the rectangle. -// Element rectangle = document.createElementNS(svgNs, "rect"); -// rectangle.setAttributeNS(null, "x", x+""); -// rectangle.setAttributeNS(null, "y", y+""); -// rectangle.setAttributeNS(null, "rx", ROUND_CORNER); -// rectangle.setAttributeNS(null, "ry", ROUND_CORNER); -// rectangle.setAttributeNS(null, "width", WIDTH); -// rectangle.setAttributeNS(null, "height", HEIGHT); -// rectangle.setAttributeNS(null, "fill", "#3D59AB"); -// rectangle.setAttributeNS(null, "stroke", "#104E8B"); -// rectangle.setAttributeNS(null, "fill-opacity", "0.75"); -// -// -//// Element text = document.createElementNS(svgNs, "text"); -//// text.setAttributeNS(null, "text-anchor", "middle"); -//// text.setAttributeNS(null, "dominant-baseline", "mathematical"); -//// text.setAttributeNS(null, "font-size", "30"); -//// text.setTextContent("A Component"); -// -// //document.appendChild(rectangle); -// //g.appendChild(text); -// -// -// return rectangle; -// } /** * Create an element with specified height and width diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Composite.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Composite.java index 48491efe69..8b04759bee 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Composite.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Composite.java @@ -24,37 +24,10 @@ import org.w3c.dom.Element; /** * Structure of a "Composite" element in SCA, as a SVG element - * @author nirmal * */ public class Composite extends Artifact{ -// -// private Document document; -// private String svgNs; -// -// public Composite(Document doc, String uri){ -// document = doc; -// svgNs = uri; -// } -// - - -// public static Element addCompositeElement(Document document, String svgNs, -// int x, int y, int width, int height) { -// // Create the rectangle. -// Element rectangle = document.createElementNS(svgNs, "rect"); -// rectangle.setAttributeNS(null, "x", x+""); -// rectangle.setAttributeNS(null, "y", y+""); -// rectangle.setAttributeNS(null, "rx", ROUND_CORNER); -// rectangle.setAttributeNS(null, "ry", ROUND_CORNER); -// rectangle.setAttributeNS(null, "width", width+""); -// rectangle.setAttributeNS(null, "height", height+""); -// rectangle.setAttributeNS(null, "fill", "#E5E5E5"); -// rectangle.setAttributeNS(null, "stroke", "#919191"); -// rectangle.setAttributeNS(null, "alignment-baseline", "central"); -// -// return rectangle; -// } + public Element addElement(Document document, String svgNs, int x, int y, int height, int width) { diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Property.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Property.java index 0d236ccfaa..ea6b0b9eeb 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Property.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Property.java @@ -27,19 +27,6 @@ public class Property extends Artifact{ public static final int MAXIMUM_HEIGHT = 20 ; //default value public static final int SPACING = 20 ; //default value -// public static Element addPropertyElement(Document document, -// String svgNs, int x, int y) { -// Element rectangle = document.createElementNS(svgNs, "rect"); -// rectangle.setAttributeNS(null, "x", x+""); -// rectangle.setAttributeNS(null, "y", y+""); -// rectangle.setAttributeNS(null, "width", WIDTH); -// rectangle.setAttributeNS(null, "height", WIDTH); -// rectangle.setAttributeNS(null, "fill", "#EEEE00"); -// rectangle.setAttributeNS(null, "stroke", "#EEC900"); -// //rectangle.setAttributeNS(null, "fill-opacity", "0.75"); -// -// return rectangle; -// } /** * Property is a square. diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Reference.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Reference.java index 6a3f7ff9d1..f89fc2ab5f 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Reference.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Reference.java @@ -27,27 +27,6 @@ public class Reference extends Artifact { public static final int MAXIMUM_HEIGHT = 30 ; //default value public static final int SPACING = 10 ; //default value -//private static final int COMMON_LENGTH = 20 ; - -// public static Element addReferenceElement(Document document, String svgNs, int midX, int midY) { -// -// // Create the rectangle. -// Element polygon = document.createElementNS(svgNs, "polygon"); -// polygon.setAttributeNS(null, "points", -// ""+ (midX-COMMON_LENGTH) +","+midY+" " + -// ""+ (midX+(COMMON_LENGTH/2)) +","+midY+" " + -// ""+ (midX+COMMON_LENGTH) +","+(midY-COMMON_LENGTH)+" " + -// ""+ (midX+(COMMON_LENGTH/2)) +","+(midY-COMMON_LENGTH*2)+" " + -// ""+ (midX-COMMON_LENGTH) +","+(midY-COMMON_LENGTH*2)+" " + -// ""+ (midX-(COMMON_LENGTH/2)) +","+(midY-COMMON_LENGTH)+" " -// ); -// polygon.setAttributeNS(null, "fill", "#BF3EFF"); -// polygon.setAttributeNS(null, "stroke", "#68228B"); -// //rectangle.setAttributeNS(null, "alignment-baseline", "central"); -// -// return polygon; -// } -// /** * In a Reference the (x,y) coordinates refers to the top corner edge of the polygon @@ -72,10 +51,10 @@ public class Reference extends Artifact { polygon.setAttributeNS(null, "points", ""+ x +","+y+" " + ""+ (x+2*halfOfHeight) +","+(y)+" " + - ""+ (x+3*halfOfHeight) +","+(y-halfOfHeight)+" " + - ""+ (x+2*halfOfHeight) +","+(y-2*halfOfHeight)+" " + - ""+ (x) +","+(y-2*halfOfHeight)+" " + - ""+ (x+halfOfHeight) +","+(y-halfOfHeight)+" " + ""+ (x+3*halfOfHeight) +","+(y+halfOfHeight)+" " + + ""+ (x+2*halfOfHeight) +","+(y+2*halfOfHeight)+" " + + ""+ (x) +","+(y+2*halfOfHeight)+" " + + ""+ (x+halfOfHeight) +","+(y+halfOfHeight)+" " ); polygon.setAttributeNS(null, "fill", "#BF3EFF"); diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Service.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Service.java index 986cc1b99c..37adb65f39 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Service.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Service.java @@ -27,22 +27,6 @@ public class Service extends Artifact{ public static final int MAXIMUM_HEIGHT = 30 ; //default value public static final int SPACING = 10 ; //default value -// public static Element addServiceElement(Document document, String svgNs, int midX, int midY) { -// // Create the rectangle. -// Element polygon = document.createElementNS(svgNs, "polygon"); -// polygon.setAttributeNS(null, "points", -// ""+ (midX-COMMON_LENGTH) +","+midY+" " + -// ""+ (midX+(COMMON_LENGTH/2)) +","+midY+" " + -// ""+ (midX+COMMON_LENGTH) +","+(midY-COMMON_LENGTH)+" " + -// ""+ (midX+(COMMON_LENGTH/2)) +","+(midY-COMMON_LENGTH*2)+" " + -// ""+ (midX-COMMON_LENGTH) +","+(midY-COMMON_LENGTH*2)+" " + -// ""+ (midX-(COMMON_LENGTH/2)) +","+(midY-COMMON_LENGTH)+" " -// ); -// polygon.setAttributeNS(null, "fill", "#00CD66"); -// polygon.setAttributeNS(null, "stroke", "#008B45"); -// -// return polygon; -// } /** * In a Service the (x,y) coordinates refers to the top corner edge of the polygon @@ -66,10 +50,10 @@ public class Service extends Artifact{ polygon.setAttributeNS(null, "points", ""+ x +","+y+" " + ""+ (x+2*halfOfHeight) +","+(y)+" " + - ""+ (x+3*halfOfHeight) +","+(y-halfOfHeight)+" " + - ""+ (x+2*halfOfHeight) +","+(y-2*halfOfHeight)+" " + - ""+ (x) +","+(y-2*halfOfHeight)+" " + - ""+ (x+halfOfHeight) +","+(y-halfOfHeight)+" " + ""+ (x+3*halfOfHeight) +","+(y+halfOfHeight)+" " + + ""+ (x+2*halfOfHeight) +","+(y+2*halfOfHeight)+" " + + ""+ (x) +","+(y+2*halfOfHeight)+" " + + ""+ (x+halfOfHeight) +","+(y+halfOfHeight)+" " ); polygon.setAttributeNS(null, "fill", "#00CD66"); diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Wire.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Wire.java index 143b622ec8..59ef48895a 100644 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Wire.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/artifacts/Wire.java @@ -24,39 +24,20 @@ import org.w3c.dom.Element; public class Wire { -// - -// public static Element addWireElement(Document document, String svgNs, int x1, int y1, -// int x2, int y2) { -// // Create the rectangle. -// Element polyline = document.createElementNS(svgNs, "polyline"); -// //polyline.setAttributeNS(null,"class", "Connect"); -// polyline.setAttributeNS(null, "points", x1+","+y1+" "+x2+","+y2); -// polyline.setAttributeNS(null, "stroke", "black"); -// polyline.setAttributeNS(null, "stroke-width", "2"); -//// polygon.setAttributeNS(null, "points", -//// ""+ (midX-COMMON_LENGTH) +","+midY+" " + -//// ""+ (midX+(COMMON_LENGTH/2)) +","+midY+" " + -//// ""+ (midX+COMMON_LENGTH) +","+(midY-COMMON_LENGTH)+" " + -//// ""+ (midX+(COMMON_LENGTH/2)) +","+(midY-COMMON_LENGTH*2)+" " + -//// ""+ (midX-COMMON_LENGTH) +","+(midY-COMMON_LENGTH*2)+" " + -//// ""+ (midX-(COMMON_LENGTH/2)) +","+(midY-COMMON_LENGTH)+" " -//// ); -// //polyline.setAttributeNS(null, "fill", "purple"); -// //rectangle.setAttributeNS(null, "alignment-baseline", "central"); -// -// return polyline; -// } public Element addElement(Document document, String svgNs, Reference aReference, Service aService){ Element polyline = document.createElementNS(svgNs, "polyline"); + int x1=aReference.getxCoordinate()+aReference.getHeight()*3/2; + int y1=aReference.getyCoordinate()+aReference.getHeight()/2; + + int x2=aService.getxCoordinate()+aService.getHeight()/2; + int y2=aService.getyCoordinate()+aService.getHeight()/2; //polyline.setAttributeNS(null,"class", "Connect"); - polyline.setAttributeNS(null, "points", aReference.getxCoordinate()+","+ - aReference.getyCoordinate()+" "+aService.getxCoordinate()+","+ - aService.getyCoordinate()); + polyline.setAttributeNS(null, "points", x1+","+ + y1+" "+x2+","+y2 + ); polyline.setAttributeNS(null, "stroke", "black"); polyline.setAttributeNS(null, "stroke-width", "2"); diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/diagram/DiagramGenerator.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/diagram/DiagramGenerator.java index b4f425aeed..6b01b16a48 100755 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/diagram/DiagramGenerator.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/diagram/DiagramGenerator.java @@ -1,16 +1,188 @@ package org.apache.tuscany.sca.impl.diagram; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Map.Entry; + +import org.apache.batik.dom.svg.SVGDOMImplementation; +import org.apache.tuscany.sca.impl.artifacts.Component; +import org.apache.tuscany.sca.impl.artifacts.Composite; +import org.apache.tuscany.sca.impl.artifacts.Property; +import org.apache.tuscany.sca.impl.artifacts.Reference; +import org.apache.tuscany.sca.impl.artifacts.Service; +import org.apache.tuscany.sca.impl.artifacts.Text; +import org.apache.tuscany.sca.impl.artifacts.Wire; import org.apache.tuscany.sca.impl.layout.Entity; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.Element; public class DiagramGenerator { Entity[] entities; + int height, width; + Document doc; + String compositeName; + String svgNS ; + Element svgRoot; + ArrayList refs= new ArrayList(); + ArrayList sers= new ArrayList(); - public DiagramGenerator(Entity[] entities) { + public DiagramGenerator(Entity[] entities, int height, int width, String compositeName) { this.entities = entities; + this.height = height; + this.width = width; + this.compositeName = compositeName; } + public Document buildSVGDocument(){ + DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); + svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; + doc = impl.createDocument(svgNS, "svg", null); + + // Get the root element (the 'svg' element). + svgRoot = doc.getDocumentElement(); + + addComposite(); + for(Entity ent: entities){ + addComponent(ent); + addService(ent); + addReference(ent); + addProperties(ent); + } + + for(Entity ent: entities){ + for(Iterator it= ent.getReferenceToServiceMap().entrySet().iterator();it.hasNext();){ + Entry entry = (Entry) it.next(); + String ref = (String)entry.getKey(); + String ser = (String)entry.getValue(); + + Reference r = getRef(ref); + Service s = getSer(ser); + + if(r != null && s != null){ + addWire(r,s); + } + } + } + return doc; + } + private void addWire(Reference r, Service s) { + + Wire edge = new Wire(); + Element wire = edge.addElement(doc, svgNS, r, s); + svgRoot.appendChild(wire); + } + + private Service getSer(String ser) { + + for(Service s: sers){ + if(s.getContainerName().equals(ser) || s.getName().equals(ser)){ + return s; + } + } + return null; + } + + private Reference getRef(String ref) { + + for(Reference r: refs){ + if(r.getContainerName().equals(ref) || r.getName().equals(ref)){ + return r; + } + } + return null; + } + + private void addProperties(Entity ent) { + int propLen = ent.getPropLength(); + int x=ent.getX()+Property.SPACING; + int y= ent.getY()-propLen/2; + + for(String prop: ent.getProperties()){ + Property pro = new Property(); + Element property = pro.addElement(doc, svgNS, x, y, propLen); + Element text = Text.addTextElement(doc, svgNS, x, y, prop); + + svgRoot.appendChild(property); + svgRoot.appendChild(text); + + x += (propLen + Property.SPACING); + + pro.setName(prop); + pro.setContainerName(ent.getComponentName()); + } + } + + private void addReference(Entity ent) { + int refHeight = ent.getRefHeight(); + int x=(ent.getX()+ent.getWidth())-(refHeight*2/3); + int y=ent.getY()+Reference.SPACING; + + for(String ref: ent.getReferences()){ + Reference refer= new Reference(); + Element polygon = refer.addElement(doc, svgNS, x, y, refHeight); + Element text = Text.addTextElement(doc, svgNS, x, y+refHeight/2, ref); + svgRoot.appendChild(polygon); + svgRoot.appendChild(text); + + y += (refHeight + Reference.SPACING); + + refer.setName(ref); + refer.setContainerName(ent.getComponentName()); + refs.add(refer); + + } + } + + private void addService(Entity ent) { + int serHeight = ent.getSerHeight(); + int x=ent.getX()-(serHeight*2/3); + int y=ent.getY()+Service.SPACING; + + for(String ser: ent.getServices()){ + Service serve= new Service(); + Element polygon = serve.addElement(doc, svgNS, x, y, serHeight); + Element text = Text.addTextElement(doc, svgNS, x, y+serHeight/2, ser); + svgRoot.appendChild(polygon); + svgRoot.appendChild(text); + + y += (serHeight + Service.SPACING); + + serve.setName(ser); + serve.setContainerName(ent.getComponentName()); + sers.add(serve); + } + } + + private void addComponent(Entity ent) { + + Component comp = new Component(); + Element com = comp.addElement(doc, svgNS, ent.getX(), ent.getY(), + ent.getHeight(), ent.getWidth()); + Element text = Text.addTextElement(doc, svgNS, ent.getX()+(ent.getWidth()/4), + ent.getY()+(ent.getHeight()+Component.TEXT_SPACING), ent.getComponentName()); + + svgRoot.appendChild(com); + svgRoot.appendChild(text); + + comp.setName(ent.getComponentName()); + } + + private void addComposite() { + + Composite composite = new Composite(); + Element composi = composite.addElement(doc, svgNS, 0, 0, height, width); + Element text = Text.addTextElement(doc, svgNS, width/2, 20, compositeName); + + svgRoot.appendChild(composi); + svgRoot.appendChild(text); + + composite.setName(compositeName); + + + } diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/io/XMLReader.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/io/XMLReader.java index 92099a7104..e90777161a 100755 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/io/XMLReader.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/io/XMLReader.java @@ -1,13 +1,8 @@ package org.apache.tuscany.sca.impl.io; -import java.io.IOException; - import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - import org.w3c.dom.Document; -import org.xml.sax.SAXException; public class XMLReader { diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/io/XMLWriter.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/io/XMLWriter.java new file mode 100755 index 0000000000..cdd3941335 --- /dev/null +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/io/XMLWriter.java @@ -0,0 +1,27 @@ +package org.apache.tuscany.sca.impl.io; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; + +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; + +public class XMLWriter { + + public void fileWriter(Document doc, File outFile) throws Exception{ + + FileWriter fileWriter = new FileWriter(outFile); + BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(doc); + StreamResult result = new StreamResult(bufferedWriter); + transformer.transform(source, result); + + } +} diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/layout/Entity.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/layout/Entity.java index a9716146bb..0ae87d8816 100755 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/layout/Entity.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/layout/Entity.java @@ -12,14 +12,13 @@ import org.apache.tuscany.sca.impl.artifacts.Service; /** * Represents an unit (a component including its references, services, properties * and adjacent units) in the diagram. - * @author nirmal * */ public class Entity { private String componentName; private int X, Y, level=-1, lane=-1, refHeight, serHeight, propLength; - + private final int height= Component.DEFAULT_HEIGHT, width= Component.DEFAULT_WIDTH; public static final int defaultNoOfSers= Component.DEFAULT_HEIGHT / (Service.MAXIMUM_HEIGHT+Service.SPACING); public static final int defaultNoOfRefs= Component.DEFAULT_HEIGHT / (Reference.MAXIMUM_HEIGHT+Reference.SPACING); //same value for defaultNoOfSers public static final int defaultNoOfProps= Component.DEFAULT_WIDTH / (Property.MAXIMUM_HEIGHT+Property.SPACING); @@ -178,6 +177,7 @@ public class Entity { public void setServices(ArrayList services) { this.services = services; } + public ArrayList getServices() { return services; } @@ -217,6 +217,10 @@ public class Entity { public ArrayList getRefProperties() { return refProperties; } + + public ArrayList getReferences() { + return references; + } public void setRefProperties(ArrayList refProperties) { this.refProperties = refProperties; @@ -258,4 +262,12 @@ public class Entity { return connectedEntities; } + public int getHeight() { + return height; + } + + public int getWidth() { + return width; + } + } diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/layout/EntityBuilder.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/layout/EntityBuilder.java index 75dd21f5c3..156d5676e5 100755 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/layout/EntityBuilder.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/impl/layout/EntityBuilder.java @@ -2,23 +2,23 @@ package org.apache.tuscany.sca.impl.layout; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; - import org.apache.tuscany.sca.impl.artifacts.Component; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class EntityBuilder { private Document dom; - private final int initPoint = 100; + private final int initPoint = 100;//initial point private final int spaceX = Component.DEFAULT_WIDTH*2; private final int spaceY = Component.DEFAULT_HEIGHT*2; private ArrayList levelCount = new ArrayList();//keeps track of levels used in lanes private HashMap> connectedEntities = new HashMap>(); - public final int totalLength=0, totalHeight=0; + private int totalWidth=0; + private int totalHeight=0; + private Entity startEnt = null; + private String compositeName; public EntityBuilder(Document aDom){ dom = aDom; @@ -28,6 +28,8 @@ public class EntityBuilder { Entity[] elts = null; //get the root element Element docEle = dom.getDocumentElement(); + compositeName = docEle.getAttribute("name"); + System.out.println("compositeName "+compositeName); //get a nodelist of elements NodeList nl = docEle.getElementsByTagName("component"); @@ -47,51 +49,36 @@ public class EntityBuilder { } } - // //get a nodelist of elements - // NodeList nl2 = docEle.getElementsByTagName("wire"); - // if(nl2 != null && nl2.getLength() > 0 ) { - // - // for(int i = 0 ; i < nl2.getLength();i++) { - // Element nVal = (Element)nl2.item(i); - // String[] source = splitValues(nVal.getAttribute("source")); - // String[] target = splitValues(nVal.getAttribute("target")); - // - // for(Entity ent: elts){ - // if(ent.getComponentName().equalsIgnoreCase(source[0])){ - // ent.addAnAdjacentEntity(target[0]); - // ent.addToRefToSerMap(source[1], target[1]); - // break; - // } - // } - // } - // } - - //correctMap(elts); - positionEntities(elts); + calculateProperties(elts); print(elts); return elts; } - // private void correctMap(Entity[] elts) { - // - // for(Entity ent: elts){ - // for(Iterator it=ent.getReferenceToServiceMap().values().iterator();it.hasNext();){ - // String target = (String)it.next(); - // if(target.startsWith("#")){ - // for(Entity e: elts){ - // if(e.getComponentName().equals(target.substring(1))){ - // - // } - // } - // } - // } - // } - // } + private void calculateProperties(Entity[] elts) { + int level=0, lane=0; + + for(Entity ent: elts){ + level = max(level, ent.getLevel()); + lane = max(lane, ent.getLane()); + + } + totalHeight += spaceY*(level+1) + initPoint; + totalWidth += spaceX*(lane+1) + initPoint; + + System.err.println(totalHeight + " :: "+totalWidth); + } + + private int max(int a, int b){ + if(a>=b) + return a; + return b; + } + private void print(Entity[] elts) { for(Entity ent: elts){ @@ -101,7 +88,7 @@ public class EntityBuilder { } private void positionEntities(Entity[] ents){ - Entity startEnt = null; + for(Entity ent: ents){ if(ent.getAdjacentEntities().size() != 0 || ents.length==1){ setPosition(ent, initPoint, initPoint, 0, 0); @@ -112,31 +99,19 @@ public class EntityBuilder { } } -// for(Entity ent: ents){ -// if(ent != startEnt){ -// assignPosition(ent, ents); -// } -// } if(startEnt != null) assignPositions(ents, startEnt); - } - - private void assignPosition(Entity ent, Entity[] ents) { - - } private void assignPositions(Entity[] ents, Entity ent){ int i=0; - - //for(Entity ent: ents){ + if(ent.getAdjacentEntities().size()>0){ - if(ent.getAdjacentEntities().size()>0){ - System.out.println(ent.getComponentName()); + System.out.println(ent.getComponentName()); for(String name: ent.getAdjacentEntities()){ - System.out.println("eee "+name); + //System.out.println("eee "+name); for(Entity aEnt: ents){ i++; if(name.equalsIgnoreCase(aEnt.getComponentName())){ @@ -150,7 +125,7 @@ public class EntityBuilder { levelCount.add(lane, level+1); setPosition(aEnt, ent.getX()+spaceX, ent.getY()+spaceY*level, level, lane); } - if(i conns = connectedEntities.get(ent.getComponentName()); - System.err.println(conns.size()); - if(conns.size()>0){ - - for(String conn: conns){ - System.err.println("conn "+conn +" : "+ent.getComponentName()); + + + else{ + ArrayList conns = connectedEntities.get(ent.getComponentName()); + System.err.println(conns.size()); + if(conns.size()>0){ + + for(String conn: conns){ + System.err.println("conn "+conn +" : "+ent.getComponentName()); for(Entity e: ents){ if(e.getLane() == -1 && e.getComponentName().equals(conn)){ - + int lane = ent.getLane()-1; System.err.println(lane); int level = levelCount.get(lane); levelCount.add(lane, level+1); setPosition(e, ent.getX()-spaceX, ent.getY()+spaceY*level, level, lane); - -// if(levelCount.size()<= lane){ -// levelCount.add(lane, 1); -// setPosition(aEnt, ent.getX()+spaceX, ent.getY(), 0, lane); -// } -// else{ -// int level = levelCount.get(lane); -// levelCount.add(lane, level+1); -// setPosition(aEnt, ent.getX()+spaceX, ent.getY()+spaceY*level, level, lane); -// } -// System.err.println(e.getComponentName()); -// assignPositions(ents, e); + break; } } - } } } + } } private void setPosition(Entity ent, int x, int y, int level, int lane){ @@ -223,9 +186,8 @@ public class EntityBuilder { addToConnectedEntities(target.split("/")[0], ent.getComponentName()); } else if(!target.equals("")){ - //adding a #, since this is not the name of the service - //but the component which has only one service - ent.addToRefToSerMap(ref, "#"+target); + + ent.addToRefToSerMap(ref, target); ent.addAnAdjacentEntity(target); addToConnectedEntities(ent.getComponentName(), target); addToConnectedEntities(target, ent.getComponentName()); @@ -274,6 +236,22 @@ public class EntityBuilder { } } + public void setCompositeName(String compositeName) { + this.compositeName = compositeName; + } + + public String getCompositeName() { + return compositeName; + } + + public int getTotalWidth() { + return totalWidth; + } + + public int getTotalHeight() { + return totalHeight; + } + } diff --git a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/main/Main.java b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/main/Main.java index 24ec643779..813549a0a8 100755 --- a/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/main/Main.java +++ b/collaboration/GSoC-2011-Nirmal/CompositeDiagramGeneratorUsingBatik/src/main/java/org/apache/tuscany/sca/main/Main.java @@ -1,10 +1,16 @@ package org.apache.tuscany.sca.main; +import java.io.File; + +import org.apache.tuscany.sca.impl.diagram.DiagramGenerator; import org.apache.tuscany.sca.impl.io.XMLReader; +import org.apache.tuscany.sca.impl.io.XMLWriter; +import org.apache.tuscany.sca.impl.layout.Entity; import org.apache.tuscany.sca.impl.layout.EntityBuilder; import org.w3c.dom.Document; public class Main { + private static final String outFileDir = System.getProperty("user.dir")+"/output/"; /** * @param args @@ -15,8 +21,12 @@ public class Main { XMLReader reader = new XMLReader(); Document doc =reader.parseXMLFile(System.getProperty("user.dir")+"/input/composite2.xml"); EntityBuilder eb = new EntityBuilder(doc); - eb.buildEntities(); - + Entity[] ents =eb.buildEntities(); + DiagramGenerator dg = new DiagramGenerator(ents, eb.getTotalHeight(), eb.getTotalWidth(), eb.getCompositeName()); + Document svg =dg.buildSVGDocument(); + File outFile = new File(outFileDir+eb.getCompositeName()+"_diagram.svg"); + XMLWriter writer = new XMLWriter(); + writer.fileWriter(svg, outFile); } } -- cgit v1.2.3