View Javadoc
1   package de.tud.plt.r43ples.existentobjects;
2   
3   import de.tud.plt.r43ples.exception.InternalErrorException;
4   import de.tud.plt.r43ples.management.Config;
5   import de.tud.plt.r43ples.triplestoreInterface.TripleStoreInterfaceSingleton;
6   import org.apache.jena.query.QuerySolution;
7   import org.apache.jena.query.ResultSet;
8   import org.apache.logging.log4j.LogManager;
9   import org.apache.logging.log4j.Logger;
10  
11  /**
12   * Provides information of an already existent statement.
13   *
14   * @author Stephan Hensel
15   */
16  public class Statement {
17  
18      /** The logger. **/
19      private Logger logger = LogManager.getLogger(Statement.class);
20  
21      /** The statement URI. */
22      private String statementURI;
23      /** The subject. */
24      private String subject;
25      /** The predicate. */
26      private String predicate;
27      /** The object. */
28      private String object;
29      /** The identification whether the object is a resource (true) or a literal (false). */
30      private boolean isResource;
31  
32      /** The revision graph URI. */
33      private String revisionGraphURI;
34      /** The corresponding revision graph. */
35      private RevisionGraph revisionGraph;
36  
37  
38      /**
39       * The constructor.
40       *
41       * @param revisionGraph the revision graph
42       * @param statementURI the statement URI
43       * @throws InternalErrorException
44       */
45      public Statement(RevisionGraph revisionGraph, String statementURI) throws InternalErrorException {
46          this.revisionGraph = revisionGraph;
47          this.revisionGraphURI = this.revisionGraph.getRevisionGraphUri();
48          this.statementURI = statementURI;
49  
50          retrieveAdditionalInformation();
51      }
52  
53      /**
54       * The constructor.
55       *
56       * @param revisionGraph the revision graph
57       * @param statementURI the statement URI
58       * @param subject the subject
59       * @param predicate the predicate
60       * @param object the object
61       * @param isResource the identification whether the object is a resource (true) or a literal (false)
62       */
63      public Statement(RevisionGraph revisionGraph, String statementURI, String subject, String predicate, String object, boolean isResource) {
64          this.revisionGraph = revisionGraph;
65          this.revisionGraphURI = this.revisionGraph.getRevisionGraphUri();
66          this.statementURI = statementURI;
67          this.subject = subject;
68          this.predicate = predicate;
69          this.object = object;
70          this.isResource = isResource;
71      }
72  
73      /**
74       * Calculate additional information of the current statement and store this information to local variables.
75       *
76       * @throws InternalErrorException
77       */
78      private void retrieveAdditionalInformation() throws InternalErrorException {
79          logger.info("Get additional information of current statement URI " + statementURI + ".");
80          String query = Config.prefixes + String.format(""
81                  + "SELECT ?subject ?predicate ?object "
82                  + "WHERE { GRAPH  <%s> {"
83                  + "	<%s> a rdf:Statement; "
84                  + "	 rdf:subject ?subject; "
85                  + "  rdf:predicate ?predicate; "
86                  + "  rdf:object ?object. "
87                  + "} }", revisionGraphURI, statementURI);
88          this.logger.debug(query);
89          ResultSet resultSet = TripleStoreInterfaceSingleton.get().executeSelectQuery(query);
90          if (resultSet.hasNext()) {
91              QuerySolution qs = resultSet.next();
92              subject = qs.getResource("?subject").toString();
93              predicate = qs.getResource("?predicate").toString();
94              if (qs.getResource("?object") != null) {
95                  object = qs.getResource("?object").toString();
96                  isResource = true;
97              } else {
98                  if (qs.getLiteral("?object") != null) {
99                      object = qs.getLiteral("?object").toString();
100                     isResource = false;
101                 }
102             }
103         } else {
104             throw new InternalErrorException("No additional information found for statement URI " + statementURI + ".");
105         }
106     }
107 
108     /**
109      * Get the statement URI.
110      *
111      * @return the statement URI
112      */
113     public String getStatementURI() {
114         return statementURI;
115     }
116 
117     /**
118      * Get the subject.
119      *
120      * @return the subject
121      */
122     public String getSubject() {
123         return subject;
124     }
125 
126     /**
127      * Get the predicate.
128      *
129      * @return the predicate
130      */
131     public String getPredicate() {
132         return predicate;
133     }
134 
135     /**
136      * Get the object.
137      *
138      * @return the object
139      */
140     public String getObject() {
141         return object;
142     }
143 
144     /**
145      * Get the identification whether the object is a resource (true) or a literal (false).
146      *
147      * @return true if the object is a resource and false if the object is a literal
148      */
149     public boolean isResource() {
150         return isResource;
151     }
152 
153     /**
154      * Get the revision graph.
155      *
156      * @return the revision graph
157      */
158     public RevisionGraph getRevisionGraph() {
159         return revisionGraph;
160     }
161 
162 }