List of usage examples for java.util SortedMap entrySet
Set<Map.Entry<K, V>> entrySet();
From source file:org.apache.nifi.provenance.MiNiFiPersistentProvenanceRepository.java
/** * Purges old events from the repository * * @throws IOException if unable to purge old events due to an I/O problem *///from w ww. j a va 2s .co m synchronized void purgeOldEvents() throws IOException { while (!recoveryFinished.get()) { try { Thread.sleep(100L); } catch (final InterruptedException ie) { } } final List<File> toPurge = new ArrayList<>(); final long timeCutoff = System.currentTimeMillis() - configuration.getMaxRecordLife(TimeUnit.MILLISECONDS); final List<File> sortedByBasename = getLogFiles(); long bytesUsed = getSize(sortedByBasename, timeCutoff); for (final Path path : idToPathMap.get().values()) { final File file = path.toFile(); final long lastModified = file.lastModified(); if (lastModified > 0L && lastModified < timeCutoff) { toPurge.add(file); } } // This comparator sorts the data based on the "basename" of the files. I.e., the numeric portion. // We do this because the numeric portion represents the ID of the first event in the log file. // As a result, we are sorting based on time, since the ID is monotonically increasing. By doing this, // are able to avoid hitting disk continually to check timestamps final Comparator<File> sortByBasenameComparator = new Comparator<File>() { @Override public int compare(final File o1, final File o2) { final String baseName1 = StringUtils.substringBefore(o1.getName(), "."); final String baseName2 = StringUtils.substringBefore(o2.getName(), "."); Long id1 = null; Long id2 = null; try { id1 = Long.parseLong(baseName1); } catch (final NumberFormatException nfe) { id1 = null; } try { id2 = Long.parseLong(baseName2); } catch (final NumberFormatException nfe) { id2 = null; } if (id1 == null && id2 == null) { return 0; } if (id1 == null) { return 1; } if (id2 == null) { return -1; } return Long.compare(id1, id2); } }; // If we have too much data (at least 90% of our max capacity), start aging it off if (bytesUsed > configuration.getMaxStorageCapacity() * 0.9) { Collections.sort(sortedByBasename, sortByBasenameComparator); for (final File file : sortedByBasename) { toPurge.add(file); bytesUsed -= file.length(); if (bytesUsed < configuration.getMaxStorageCapacity()) { // we've shrunk the repo size down enough to stop break; } } } // Sort all of the files that we want to purge such that the oldest events are aged off first Collections.sort(toPurge, sortByBasenameComparator); logger.debug("Purging old event files: {}", toPurge); // Remove any duplicates that we may have. final Set<File> uniqueFilesToPurge = new LinkedHashSet<>(toPurge); // Age off the data. final Set<String> removed = new LinkedHashSet<>(); for (File file : uniqueFilesToPurge) { final String baseName = StringUtils.substringBefore(file.getName(), "."); ExpirationAction currentAction = null; try { for (final ExpirationAction action : expirationActions) { currentAction = action; if (!action.hasBeenPerformed(file)) { final File fileBeforeAction = file; final StopWatch stopWatch = new StopWatch(true); file = action.execute(file); stopWatch.stop(); logger.info("Successfully performed Expiration Action {} on Provenance Event file {} in {}", action, fileBeforeAction, stopWatch.getDuration()); } } removed.add(baseName); } catch (final FileNotFoundException fnf) { logger.warn( "Failed to perform Expiration Action {} on Provenance Event file {} because the file no longer exists; will not " + "perform additional Expiration Actions on this file", currentAction, file); removed.add(baseName); } catch (final Throwable t) { logger.warn( "Failed to perform Expiration Action {} on Provenance Event file {} due to {}; will not perform additional " + "Expiration Actions on this file at this time", currentAction, file, t.toString()); logger.warn("", t); eventReporter.reportEvent(Severity.WARNING, EVENT_CATEGORY, "Failed to perform Expiration Action " + currentAction + " on Provenance Event file " + file + " due to " + t.toString() + "; will not perform additional Expiration Actions " + "on this file at this time"); } } // Update the Map ID to Path map to not include the removed file // We cannot obtain the write lock here because there may be a need for the lock in the rollover method, // if we have 'backpressure applied'. This would result in a deadlock because the rollover method would be // waiting for purgeOldEvents, and purgeOldEvents would be waiting for the write lock held by rollover. boolean updated = false; while (!updated) { final SortedMap<Long, Path> existingPathMap = idToPathMap.get(); final SortedMap<Long, Path> newPathMap = new TreeMap<>(new PathMapComparator()); newPathMap.putAll(existingPathMap); final Iterator<Map.Entry<Long, Path>> itr = newPathMap.entrySet().iterator(); while (itr.hasNext()) { final Map.Entry<Long, Path> entry = itr.next(); final String filename = entry.getValue().toFile().getName(); final String baseName = StringUtils.substringBefore(filename, "."); if (removed.contains(baseName)) { itr.remove(); } } updated = idToPathMap.compareAndSet(existingPathMap, newPathMap); logger.debug("After expiration, path map: {}", newPathMap); } }
From source file:org.grycap.gpf4med.conf.ConfigurationManager.java
private ConfigurationManager.Configuration configuration() { if (dont_use == null) { synchronized (ConfigurationManager.Configuration.class) { if (dont_use == null && urls != null) { try { XMLConfiguration main = null; // sorting secondary configurations ensures that combination // always result the same final SortedMap<String, XMLConfiguration> secondary = new TreeMap<String, XMLConfiguration>(); // extract main configuration for (final URL url : urls) { final String filename = FilenameUtils.getName(url.getPath()); if (MAIN_CONFIGURATION.equalsIgnoreCase(filename)) { main = new XMLConfiguration(url); LOGGER.info("Loading main configuration from: " + url.toString()); } else if (!IGNORE_LIST.contains(FilenameUtils.getName(url.getPath()))) { secondary.put(filename, new XMLConfiguration(url)); LOGGER.info("Loading secondary configuration from: " + url.toString()); } else { LOGGER.info("Ignoring: " + url.toString()); }/*from ww w . j ava2s . com*/ } if (main != null) { final CombinedConfiguration configuration = new CombinedConfiguration( new OverrideCombiner()); configuration.addConfiguration(main, MAIN_CONFIGURATION); for (final Map.Entry<String, XMLConfiguration> entry : secondary.entrySet()) { configuration.addConfiguration(entry.getValue(), entry.getKey()); } if (LOGGER.isDebugEnabled()) { String names = ""; for (final String name : configuration.getConfigurationNameList()) { names += name + " "; } LOGGER.trace("Loading configuration from: " + names); } final List<String> foundNameList = new ArrayList<String>(); // get main property will fail if the requested property is missing configuration.setThrowExceptionOnMissing(true); final File rootDir = getFile("gpf4med-root", configuration, foundNameList, true, null); final URL templatesUrl = getUrl("storage.templates", configuration, foundNameList, null); final URL connectorsUrl = getUrl("storage.connectors", configuration, foundNameList, null); final File localCacheDir = getFile("storage.local-cache", configuration, foundNameList, true, null); final File htdocsDir = getFile("storage.htdocs", configuration, foundNameList, false, null); final boolean encryptLocalStorage = getBoolean("security.encrypt-local-storage", configuration, foundNameList, true); final boolean useStrongCryptography = getBoolean("security.use-strong-cryptography", configuration, foundNameList, false); final String templatesVersion = getString("dicom.version", configuration, foundNameList, null); final URL templatesIndex = getUrl("dicom.index", configuration, foundNameList, null); final String connectorsVersion = getString("graph.version", configuration, foundNameList, null); final URL connectorsIndex = getUrl("graph.index", configuration, foundNameList, null); // get secondary property will return null if the requested property is missing configuration.setThrowExceptionOnMissing(false); final String containerHostname = getString("service-container.hostname", configuration, foundNameList, null); final int containerPort = getInteger("service-container.port", configuration, foundNameList, new Integer(8080)); final String enactorProvider = getString("enactor.provider", configuration, foundNameList, null); final File enactorIdentity = getFile("enactor.identity", configuration, foundNameList, false, null); final File enactorCredential = getFile("enactor.credential", configuration, foundNameList, false, null); final String serverVersion = getString("container-server.version", configuration, foundNameList, null); final URL serverInstallerUrl = getUrl("container-server.installer.url", configuration, foundNameList, null); final File serverHome = getFile("container-server.home", configuration, foundNameList, false, null); // Add this for read the TRENCADIS configuration final File trencadisConfiguration = getFile("trencadis.config-file", configuration, foundNameList, false, null); final String trencadisPassword = getString("trencadis.pass", configuration, foundNameList, null); // get other (free-format) properties final Iterator<String> keyIterator = configuration.getKeys(); final Map<String, String> othersMap = new Hashtable<String, String>(); while (keyIterator.hasNext()) { final String key = keyIterator.next(); if (key != null && !foundNameList.contains(key)) { final String value = configuration.getString(key); if (value != null) { othersMap.put(key, value); } } } dont_use = new Configuration(rootDir, templatesUrl, connectorsUrl, localCacheDir, htdocsDir, encryptLocalStorage, useStrongCryptography, templatesVersion, templatesIndex, connectorsVersion, connectorsIndex, containerHostname, containerPort, enactorProvider, enactorIdentity, enactorCredential, serverVersion, serverInstallerUrl, serverHome, trencadisConfiguration, trencadisPassword, othersMap); LOGGER.info(dont_use.toString()); } else { throw new IllegalStateException("Main configuration not found"); } } catch (IllegalStateException e1) { throw e1; } catch (ConfigurationException e2) { throw new IllegalStateException(e2); } catch (Exception e) { LOGGER.error("Failed to load configuration", e); } } } } return dont_use; }
From source file:org.openconcerto.sql.element.SQLElement.java
private final String toStringExtern(SortedMap<SQLField, Integer> externRef) { final List<String> l = new ArrayList<String>(); final Map<String, Object> map = new HashMap<String, Object>(4); for (final Map.Entry<SQLField, Integer> entry : externRef.entrySet()) { final SQLField foreignKey = entry.getKey(); final int count = entry.getValue(); final String label = Configuration.getTranslator(foreignKey.getTable()).getLabelFor(foreignKey); final SQLElement elem = getElement(foreignKey.getTable()); map.put("elementName", elem.getName()); map.put("count", count); map.put("linkName", label); l.add(getTM().trM("sqlElement.linksWillBeCut", map)); }/* w ww . j a va 2 s . c o m*/ return CollectionUtils.join(l, "\n"); }
From source file:com.tojc.ormlite.android.framework.TableInfo.java
public TableInfo(Class<?> tableClassType) { // can't happen // keep a while, May 18th 2013 // TODO remove after a while // if (!(tableClassType instanceof Class<?>)) { // throw new IllegalArgumentException("Parameter is not a Class<?>."); // }/*w ww .ja v a 2 s. com*/ this.classType = tableClassType; this.name = OrmLiteAnnotationAccessor.getAnnotationTableName(tableClassType); this.defaultContentUriInfo = new ContentUriInfo(tableClassType); this.defaultContentMimeTypeVndInfo = new ContentMimeTypeVndInfo(tableClassType); this.columns = new HashMap<String, ColumnInfo>(); this.projectionMap = new HashMap<String, String>(); SortedMap<Integer, String> defaultSortOrderMap = new TreeMap<Integer, String>(); this.idColumnInfo = null; for (Field classfield : tableClassType.getDeclaredFields()) { if (classfield.isAnnotationPresent(DatabaseField.class)) { classfield.setAccessible(true); // private field accessible ColumnInfo columnInfo = new ColumnInfo(classfield); this.columns.put(columnInfo.getColumnName(), columnInfo); // check id if (columnInfo.getColumnName().equals(BaseColumns._ID)) { boolean generatedId = classfield.getAnnotation(DatabaseField.class).generatedId(); if (generatedId) { this.idColumnInfo = columnInfo; } } // DefaultSortOrder SortOrderInfo defaultSortOrderInfo = columnInfo.getDefaultSortOrderInfo(); if (defaultSortOrderInfo.isValid()) { defaultSortOrderMap.put(defaultSortOrderInfo.getWeight(), defaultSortOrderInfo.makeSqlOrderString(columnInfo.getColumnName())); } // ProjectionMap this.projectionMap.put(columnInfo.getProjectionColumnName(), columnInfo.getColumnName()); } } if (this.idColumnInfo == null) { // @DatabaseField(columnName = _ID, generatedId = true) // private int _id; throw new IllegalArgumentException("Proper ID is not defined for field."); } // DefaultSortOrder if (defaultSortOrderMap.size() >= 1) { // make SQL OrderBy StringBuilder result = new StringBuilder(); String comma = ""; for (Map.Entry<Integer, String> entry : defaultSortOrderMap.entrySet()) { result.append(comma); result.append(entry.getValue()); comma = ", "; } this.defaultSortOrder = result.toString(); } else { this.defaultSortOrder = ""; } }
From source file:de.uni_potsdam.hpi.asg.logictool.mapping.SequenceBasedAndGateDecomposer.java
private Map<Integer, List<Partition>> getPartitions(Set<Signal> signals, int startgatesize) { // cost function SortedMap<Integer, List<Partition>> retVal = new TreeMap<>(Collections.reverseOrder()); Set<Set<Set<Signal>>> parts = getTailCombinations(signals); if (parts == null) { return null; }//from w w w . jav a2s .c om for (Set<Set<Signal>> partition : parts) { int cost = 0; Set<PartitionPart> parts2 = new HashSet<>(); for (Set<Signal> partpart : partition) { parts2.add(new PartitionPart(partpart)); if (partpart.size() != 1) { cost += partpart.size(); } } if (partition.size() != 1) { cost += partition.size(); } if (!retVal.containsKey(cost)) { retVal.put(cost, new ArrayList<Partition>()); } retVal.get(cost).add(new Partition(parts2, cost)); } // System.out.println("Startgatesize: " + startgatesize); // filter too large List<Partition> rmPart = new ArrayList<>(); List<Integer> rmKey = new ArrayList<>(); for (Entry<Integer, List<Partition>> entry : retVal.entrySet()) { // System.out.println(entry.getKey()); rmPart.clear(); for (Partition p : entry.getValue()) { // System.out.println("\t" + p.toString()); if (p.getPartition().size() >= startgatesize) { // System.out.println("Rm: " + p); rmPart.add(p); continue; } for (PartitionPart p2 : p.getPartition()) { if (p2.getPart().size() >= startgatesize) { // System.out.println("Rm: " + p); rmPart.add(p); continue; } } } entry.getValue().removeAll(rmPart); if (entry.getValue().isEmpty()) { rmKey.add(entry.getKey()); } } for (int i : rmKey) { retVal.remove(i); } return retVal; }
From source file:org.kuali.coeus.common.budget.impl.calculator.BudgetCalculationServiceImpl.java
/** * This method is to group personnel items for budget summary * Start building data structure applicable for personnel items to format it with * required details for budget summary/*from w w w.ja v a 2s . co m*/ */ private LineItemGroup getPersonnelBudgetSummaryPeriods(BudgetPeriod budgetPeriod, SortedMap<CostElement, List<BudgetLineItem>> uniqueBudgetLineItemCostElements, String personnelBudgetCategoryType) { LineItemGroup personnelGroup = new LineItemGroup(BUDGET_SUMMARY_PERSONNEL_GROUP_LABEL, true); LineItemObject personnelSalaries = new LineItemObject(BudgetSummaryConstants.PersonSalary.getKey(), BudgetSummaryConstants.PersonSalary.getLabel(), ScaleTwoDecimal.ZERO); LineItemObject personnelFringe = new LineItemObject(BudgetSummaryConstants.PersonFringe.getKey(), BudgetSummaryConstants.PersonFringe.getLabel(), ScaleTwoDecimal.ZERO); ScaleTwoDecimal totalSalary = ScaleTwoDecimal.ZERO; ScaleTwoDecimal totalFringe = ScaleTwoDecimal.ZERO; for (Map.Entry<CostElement, List<BudgetLineItem>> uniqueLineItem : uniqueBudgetLineItemCostElements .entrySet()) { CostElement personnelCostElement = uniqueLineItem.getKey(); List<BudgetLineItem> personnelLineItemsForCostElement = uniqueLineItem.getValue(); QueryList<BudgetLineItem> periodLineItemCostElementQueryList = getLineItemsFilteredByCostElement( budgetPeriod, personnelCostElement.getCostElement()); QueryList<BudgetPersonnelDetails> periodLineItemPersonnelDetailsQueryList = getBudgetPersonnelDetails( periodLineItemCostElementQueryList, personnelBudgetCategoryType); ScaleTwoDecimal totalSalaryForCostElement = periodLineItemPersonnelDetailsQueryList .sumObjects("salaryRequested"); ScaleTwoDecimal totalFringeForCostElement = periodLineItemPersonnelDetailsQueryList .sumObjects("calculatedFringe"); Map<String, String> uniquePersonList = getUniquePersonList(personnelLineItemsForCostElement, personnelBudgetCategoryType); LineItemObject salaryLineItemObject = new LineItemObject(personnelCostElement.getCostElement(), personnelCostElement.getDescription(), totalSalaryForCostElement); LineItemObject fringeLineItemObject = new LineItemObject(personnelCostElement.getCostElement(), personnelCostElement.getDescription(), totalFringeForCostElement); for (Map.Entry<String, String> personInfo : uniquePersonList.entrySet()) { String personId = personInfo.getKey(); String personName = personInfo.getValue(); ScaleTwoDecimal personSalaryTotalsForCurrentPeriod = ScaleTwoDecimal.ZERO; ScaleTwoDecimal personFringeTotalsForCurrentPeriod = ScaleTwoDecimal.ZERO; Equals personIdEquals = new Equals("personId", personId); QueryList<BudgetPersonnelDetails> personOccurrencesForSameObjectCode = periodLineItemPersonnelDetailsQueryList .filter(personIdEquals); if (personOccurrencesForSameObjectCode != null && !personOccurrencesForSameObjectCode.isEmpty()) { personSalaryTotalsForCurrentPeriod = personOccurrencesForSameObjectCode .sumObjects("salaryRequested"); personFringeTotalsForCurrentPeriod = personOccurrencesForSameObjectCode .sumObjects("calculatedFringe"); } salaryLineItemObject.getLineItems() .add(new LineItemObject(personId, personName, personSalaryTotalsForCurrentPeriod)); fringeLineItemObject.getLineItems() .add(new LineItemObject(personId, personName, personFringeTotalsForCurrentPeriod)); } totalSalary = totalSalary.add(totalSalaryForCostElement); totalFringe = totalFringe.add(totalFringeForCostElement); personnelSalaries.getLineItems().add(salaryLineItemObject); personnelFringe.getLineItems().add(fringeLineItemObject); } personnelSalaries.setAmount(totalSalary); personnelFringe.setAmount(totalFringe); personnelGroup.getLineItems().add(personnelSalaries); personnelGroup.getLineItems().add(personnelFringe); return personnelGroup; }
From source file:org.apache.taverna.scufl2.translator.t2flow.T2FlowParser.java
protected Revision parseIdentificationAnnotations(Annotations annotations) { SortedMap<Calendar, UUID> revisions = new TreeMap<>(); if (annotations == null || annotations.getAnnotationChainOrAnnotationChain22() == null) return null; for (JAXBElement<AnnotationChain> el : annotations.getAnnotationChainOrAnnotationChain22()) { NetSfTavernaT2AnnotationAnnotationAssertionImpl ann = el.getValue() .getNetSfTavernaT2AnnotationAnnotationChainImpl().getAnnotationAssertions() .getNetSfTavernaT2AnnotationAnnotationAssertionImpl(); String annClass = ann.getAnnotationBean().getClazz(); if (!annClass.equals(IDENTIFICATION_ASSERTION)) continue; for (Object obj : ann.getAnnotationBean().getAny()) { if (!(obj instanceof Element)) continue; Element elem = (Element) obj; if (elem.getNamespaceURI() == null && elem.getLocalName().equals("identification")) { String uuid = elem.getTextContent().trim(); String date = ann.getDate(); Calendar cal = parseDate(date); revisions.put(cal, UUID.fromString(uuid)); }/* www.ja v a 2 s . c om*/ } } Revision rev = null; for (Entry<Calendar, UUID> entry : revisions.entrySet()) { Calendar cal = entry.getKey(); UUID uuid = entry.getValue(); URI uri = WORKFLOW_ROOT.resolve(uuid.toString() + "/"); rev = new Revision(uri, rev); rev.setGeneratedAtTime(cal); } return rev; }
From source file:com.emc.vipr.services.s3.ViPRS3Signer.java
/** * Calculate the canonical string for a REST/HTTP request to S3. * <p/>/* w w w. jav a 2s . c om*/ * When expires is non-null, it will be used instead of the Date header. */ protected String makeS3CanonicalString(String method, String resource, Request<?> request, String expires) { StringBuilder buf = new StringBuilder(); buf.append(method).append("\n"); // Add all interesting headers to a list, then sort them. "Interesting" // is defined as Content-MD5, Content-Type, Date, and x-amz- and x-emc- Map<String, String> headersMap = request.getHeaders(); SortedMap<String, String> interestingHeaders = new TreeMap<String, String>(); if (headersMap != null && headersMap.size() > 0) { for (Map.Entry<String, String> entry : headersMap.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key == null) continue; String lk = key.toLowerCase(Locale.getDefault()); // Ignore any headers that are not particularly interesting. if (lk.equals("content-type") || lk.equals("content-md5") || lk.equals("date") || lk.startsWith(Headers.AMAZON_PREFIX) || lk.startsWith(ViPRConstants.EMC_PREFIX)) { interestingHeaders.put(lk, value); } } } // Remove default date timestamp if "x-amz-date" is set. if (interestingHeaders.containsKey(Headers.S3_ALTERNATE_DATE)) { interestingHeaders.put("date", ""); } // Use the expires value as the timestamp if it is available. This trumps both the default // "date" timestamp, and the "x-amz-date" header. if (expires != null) { interestingHeaders.put("date", expires); } // These headers require that we still put a new line in after them, // even if they don't exist. if (!interestingHeaders.containsKey("content-type")) { interestingHeaders.put("content-type", ""); } if (!interestingHeaders.containsKey("content-md5")) { interestingHeaders.put("content-md5", ""); } // Any parameters that are prefixed with "x-amz-" need to be included // in the headers section of the canonical string to sign for (Map.Entry<String, String> parameter : request.getParameters().entrySet()) { if (parameter.getKey().startsWith("x-amz-")) { interestingHeaders.put(parameter.getKey(), parameter.getValue()); } } // Add all the interesting headers (i.e.: all that startwith x-amz- or x-emc- ;-)) for (Map.Entry<String, String> entry : interestingHeaders.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (key.startsWith(Headers.AMAZON_PREFIX) || key.startsWith(ViPRConstants.EMC_PREFIX)) { buf.append(key).append(':').append(value); } else { buf.append(value); } buf.append("\n"); } // Add all the interesting parameters buf.append(resource); String[] parameterNames = request.getParameters().keySet() .toArray(new String[request.getParameters().size()]); Arrays.sort(parameterNames); char separator = '?'; for (String parameterName : parameterNames) { // Skip any parameters that aren't part of the canonical signed string if (!SIGNED_PARAMETERS.contains(parameterName)) continue; buf.append(separator); buf.append(parameterName); String parameterValue = request.getParameters().get(parameterName); if (parameterValue != null) { buf.append("=").append(parameterValue); } separator = '&'; } return buf.toString(); }
From source file:fll.scheduler.TournamentSchedule.java
public void outputPerformanceSheets(final OutputStream output, final ChallengeDescription description) throws DocumentException, SQLException, IOException { final ScoresheetGenerator scoresheets = new ScoresheetGenerator(getNumberOfRounds() * _schedule.size(), description);/*from ww w .ja v a2 s . c o m*/ final SortedMap<PerformanceTime, TeamScheduleInfo> performanceTimes = new TreeMap<PerformanceTime, TeamScheduleInfo>(); for (int round = 0; round < getNumberOfRounds(); ++round) { for (final TeamScheduleInfo si : _schedule) { performanceTimes.put(si.getPerf(round), si); } } int sheetIndex = 0; for (final Map.Entry<PerformanceTime, TeamScheduleInfo> entry : performanceTimes.entrySet()) { final PerformanceTime performance = entry.getKey(); final TeamScheduleInfo si = entry.getValue(); final int round = si.computeRound(performance); scoresheets.setTime(sheetIndex, performance.getTime()); scoresheets.setTable(sheetIndex, String.format("%s %d", performance.getTable(), performance.getSide())); scoresheets.setRound(sheetIndex, String.valueOf(round + 1)); scoresheets.setNumber(sheetIndex, si.getTeamNumber()); scoresheets.setDivision(sheetIndex, ScoresheetGenerator.AWARD_GROUP_LABEL, si.getAwardGroup()); scoresheets.setName(sheetIndex, si.getTeamName()); ++sheetIndex; } final boolean orientationIsPortrait = ScoresheetGenerator.guessOrientation(description); scoresheets.writeFile(output, orientationIsPortrait); }
From source file:org.dkpro.tc.ml.svmhmm.writer.SVMHMMDataWriter.java
@Override public void write(File aOutputDirectory, FeatureStore featureStore, boolean aUseDenseInstances, String aLearningMode, boolean applyWeighting) throws Exception { // map features to feature numbers BidiMap featureNameToFeatureNumberMapping = SVMHMMUtils .mapVocabularyToIntegers(featureStore.getFeatureNames()); // prepare output file File outputFile = new File(aOutputDirectory, new SVMHMMAdapter() .getFrameworkFilename(TCMachineLearningAdapter.AdapterNameEntries.featureVectorsFile)); BufferedWriter bf = new BufferedWriter(new FileWriter(outputFile)); PrintWriter pw = new PrintWriter(bf); log.info("Start writing features to file " + outputFile.getAbsolutePath()); log.debug("Total instances: " + featureStore.getNumberOfInstances()); log.debug("Feature vector size: " + featureStore.getFeatureNames().size()); if (featureStore instanceof SparseFeatureStore) { SparseFeatureStore sparseFeatureStore = (SparseFeatureStore) featureStore; log.debug("Non-null feature sparsity ratio: " + sparseFeatureStore.getFeatureSparsityRatio()); }/*w ww. ja va2s .c o m*/ for (int i = 0; i < featureStore.getNumberOfInstances(); i++) { Instance instance; instance = featureStore.getInstance(i); // placeholder for original token String originalToken = null; // other "features" - meta data features that will be stored in the comment SortedMap<String, String> metaDataFeatures = new TreeMap<>(); // feature values SortedMap<Integer, Number> featureValues = new TreeMap<>(); for (Feature f : instance.getFeatures()) { String featureName = f.getName(); Object featureValue = f.getValue(); // we ignore null feature values if (featureValue == null) { continue; } // get original token stored in OriginalToken feature if (OriginalTextHolderFeatureExtractor.ORIGINAL_TEXT.equals(featureName)) { // if original token/text was multi line, join it to a single line // originalToken = ((String) featureValue).replaceAll("\\n", " "); originalToken = (String) featureValue; continue; } // handle other possible features as metadata? if (isMetaDataFeature(featureName)) { metaDataFeatures.put(featureName, (String) featureValue); continue; } // not allow other non-number features if (!(featureValue instanceof Number)) { log.debug("Only features with number values are allowed, but was " + f); continue; } // in case the feature store produced dense feature vector with zeros for // non-present features, we ignore zero value features here Number featureValueNumber = (Number) featureValue; if (Math.abs(featureValueNumber.doubleValue() - 0d) < EPS) { continue; } // get number and int value of the feature Integer featureNumber = (Integer) featureNameToFeatureNumberMapping.get(featureName); featureValues.put(featureNumber, featureValueNumber); } // print formatted output: label name and sequence id pw.printf(Locale.ENGLISH, "%s qid:%d ", instance.getOutcome(), getUniqueSequenceId(instance)); // print sorted features for (Map.Entry<Integer, Number> entry : featureValues.entrySet()) { if (entry.getValue() instanceof Double) { // format double on 8 decimal places pw.printf(Locale.ENGLISH, "%d:%.8f ", entry.getKey(), entry.getValue().doubleValue()); } else { // format as integer pw.printf(Locale.ENGLISH, "%d:%d ", entry.getKey(), entry.getValue().intValue()); } } // print original token and label as comment pw.printf(Locale.ENGLISH, "# %s %d %s ", instance.getOutcome(), instance.getSequenceId(), (originalToken != null) ? (URLEncoder.encode(originalToken, "utf-8")) : ""); // print meta-data features at the end for (Map.Entry<String, String> entry : metaDataFeatures.entrySet()) { pw.printf(" %s:%s", URLEncoder.encode(entry.getKey(), "utf-8"), URLEncoder.encode(entry.getValue(), "utf-8")); } // new line at the end pw.println(); } IOUtils.closeQuietly(pw); // writing feature mapping File mappingFile = new File(aOutputDirectory, "featuresIntToNames_forDebug.txt"); SVMHMMUtils.saveMappingTextFormat(featureNameToFeatureNumberMapping, mappingFile); log.info("Finished writing features to file " + outputFile.getAbsolutePath()); }