| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | package org.semanticdesktop.nepomuk.comp.pimoservice.test; |
|---|
| 5 | |
|---|
| 6 | import java.io.FileInputStream; |
|---|
| 7 | import java.io.IOException; |
|---|
| 8 | import java.io.InputStream; |
|---|
| 9 | import java.util.Collection; |
|---|
| 10 | |
|---|
| 11 | import junit.framework.TestCase; |
|---|
| 12 | |
|---|
| 13 | import org.ontoware.aifbcommons.collection.ClosableIterator; |
|---|
| 14 | import org.ontoware.rdf2go.RDF2Go; |
|---|
| 15 | import org.ontoware.rdf2go.model.Model; |
|---|
| 16 | import org.ontoware.rdf2go.model.ModelSet; |
|---|
| 17 | import org.ontoware.rdf2go.model.Statement; |
|---|
| 18 | import org.ontoware.rdf2go.model.Syntax; |
|---|
| 19 | import org.ontoware.rdf2go.model.node.LanguageTagLiteral; |
|---|
| 20 | import org.ontoware.rdf2go.model.node.Node; |
|---|
| 21 | import org.ontoware.rdf2go.model.node.PlainLiteral; |
|---|
| 22 | import org.ontoware.rdf2go.model.node.URI; |
|---|
| 23 | import org.ontoware.rdf2go.model.node.Variable; |
|---|
| 24 | import org.ontoware.rdf2go.model.node.impl.PlainLiteralImpl; |
|---|
| 25 | import org.ontoware.rdf2go.model.node.impl.URIImpl; |
|---|
| 26 | import org.ontoware.rdf2go.vocabulary.RDF; |
|---|
| 27 | import org.ontoware.rdf2go.vocabulary.RDFS; |
|---|
| 28 | import org.semanticdesktop.nepomuk.comp.pimoservice.PimoQuery; |
|---|
| 29 | import org.semanticdesktop.nepomuk.comp.pimoservice.client.ClientSession; |
|---|
| 30 | import org.semanticdesktop.nepomuk.comp.pimoservice.client.PimoClient; |
|---|
| 31 | import org.semanticdesktop.nepomuk.comp.pimoservice.impl.PimoServiceImpl; |
|---|
| 32 | import org.semanticdesktop.nepomuk.mockup.MockupNepomukServices; |
|---|
| 33 | import org.semanticdesktop.nepomuk.util.TestTool; |
|---|
| 34 | import org.semanticdesktop.services.NepomukServices; |
|---|
| 35 | import org.semanticdesktop.services.ServiceNotFoundException; |
|---|
| 36 | import org.semanticdesktop.services.pimoservice.PimoService; |
|---|
| 37 | import org.semanticdesktop.services.rdfrepository.RDF2GoRepository; |
|---|
| 38 | import org.semanticdesktop.services.rdfrepository.RDFRepositoryCommon; |
|---|
| 39 | import org.semanticdesktop.services.vocabulary.NAO; |
|---|
| 40 | import org.semanticdesktop.services.vocabulary.NCAL; |
|---|
| 41 | import org.semanticdesktop.services.vocabulary.NCO; |
|---|
| 42 | import org.semanticdesktop.services.vocabulary.PIMO; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | public class PimoOntologyTest extends TestCase { |
|---|
| 55 | |
|---|
| 56 | NepomukServices services; |
|---|
| 57 | PimoService pimoservice; |
|---|
| 58 | PimoClient pimoclient; |
|---|
| 59 | PimoQuery query; |
|---|
| 60 | ClientSession session; |
|---|
| 61 | RDF2GoRepository repository; |
|---|
| 62 | ModelSet main; |
|---|
| 63 | |
|---|
| 64 | final String claudiaNameSpace = "http://www.sap.com/people/claudia#"; |
|---|
| 65 | final String claudiaEmail = "claudi-1@csc.kth.se"; |
|---|
| 66 | final String claudiaPimoUri = "http://www.sap.com/people/claudia#PIMO"; |
|---|
| 67 | final String claudiaUserUri = "http://www.sap.com/people/claudia#Claudia"; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | protected void setUp() throws Exception { |
|---|
| 73 | TestTool.setFullLogging(); |
|---|
| 74 | super.setUp(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | protected void tearDown() throws Exception { |
|---|
| 81 | super.tearDown(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | public void testClaudiasScenario() throws Exception { |
|---|
| 89 | |
|---|
| 90 | MockupNepomukServices mockup = new MockupNepomukServices(); |
|---|
| 91 | RDF2GoRepository repository = mockup.getRdf2gorepository(); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | { |
|---|
| 98 | ModelSet ms = repository.getRepository(RDFRepositoryCommon.CONFIG_REPOSITORY_ID); |
|---|
| 99 | Model m = ms.getModel(new URIImpl(RDFRepositoryCommon.DEFAULTGRAPH_URI)); |
|---|
| 100 | |
|---|
| 101 | m.open(); |
|---|
| 102 | InputStream in = getClass().getResourceAsStream("claudiaconfig.n3"); |
|---|
| 103 | try { |
|---|
| 104 | try { |
|---|
| 105 | m.readFrom(in, Syntax.Turtle); |
|---|
| 106 | } finally { |
|---|
| 107 | in.close(); |
|---|
| 108 | } |
|---|
| 109 | } catch (IOException x) |
|---|
| 110 | { |
|---|
| 111 | throw new ServiceNotFoundException("Cannot initialize pimo config: "+x, x); |
|---|
| 112 | } |
|---|
| 113 | m.close(); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | pimoclient = mockup.getPimoClient(); |
|---|
| 118 | query = pimoclient.getPimoQuery(); |
|---|
| 119 | main = pimoclient.getMainRepository(); |
|---|
| 120 | session = pimoclient.createSession(); |
|---|
| 121 | ModelSet workmodelset = session.getWorkModelSet(); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | URI claudiaURI = pimoclient.getUserUri(); |
|---|
| 126 | assertEquals(claudiaUserUri,claudiaURI.toString()); |
|---|
| 127 | assertEquals(claudiaNameSpace,pimoclient.getUserNamespace().toString()); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | URI claudiaPimo = pimoclient.getPimoUri(); |
|---|
| 131 | assertEquals(claudiaPimoUri,claudiaPimo.toString()); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | URI dirkPersonContact = new URIImpl("file://home/claudia/dirk.vcf#dirk"); |
|---|
| 139 | Model mDirk = main.getModel(dirkPersonContact); |
|---|
| 140 | mDirk.open(); |
|---|
| 141 | InputStream inDirk = getClass().getResourceAsStream("dirk.n3"); |
|---|
| 142 | try { |
|---|
| 143 | try { |
|---|
| 144 | mDirk.readFrom(inDirk, Syntax.Turtle); |
|---|
| 145 | } finally { |
|---|
| 146 | inDirk.close(); |
|---|
| 147 | } |
|---|
| 148 | } catch (IOException x) |
|---|
| 149 | { |
|---|
| 150 | throw new ServiceNotFoundException("Cannot load Dirk: "+x, x); |
|---|
| 151 | } |
|---|
| 152 | inDirk.close(); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | URI dirkPimo = session.createResourceFromGroundingOccurence(dirkPersonContact, "Dirk Hagemann", PIMO.Person); |
|---|
| 161 | session.commit(); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | assertTrue(main.containsStatements(Variable.ANY, dirkPimo, NAO.identifier, new PlainLiteralImpl("dirk@sap.com"))); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | session.bufferResourceClosure(dirkPimo, PIMO.GroundingClosure); |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | assertTrue(workmodelset.containsStatements(Variable.ANY, dirkPimo, NCO.nameFamily, new PlainLiteralImpl("Hagemann"))); |
|---|
| 178 | assertTrue(workmodelset.containsStatements(Variable.ANY, dirkPimo, NCO.nameGiven, new PlainLiteralImpl("Dirk"))); |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | try { |
|---|
| 182 | workmodelset.removeStatement(PIMO.GroundingClosure, dirkPimo, NCO.nameGiven, new PlainLiteralImpl("Dirk")); |
|---|
| 183 | fail("can remove a fact inferred from the GroundingClosure"); |
|---|
| 184 | } catch (Exception e) { |
|---|
| 185 | |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | { |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | URI contextURI = session.getWorkModel().getContextURI(); |
|---|
| 198 | |
|---|
| 199 | URI dirkSAPPage = new URIImpl("http://www.sap.com/people/DirkHagemann"); |
|---|
| 200 | |
|---|
| 201 | workmodelset.addStatement(contextURI, dirkPimo, PIMO.referencingOccurrence, dirkSAPPage); |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | URI dirkID = new URIImpl("http://id.sap.com/person/1650"); |
|---|
| 206 | session.getMagicModel().addStatement(dirkPimo, PIMO.hasOtherRepresentation, dirkID); |
|---|
| 207 | assertEquals("context uri stays the same until commit", contextURI, session.getWorkModel().getContextURI()); |
|---|
| 208 | assertEquals("the magicmodel has the same context as the workmodel, for convenience", |
|---|
| 209 | session.getWorkModel().getContextURI(), session.getMagicModel().getContextURI()); |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | session.commit(); |
|---|
| 215 | |
|---|
| 216 | assertTrue(main.containsStatements(contextURI, dirkPimo, PIMO.referencingOccurrence, new URIImpl("http://www.sap.com/people/DirkHagemann"))); |
|---|
| 217 | assertTrue(main.containsStatements(contextURI, dirkPimo, PIMO.hasOtherRepresentation, dirkID)); |
|---|
| 218 | assertTrue(main.containsStatements(contextURI, dirkPimo, PIMO.referencingOccurrence, dirkSAPPage)); |
|---|
| 219 | assertTrue(main.containsStatements(contextURI, dirkPimo, PIMO.hasOtherRepresentation, dirkID)); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | URI belfastMeeting = session.createResource("Initial Meeting in Belfast", PIMO.Meeting); |
|---|
| 223 | session.commit(); |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | Collection<URI> meetProps = query.getPossiblePropertiesForThing(belfastMeeting.toString(), true, true); |
|---|
| 227 | assertFalse(meetProps.contains(NCAL.dtstart)); |
|---|
| 228 | assertFalse(meetProps.contains(NCAL.categories)); |
|---|
| 229 | assertFalse(meetProps.contains(NCAL.contact)); |
|---|
| 230 | assertFalse(meetProps.contains(NCAL.location)); |
|---|
| 231 | assertFalse(meetProps.contains(NCAL.duration)); |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | session.getMagicModel().addStatement(belfastMeeting, RDF.type, NCAL.Event); |
|---|
| 235 | |
|---|
| 236 | session.commit(); |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | if (1==2) |
|---|
| 242 | { |
|---|
| 243 | meetProps = query.getPossiblePropertiesForThing(belfastMeeting.toString(), true, true); |
|---|
| 244 | assertTrue(meetProps.contains(NCAL.dtstart)); |
|---|
| 245 | assertTrue(meetProps.contains(NCAL.categories)); |
|---|
| 246 | assertTrue(meetProps.contains(NCAL.contact)); |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | assertTrue(meetProps.contains(NCAL.duration)); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | PlainLiteral belfastLocation = new PlainLiteralImpl("Hotel Belfast"); |
|---|
| 254 | PlainLiteral belfastStart = new PlainLiteralImpl("20070304T230000"); |
|---|
| 255 | PlainLiteral belfastDuration = new PlainLiteralImpl("DURATION:PT1H0M0S"); |
|---|
| 256 | URI belfastDate = session.createResource(NCAL.NcalDateTime); |
|---|
| 257 | session.getMagicModel().addStatement(belfastDate, NCAL.dateTime, belfastStart); |
|---|
| 258 | session.getMagicModel().addStatement(belfastMeeting, NCAL.duration, belfastDuration); |
|---|
| 259 | session.getMagicModel().addStatement(belfastMeeting, NCAL.location, belfastLocation); |
|---|
| 260 | session.commit(); |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | URI flight = session.createClass("Flight", PIMO.Event); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | URI LHL4573 = session.createResource("LHL 4573 - Frankfurt - Belfast", flight); |
|---|
| 270 | URI LHL7452 = session.createResource("LHL 7452 - Belfast - Frankfurt", flight); |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | Model wk = session.getWorkModel(); |
|---|
| 274 | wk.addStatement(belfastMeeting, PIMO.hasPart, LHL4573); |
|---|
| 275 | wk.addStatement(belfastMeeting, PIMO.hasPart, LHL7452); |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | session.commit(); |
|---|
| 279 | |
|---|
| 280 | assertTrue(main.containsStatements(Variable.ANY, belfastMeeting, RDF.type, PIMO.Meeting)); |
|---|
| 281 | assertTrue(main.containsStatements(Variable.ANY, flight, RDFS.subClassOf, PIMO.Event)); |
|---|
| 282 | assertTrue(main.containsStatements(Variable.ANY, LHL4573, RDF.type, flight)); |
|---|
| 283 | assertTrue(main.containsStatements(Variable.ANY, LHL7452, RDF.type, flight)); |
|---|
| 284 | |
|---|
| 285 | assertTrue(main.containsStatements(Variable.ANY, LHL4573, PIMO.partOf, belfastMeeting)); |
|---|
| 286 | assertTrue(main.containsStatements(Variable.ANY, LHL7452, PIMO.partOf, belfastMeeting)); |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | URI belfast = session.createResource("Belfast", PIMO.City); |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | session.getMagicModel().addStatement(belfastMeeting, PIMO.hasLocation, belfast); |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | session.getMagicModel().addStatement(belfast,PIMO.referencingOccurrence,new URIImpl("http://en.wikipedia.org/wiki/Belfast")); |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | session.getMagicModel().addStatement(belfast,PIMO.hasOtherRepresentation,new URIImpl("http://dbpedia.org/resource/Belfast")); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | session.commit(); |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | { |
|---|
| 317 | |
|---|
| 318 | session.bufferOpenLinkedData(belfast); |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | String nonEnglishLiteral = ""; |
|---|
| 324 | boolean englishLiteral = false; |
|---|
| 325 | ClosableIterator<? extends Statement> iter = null; |
|---|
| 326 | try { |
|---|
| 327 | iter = workmodelset.findStatements(Variable.ANY, belfast, Variable.ANY, Variable.ANY); |
|---|
| 328 | while (iter.hasNext()) { |
|---|
| 329 | Statement s = iter.next(); |
|---|
| 330 | Node node = s.getObject(); |
|---|
| 331 | if (node instanceof LanguageTagLiteral) { |
|---|
| 332 | LanguageTagLiteral literal = node.asLanguageTagLiteral(); |
|---|
| 333 | if(literal.getLanguageTag().equals("en")) { |
|---|
| 334 | englishLiteral = true; |
|---|
| 335 | } else { |
|---|
| 336 | nonEnglishLiteral +=literal.getLanguageTag()+","; |
|---|
| 337 | } |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | } finally { |
|---|
| 341 | iter.close(); |
|---|
| 342 | } |
|---|
| 343 | assertTrue("Result contains a statement about Belfast which is English",englishLiteral); |
|---|
| 344 | assertTrue("Result should contain statement about Belfast which is not English",nonEnglishLiteral.length()>0); |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | session.setLanguageFilter(true); |
|---|
| 350 | |
|---|
| 351 | nonEnglishLiteral = ""; |
|---|
| 352 | englishLiteral = false; |
|---|
| 353 | try { |
|---|
| 354 | iter = workmodelset.findStatements(Variable.ANY, belfast, Variable.ANY, Variable.ANY); |
|---|
| 355 | while (iter.hasNext()) { |
|---|
| 356 | Statement s = iter.next(); |
|---|
| 357 | Node node = s.getObject(); |
|---|
| 358 | if (node instanceof LanguageTagLiteral) { |
|---|
| 359 | LanguageTagLiteral literal = node.asLanguageTagLiteral(); |
|---|
| 360 | if(literal.getLanguageTag().equals("en")) { |
|---|
| 361 | englishLiteral = true; |
|---|
| 362 | } else { |
|---|
| 363 | nonEnglishLiteral +=literal.getLanguageTag()+","; |
|---|
| 364 | } |
|---|
| 365 | } |
|---|
| 366 | } |
|---|
| 367 | } finally { |
|---|
| 368 | iter.close(); |
|---|
| 369 | } |
|---|
| 370 | assertTrue("Result contains a statement about Belfast which is English",englishLiteral); |
|---|
| 371 | assertTrue("Result contains a statement about Belfast which is not English, languages are: "+nonEnglishLiteral,nonEnglishLiteral.length()==0); |
|---|
| 372 | |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | main.open(); |
|---|
| 383 | main.readFrom(getClass().getResourceAsStream("ClaudiaDocumentsCrawl.trix"),Syntax.Trix); |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | URI cid = session.createResource("CID", PIMO.Project); |
|---|
| 387 | session.commit(); |
|---|
| 388 | |
|---|
| 389 | URI CIDJobOffer = new URIImpl("file:/D:/Nepomuk/workspace/claudia_stern/documents/projects/CID/Job%20Offer%20CID%20Training.doc"); |
|---|
| 390 | session.addTag(CIDJobOffer, cid); |
|---|
| 391 | session.commit(); |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | URI CIDJobOfferDocument = null; |
|---|
| 400 | ClosableIterator<? extends Statement> iter = null; |
|---|
| 401 | try { |
|---|
| 402 | iter = main.findStatements(Variable.ANY, Variable.ANY, NAO.hasTag, cid); |
|---|
| 403 | for (int i = 0; iter.hasNext(); i++) { |
|---|
| 404 | if (i==0) { |
|---|
| 405 | |
|---|
| 406 | CIDJobOfferDocument = iter.next().getSubject().asURI(); |
|---|
| 407 | assertFalse(CIDJobOfferDocument==null); |
|---|
| 408 | } else { |
|---|
| 409 | |
|---|
| 410 | fail("There are more than one thing, which has the tag: "+cid); |
|---|
| 411 | } |
|---|
| 412 | } |
|---|
| 413 | } finally { |
|---|
| 414 | iter.close(); |
|---|
| 415 | } |
|---|
| 416 | assertTrue(main.containsStatements(Variable.ANY, CIDJobOfferDocument, NAO.hasTag, cid)); |
|---|
| 417 | assertTrue(main.containsStatements(Variable.ANY, CIDJobOfferDocument, PIMO.groundingOccurrence, CIDJobOffer)); |
|---|
| 418 | |
|---|
| 419 | session.addTag(dirkPimo, cid); |
|---|
| 420 | session.commit(); |
|---|
| 421 | assertTrue(main.containsStatements(Variable.ANY, dirkPimo, NAO.hasTag, cid)); |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | } |
|---|