1   package eu.fbk.knowledgestore;
2   
3   import eu.fbk.knowledgestore.Operation.*;
4   import eu.fbk.knowledgestore.data.Criteria;
5   import eu.fbk.knowledgestore.data.Data;
6   import eu.fbk.knowledgestore.data.XPath;
7   import eu.fbk.knowledgestore.vocabulary.KS;
8   import org.openrdf.model.URI;
9   
10  import javax.annotation.Nullable;
11  import java.io.Closeable;
12  import java.util.Map;
13  
14  /**
15   * A KnowledgeStore user session.
16   * <p>
17   * Interaction with the KnowledgeStore through its Java API occurs within a {@code Session}, which
18   * encapsulates user-specific configuration and, if authenticated, user credentials. A
19   * {@code Session} can be obtained via {@link KnowledgeStore#newSession()} (for anonymous user) or
20   * {@link KnowledgeStore#newSession(URI, String)} (authenticated user) and must be released with
21   * {@link #close()} to freed resources possibly allocated to the session.
22   * </p>
23   * <p>
24   * The identifier of the session's user, if any, is provided by method {@link #getUser()}, while
25   * the configured password is available using {@link #getPassword()}. Method
26   * {@link #getNamespaces()} returns a session-specific, modifiable prefix-to-namespace map that
27   * can be used for interacting with the KnowledgeStore; this map is initially filled with mappings
28   * from {@link Data#getNamespaceMap()}. Namespace mappings play a relevant role in the interaction
29   * with the KnowledgeStore, as they are used for encoding and decoding {@link XPath} expressions,
30   * merge {@link Criteria}s and SPARQL queries.
31   * </p>
32   * <p>
33   * Operations of the KnowledgeStore API can be invoked starting from a {@code Session} object and
34   * using <i>operation objects</i>. Every operation has its own {@link Operation} sub-class, an
35   * instance of which can be obtained by calling a method in {@code Session}; after an operation
36   * object is created, a number of common and operation-specific parameters can be set on this
37   * object to specify and control the execution of the operation, which is triggered by calling one
38   * of the {@code exec()} / {@code execXXX()} methods on the operation objects. More in details,
39   * the API operations in the following table are offered:
40   * </p>
41   * <blockquote>
42   * <table border="1">
43   * <tr>
44   * <th>{@code Session} method</th>
45   * <th>{@code Operation} sub-class</th>
46   * <th>Type</th>
47   * <th>Endpoint</th>
48   * <th>Description</th>
49   * </tr>
50   * <tr>
51   * <td>{@link #download()}</td>
52   * <td>{@link Download}</td>
53   * <td>read-only</td>
54   * <td>CRUD</td>
55   * <td>Downloads one or more resource representations.</td>
56   * </tr>
57   * <tr>
58   * <td>{@link #upload()}</td>
59   * <td>{@link Upload}</td>
60   * <td>read-write</td>
61   * <td>CRUD</td>
62   * <td>Uploads one or more resource representations.</td>
63   * </tr>
64   * <tr>
65   * <td>{@link #count(URI)}</td>
66   * <td>{@link Count}</td>
67   * <td>read-only</td>
68   * <td>CRUD</td>
69   * <td>Count the number of resources / mentions / entities / axioms that match an optional
70   * condition.</td>
71   * </tr>
72   * <tr>
73   * <td>{@link #retrieve(URI)}</td>
74   * <td>{@link Retrieve}</td>
75   * <td>read-only</td>
76   * <td>CRUD</td>
77   * <td>Retrieves selected properties of resources / mentions / entities / axioms that match an
78   * optional condition</td>
79   * </tr>
80   * <tr>
81   * <td>{@link #create(URI)}</td>
82   * <td>{@link Create}</td>
83   * <td>read-write</td>
84   * <td>CRUD</td>
85   * <td>Creates one or more new resources / mentions / entities / axioms.</td>
86   * </tr>
87   * <tr>
88   * <td>{@link #merge(URI)}</td>
89   * <td>{@link Merge}</td>
90   * <td>read-write</td>
91   * <td>CRUD</td>
92   * <td>Merges local record descriptions of resources / mentions / entities / axioms with
93   * descriptions stored in the KnowledgeStore, applying supplied merge criteria.</td>
94   * </tr>
95   * <tr>
96   * <td>{@link #update(URI)}</td>
97   * <td>{@link Update}</td>
98   * <td>read-write</td>
99   * <td>CRUD</td>
100  * <td>Updates one or more properties of all the resources / mentions / entities / axioms that
101  * match an optional condition.</td>
102  * </tr>
103  * <tr>
104  * <td>{@link #delete(URI)}</td>
105  * <td>{@link Delete}</td>
106  * <td>read-write</td>
107  * <td>CRUD</td>
108  * <td>Deletes all the resources / mentions / entities / axioms matching an optional condition.</td>
109  * </tr>
110  * <tr>
111  * <td>{@link #match()}</td>
112  * <td>{@link Match}</td>
113  * <td>read-only</td>
114  * <td>CRUD</td>
115  * <td>Returns all the &lt;resource, mention, entity, axioms&gt; combinations matching optional
116  * condition where the mention refers to the resource and entity and the axioms have been
117  * extracted from the mention.</td>
118  * </tr>
119  * <tr>
120  * <td>{@link #sparql(String, Object...)}</td>
121  * <td>{@link Sparql}</td>
122  * <td>read-only</td>
123  * <td>SPARQL</td>
124  * <td>Evaluates a SPARQL select / construct / describe / ask query on crystallized axiom data.</td>
125  * </tr>
126  * </table>
127  * </blockquote>
128  * <p>
129  * Note that operation marked as read-write may be forbidden to anonymous or non-privileged users,
130  * based on the security configuration of the KnowledgeStore. {@code Session} objects are thread
131  * safe. Object equality is used for comparing {@code Session} instances.
132  * </p>
133  */
134 public interface Session extends Closeable {
135 
136     /**
137      * Returns the username associated to this {@code Session}, if any.
138      * 
139      * @return the username, null if the {@code Session} is anonymous
140      * @throws IllegalStateException
141      *             if the {@code Session} has been closed
142      */
143     @Nullable
144     String getUsername() throws IllegalStateException;
145 
146     /**
147      * Returns the user password, if any.
148      * 
149      * @return the password
150      * @throws IllegalStateException
151      *             if the {@code Session} has been closed
152      */
153     @Nullable
154     String getPassword() throws IllegalStateException;
155 
156     /**
157      * Returns the modifiable namespace map of this {@code Session}. The map is backed by the
158      * default mappings of {@code Data#getNamespaceMap()}. New mappings are stored in the
159      * {@code Session} map and may override the default mappings. Removal of mappings is performed
160      * on the {@code Session} map, and may reactivate the default mapping as a consequence.
161      * 
162      * @return the modifiable namespace map of this {@code Session}
163      * @throws IllegalStateException
164      *             if the {@code Session} has been closed
165      */
166     Map<String, String> getNamespaces() throws IllegalStateException;
167 
168     /**
169      * Creates a new {@code Download} operation object.
170      * 
171      * @param resourceID
172      *            the ID of the resource whose representation should be downloaded
173      * @return the created {@code Download} operation object
174      * @throws IllegalStateException
175      *             if the {@code Session} has been closed
176      */
177     Download download(URI resourceID) throws IllegalStateException;
178 
179     /**
180      * Creates a new {@code Upload} operation object.
181      * 
182      * @param resourceID
183      *            the ID of the resource whose representation should be updated
184      * @return the created {@code Upload} operation object
185      * @throws IllegalStateException
186      *             if the {@code Session} has been closed
187      */
188     Upload upload(URI resourceID) throws IllegalStateException;
189 
190     /**
191      * Creates a new {@code Count} operation object for counting records of the type specified.
192      * 
193      * @param type
194      *            the URI of the type of record to count, either {@link KS#RESOURCE},
195      *            {@link KS#MENTION}, {@link KS#ENTITY} or {@link KS#AXIOM}
196      * @return the created {@code Count} operation object
197      * @throws IllegalStateException
198      *             if the {@code Session} has been closed
199      */
200     Count count(URI type) throws IllegalStateException;
201 
202     /**
203      * Creates a new {@code Retrieve} operation object for retrieving records of the type
204      * specified.
205      * 
206      * @param type
207      *            the URI of the type of record to retrieve, either {@link KS#RESOURCE},
208      *            {@link KS#MENTION}, {@link KS#ENTITY} or {@link KS#AXIOM}
209      * @return the created {@code Retrieve} operation object
210      * @throws IllegalStateException
211      *             if the {@code Session} has been closed
212      */
213     Retrieve retrieve(URI type) throws IllegalStateException;
214 
215     /**
216      * Creates a new {@code Create} operation object for storing new records of the type
217      * specified.
218      * 
219      * @param type
220      *            the URI of the type of record to create, either {@link KS#RESOURCE},
221      *            {@link KS#MENTION}, {@link KS#ENTITY} or {@link KS#AXIOM}
222      * @return the created {@code Create} operation object
223      * @throws IllegalStateException
224      *             if the {@code Session} has been closed
225      */
226     Create create(URI type) throws IllegalStateException;
227 
228     /**
229      * Creates a new {@code Merge} operation object for merging local record description of the
230      * type specified with descriptions stored in the KnowledgeStore.
231      * 
232      * @param type
233      *            the URI of the type of record to merge, either {@link KS#RESOURCE},
234      *            {@link KS#MENTION}, {@link KS#ENTITY} or {@link KS#AXIOM}
235      * @return the created {@code Merge} operation object
236      * @throws IllegalStateException
237      *             if the {@code Session} has been closed
238      */
239     Merge merge(URI type) throws IllegalStateException;
240 
241     /**
242      * Creates a new {@code Update} operation object for updating one or more properties of
243      * records of the type specified.
244      * 
245      * @param type
246      *            the URI of the type of record to update, either {@link KS#RESOURCE},
247      *            {@link KS#MENTION}, {@link KS#ENTITY} or {@link KS#AXIOM}
248      * @return the created {@code Update} operation object
249      * @throws IllegalStateException
250      *             if the {@code Session} has been closed
251      */
252     Update update(URI type) throws IllegalStateException;
253 
254     /**
255      * Creates a new {@code Delete} operation object for deleting records of the type specified.
256      * 
257      * @param type
258      *            the URI of the type of record to delete, either {@link KS#RESOURCE},
259      *            {@link KS#MENTION}, {@link KS#ENTITY} or {@link KS#AXIOM}
260      * @return the created {@code Delete} operation object
261      * @throws IllegalStateException
262      *             if the {@code Session} has been closed
263      */
264     Delete delete(URI type) throws IllegalStateException;
265 
266     /**
267      * Creates a new {@code Match} operation object for matching &lt;resource, mention, entity,
268      * axioms&gt; 4-tuples matching specific conditions.
269      * 
270      * @return the created {@code Match} operation object
271      * @throws IllegalStateException
272      *             if the {@code Session} has been closed
273      */
274     Match match() throws IllegalStateException;
275 
276     /**
277      * Creates a new {@code Sparql} operation object for evaluating a SPARQL query on crystallized
278      * axiom data.
279      * 
280      * @param expression
281      *            the SPARQL query expression, not null
282      * @param arguments
283      *            arguments to be injected in {@code $$} placeholders in the query expression; can
284      *            be {@code URI}s, {@code Literal}s or scalar values that can be converted to
285      *            {@code Literal}s (e.g., strings, integers)
286      * @return the created {@code Sparql} operation object
287      * @throws IllegalStateException
288      *             if the {@code Session} has been closed
289      */
290     Sparql sparql(String expression, Object... arguments) throws IllegalStateException;
291 
292     SparqlUpdate sparqlupdate() throws IllegalStateException;
293     SparqlDelete sparqldelete() throws IllegalStateException;
294 
295     /**
296      * Tests whether this {@code Session} instance has been closed.
297      * 
298      * @return true, if this {@code Session} instance has been closed
299      */
300     boolean isClosed();
301 
302     /**
303      * {@inheritDoc} Closes the {@code Session} instance, releasing any resource possibly
304      * allocated. Calling this method additional times has no effect. After this method is called,
305      * calls to other method of the {@code Session} interface will result in
306      * {@link IllegalStateException}s to be thrown.
307      */
308     @Override
309     void close();
310 
311 }