5555import org .dllearner .utilities .owl .OWLClassExpressionToSPARQLConverter ;
5656import org .dllearner .utilities .sparql .LogStepProvider ;
5757import org .dllearner .utilities .sparql .QueryExecutionFactoryQueryLogging ;
58+ import org .openrdf .model .vocabulary .XMLSchema ;
5859import org .semanticweb .owlapi .model .*;
5960import org .semanticweb .owlapi .util .OWLObjectDuplicator ;
6061import org .semanticweb .owlapi .vocab .OWL2Datatype ;
6364import org .slf4j .LoggerFactory ;
6465import org .slf4j .Marker ;
6566import org .slf4j .helpers .BasicMarkerFactory ;
67+ import org .springframework .beans .factory .DisposableBean ;
6668import uk .ac .manchester .cs .owl .owlapi .OWLDataFactoryImpl ;
6769
68- import java .io .FileDescriptor ;
69- import java .io .FileNotFoundException ;
70- import java .io .FileOutputStream ;
71- import java .io .OutputStream ;
70+ import javax .annotation .PreDestroy ;
71+ import java .io .*;
7272import java .util .*;
7373import java .util .Map .Entry ;
7474import java .util .concurrent .TimeUnit ;
9292 *
9393 */
9494@ ComponentAnn (name = "SPARQL Reasoner" , shortName = "spr" , version = 0.1 )
95- public class SPARQLReasoner extends AbstractReasonerComponent implements SchemaReasoner , IndividualReasoner , LogStepProvider {
95+ public class SPARQLReasoner extends AbstractReasonerComponent implements SchemaReasoner , IndividualReasoner , LogStepProvider , DisposableBean {
9696
9797 private static final Logger logger = LoggerFactory .getLogger (SPARQLReasoner .class );
9898 private final static Marker sparql_debug = new BasicMarkerFactory ().getMarker ("SD" );
99+ private static final String REQUEST_LOG_BASE = "http://dl-learner.org/spr-log/" ;
99100
100101 public enum PopularityType {
101102 CLASS , OBJECT_PROPERTY , DATA_PROPERTY
@@ -130,6 +131,7 @@ public enum PopularityType {
130131
131132 private long stepCount ;
132133 private Model requestLog ;
134+ private Model requestLogMeta ;
133135 private Resource currentStep ;
134136 private OutputStream requestLogStream ;
135137
@@ -176,7 +178,28 @@ public SPARQLReasoner(QueryExecutionFactory qef) {
176178 this ();
177179 this .qef = qef ;
178180 }
179-
181+
182+ //@PreDestroy
183+ public void destroy () {
184+ if (requestLogStream !=null ) {
185+ dumpRequestLog ();
186+ RDFDataMgr .write (requestLogStream ,requestLogMeta ,RDFFormat .NTRIPLES_UTF8 );
187+ requestLogMeta .close ();
188+ try {
189+ requestLogStream .flush ();
190+ } catch (IOException e ) {
191+ //ignore
192+ }
193+ if (requestLogStream !=System .out ) {
194+ try {
195+ requestLogStream .close ();
196+ } catch (IOException e ) {
197+ //ignore
198+ }
199+ }
200+ }
201+ }
202+
180203 /* (non-Javadoc)
181204 * @see org.dllearner.core.Component#init()
182205 */
@@ -185,6 +208,7 @@ public void init() throws ComponentInitException {
185208 classPopularityMap = new HashMap <>();
186209 if (requestLogging ) {
187210 stepCount = 0L ;
211+ requestLogMeta = ModelFactory .createDefaultModel ();
188212 if (requestLogFile !=null ) {
189213 try {
190214 requestLogStream = new FileOutputStream (requestLogFile );
@@ -237,30 +261,27 @@ public void init() throws ComponentInitException {
237261 private void setCurrentStep (String stepType ) {
238262 dumpRequestLog ();
239263 stepCount ++;
240- currentStep = requestLog .createResource ("step-" +stepCount
241- /*getStepUri()*/ , requestLog .createResource (stepType ));
264+ currentStep = requestLog .createResource ("step-" +stepCount , requestLog .createResource (stepType ));
265+ currentStep .addLiteral (requestLog .createProperty ("stepCount" ), stepCount );
266+ currentStep .addProperty (RDF .type , requestLog .createResource ("sprStep" ));
267+ requestLogMeta .add (requestLogMeta .createResource (stepType ), RDFS .subClassOf , requestLogMeta .createResource ("sprStep" ));
242268 }
243269
244270 private void dumpRequestLog () {
245- if (requestLog !=null ){
271+ if (requestLog ==null ) {
272+ Model preamble = ModelFactory .createDefaultModel ();
273+ preamble .setNsPrefix ("" , REQUEST_LOG_BASE );
274+ preamble .setNsPrefix (XMLSchema .PREFIX , XMLSchema .NAMESPACE );
275+ (new PrintStream (requestLogStream , true )).println ("@base <" + REQUEST_LOG_BASE + "> ." );
276+ RDFDataMgr .write (requestLogStream ,preamble ,RDFFormat .TURTLE );
277+ preamble .close ();
278+ } else {
246279 RDFDataMgr .write (requestLogStream ,requestLog ,RDFFormat .NTRIPLES_UTF8 );
247280 requestLog .close ();
248281 }
249282 requestLog = ModelFactory .createDefaultModel ();
250283 }
251284
252- @ Override
253- protected void finalize () throws Throwable {
254- if (requestLogStream !=null ) {
255- dumpRequestLog ();
256- requestLogStream .flush ();
257- if (requestLogStream !=System .out ) {
258- requestLogStream .close ();
259- }
260- }
261- super .finalize ();
262- }
263-
264285 public QueryExecutionFactory getQueryExecutionFactory () {
265286 return qef ;
266287 }
0 commit comments