123 | | private static List<String> convertJ2Path(String jpath) throws JSONException{ |
| 122 | private Path convertJ2Path(String jpath) throws JSONException{ |
| 123 | |
| 124 | JSONObject object = new JSONObject(jpath); |
| 125 | String startClass = object.getJSONObject("startClassURI").toString(); |
| 126 | int width = Integer.parseInt(object.getJSONObject("width").toString()); |
| 127 | JSONArray classLinks = object.getJSONArray("classLinks"); |
| 128 | JSONObject jsonObject; |
| 129 | List <ClassLink> list = null; |
| 130 | |
| 131 | for (int i=0;i<classLinks.length();i++) |
| 132 | { |
| 133 | jsonObject = classLinks.getJSONObject(i); |
| 134 | String propertyURI = jsonObject.getJSONObject("propertyURI").toString(); |
| 135 | String linkedClassURI = jsonObject.getJSONObject("linkedClassURI").toString(); |
| 136 | String linkedLiteralDatatypeURI = jsonObject.getJSONObject("linkedLiteralDatatypeURI").toString(); |
| 137 | int numOfLinks = Integer.parseInt(jsonObject.getJSONObject("numOfLinks").toString()); |
| 138 | int numOfLinkedInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedInstances").toString()); |
| 139 | int numOfOriginInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
| 140 | int numOfOriginClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
| 141 | int numOfLinkedClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedClassInstances").toString()); |
| 142 | |
| 143 | ClassLink classLink =new ClassLink(propertyURI, linkedClassURI, linkedLiteralDatatypeURI, null, |
| 144 | numOfLinks, numOfOriginInstances, numOfLinkedInstances, |
| 145 | numOfOriginClassInstances, numOfLinkedClassInstances, |
| 146 | false, false); |
| 147 | list.add(classLink); |
| 148 | } |
| 149 | Path path = new Path(startClass, list, width); |
| 150 | return path; |
| 151 | } |
| 152 | |
| 153 | private String convertPath2SPARQL(Path path) throws Exception{ |
| 154 | if( path == null ){ |
| 155 | throw new Exception("Path is null."); |
| 156 | } |
| 157 | String startClass = path.getStartClass(); |
| 158 | List<ClassLink> classLinks = path.getClassLinks(); |
| 159 | |
| 160 | StringBuffer queryStr = new StringBuffer(); |
| 161 | StringBuffer selStr = new StringBuffer(); |
| 162 | StringBuffer whereStr = new StringBuffer(); |
| 163 | // if(num==0){ |
| 164 | int num = classLinks.size(); |
| 165 | // } |
| 166 | |
| 167 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
| 168 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
| 169 | |
| 170 | selStr.append("SELECT "); |
| 171 | whereStr.append("WHERE { \n"); |
| 172 | |
| 173 | String properties = null; |
| 174 | String objectClasses = null; |
| 175 | String subjectClasses = null; |
| 176 | Direction direction = null; |
| 177 | int i = 0; |
| 178 | int k = 0; |
| 179 | for (ClassLink link :classLinks ) |
| 180 | { |
| 181 | properties = link.getPropertyURI(); |
| 182 | objectClasses = link.getLinkedClassURI(); |
| 183 | direction = link.getDirection(); |
| 184 | |
| 185 | if (i==0) |
| 186 | subjectClasses = startClass; |
| 187 | |
| 188 | selStr.append("?c").append(i).append(" "); |
| 189 | selStr.append("?l").append(i).append(" "); |
| 190 | |
| 191 | if(i == classLinks.size()) |
| 192 | selStr.append("\n"); |
| 193 | |
| 194 | |
| 195 | whereStr.append("?c").append(i). |
| 196 | append(" rdf:type "). |
| 197 | append("<"). |
| 198 | append(subjectClasses). |
| 199 | append(">"). |
| 200 | append(".\n"); |
| 201 | |
| 202 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
| 203 | |
| 204 | |
| 205 | if(direction == Direction.forward) |
| 206 | { |
| 207 | whereStr.append("?c").append(i).append(" "); |
| 208 | whereStr.append("<").append(properties).append("> "); |
| 209 | whereStr.append("?c").append(i+1).append(".\n"); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | whereStr.append("?c").append(i+1).append(" "); |
| 214 | whereStr.append("<").append(properties).append("> "); |
| 215 | whereStr.append("?c").append(i).append(".\n"); |
| 216 | } |
| 217 | |
| 218 | subjectClasses = objectClasses; |
| 219 | i++; |
| 220 | k++; |
| 221 | if(k>=num){ |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | selStr.append("?c").append(i).append(" \n"); |
| 227 | selStr.append("?l").append(i).append(" \n"); |
| 228 | whereStr.append("?c").append(i).append(" rdf:type "). |
| 229 | append("<"). |
| 230 | append(subjectClasses). |
| 231 | append(">"). |
| 232 | append(".\n"); |
| 233 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
| 234 | |
| 235 | |
| 236 | queryStr.append(selStr).append(whereStr); |
| 237 | |
| 238 | queryStr.append("}"); |
| 239 | //OPTIONAL |
| 240 | queryStr.append("LIMIT 100\n");; |
| 241 | |
| 242 | //System.out.println(queryStr); |
| 243 | return queryStr.toString(); |
| 244 | |
| 245 | |
| 246 | } |
| 247 | private static List<String> convertJ2Path2(String jpath) throws JSONException{ |