public WikidataEntity parse(String json) throws WpParseException { JacksonTermedStatementDocument mwDoc; try { mwDoc = mapper.readValue(json, JacksonTermedStatementDocument.class); } catch (IOException e) { LOG.info("Error parsing: " + json); throw new WpParseException(e); } WikidataEntity record = new WikidataEntity(mwDoc.getEntityId().getId()); // Aliases (multiple per language) for (List<MonolingualTextValue> vlist : mwDoc.getAliases().values()) { if (vlist.isEmpty()) continue; if (!validLanguage(vlist.get(0).getLanguageCode())) continue; Language lang = Language.getByLangCodeLenient(vlist.get(0).getLanguageCode()); record.getAliases().put(lang, new ArrayList<String>()); for (MonolingualTextValue v : vlist) { record.getAliases().get(lang).add(v.getText()); } } // Descriptions (one per language) for (MonolingualTextValue v : mwDoc.getDescriptions().values()) { if (validLanguage(v.getLanguageCode())) { Language lang = Language.getByLangCodeLenient(v.getLanguageCode()); record.getDescriptions().put(lang, v.getText()); } } // Labels (one per language) for (MonolingualTextValue v : mwDoc.getLabels().values()) { if (validLanguage(v.getLanguageCode())) { Language lang = Language.getByLangCodeLenient(v.getLanguageCode()); record.getLabels().put(lang, v.getText()); } } // Claims (only for Item entities) if (mwDoc instanceof JacksonItemDocument) { for (List<JacksonStatement> statements : ((JacksonItemDocument) mwDoc).getJsonClaims().values()) { for (JacksonStatement s : statements) { record.getStatements().add(parseOneClaim(record, s)); } } } return record; }
private <T> JsonElement serializeAsElementOrArray( List<T> items, JsonSerializationContext context) { if (items.isEmpty()) { return null; } if (items.size() == 1) { return context.serialize(items.get(0)); } else { return context.serialize(items); } }
public RollupTask parseRollupTask(JsonObject rollupTask, String context) throws BeanValidationException, QueryException { RollupTask task = m_gson.fromJson(rollupTask.getAsJsonObject(), RollupTask.class); validateObject(task); JsonArray rollups = rollupTask.getAsJsonObject().getAsJsonArray("rollups"); if (rollups != null) { for (int j = 0; j < rollups.size(); j++) { JsonObject rollupObject = rollups.get(j).getAsJsonObject(); Rollup rollup = m_gson.fromJson(rollupObject, Rollup.class); context = context + "rollup[" + j + "]"; validateObject(rollup, context); JsonObject queryObject = rollupObject.getAsJsonObject("query"); List<QueryMetric> queries = parseQueryMetric(queryObject, context); for (int k = 0; k < queries.size(); k++) { QueryMetric query = queries.get(k); context += ".query[" + k + "]"; validateHasRangeAggregator(query, context); // Add aggregators needed for rollups SaveAsAggregator saveAsAggregator = (SaveAsAggregator) m_aggregatorFactory.createAggregator("save_as"); saveAsAggregator.setMetricName(rollup.getSaveAs()); TrimAggregator trimAggregator = (TrimAggregator) m_aggregatorFactory.createAggregator("trim"); trimAggregator.setTrim(TrimAggregator.Trim.LAST); query.addAggregator(saveAsAggregator); query.addAggregator(trimAggregator); } rollup.addQueries(queries); task.addRollup(rollup); } } return task; }
public String postFile(URLFetchService us, List<PostObj> postobjlist) throws Exception { int index; Gson gson; String linkID; List<String> linkList; HTTPRequest req; String param; gson = new Gson(); linkID = createUID(); linkList = new ArrayList<String>(); for (index = 0; index < postobjlist.size(); index++) { linkList.add(postobjlist.get(index).filelink); } param = URLEncoder.encode("postlist", "UTF-8") + "=" + URLEncoder.encode(gson.toJson(postobjlist), "UTF-8") + '&' + URLEncoder.encode("linkid", "UTF-8") + "=" + URLEncoder.encode(linkID, "UTF-8") + '&' + URLEncoder.encode("linklist", "UTF-8") + "=" + URLEncoder.encode(gson.toJson(linkList), "UTF-8"); req = new HTTPRequest( new URL("http://tnfshmoe.appspot.com/postdf89ksfxsyx9sfdex09usdjksd"), HTTPMethod.POST); req.setPayload(param.getBytes()); us.fetch(req); return linkID; }
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { int index; DatastoreService ds; MemcacheService ms; BlobstoreService bs; URLFetchService us; FileService fs; Map<String, List<BlobKey>> blobMap; BlobKey blobKey; BlobInfoFactory blobInfoFactory; BlobInfo blobInfo; int postCount; int postIndex; String postType; String postText; String postDelpw; String postShowgallery; DataObj dataObj; String filelink; String picUrl; HTTPResponse picRes; List<HTTPHeader> headerList; String picMime; String[] fileNamePart; AppEngineFile picFile; FileWriteChannel writeChannel; PostObj postObj; List<PostObj> postObjList; String linkID; resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/plain"); try { ds = DatastoreServiceFactory.getDatastoreService(); ms = MemcacheServiceFactory.getMemcacheService(); bs = BlobstoreServiceFactory.getBlobstoreService(); us = URLFetchServiceFactory.getURLFetchService(); fs = FileServiceFactory.getFileService(); blobInfoFactory = new BlobInfoFactory(ds); blobMap = bs.getUploads(req); postCount = Integer.valueOf(req.getParameter("input_post_count")); postObjList = new ArrayList<PostObj>(); for (postIndex = 0; postIndex < postCount; postIndex++) { try { postType = req.getParameter("input_post_type_" + postIndex); if (postType == null) { continue; } postText = req.getParameter("input_post_text_" + postIndex); postDelpw = req.getParameter("input_post_delpw_" + postIndex); postShowgallery = req.getParameter("input_post_showgallery_" + postIndex); if (postShowgallery == null) { postShowgallery = ""; } if (postType.equals("file") == true) { blobKey = blobMap.get("input_post_file_" + postIndex).get(0); if (blobKey == null) { throw new Exception(); } dataObj = new DataObj(); dataObj.fileid = createUID(); dataObj.posttime = new Date().getTime(); dataObj.delpw = postDelpw; dataObj.blobkey = blobKey; dataObj.putDB(ds); blobInfo = blobInfoFactory.loadBlobInfo(dataObj.blobkey); filelink = "http://" + req.getServerName() + "/down/" + dataObj.fileid + "/" + blobInfo.getFilename(); postObj = new PostObj( dataObj.fileid, filelink, blobInfo.getSize(), dataObj.posttime, dataObj.delpw, postShowgallery); postObjList.add(postObj); } else if (postType.equals("url") == true) { picUrl = postText; picRes = us.fetch(new URL(picUrl)); headerList = picRes.getHeaders(); picMime = "application/octet-stream"; for (index = 0; index < headerList.size(); index++) { if (headerList.get(index).getName().compareToIgnoreCase("Content-Type") == 0) { picMime = headerList.get(index).getValue(); break; } } fileNamePart = picUrl.split("/"); picFile = fs.createNewBlobFile(picMime, fileNamePart[fileNamePart.length - 1]); writeChannel = fs.openWriteChannel(picFile, true); writeChannel.write(ByteBuffer.wrap(picRes.getContent())); writeChannel.closeFinally(); dataObj = new DataObj(); dataObj.fileid = createUID(); dataObj.posttime = new Date().getTime(); dataObj.delpw = postDelpw; dataObj.blobkey = fs.getBlobKey(picFile); dataObj.putDB(ds); blobInfo = blobInfoFactory.loadBlobInfo(dataObj.blobkey); filelink = "http://" + req.getServerName() + "/down/" + dataObj.fileid + "/" + blobInfo.getFilename(); postObj = new PostObj( dataObj.fileid, filelink, blobInfo.getSize(), dataObj.posttime, dataObj.delpw, postShowgallery); postObjList.add(postObj); } } catch (Exception e) { } } linkID = postFile(us, postObjList); if (req.getParameter("specflag") != null) { resp.getWriter().print("http://tnfshmoe.appspot.com/link.jsp?linkid=" + linkID); } else { resp.sendRedirect("http://tnfshmoe.appspot.com/link.jsp?linkid=" + linkID); } } catch (Exception e) { } }
private static LifeLine getLifeLine(List<LifeLine> lifeLines, String $ref) { List<LifeLine> list = lifeLines.stream().filter(f -> f.getId().equals($ref)).collect(Collectors.toList()); return list.get(0); }
public void generate(String inputFileName) throws Exception { List<MetaClass> metaClasses = new ArrayList<>(); List<LifeLine> lifeLines = new ArrayList<>(); List<MethodInvocation> rootMessages = new ArrayList<>(); MethodInvocation parentMessage = new MethodInvocation(); GsonBuilder builder = new GsonBuilder(); List<MethodInvocation> methodInvocations = new ArrayList<>(); Package mainPackage = new Package(); List<Guard> listOfGuards = new ArrayList<>(); Map<Guard, Instruction> guardToCFMap = new HashMap<>(); List<Instruction> combinedFragments = new ArrayList<Instruction>(); List<Operation> operationsList = new ArrayList<>(); builder.registerTypeAdapter(RefObject.class, new RefObjectJsonDeSerializer()); Gson gson = builder.create(); Element myTypes = gson.fromJson(new FileReader(inputFileName), Element.class); if (myTypes._type.equals("Project")) { List<Element> umlElements = myTypes .ownedElements .stream() .filter(f -> f._type.equals("UMLModel")) .collect(Collectors.toList()); if (umlElements.size() > 0) { // There has be to atleast one UMLModel package Element element = umlElements.get(0); // package that the classes are supposed to be in mainPackage.setName(element.name); List<Element> umlPackages = element .ownedElements .stream() .filter(g -> g._type.equals("UMLPackage")) .collect(Collectors.toList()); if (umlPackages.size() > 1) { // There has to be two packages- one for class one for behaviour Element classes = umlPackages.get(0); Element behaviour = umlPackages.get(1); // *--------------------------CLASSES-------------------------------*// // in the first pass, get all classes that are defined in the diagram // get details that can be directly inferred from the json like, fields and operations, // which do not refer to other classes for (Element umlClass : classes.getOwnedElements()) { MetaClass metaClass = new MetaClass(umlClass.name, umlClass._id); // check if class is interface or not because there is no distinction in json if (umlClass._type.equals("UMLClass")) { metaClass.setInterface(false); } else { metaClass.setInterface(true); } if (umlClass.operations != null) { metaClass.setOperations(umlClass.operations); operationsList.addAll(metaClass.operations); } if (umlClass.attributes != null) { metaClass.setFields(umlClass.attributes); } metaClasses.add(metaClass); } // in second pass, define associations and generalizations for these classes for (Element umlClass : classes.getOwnedElements()) { if (umlClass.ownedElements != null) { // find corresponding metaclass, then populate the secondary inferences List<MetaClass> correspondingMetaClassList = metaClasses .stream() .filter(f -> f._id.equals(umlClass._id)) .collect(Collectors.toList()); MetaClass correspondingMetaClass = correspondingMetaClassList.get(0); List<Element> umlAssociations = umlClass .ownedElements .stream() .filter(f -> f._type.equals("UMLAssociation")) .collect(Collectors.toList()); if (umlAssociations.size() > 0) { correspondingMetaClass.setAssociations(metaClasses, umlAssociations); } List<Element> umlGeneralization = umlClass .ownedElements .stream() .filter(f -> f._type.equals("UMLGeneralization")) .collect(Collectors.toList()); if (umlGeneralization.size() > 0) { correspondingMetaClass.setGeneralizations(metaClasses, umlGeneralization); } List<Element> umlRealization = umlClass .ownedElements .stream() .filter(f -> f._type.equals("UMLInterfaceRealization")) .collect(Collectors.toList()); if (umlRealization.size() > 0) { correspondingMetaClass.setInterfaceRealization(metaClasses, umlRealization); } } } // *--------------------------CLASSES-------------------------------*// // *----------------------- BEHAVIOUR---------------------------------*// for (Element umlCollaboration : behaviour.getOwnedElements()) { // Role to Class mapping ArrayList<Element> attributes = umlCollaboration.attributes; HashMap<String, MetaClass> roleToClassMap = new HashMap<>(); if (attributes != null) { for (Element attribute : attributes) { List<MetaClass> roleClass = metaClasses .stream() .filter(f -> f._id.equals(attribute.type.$ref)) .collect(Collectors.toList()); roleToClassMap.put(attribute._id, roleClass.get(0)); } } for (Element umlInteraction : umlCollaboration.ownedElements) { // mapping lifelines to the classes they correspond ArrayList<Element> participants = umlInteraction.participants; if (participants != null && participants.size() > 0) { for (Element participant : participants) { MetaClass participantClass = roleToClassMap.get(participant.represent.$ref); LifeLine lifeLine = new LifeLine(); lifeLine.setName(participant.name); lifeLine.setId(participant._id); lifeLine.setMetaClass(participantClass); lifeLines.add(lifeLine); } } // first parse all the combined fragments and get ready if (umlInteraction.fragments != null) { for (Element fragment : umlInteraction.fragments) { // depending on the fragment set the class Instruction instruction = null; if (fragment.interactionOperator.equals("loop")) { Loop loop = new Loop(); loop.setId(fragment._id); loop.setWeight(0); Guard guard = new Guard(fragment.operands.get(0)._id); // loop can have only one condition--- one condition-- condition is made up of // AND or OR's guard.setCondition(fragment.operands.get(0).guard); loop.setGuard(guard); instruction = loop; combinedFragments.add(loop); listOfGuards.add(guard); guardToCFMap.put(guard, loop); } if (fragment.interactionOperator.equals("alt")) { Conditional c = new Conditional(); c.setId(fragment._id); c.setWeight(0); instruction = c; combinedFragments.add(c); Guard consequence = new Guard(fragment.operands.get(0)._id); consequence.setCondition(fragment.operands.get(0).guard); c.setCons(consequence); listOfGuards.add(consequence); guardToCFMap.put(consequence, c); consequence.setConsequence(true); if (fragment.operands.size() > 1) { Guard alternate = new Guard(fragment.operands.get(1)._id); alternate.setCondition(fragment.operands.get(1).guard); c.setAlt(alternate); listOfGuards.add(alternate); guardToCFMap.put(alternate, c); alternate.setAlternative(true); } } if (fragment.tags != null) { for (Element tag : fragment.tags) { if (tag.name.equals("parent")) { List<Instruction> instructionList = combinedFragments .stream() .filter(e -> e.getId().equals(tag.reference.$ref)) .collect(Collectors.toList()); if (instructionList.size() > 0) { instructionList.get(0).getBlock().add(instruction); instruction.setParent(instructionList.get(0)); } } } } } } // parsing the messages and make nodes out them to later build a tree from the // lifelines ArrayList<Element> messages = umlInteraction.messages; Element startMessage = messages.get(0); String sourceRef = startMessage.source.$ref; String targetRef = startMessage.target.$ref; Element endMessage = null; LifeLine sourceLifeLine = getLifeLine(lifeLines, sourceRef); LifeLine targetLifeLine = getLifeLine(lifeLines, targetRef); // First message processing parentMessage = new MethodInvocation(); parentMessage.setAssignmentTarget(startMessage.assignmentTarget); parentMessage.setMessageSort(startMessage.messageSort); parentMessage.setSource(sourceLifeLine.getMetaClass()); parentMessage.setTarget(targetLifeLine.getMetaClass()); parentMessage.setName(startMessage.name); parentMessage.setId(startMessage._id); if (sourceLifeLine.getId().equals(targetLifeLine.getId())) { parentMessage.setCallerObject("this"); } else { parentMessage.setCallerObject(targetLifeLine.getName()); } int weight = 0; parentMessage.setWeight(weight++); if (startMessage.signature != null) { parentMessage.setSignature(startMessage.signature.$ref); } if (startMessage.tags != null) { for (Element tag : startMessage.tags) { // if (tag.name.equals("CF")) { // parentMessage.setInCF(true); // // parentMessage.setCfID(tag.reference.$ref); // } if (tag.name.equals("operand")) { parentMessage.setOperandId(tag.reference.$ref); } } } MethodInvocation rootMessage = parentMessage; methodInvocations.add(rootMessage); rootMessages.add(rootMessage); Iterator<Element> iter = messages.iterator(); while (iter.hasNext()) { if (iter.next() == endMessage) { continue; } iter.remove(); List<Element> childMessages = getChildMessages(messages, targetRef); for (Element child : childMessages) { LifeLine childSource = getLifeLine(lifeLines, child.source.$ref); LifeLine childTarget = getLifeLine(lifeLines, child.target.$ref); MethodInvocation childMessage = new MethodInvocation(); childMessage.setMessageSort(child.messageSort); childMessage.setSource(childSource.getMetaClass()); childMessage.setTarget(childTarget.getMetaClass()); childMessage.setAssignmentTarget(child.assignmentTarget); childMessage.setName(child.name); childMessage.setId(child._id); childMessage.setWeight(weight++); childMessage.setArguments(child.arguments); if (childSource.getId().equals(childTarget.getId())) { childMessage.setCallerObject("this"); } else { childMessage.setCallerObject(childTarget.getName()); } if (child.signature != null) { childMessage.setSignature(child.signature.$ref); } if (child.tags != null) { for (Element tag : child.tags) { // if (tag.name.equals("CF")) { // childMessage.setInCF(true); // // childMessage.setCfID(tag.reference.$ref); // } if (tag.name.equals("operand")) { childMessage.setOperandId(tag.reference.$ref); } } } parentMessage.childNodes.add(childMessage); methodInvocations.add(childMessage); } if (childMessages.size() > 0) { List<MethodInvocation> nextMessage = parentMessage .childNodes .stream() .filter(f -> !f.source.equals(f.target)) .collect(Collectors.toList()); List<Element> startMessageNext = childMessages .stream() .filter(f -> !f.source.$ref.equals(f.target.$ref)) .collect(Collectors.toList()); startMessage = startMessageNext.get(0); targetRef = startMessage.target.$ref; sourceRef = startMessage.source.$ref; parentMessage = nextMessage.get(0); if (childMessages.size() > 1) { endMessage = childMessages.get(childMessages.size() - 1); } } } } for (MethodInvocation methodInvocation : methodInvocations) { List<Operation> matchingOperation = operationsList .stream() .filter(f -> f._id.equals(methodInvocation.getSignature())) .collect(Collectors.toList()); if (matchingOperation.size() > 0) { operationMap.put(methodInvocation, matchingOperation.get(0)._id); methodInvocation.setOperation(matchingOperation.get(0)); } } Stack stack = new Stack(); for (MethodInvocation root : methodInvocations) { stack.push(root); while (!stack.empty()) { MethodInvocation methodInvocation = (MethodInvocation) stack.pop(); Operation currentOperation = methodInvocation.getOperation(); if (currentOperation != null) { // all child nodes of this node make up its body List<MethodInvocation> childNodes = methodInvocation.childNodes; for (MethodInvocation child : childNodes) { stack.push(child); } for (MethodInvocation childNode : childNodes) { if (childNode.getOperandId() != null) { List<Instruction> combinedFragmentsList = combinedFragments .stream() .filter(f -> f.getId().equals(childNode.getCfID())) .collect(Collectors.toList()); List<Guard> guardList = listOfGuards .stream() .filter(f -> f.id.equals(childNode.getOperandId())) .collect(Collectors.toList()); if (guardList.size() > 0) { Guard currentGuard = guardList.get(0); Instruction instruction = guardToCFMap.get(guardList.get(0)); // get the topmost CF if it is in a tree Instruction parent = instruction.getParent(); while (instruction.getParent() != null) { instruction = instruction.getParent(); } if (currentGuard.isConsequence) { Conditional conditional = (Conditional) instruction; if (!conditional.getConsequence().contains(childNode)) { conditional.getConsequence().add(childNode); } } if (currentGuard.isAlternative) { Conditional conditional = (Conditional) instruction; if (!conditional.getAlternative().contains(childNode)) { conditional.getAlternative().add(childNode); } } if (!currentGuard.isAlternative && !currentGuard.isConsequence) { Loop loop = (Loop) instruction; loop.getBlock().add(childNode); } else { if (!currentOperation.getBlock().contains(instruction)) { currentOperation.getBlock().add(instruction); } } } } else { if (!currentOperation.getBlock().contains(childNode)) { currentOperation.getBlock().add(childNode); } } } } } } } } } } // //// printAllData(metaClasses); // while (parentMessage.childNodes != null || parentMessage.childNodes.size() > 0) { // System.out.println("parent " + parentMessage.name); // for (com.cbpro.main.MethodInvocation child : parentMessage.childNodes) { // System.out.println("child " + child.name); // } // if (parentMessage.childNodes.size() > 0) { // parentMessage = parentMessage.childNodes.get(0); // } else { // break; // } // } mainPackage.print(); File dir = new File("/home/ramyashenoy/Desktop/DemoFolder/" + mainPackage.getName()); boolean successful = dir.mkdir(); if (successful) { System.out.println("directory was created successfully"); for (MetaClass metaClass : metaClasses) { if (metaClass.name.equals("Main")) { continue; } else { String data = metaClass.print(); BufferedWriter out = null; try { FileWriter fstream = new FileWriter( dir.getPath() + "/" + metaClass.name + ".java", true); // true tells to append data. out = new BufferedWriter(fstream); out.write(data); } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } finally { if (out != null) { out.close(); } } } } } else { // creating the directory failed System.out.println("failed trying to create the directory"); } mainPackage.setClasses(metaClasses); }
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { int index; DatastoreService ds; MemcacheService ms; Cookie[] cookies; boolean insideFlag; String delpw; String paramOffset; String paramSize; int offset; int size; Key postObjGroupKey; Query q; List<Entity> postObjList; PostObj postObj; Gson gson; List<String> filelinkList; resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/plain"); ds = DatastoreServiceFactory.getDatastoreService(); ms = MemcacheServiceFactory.getMemcacheService(); insideFlag = false; try { cookies = req.getCookies(); if (cookies.length > 0) { if (ms.contains(cookies[0].getValue()) == true) { insideFlag = true; } } } catch (Exception e) { insideFlag = false; } delpw = req.getParameter("delpw"); if (delpw != null) { if (delpw.equals("") == true) { delpw = null; } } paramOffset = req.getParameter("offset"); if (paramOffset != null) { offset = Integer.valueOf(paramOffset); } else { offset = 0; } paramSize = req.getParameter("size"); if (paramSize != null) { size = Integer.valueOf(paramSize); } else { size = 4096; } postObjGroupKey = KeyFactory.createKey("PostObjGroup", 1L); q = new Query("PostObj", postObjGroupKey); if (delpw != null) { q.addFilter("delpw", FilterOperator.EQUAL, delpw); q.addSort("delpw"); } q.addSort("posttime", SortDirection.DESCENDING); postObjList = ds.prepare(q).asList(FetchOptions.Builder.withOffset(offset).limit(size)); postObj = new PostObj(); filelinkList = new ArrayList<String>(); for (index = 0; index < postObjList.size(); index++) { postObj.getDB(postObjList.get(index)); if ((postObj.flag.equals("showgallery") == false && insideFlag == true) || (postObj.flag.equals("showgallery") == true && insideFlag == false) || delpw != null) { filelinkList.add(postObj.filelink); } } gson = new Gson(); resp.getWriter().print(gson.toJson(filelinkList)); }