/
KBCaller.java
187 lines (159 loc) · 4.24 KB
/
KBCaller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import com.sun.net.httpserver.Headers;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.Scanner;
import java.util.ArrayList;
public class KBCaller
{
private String url;
public KBCaller( String url )
{
this.url = url;
}
/*
* Minimalist self-contained partial JSON parser, for if you
* want to get results as ArrayList<String> instead of as String
*/
public ArrayList<String> parse_json( String json ) throws IOException
{
return parse_json_dont_clutter_top_of_file_with_code( json );
}
/*
* Get all subterms of given term
*/
public String subterms( String exp ) throws IOException
{
return launch_http( "/subterms/" + encode( exp ) );
}
/*
* Get all parents (i.e., direct superclasses) of a given term.
*/
public String parents( String exp ) throws IOException
{
return launch_http("/parents/" + encode( exp ) );
}
/*
* Get all children (i.e., direct subclasses) of a given term.
*/
public String children( String exp ) throws IOException
{
return launch_http("/children/" + encode( exp ) );
}
/*
* Get all sublings of given term.
* Term X is a subling of term Y if there is an immediate
* superterm Z of X such that Y is an immediate subterm
* of Z.
*/
public String siblings( String exp ) throws IOException
{
return launch_http( "/siblings/" + encode( exp ) );
}
/*
* Get subterms of a given term, in a hierarchical JSON format
*/
public String subhierarchy( String exp ) throws IOException
{
return launch_http( "/subhierarchy/" + encode( exp ) );
}
/*
* Get all terms equivalent to given term
*/
public String eqterms( String exp ) throws IOException
{
return launch_http( "/eqterms/" + encode( exp ) );
}
/*
* Get all terms equivalent to, or subclass of, given term
*/
public String terms( String exp ) throws IOException
{
return launch_http( "/terms/" + encode( exp ) );
}
/*
* Get all individuals that are instances of given term
*/
public String instances( String exp ) throws IOException
{
return launch_http( "/instances/" + encode( exp ) );
}
/*
* Get all rdfs:labels of given term
*/
public String labels( String exp ) throws IOException
{
return launch_http( "/labels/" + encode( exp ) );
}
/*
* Get all terms with given rdfs:label
*/
public String search( String label ) throws IOException
{
return launch_http( "/search/" + encode( label ) );
}
/*
* Attempt to add label to given term
*/
public void addlabel( String exp ) throws IOException
{
launch_http( "/addlabel/" + encode( exp ) );
}
private String encode( String exp )
{
try
{
return URLEncoder.encode( exp, "UTF-8" );
}
catch( Exception e )
{
return exp;
}
}
private String launch_http( String cmd ) throws IOException
{
URL u;
HttpURLConnection c;
try
{
u = new URL(url + cmd);
c = (HttpURLConnection) u.openConnection();
c.setConnectTimeout(2000);
c.setReadTimeout(5000);
}
catch ( Exception e )
{
throw new IOException("Could not connect to OWLKB");
}
c.setRequestProperty( "Accept", "application/json" );
Scanner sc = null;
try
{
sc = new Scanner(c.getInputStream( ) );
}
catch( Exception e )
{
throw new IOException("Could not read from OWLKB");
}
return sc.useDelimiter("\\A").next();
}
private ArrayList<String> parse_json_dont_clutter_top_of_file_with_code( String j ) throws IOException
{
ArrayList<String> L = new ArrayList<String>();
if ( j.charAt(0) != '[' || j.charAt(j.length()-1) != ']' )
throw new IOException( "String is not a JSON list" );
String inside = j.substring(1, j.length()-1).trim();
String[] split = inside.split(",");
for ( int i = 0; i < split.length; i++ )
{
String entry = split[i].trim();
if ( entry.charAt(0) != '\'' || entry.charAt(entry.length()-1) != '\'' )
throw new IOException( "String is not a properly enquoted JSON list ("+entry+")" );
L.add( entry.substring(1,entry.length()-1) );
}
return L;
}
}