Produced by Araxis Merge on 12/21/2017 6:15:14 PM Eastern Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | Genisis_2.0_v7_bld7.zip\TS\Service\testing\cli-test-scripts\src\main\java\gov\va\genisis2 | CommandClient.java | Thu Dec 14 19:57:07 2017 UTC |
| 2 | Genisis_2.0_v7_bld7.zip\TS\Service\testing\cli-test-scripts\src\main\java\gov\va\genisis2 | CommandClient.java | Thu Dec 21 22:18:26 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 2 | 666 |
| Changed | 1 | 2 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | package go v.va.genis is2; | |
| 2 | ||
| 3 | import jav a.io.Buffe redReader; | |
| 4 | import jav a.io.Buffe redWriter; | |
| 5 | import jav a.io.ByteA rrayOutput Stream; | |
| 6 | import jav a.io.File; | |
| 7 | import jav a.io.FileI nputStream ; | |
| 8 | import jav a.io.FileO utputStrea m; | |
| 9 | import jav a.io.FileW riter; | |
| 10 | import jav a.io.IOExc eption; | |
| 11 | import jav a.io.Input StreamRead er; | |
| 12 | import jav a.io.Outpu tStream; | |
| 13 | import jav a.util.Arr ayList; | |
| 14 | import jav a.util.Lis t; | |
| 15 | import jav a.util.Ran dom; | |
| 16 | ||
| 17 | import org .apache.je na.query.Q ueryExecut ion; | |
| 18 | import org .apache.je na.query.Q ueryExecut ionFactory ; | |
| 19 | import org .apache.je na.query.Q uerySoluti on; | |
| 20 | import org .apache.je na.query.R esultSet; | |
| 21 | import org .apache.je na.rdf.mod el.Literal ; | |
| 22 | import org .apache.je na.rdf.mod el.RDFNode ; | |
| 23 | import org .apache.lo g4j.Logger ; | |
| 24 | ||
| 25 | import com .fasterxml .jackson.d atabind.Ob jectMapper ; | |
| 26 | import com .fasterxml .jackson.d atabind.Se rializatio nFeature; | |
| 27 | ||
| 28 | import gov .va.genisi s2.ts.comm on.dto.Tri pleDTO; | |
| 29 | ||
| 30 | /** | |
| 31 | * | |
| 32 | * @author PII | |
| 33 | * | |
| 34 | */ | |
| 35 | public cla ss Command Client { | |
| 36 | ||
| 37 | pr ivate stat ic final L ogger LOGG ER = Logge r.getLogge r(CommandC lient.clas s); | |
| 38 | ||
| 39 | pr ivate stat ic final l ong LIMIT = 1000L; | |
| 40 | pr ivate stat ic long la st = 0; | |
| 41 | ||
| 42 | pu blic stati c String s ubjectQuer y = "SELEC T DISTINCT ?s { ?s ? p ?o . }"; | |
| 43 | pu blic stati c String p redicateQu ery = "SEL ECT DISTIN CT ?p { ?s ?p ?o . } "; | |
| 44 | pu blic stati c String o bjectQuery = "SELECT DISTINCT ?o { ?s ?p ?o . }"; | |
| 45 | ||
| 46 | pu blic Fusek iClient fu sekiClient ; | |
| 47 | pu blic Prope rtyManager propertyM anager; | |
| 48 | pu blic Strin g endpoint ; | |
| 49 | /* * | |
| 50 | * limit for how many data rows should be returned | |
| 51 | * / | |
| 52 | pu blic int g enerationL imit; | |
| 53 | pu blic int r andomOffse t; | |
| 54 | ||
| 55 | pu blic int c hanceForCr eatingNewT riple; | |
| 56 | ||
| 57 | pu blic Comma ndClient ( PropertyMa nager prop ) { | |
| 58 | fuse kiClient = FusekiCli ent.getIns tance(); | |
| 59 | prop ertyManage r = prop; | |
| 60 | ||
| 61 | this .chanceFor CreatingNe wTriple = propertyMa nager.getP ropertyVal ue | |
| 62 | (CliScri ptConstant s.CHANCE_N EW_TRIPLE, 10); | |
| 63 | this .randomOff set = prop ertyManage r.getPrope rtyValue(C liScriptCo nstants.RA NDOM_OFFSE T, 50); | |
| 64 | this .generatio nLimit = p ropertyMan ager.getPr opertyValu e(CliScrip tConstants .GENERATIO N_LIMIT, 1 000); | |
| 65 | } | |
| 66 | ||
| 67 | /* * | |
| 68 | * Generate existing t riples | |
| 69 | * @param en dpoint | |
| 70 | * / | |
| 71 | pu blic void generateEx istingTrip leFiles(St ring endpo int) { | |
| 72 | ||
| 73 | this .endpoint = endpoint ; | |
| 74 | //ge nerate sub jects file | |
| 75 | gene rateDistin ctQueryFil e(subjectQ uery, CliS criptConst ants.SUBJE CT_FILE, " ?s"); | |
| 76 | ||
| 77 | //ge nerate pre dicates fi le | |
| 78 | gene rateDistin ctQueryFil e(predicat eQuery, Cl iScriptCon stants.PRE DICATE_FIL E, "?p"); | |
| 79 | ||
| 80 | //ge nerate obj ects file | |
| 81 | gene rateDistin ctQueryFil e(objectQu ery, CliSc riptConsta nts.OBJECT _FILE, "?o "); | |
| 82 | } | |
| 83 | ||
| 84 | /* * | |
| 85 | * | |
| 86 | * @param qu ery | |
| 87 | * @param fi lename | |
| 88 | * @param he adVar | |
| 89 | * / | |
| 90 | pu blic void generateDi stinctQuer yFile(Stri ng query, String fil ename, Str ing headVa r) { | |
| 91 | Quer yExecution qe = null ; | |
| 92 | Resu ltSet resu ltSet = nu ll; | |
| 93 | Stri ng results = ""; | |
| 94 | ||
| 95 | Rand om rand = new Random (); | |
| 96 | //ME : dont kno w how larg e the data set is...r andomly ch oosing | |
| 97 | //a number as an offset could poss ible move over the r ange of tr iples...ma y need to ask for th e | |
| 98 | //nu mber of tr iples firs t..gonna g o with 50 for now | |
| 99 | quer y+=" OFFSE T "+rand.n extInt(ran domOffset) +" LIMIT " +generatio nLimit; | |
| 100 | try { | |
| 101 | qe = Q ueryExecut ionFactory .sparqlSer vice(endpo int, query ); | |
| 102 | ||
| 103 | result Set = fuse kiClient.p erformSele ctQuery(qe ); | |
| 104 | ||
| 105 | if (nu ll != resu ltSet) { | |
| 106 | while (r esultSet.h asNext()) { | |
| 107 | QuerySolut ion bindin g = result Set.nextSo lution(); | |
| 108 | results += getValueO fLiteralOr Uri(bindin g, headVar )+"\n"; | |
| 109 | } | |
| 110 | } | |
| 111 | } ca tch (Excep tion e) { | |
| 112 | LOGGER .error("Er ror in exe cution que ry", e); | |
| 113 | throw e; | |
| 114 | } fi nally { | |
| 115 | if (!q e.isClosed ()) { | |
| 116 | qe.close (); | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | writ eToFile( r esults, fi lename ); | |
| 121 | // writ eToFile( r esults, ne w File(fil ename)); | |
| 122 | } | |
| 123 | ||
| 124 | ||
| 125 | // pu blic void writeToFil e(String c ontents, F ile file) { | |
| 126 | // if ( file.getPa rentFile() .mkdir()) { | |
| 127 | // try { | |
| 128 | // file.cre ateNewFile (); | |
| 129 | // } catc h (IOExcep tion e) { | |
| 130 | // LOGGER.e rror(e.get Message() ); | |
| 131 | // } | |
| 132 | // } | |
| 133 | // writ eToFile( c ontents, f ile.getNam e()); | |
| 134 | // } | |
| 135 | ||
| 136 | /* * | |
| 137 | * Will writ e contents to a file given a f ile | |
| 138 | * @param co ntents | |
| 139 | * @param fi lename | |
| 140 | * / | |
| 141 | pu blic void writeToFil e(String c ontents, S tring file name) { | |
| 142 | try( BufferedW riter writ er = new B ufferedWri ter( new F ileWriter( filename ))){ | |
| 143 | writer .write(con tents); | |
| 144 | } ca tch (IOExc eption e) { | |
| 145 | LOGGER .error(e.g etMessage( ) ); | |
| 146 | } | |
| 147 | } | |
| 148 | ||
| 149 | /* * | |
| 150 | * | |
| 151 | * @param tr iples | |
| 152 | * @param pr intToStdOu t | |
| 153 | * / | |
| 154 | pu blic void checkIfTri plesExists (List<Trip leDTO> tri ples, bool ean printT oStdOut, S tring endp oint) { | |
| 155 | ||
| 156 | Fuse kiClient f s = Fuseki Client.get Instance() ; | |
| 157 | List <TripleExi stDTO> exi stsList = new ArrayL ist<Triple ExistDTO>( ); | |
| 158 | bool ean exists = false; | |
| 159 | ||
| 160 | for (TripleDTO tripleDTO : triples ) { | |
| 161 | try { | |
| 162 | exists = fs.perfor mAsk(getAs kForm(trip leDTO), en dpoint); | |
| 163 | } catc h (Excepti on e) { | |
| 164 | LOGGER.e rror(e.get Message()) ; | |
| 165 | } | |
| 166 | Triple ExistDTO t ripleExist = new Tri pleExistDT O(); | |
| 167 | triple Exist.setA lreadyExis ts(exists) ; | |
| 168 | triple Exist.setT riple(trip leDTO); | |
| 169 | exists List.add(t ripleExist ); | |
| 170 | } | |
| 171 | ||
| 172 | Obje ctMapper m apper = ne w ObjectMa pper(); | |
| 173 | mapp er.enable( Serializat ionFeature .INDENT_OU TPUT); | |
| 174 | ||
| 175 | if ( printToStd Out) { | |
| 176 | // pri nt to STDO UT | |
| 177 | try (O utputStrea m out = ne w ByteArra yOutputStr eam()) { | |
| 178 | ||
| 179 | // Objec t to JSON in file | |
| 180 | mapper.w riteValue( out, exist sList); | |
| 181 | ||
| 182 | final by te[] data = ((ByteAr rayOutputS tream) out ).toByteAr ray(); | |
| 183 | System.o ut.println (new Strin g(data)); | |
| 184 | ||
| 185 | } catc h (IOExcep tion e) { | |
| 186 | LOGGER.e rror(e.get Message()) ; | |
| 187 | } | |
| 188 | } el se { | |
| 189 | // pri nt to a fi le | |
| 190 | try (O utputStrea m out = ne w FileOutp utStream( | |
| 191 | new File(C liScriptCo nstants.EX ISTS_OUTPU T_FILENAME ))) { | |
| 192 | ||
| 193 | // Objec t to JSON in file | |
| 194 | mapper.w riteValue( out, exist sList); | |
| 195 | } catc h (IOExcep tion e) { | |
| 196 | LOGGER.e rror(e.get Message()) ; | |
| 197 | } | |
| 198 | } | |
| 199 | } | |
| 200 | ||
| 201 | /* * | |
| 202 | * | |
| 203 | * @param nu mberOfTrip les | |
| 204 | * / | |
| 205 | pu blic void createTrip les(int nu mberOfTrip les) { | |
| 206 | Stri ng mvpPref ix = "http ://genisis .va.gov/mv p-schema#D efinition_ "; | |
| 207 | ||
| 208 | List <TripleDTO > triples = new Arra yList<Trip leDTO>(); | |
| 209 | ||
| 210 | //re ad files i nto memory ..if a fil e is not f ound STOP! | |
| 211 | Rand omAccessLi st<String> randomSub jects = ne w RandomAc cessList<S tring>( | |
| 212 | readLine s( CliScri ptConstant s.SUBJECT_ FILE )); | |
| 213 | Rand omAccessLi st<String> randomPre dicates = new Random AccessList <String>( | |
| 214 | readLine s ( CliScr iptConstan ts.PREDICA TE_FILE )) ; | |
| 215 | Rand omAccessLi st<String> randomObj ects = new RandomAcc essList<St ring>( | |
| 216 | readLine s( CliScri ptConstant s.OBJECT_F ILE )); | |
| 217 | ||
| 218 | Rand om rand = new Random (); | |
| 219 | ||
| 220 | for( int i = 0; i < numbe rOfTriples ; i++ ) { | |
| 221 | Triple DTO dto = new Triple DTO(); | |
| 222 | if (ra nd.nextInt (chanceFor CreatingNe wTriple) = = 0) { | |
| 223 | // manua lly create a new tri ple | |
| 224 | dto.setS (mvpPrefix + getID() ); | |
| 225 | //TODO: need a pro per list o f predicat es to draw from | |
| 226 | dto.setP ("http://w ww.w3.org/ 2000/01/rd f-schema#l abel"); | |
| 227 | dto.setO ("random-t est-genera tion"); | |
| 228 | } else { | |
| 229 | // find an existin g triple | |
| 230 | dto.setS (randomSub jects.pick ()); | |
| 231 | dto.setP (randomPre dicates.pi ck()); | |
| 232 | dto.setO (randomObj ects.pick( )); | |
| 233 | } | |
| 234 | triple s.add(dto) ; | |
| 235 | } | |
| 236 | ||
| 237 | writ eJsontoFil e(triples, CliScript Constants. CREATE_OUT PUT_FILENA ME); | |
| 238 | } | |
| 239 | ||
| 240 | /* * | |
| 241 | * Uses jack sons objec t mapper t o write js on formatt ed strings given a l ist DTO ob jects | |
| 242 | * and a fil ename | |
| 243 | * @param tr iples | |
| 244 | * @param fi lename | |
| 245 | * / | |
| 246 | pu blic void writeJsont oFile(List <?> triple s, String filename) { | |
| 247 | try (OutputStr eam out = new FileOu tputStream (filename) ) { | |
| 248 | Object Mapper map per = new ObjectMapp er(); | |
| 249 | mapper .enable(Se rializatio nFeature.I NDENT_OUTP UT); | |
| 250 | ||
| 251 | // Obj ect to JSO N in file | |
| 252 | mapper .writeValu e(out, tri ples); | |
| 253 | } ca tch (IOExc eption e) { | |
| 254 | LOGGER .error(e.g etMessage( )); | |
| 255 | } | |
| 256 | } | |
| 257 | ||
| 258 | /* * | |
| 259 | * Opens the given fil e and read s the data into a li st of stri ngs | |
| 260 | * @param fi lename | |
| 261 | * @return l ist of lin es | |
| 262 | * / | |
| 263 | pu blic List< String> re adLines(St ring filen ame) { | |
| 264 | Arra yList<Stri ng> array = null; | |
| 265 | ||
| 266 | try (FileInput Stream fs = new File InputStrea m(filename ); | |
| 267 | InputStr eamReader is = new I nputStream Reader(fs) ; | |
| 268 | Buffered Reader br = new Buff eredReader (is)) { | |
| 269 | ||
| 270 | array = new Arra yList<Stri ng>(); | |
| 271 | String line; | |
| 272 | while ((line = b r.readLine ()) != nul l) | |
| 273 | array.ad d(br.readL ine()); | |
| 274 | } ca tch (IOExc eption ie) { | |
| 275 | LOGGER .error("Ca n't read i nput file : "+filena me+". Plea se run thi s program with " | |
| 276 | + "the -ge nerate fla g so input files can be genera ted"); | |
| 277 | System .exit(-1); | |
| 278 | } | |
| 279 | retu rn array; | |
| 280 | } | |
| 281 | ||
| 282 | /* * | |
| 283 | * Generate unique id s | |
| 284 | * Taken fr om: https: //stackove rflow.com/ questions/ 18227787/j ava-unique -10-digit- id | |
| 285 | * @return | |
| 286 | * / | |
| 287 | pu blic stati c long get ID() { | |
| 288 | lo ng id = Sy stem.curre ntTimeMill is() % LIM IT; | |
| 289 | if ( id <= l ast ) { | |
| 290 | id = (last + 1) % LI MIT; | |
| 291 | } | |
| 292 | re turn last = id; | |
| 293 | } | |
| 294 | ||
| 295 | /* * | |
| 296 | * Formats a triple in to ask for m | |
| 297 | * @param tr iple | |
| 298 | * @return | |
| 299 | * / | |
| 300 | pr ivate stat ic String getAskForm (TripleDTO triple) { | |
| 301 | if ( null == tr iple) { | |
| 302 | return null; | |
| 303 | } el se { | |
| 304 | return "ASK { <" + triple. getS() + " > <" + tri ple.getP() + "> \"" + triple.g etO() + "\ " }"; | |
| 305 | } | |
| 306 | } | |
| 307 | ||
| 308 | /* * | |
| 309 | * Will retr eive the l iteral or uri given the bindin g header | |
| 310 | * @param bi nding | |
| 311 | * @param he adVar | |
| 312 | * @return | |
| 313 | * / | |
| 314 | pr ivate Stri ng getValu eOfLiteral OrUri(Quer ySolution binding, S tring head Var) { | |
| 315 | RDFN ode litera lOrUriNode ; | |
| 316 | Stri ng valueOf iteralOrUr i = null; | |
| 317 | if ( null == bi nding) { | |
| 318 | return null; | |
| 319 | } | |
| 320 | if ( binding.co ntains(hea dVar) && n ull != (li teralOrUri Node = bin ding.get(h eadVar))) { | |
| 321 | org.ap ache.jena. rdf.model. Resource u riResource ; | |
| 322 | if (li teralOrUri Node.isLit eral()) { | |
| 323 | Literal literal = literalOrU riNode.asL iteral(); | |
| 324 | if (null != litera l) { | |
| 325 | valueOfite ralOrUri = literal.t oString(). trim(); | |
| 326 | } | |
| 327 | } else if (liter alOrUriNod e.isURIRes ource() && | |
| 328 | null != (u riResource = literal OrUriNode. asResource ())) { | |
| 329 | valueOfi teralOrUri = uriReso urce.getUR I().trim() ; | |
| 330 | } | |
| 331 | } | |
| 332 | retu rn valueOf iteralOrUr i; | |
| 333 | } | |
| 334 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.