@@ -53,46 +53,55 @@ public class ReasoningBenchmark {
5353 @ Benchmark
5454 public void noReasoning () throws IOException {
5555 SailRepository sail = new SailRepository (new MemoryStore ());
56+ try {
57+ try (SailRepositoryConnection connection = sail .getConnection ()) {
58+ connection .begin ();
5659
57- try ( SailRepositoryConnection connection = sail . getConnection ()) {
58- connection . begin ( );
60+ addRequiredResource ( connection , "schema.ttl" );
61+ addAllDataSingleTransaction ( connection );
5962
60- connection .add ( resourceAsStream ( "schema.ttl" ), "" , RDFFormat . TURTLE );
61- addAllDataSingleTransaction ( connection );
62-
63- connection . commit ();
63+ connection .commit ( );
64+ }
65+ } finally {
66+ sail . shutDown ();
6467 }
6568 }
6669
6770 @ Benchmark
6871 public void noReasoningMultipleTransactions () throws IOException {
6972 SailRepository sail = new SailRepository (new MemoryStore ());
73+ try {
74+ try (SailRepositoryConnection connection = sail .getConnection ()) {
7075
71- try (SailRepositoryConnection connection = sail .getConnection ()) {
76+ connection .begin ();
77+ addRequiredResource (connection , "schema.ttl" );
78+ connection .commit ();
7279
73- connection .begin ();
74- connection .add (resourceAsStream ("schema.ttl" ), "" , RDFFormat .TURTLE );
75- connection .commit ();
76-
77- addAllDataMultipleTransactions (connection );
80+ addAllDataMultipleTransactions (connection );
7881
82+ }
83+ } finally {
84+ sail .shutDown ();
7985 }
8086 }
8187
8288 @ Benchmark
8389 public void forwardChainingSchemaCachingRDFSInferencer () throws IOException {
8490 SailRepository sail = new SailRepository (new SchemaCachingRDFSInferencer (new MemoryStore ()));
91+ try {
92+ try (SailRepositoryConnection connection = sail .getConnection ()) {
93+ connection .begin ();
8594
86- try ( SailRepositoryConnection connection = sail . getConnection ()) {
87- connection . begin ( );
95+ addRequiredResource ( connection , "schema.ttl" );
96+ addAllDataSingleTransaction ( connection );
8897
89- connection .add ( resourceAsStream ( "schema.ttl" ), "" , RDFFormat . TURTLE );
90- addAllDataSingleTransaction ( connection );
98+ connection .commit ( );
99+ }
91100
92- connection .commit ();
101+ checkSize (sail );
102+ } finally {
103+ sail .shutDown ();
93104 }
94-
95- checkSize (sail );
96105 }
97106
98107 private void checkSize (SailRepository sail ) {
@@ -115,89 +124,121 @@ private int getSize(SailRepository sail) {
115124 @ Benchmark
116125 public void forwardChainingSchemaCachingRDFSInferencerMultipleTransactions () throws IOException {
117126 SailRepository sail = new SailRepository (new SchemaCachingRDFSInferencer (new MemoryStore ()));
127+ try {
128+ try (SailRepositoryConnection connection = sail .getConnection ()) {
118129
119- try (SailRepositoryConnection connection = sail .getConnection ()) {
120-
121- connection .begin ();
122- connection .add (resourceAsStream ("schema.ttl" ), "" , RDFFormat .TURTLE );
123- connection .commit ();
130+ connection .begin ();
131+ addRequiredResource (connection , "schema.ttl" );
132+ connection .commit ();
124133
125- addAllDataMultipleTransactions (connection );
134+ addAllDataMultipleTransactions (connection );
126135
136+ }
137+ checkSize (sail );
138+ } finally {
139+ sail .shutDown ();
127140 }
128- checkSize (sail );
129-
130141 }
131142
132143 @ Benchmark
133144 public void forwardChainingSchemaCachingRDFSInferencerSchema () throws IOException {
134- SailRepository sail = new SailRepository (new SchemaCachingRDFSInferencer (new MemoryStore (), createSchema ()));
135-
136- try (SailRepositoryConnection connection = sail .getConnection ()) {
137- connection .begin ();
138- addAllDataSingleTransaction (connection );
139- connection .commit ();
145+ SailRepository schema = createSchema ();
146+ SailRepository sail = new SailRepository (new SchemaCachingRDFSInferencer (new MemoryStore (), schema ));
147+ try {
148+ try (SailRepositoryConnection connection = sail .getConnection ()) {
149+ connection .begin ();
150+ addAllDataSingleTransaction (connection );
151+ connection .commit ();
152+ }
153+ checkSize (sail );
154+ } finally {
155+ try {
156+ sail .shutDown ();
157+ } finally {
158+ schema .shutDown ();
159+ }
140160 }
141- checkSize (sail );
142-
143161 }
144162
145163 @ Benchmark
146164 public void forwardChainingSchemaCachingRDFSInferencerMultipleTransactionsSchema () throws IOException {
147- SailRepository sail = new SailRepository (new SchemaCachingRDFSInferencer (new MemoryStore (), createSchema ()));
148-
149- try (SailRepositoryConnection connection = sail .getConnection ()) {
150- addAllDataMultipleTransactions (connection );
165+ SailRepository schema = createSchema ();
166+ SailRepository sail = new SailRepository (new SchemaCachingRDFSInferencer (new MemoryStore (), schema ));
167+ try {
168+ try (SailRepositoryConnection connection = sail .getConnection ()) {
169+ addAllDataMultipleTransactions (connection );
170+ }
171+ checkSize (sail );
172+ } finally {
173+ try {
174+ sail .shutDown ();
175+ } finally {
176+ schema .shutDown ();
177+ }
151178 }
152- checkSize (sail );
153-
154179 }
155180
156181 private SailRepository createSchema () throws IOException {
157182 SailRepository schema = new SailRepository (new MemoryStore ());
158183 try (SailRepositoryConnection schemaConnection = schema .getConnection ()) {
159184 schemaConnection .begin ();
160- schemaConnection . add ( resourceAsStream ( "schema.ttl" ), "" , RDFFormat . TURTLE );
185+ addRequiredResource ( schemaConnection , "schema.ttl" );
161186 schemaConnection .commit ();
162187 }
163188 return schema ;
164189 }
165190
166191 private void addAllDataSingleTransaction (SailRepositoryConnection connection ) throws IOException {
167- InputStream data = resourceAsStream ( "data.ttl" );
192+ addOptionalResource ( connection , "data.ttl" );
168193
169- if (data != null ) {
170- connection .add (data , "" , RDFFormat .TURTLE );
194+ int counter = 0 ;
195+ while (true ) {
196+ if (!addOptionalResource (connection , "data" + counter ++ + ".ttl" )) {
197+ break ;
198+ }
171199 }
200+ }
201+
202+ private void addAllDataMultipleTransactions (SailRepositoryConnection connection ) throws IOException {
203+ addOptionalResourceInTransaction (connection , "data.ttl" );
172204
173205 int counter = 0 ;
174206 while (true ) {
175- data = resourceAsStream ("data" + counter ++ + ".ttl" );
176- if (data == null ) {
207+ if (!addOptionalResourceInTransaction (connection , "data" + counter ++ + ".ttl" )) {
177208 break ;
178209 }
179- connection .add (data , "" , RDFFormat .TURTLE );
180210 }
181211 }
182212
183- private void addAllDataMultipleTransactions (SailRepositoryConnection connection ) throws IOException {
184- InputStream data = resourceAsStream ("data.ttl" );
213+ private void addRequiredResource (SailRepositoryConnection connection , String resourceName ) throws IOException {
214+ try (InputStream data = resourceAsStream (resourceName )) {
215+ if (data == null ) {
216+ throw new IOException ("Resource not found: " + resourceName );
217+ }
218+ connection .add (data , "" , RDFFormat .TURTLE );
219+ }
220+ }
185221
186- if (data != null ) {
187- connection .begin ();
222+ private boolean addOptionalResource (SailRepositoryConnection connection , String resourceName ) throws IOException {
223+ try (InputStream data = resourceAsStream (resourceName )) {
224+ if (data == null ) {
225+ return false ;
226+ }
188227 connection .add (data , "" , RDFFormat .TURTLE );
189- connection . commit () ;
228+ return true ;
190229 }
230+ }
191231
192- int counter = 0 ;
193- while ( true ) {
194- data = resourceAsStream ("data" + counter ++ + ".ttl" );
232+ private boolean addOptionalResourceInTransaction ( SailRepositoryConnection connection , String resourceName )
233+ throws IOException {
234+ try ( InputStream data = resourceAsStream (resourceName )) {
195235 if (data == null ) {
196- break ;
236+ return false ;
197237 }
198238 connection .begin ();
199239 connection .add (data , "" , RDFFormat .TURTLE );
200240 connection .commit ();
241+ return true ;
201242 }
202243 }
203244
0 commit comments