|
| 1 | +package model.conceptual; |
| 2 | + |
| 3 | +import javafx.event.EventTarget; |
| 4 | +import javafx.geometry.Bounds; |
| 5 | +import javafx.scene.shape.Ellipse; |
| 6 | +import javafx.scene.shape.Shape; |
| 7 | +import javafx.scene.text.Text; |
| 8 | +import javafx.util.Pair; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | + |
| 12 | +public class Class extends Vertex { |
| 13 | + private boolean isBlankNode; |
| 14 | + private boolean isIri; |
| 15 | + private String typeDefinition; |
| 16 | + private String rdfsLabel, rdfsComment; |
| 17 | + |
| 18 | + private static char nextBlankNodeName = (char) 96; |
| 19 | + private static final ArrayList<Character> blankNodeNames = new ArrayList<>(); |
| 20 | + |
| 21 | + |
| 22 | + /** |
| 23 | + * Constructor for a Vertex with meta-information, namely it's human-readable label and comment (defined in RDFS). |
| 24 | + * |
| 25 | + * @param element the container of the shape and name of the Vertex. |
| 26 | + * @param rdfsLabel the human-readable label of the Vertex. |
| 27 | + * @param rdfsComment the comment regarding the Vertex. |
| 28 | + * @throws OutsideElementException if the container is not castable to a stackpane (aka it is outside the canvas. |
| 29 | + */ |
| 30 | + public Class(EventTarget element, String rdfsLabel, String rdfsComment) throws OutsideElementException { |
| 31 | + this(element); |
| 32 | + this.rdfsLabel = rdfsLabel; |
| 33 | + this.rdfsComment = rdfsComment; |
| 34 | + } |
| 35 | + |
| 36 | + public Class(EventTarget container) throws OutsideElementException { |
| 37 | + super(container); |
| 38 | + |
| 39 | + if (super.name.charAt(0) == '_') { |
| 40 | + ((Text) super.container.getChildren().get(1)).setText(""); |
| 41 | + isBlankNode = true; |
| 42 | + isIri = false; |
| 43 | + } else if (this.name.matches("https?:.*|mailto:.*")){ |
| 44 | + isBlankNode = false; |
| 45 | + isIri = true; |
| 46 | + } else { |
| 47 | + isBlankNode = false; |
| 48 | + isIri = false; |
| 49 | + } |
| 50 | + |
| 51 | + Shape shape = (Shape) super.container.getChildren().get(0); |
| 52 | + if (shape instanceof Ellipse && shape.getStrokeDashArray().size() == 0) |
| 53 | + super.elementType = GraphElemType.GLOBAL_CLASS; |
| 54 | + else if (shape instanceof Ellipse) super.elementType = GraphElemType.INSTANCE_CLASS; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Snap the users property arrow as close to the edge of the shape as possible. This is fairly straightforward for |
| 59 | + * a Literal, but is much more involved for a Class. |
| 60 | + * |
| 61 | + * @param subX the x value of the subject of the property arrow - needed to find the intercept of the line and the |
| 62 | + * ellipse. |
| 63 | + * @param subY the y value of the subject of the property arrow - needed to find the intercept of the line and the |
| 64 | + * ellipse. |
| 65 | + * @param x the x value of the users click. |
| 66 | + * @param y the y value of the users click. |
| 67 | + */ |
| 68 | + @Override |
| 69 | + public void setSnapTo(double subX, double subY, double x, double y) { |
| 70 | + this.snapToCenter(); |
| 71 | + |
| 72 | + Bounds b = container.getBoundsInParent(); |
| 73 | + Ellipse e = (Ellipse) container.getChildren().get(0); |
| 74 | + |
| 75 | + ArrayList<Pair<Double, Double>> coords = getIntersection( |
| 76 | + subX, subY, |
| 77 | + this.x, this.y, |
| 78 | + b.getMinX() + e.getRadiusX(), b.getMinY() + e.getRadiusY(), |
| 79 | + e.getRadiusX(), e.getRadiusY() |
| 80 | + ); |
| 81 | + |
| 82 | + if (coords.size() == 1) { |
| 83 | + this.x = coords.get(0).getKey(); |
| 84 | + this.y = coords.get(0).getValue(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Find the intersection(s) of the property arrow and the Object ellipse. Should usually be a single intersection, |
| 90 | + * multiple denote a pass across the shape rather than into it. |
| 91 | + * @param x1 subject point x-value. |
| 92 | + * @param y1 subject point y-value. |
| 93 | + * @param x2 object x-value. |
| 94 | + * @param y2 object y-value. |
| 95 | + * @param midX x-value of the midpoint of the ellipse. |
| 96 | + * @param midY y-value of the midpoint of the ellipse. |
| 97 | + * @param h the major axis of the ellipse. |
| 98 | + * @param v the minor axis of the ellipse. |
| 99 | + * @return list of coordinates in which the ellipse and the line intersect. |
| 100 | + */ |
| 101 | + private static ArrayList<Pair<Double, Double>> getIntersection( |
| 102 | + double x1, double y1, |
| 103 | + double x2, double y2, |
| 104 | + double midX, double midY, |
| 105 | + double h, double v) { |
| 106 | + ArrayList<Pair<Double, Double>> points = new ArrayList<>(); |
| 107 | + |
| 108 | + x1 -= midX; |
| 109 | + y1 -= midY; |
| 110 | + x2 -= midX; |
| 111 | + y2 -= midY; |
| 112 | + |
| 113 | + if (x1 == x2) { |
| 114 | + double y = (v/h)*Math.sqrt(h*h-x1*x1); |
| 115 | + if (Math.min(y1, y2) <= y && y <= Math.max(y1, y2)) points.add(new Pair<>(x1+midX, y+midY)); |
| 116 | + if (Math.min(y1, y2) <= -y && -y <= Math.max(y1, y2)) points.add(new Pair<>(x1+midX, -y+midY)); |
| 117 | + } else { |
| 118 | + double a = (y2 - y1) / (x2 - x1); |
| 119 | + double b = (y1 - a*x1); |
| 120 | + |
| 121 | + double r = a*a*h*h + v*v; |
| 122 | + double s = 2*a*b*h*h; |
| 123 | + double t = h*h*b*b - h*h*v*v; |
| 124 | + |
| 125 | + double d = s*s - 4*r*t; |
| 126 | + |
| 127 | + if (d > 0) { |
| 128 | + double xi1 = (-s+Math.sqrt(d))/(2*r); |
| 129 | + double xi2 = (-s-Math.sqrt(d))/(2*r); |
| 130 | + |
| 131 | + double yi1 = a*xi1+b; |
| 132 | + double yi2 = a*xi2+b; |
| 133 | + |
| 134 | + if (isPointInLine(x1, y1, x2, y2, xi1, yi1)) points.add(new Pair<>(xi1+midX, yi1+midY)); |
| 135 | + if (isPointInLine(x1, y1, x2, y2, xi2, yi2)) points.add(new Pair<>(xi2+midX, yi2+midY)); |
| 136 | + } else if (d == 0) { |
| 137 | + double xi = -s/(2*r); |
| 138 | + double yi = a*xi+b; |
| 139 | + |
| 140 | + if (isPointInLine(x1, y1, x2, y2, xi, yi)) points.add(new Pair<>(xi+midX, yi+midY)); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return points; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Determines if a given point is within the the given line. |
| 149 | + * @param x1 subject x-value. |
| 150 | + * @param y1 subject y-value. |
| 151 | + * @param x2 object x-value. |
| 152 | + * @param y2 object y-value. |
| 153 | + * @param px given points x-value. |
| 154 | + * @param py given points y-value. |
| 155 | + * @return whether the point is within the line or not. |
| 156 | + */ |
| 157 | + private static boolean isPointInLine(double x1, double y1, double x2, double y2, double px, double py) { |
| 158 | + double xMin = Math.min(x1, x2); |
| 159 | + double xMax = Math.max(x1, x2); |
| 160 | + |
| 161 | + double yMin = Math.min(y1, y2); |
| 162 | + double yMax = Math.max(y1, y2); |
| 163 | + |
| 164 | + return (xMin <= px && px <= xMax) && (yMin <= py && py <= yMax); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void addOutgoingEdge(Edge e) { |
| 169 | + if (e.getName().matches("a|rdf:type|http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) |
| 170 | + typeDefinition = e.getObject().name; |
| 171 | + else |
| 172 | + super.addOutgoingEdge(e); |
| 173 | + } |
| 174 | + |
| 175 | + public boolean isBlank() { return isBlankNode; } |
| 176 | + |
| 177 | + public boolean isIri() { return isIri; } |
| 178 | + |
| 179 | + public static char getNextBlankNodeName() { |
| 180 | + nextBlankNodeName += 1; |
| 181 | + blankNodeNames.add(nextBlankNodeName); |
| 182 | + return nextBlankNodeName; |
| 183 | + } |
| 184 | + |
| 185 | + public static ArrayList<Character> getBlankNodeNames(){ return blankNodeNames; } |
| 186 | + |
| 187 | + public String getTypeDefinition() { return typeDefinition; } |
| 188 | + |
| 189 | + public String getRdfsLabel() { |
| 190 | + return rdfsLabel; |
| 191 | + } |
| 192 | + |
| 193 | + public String getRdfsComment() { |
| 194 | + return rdfsComment; |
| 195 | + } |
| 196 | +} |
0 commit comments