Example usage for java.util TreeMap size

List of usage examples for java.util TreeMap size

Introduction

In this page you can find the example usage for java.util TreeMap size.

Prototype

int size

To view the source code for java.util TreeMap size.

Click Source Link

Document

The number of entries in the tree

Usage

From source file:org.apache.hama.bsp.sync.ZooKeeperSyncClientImpl.java

@Override
public String[] getAllPeerNames(BSPJobID jobID) {
    if (allPeers == null) {
        TreeMap<Integer, String> sortedMap = new TreeMap<Integer, String>();
        try {/* w  ww  .  j a  v  a2  s  .  c o  m*/
            List<String> var = zk.getChildren(constructKey(jobID, "peers"), this);
            allPeers = var.toArray(new String[var.size()]);

            TreeMap<TaskAttemptID, String> taskAttemptSortedMap = new TreeMap<TaskAttemptID, String>();
            for (String s : allPeers) {
                byte[] data = zk.getData(constructKey(jobID, "peers", s), this, null);
                TaskAttemptID thatTask = new TaskAttemptID();
                boolean result = getValueFromBytes(data, thatTask);

                if (result) {
                    taskAttemptSortedMap.put(thatTask, s);
                }
            }
            for (Map.Entry<TaskAttemptID, String> entry : taskAttemptSortedMap.entrySet()) {
                TaskAttemptID thatTask = entry.getKey();
                String s = entry.getValue();
                LOG.debug("TASK mapping from zookeeper: " + thatTask + " ID:" + thatTask.getTaskID().getId()
                        + " : " + s);
                sortedMap.put(thatTask.getTaskID().getId(), s);
            }
        } catch (Exception e) {
            LOG.error(e);
            throw new RuntimeException("All peer names could not be retrieved!");
        }

        allPeers = new String[sortedMap.size()];
        int count = 0;
        for (Entry<Integer, String> entry : sortedMap.entrySet()) {
            allPeers[count++] = entry.getValue();
            LOG.debug("TASK mapping from zookeeper: " + entry.getKey() + " : " + entry.getValue() + " at index "
                    + (count - 1));
        }

    }
    return allPeers;
}

From source file:com.espertech.esper.rowregex.EventRowRegexNFAView.java

private List<RegexNFAStateEntry> rankEndStatesWithinPartitionByStart(List<RegexNFAStateEntry> endStates) {
    if (endStates.isEmpty()) {
        return endStates;
    }/*from  ww  w.  ja  v  a  2  s .co m*/
    if (endStates.size() == 1) {
        return endStates;
    }

    TreeMap<Integer, Object> endStatesPerBeginEvent = new TreeMap<Integer, Object>();
    for (RegexNFAStateEntry entry : endStates) {
        Integer endNum = entry.getMatchBeginEventSeqNo();
        Object value = endStatesPerBeginEvent.get(endNum);
        if (value == null) {
            endStatesPerBeginEvent.put(endNum, entry);
        } else if (value instanceof List) {
            List<RegexNFAStateEntry> entries = (List<RegexNFAStateEntry>) value;
            entries.add(entry);
        } else {
            List<RegexNFAStateEntry> entries = new ArrayList<RegexNFAStateEntry>();
            entries.add((RegexNFAStateEntry) value);
            entries.add(entry);
            endStatesPerBeginEvent.put(endNum, entries);
        }
    }

    if (endStatesPerBeginEvent.size() == 1) {
        List<RegexNFAStateEntry> endStatesUnranked = (List<RegexNFAStateEntry>) endStatesPerBeginEvent.values()
                .iterator().next();
        if (matchRecognizeSpec.isAllMatches()) {
            return endStatesUnranked;
        }
        RegexNFAStateEntry chosen = rankEndStates(endStatesUnranked);
        return Collections.singletonList(chosen);
    }

    List<RegexNFAStateEntry> endStatesRanked = new ArrayList<RegexNFAStateEntry>();
    Set<Integer> keyset = endStatesPerBeginEvent.keySet();
    Integer[] keys = keyset.toArray(new Integer[keyset.size()]);
    for (Integer key : keys) {
        Object value = endStatesPerBeginEvent.remove(key);
        if (value == null) {
            continue;
        }

        RegexNFAStateEntry entryTaken;
        if (value instanceof List) {
            List<RegexNFAStateEntry> endStatesUnranked = (List<RegexNFAStateEntry>) value;
            if (endStatesUnranked.isEmpty()) {
                continue;
            }
            entryTaken = rankEndStates(endStatesUnranked);

            if (matchRecognizeSpec.isAllMatches()) {
                endStatesRanked.addAll(endStatesUnranked); // we take all matches and don't rank except to determine skip-past
            } else {
                endStatesRanked.add(entryTaken);
            }
        } else {
            entryTaken = (RegexNFAStateEntry) value;
            endStatesRanked.add(entryTaken);
        }
        // could be null as removals take place

        if (entryTaken != null) {
            if (matchRecognizeSpec.getSkip().getSkip() == MatchRecognizeSkipEnum.PAST_LAST_ROW) {
                int skipPastRow = entryTaken.getMatchEndEventSeqNo();
                removeSkippedEndStates(endStatesPerBeginEvent, skipPastRow);
            } else if (matchRecognizeSpec.getSkip().getSkip() == MatchRecognizeSkipEnum.TO_NEXT_ROW) {
                int skipPastRow = entryTaken.getMatchBeginEventSeqNo();
                removeSkippedEndStates(endStatesPerBeginEvent, skipPastRow);
            }
        }
    }

    return endStatesRanked;
}

From source file:org.apache.hadoop.mapred.HFSPScheduler.java

/**
 * Preempt missingSlots number of slots from jobs bigger
 * //from   www.  j a va 2  s  . c om
 * @param jip
 *          Job that clamies slots
 * @param allJobs
 *          all the size based jobs in the cluster
 * @param localJobs
 *          size based jobs that can be immediately suspended
 * @param missingSlots
 *          number of slots to claim
 * @param numToSkip
 *          number of slots on non-local
 * 
 * @return number of tasks preemped in the cluster for jip. The first elem of
 *         the tuple is the number of tasks preempted for new tasks, the
 *         second in the number of tasks preempted for tasks to be resumed
 */
private ClaimedSlots claimSlots(HelperForType helper, final Phase phase, final JobInProgress jip,
        int missingNewSlots, int missingResumableSlots, int numToSkip,
        TreeMap<JobDurationInfo, JobInProgress> allJobs, TreeMap<JobDurationInfo, TaskStatuses> localJobs) {

    assert phase == Phase.SIZE_BASED || missingResumableSlots == 0;

    final TaskType type = helper.taskType;
    JobDurationInfo jdi = this.getDuration(jip.getJobID(), type);

    /* #size based tasks that occupies train slots in the cluster (suspendable) */
    int numTasksToPreempt = 0;
    if (phase == Phase.TRAIN) {
        /** num of size based tasks that can be suspended for training */
        int numOverflowSizeBasedTasks = helper.maxSizeBasedSlots > helper.runningSizeBasedTasks ? 0
                : helper.runningSizeBasedTasks - helper.maxSizeBasedSlots;

        /* num of size base tasks to preempt for the training of jip */
        numTasksToPreempt = Math.min(missingNewSlots, numOverflowSizeBasedTasks);
        if (LOG.isDebugEnabled()) {
            LOG.debug(phase.toString() + "(" + jip.getJobID() + ":" + type + "):"
                    + " numOverflowSizeBasedTasks: " + numOverflowSizeBasedTasks + " numTasksToPreempt: "
                    + numTasksToPreempt + " missingNewSlots: " + missingNewSlots + " numTrainTasksForJob: "
                    + helper.numTrainTasksForJob + " canAssignTrain: " + helper.canAssignTrain()
                    + " numToSkip: " + numToSkip);
        }
    } else {
        numTasksToPreempt = missingNewSlots;
        if (LOG.isDebugEnabled()) {
            LOG.debug(phase.toString() + "(" + jip.getJobID() + ":" + type + "):" + " missingNewSlots: "
                    + missingNewSlots + " missingResumableSlots: " + missingResumableSlots
                    + " numTrainTasksForJob: " + helper.numTrainTasksForJob + " canAssignTrain: "
                    + helper.canAssignTrain() + " numToSkip: " + numToSkip);
        }
    }
    final int startingNumTasksToPreemptForNew = numTasksToPreempt;
    final int startingResumableSlots = missingResumableSlots;

    // try to free pendingTasks number of slots among running on this TT
    Iterator<Entry<JobDurationInfo, JobInProgress>> sizeBasedJobsDescIter = allJobs.descendingMap().entrySet()
            .iterator();
    Iterator<Entry<JobDurationInfo, TaskStatuses>> sizeBasedJobsDescIterOnTT = localJobs.entrySet().iterator();

    Entry<JobDurationInfo, TaskStatuses> biggerOnTT = sizeBasedJobsDescIterOnTT.hasNext()
            ? sizeBasedJobsDescIterOnTT.next()
            : null;
    while (this.preemptionStrategy.isPreemptionActive()
            && (numTasksToPreempt > 0 || missingResumableSlots > 0)) {
        if (!sizeBasedJobsDescIter.hasNext()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(phase.toString() + "(" + jip.getJobID() + ":" + type + "):" + " should preempt "
                        + numTasksToPreempt + " for new tasks and " + missingResumableSlots + " for resumable "
                        + "tasks but no sizeBasedJob is running");
            }
            break;
        }

        Entry<JobDurationInfo, JobInProgress> nextSBJ = sizeBasedJobsDescIter.next();

        JobInProgress jipToPreempt = nextSBJ.getValue();

        /* don't try to suspend if jip is bigger than any other jip */
        if (jdi != null) {

            if (jipToPreempt.getJobID().equals(jip.getJobID())) {
                return new ClaimedSlots(startingNumTasksToPreemptForNew - numTasksToPreempt,
                        startingResumableSlots - missingResumableSlots);
            }

            if (JOB_DURATION_COMPARATOR.compare(nextSBJ.getKey(), jdi) <= 0) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(phase.toString() + "(" + jip.getJobID() + ":" + type + "):" + " should preempt "
                            + numTasksToPreempt + ", but bigger job avail is " + jip.getJobID() + ".len: "
                            + jdi.getPhaseDuration() + " > " + nextSBJ.getValue().getJobID() + ".len: "
                            + nextSBJ.getKey().getPhaseDuration());
                }
                return new ClaimedSlots(startingNumTasksToPreemptForNew - numTasksToPreempt,
                        startingResumableSlots - missingResumableSlots);
            }
        }

        if (jipToPreempt.getJobID().equals(jip.getJobID())) {
            continue;
        }

        /*
         * don't try to claim slots from a job in training
         * 
         * FIXME: ideally a job can claim slots from a training job until this job
         * has enough tasks for training
         */
        if (!this.isTrained(jipToPreempt, type)) {
            LOG.debug(phase.toString() + "(" + jip.getJobID() + ":" + type + "):" + " ignoring "
                    + jipToPreempt.getJobID() + " because in training");
            continue;
        }

        int numSuspendedOnThisTT = 0;

        /* if jipToPreempt has tasks on this TT, then suspend them */
        if (biggerOnTT != null // && type == TaskType.REDUCE
                && biggerOnTT.getKey().getJobID().equals(nextSBJ.getKey().getJobID())) {

            TreeMap<TaskAttemptID, TaskStatus> preemptableTAIDS = biggerOnTT.getValue().taskStatuses;
            int numPreemptions = Math.min(preemptableTAIDS.size(), missingResumableSlots + numTasksToPreempt);
            for (int i = 0; i < numPreemptions; i++) {
                TaskAttemptID pTAID = preemptableTAIDS.firstKey();
                TaskStatus pTS = preemptableTAIDS.remove(pTAID);
                JobInProgress pJIP = this.taskTrackerManager.getJob(pTAID.getJobID());
                TaskInProgress pTIP = pJIP.getTaskInProgress(pTAID.getTaskID());

                if (type == TaskType.REDUCE) {
                    // if (this.eagerPreemption == PreemptionType.KILL
                    // && pTIP.killTask(pTAID, false)) {
                    // if (missingResumableSlots > 0)
                    // missingResumableSlots -= 1;
                    // else
                    // numTasksToPreempt -= 1;
                    // numSuspendedOnThisTT += 1;
                    // if (jdi == null) {
                    // taskHelper.kill(pTAID, jip.getJobID(), phase);
                    // } else {
                    // taskHelper.kill(pTAID, jip.getJobID(), phase, nextSBJ.getKey(),
                    // jdi);
                    // }
                    // } else if (this.preemptionStrategy.isPreemptionActive()
                    // && this.canBeSuspended(pTS) && pTIP.suspendTaskAttempt(pTAID)) {
                    if (this.preemptionStrategy.isPreemptionActive()
                            && this.preemptionStrategy.canBePreempted(pTS)
                            && this.preemptionStrategy.preempt(pTIP, pTS)) {
                        if (missingResumableSlots > 0)
                            missingResumableSlots -= 1;
                        else
                            numTasksToPreempt -= 1;
                        numSuspendedOnThisTT += 1;
                        if (jdi == null) {
                            taskHelper.suspend(pTAID, jip.getJobID(), phase);
                        } else {
                            taskHelper.suspend(pTAID, jip.getJobID(), phase, nextSBJ.getKey(), jdi);
                        }
                    } else {
                        LOG.debug(phase.toString() + "(" + jip.getJobID() + ":" + type + "): cannot suspend "
                                + pTAID + " for " + jip);
                    }
                }
            }

            if (preemptableTAIDS.size() - numPreemptions <= 0) {
                biggerOnTT = sizeBasedJobsDescIterOnTT.hasNext() ? sizeBasedJobsDescIterOnTT.next() : null;
            }
        }

        /* #tasks that can be preempted */
        int numPreemptibleRunTasks = this.getNumRunningTasks(jipToPreempt, type) - numSuspendedOnThisTT;

        /*
         * Two cases: numToSkip is bigger then preemptible tasks or it is not: -
         * is bigger: then we skip this preemptible jip - it is not: then
         * numToSkip is set to 0 and we do the real wait preemption
         */
        if (numPreemptibleRunTasks <= numToSkip) {
            numToSkip -= numPreemptibleRunTasks;
        } else {
            /* #tasks that can be preempted by jip */
            int numPreemptibleByJIPRunTasks = numPreemptibleRunTasks - numToSkip;

            numToSkip = 0;

            /* #tasks that will be preempted by jip on other TTs s */
            int numRunTasksEventuallyPreemptedByJIP = Math.min(numTasksToPreempt, numPreemptibleByJIPRunTasks);

            numTasksToPreempt -= numRunTasksEventuallyPreemptedByJIP;
        }
    }

    return new ClaimedSlots(startingNumTasksToPreemptForNew - numTasksToPreempt,
            startingResumableSlots - missingNewSlots);
}

From source file:website.openeng.anki.DeckPicker.java

public void confirmDeckDeletion(DialogFragment parent) {
    Resources res = getResources();
    if (!colIsOpen()) {
        return;/*from  ww w  .  j  a v  a 2 s  .c om*/
    }
    if (mContextMenuDid == 1) {
        showSimpleSnackbar(R.string.delete_deck_default_deck, true);
        dismissAllDialogFragments();
        return;
    }
    // Get the number of cards contained in this deck and its subdecks
    TreeMap<String, Long> children = getCol().getDecks().children(mContextMenuDid);
    long[] dids = new long[children.size() + 1];
    dids[0] = mContextMenuDid;
    int i = 1;
    for (Long l : children.values()) {
        dids[i++] = l;
    }
    String ids = Utils.ids2str(dids);
    int cnt = getCol().getDb()
            .queryScalar("select count() from cards where did in " + ids + " or odid in " + ids);
    // Delete empty decks without warning
    if (cnt == 0) {
        deleteContextMenuDeck();
        dismissAllDialogFragments();
        return;
    }
    // Otherwise we show a warning and require confirmation
    String msg;
    String deckName = "\'" + getCol().getDecks().name(mContextMenuDid) + "\'";
    boolean isDyn = getCol().getDecks().isDyn(mContextMenuDid);
    if (isDyn) {
        msg = String.format(res.getString(R.string.delete_cram_deck_message), deckName);
    } else {
        msg = res.getQuantityString(R.plurals.delete_deck_message, cnt, deckName, cnt);
    }
    showDialogFragment(DeckPickerConfirmDeleteDeckDialog.newInstance(msg));
}

From source file:com.joliciel.talismane.parser.TransitionBasedParserImpl.java

@Override
public List<ParseConfiguration> parseSentence(List<PosTagSequence> posTagSequences) {
    MONITOR.startTask("parseSentence");
    try {/*w  w  w . j  a va2s .  co  m*/
        long startTime = (new Date()).getTime();
        int maxAnalysisTimeMilliseconds = maxAnalysisTimePerSentence * 1000;
        int minFreeMemoryBytes = minFreeMemory * KILOBYTE;

        TokenSequence tokenSequence = posTagSequences.get(0).getTokenSequence();

        TreeMap<Integer, PriorityQueue<ParseConfiguration>> heaps = new TreeMap<Integer, PriorityQueue<ParseConfiguration>>();

        PriorityQueue<ParseConfiguration> heap0 = new PriorityQueue<ParseConfiguration>();
        for (PosTagSequence posTagSequence : posTagSequences) {
            // add an initial ParseConfiguration for each postag sequence
            ParseConfiguration initialConfiguration = this.getParserServiceInternal()
                    .getInitialConfiguration(posTagSequence);
            initialConfiguration.setScoringStrategy(decisionMaker.getDefaultScoringStrategy());
            heap0.add(initialConfiguration);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding initial posTagSequence: " + posTagSequence);
            }
        }
        heaps.put(0, heap0);
        PriorityQueue<ParseConfiguration> backupHeap = null;

        PriorityQueue<ParseConfiguration> finalHeap = null;
        PriorityQueue<ParseConfiguration> terminalHeap = new PriorityQueue<ParseConfiguration>();
        while (heaps.size() > 0) {
            Entry<Integer, PriorityQueue<ParseConfiguration>> heapEntry = heaps.pollFirstEntry();
            PriorityQueue<ParseConfiguration> currentHeap = heapEntry.getValue();
            int currentHeapIndex = heapEntry.getKey();
            if (LOG.isTraceEnabled()) {
                LOG.trace("##### Polling next heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
            }

            boolean finished = false;
            // systematically set the final heap here, just in case we exit "naturally" with no more heaps
            finalHeap = heapEntry.getValue();
            backupHeap = new PriorityQueue<ParseConfiguration>();

            // we jump out when either (a) all tokens have been attached or (b) we go over the max alloted time
            ParseConfiguration topConf = currentHeap.peek();
            if (topConf.isTerminal()) {
                LOG.trace("Exiting with terminal heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
                finished = true;
            }

            if (earlyStop && terminalHeap.size() >= beamWidth) {
                LOG.debug(
                        "Early stop activated and terminal heap contains " + beamWidth + " entries. Exiting.");
                finalHeap = terminalHeap;
                finished = true;
            }

            long analysisTime = (new Date()).getTime() - startTime;
            if (maxAnalysisTimePerSentence > 0 && analysisTime > maxAnalysisTimeMilliseconds) {
                LOG.info("Parse tree analysis took too long for sentence: " + tokenSequence.getText());
                LOG.info("Breaking out after " + maxAnalysisTimePerSentence + " seconds.");
                finished = true;
            }

            if (minFreeMemory > 0) {
                long freeMemory = Runtime.getRuntime().freeMemory();
                if (freeMemory < minFreeMemoryBytes) {
                    LOG.info("Not enough memory left to parse sentence: " + tokenSequence.getText());
                    LOG.info("Min free memory (bytes):" + minFreeMemoryBytes);
                    LOG.info("Current free memory (bytes): " + freeMemory);
                    finished = true;
                }
            }

            if (finished) {
                break;
            }

            // limit the breadth to K
            int maxSequences = currentHeap.size() > this.beamWidth ? this.beamWidth : currentHeap.size();

            int j = 0;
            while (currentHeap.size() > 0) {
                ParseConfiguration history = currentHeap.poll();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("### Next configuration on heap " + heapEntry.getKey() + ":");
                    LOG.trace(history.toString());
                    LOG.trace("Score: " + df.format(history.getScore()));
                    LOG.trace(history.getPosTagSequence());
                }

                List<Decision<Transition>> decisions = new ArrayList<Decision<Transition>>();

                // test the positive rules on the current configuration
                boolean ruleApplied = false;
                if (parserPositiveRules != null) {
                    MONITOR.startTask("check rules");
                    try {
                        for (ParserRule rule : parserPositiveRules) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Checking rule: " + rule.toString());
                            }
                            RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                            FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                            if (ruleResult != null && ruleResult.getOutcome()) {
                                Decision<Transition> positiveRuleDecision = TalismaneSession
                                        .getTransitionSystem().createDefaultDecision(rule.getTransition());
                                decisions.add(positiveRuleDecision);
                                positiveRuleDecision.addAuthority(rule.getCondition().getName());
                                ruleApplied = true;
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Rule applies. Setting transition to: "
                                            + rule.getTransition().getCode());
                                }
                                break;
                            }
                        }
                    } finally {
                        MONITOR.endTask("check rules");
                    }
                }

                if (!ruleApplied) {
                    // test the features on the current configuration
                    List<FeatureResult<?>> parseFeatureResults = new ArrayList<FeatureResult<?>>();
                    MONITOR.startTask("feature analyse");
                    try {
                        for (ParseConfigurationFeature<?> feature : this.parseFeatures) {
                            MONITOR.startTask(feature.getName());
                            try {
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<?> featureResult = feature.check(history, env);
                                if (featureResult != null)
                                    parseFeatureResults.add(featureResult);
                            } finally {
                                MONITOR.endTask(feature.getName());
                            }
                        }
                        if (LOG_FEATURES.isTraceEnabled()) {
                            for (FeatureResult<?> featureResult : parseFeatureResults) {
                                LOG_FEATURES.trace(featureResult.toString());
                            }
                        }
                    } finally {
                        MONITOR.endTask("feature analyse");
                    }

                    // evaluate the feature results using the decision maker
                    MONITOR.startTask("make decision");
                    try {
                        decisions = this.decisionMaker.decide(parseFeatureResults);

                        for (ClassificationObserver<Transition> observer : this.observers) {
                            observer.onAnalyse(history, parseFeatureResults, decisions);
                        }

                        List<Decision<Transition>> decisionShortList = new ArrayList<Decision<Transition>>(
                                decisions.size());
                        for (Decision<Transition> decision : decisions) {
                            if (decision.getProbability() > MIN_PROB_TO_STORE)
                                decisionShortList.add(decision);
                        }
                        decisions = decisionShortList;
                    } finally {
                        MONITOR.endTask("make decision");
                    }

                    // apply the negative rules
                    Set<Transition> eliminatedTransitions = new HashSet<Transition>();
                    if (parserNegativeRules != null) {
                        MONITOR.startTask("check negative rules");
                        try {
                            for (ParserRule rule : parserNegativeRules) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Checking negative rule: " + rule.toString());
                                }
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                                if (ruleResult != null && ruleResult.getOutcome()) {
                                    eliminatedTransitions.addAll(rule.getTransitions());
                                    if (LOG.isTraceEnabled()) {
                                        for (Transition eliminatedTransition : rule.getTransitions())
                                            LOG.trace("Rule applies. Eliminating transition: "
                                                    + eliminatedTransition.getCode());
                                    }
                                }
                            }

                            if (eliminatedTransitions.size() > 0) {
                                List<Decision<Transition>> decisionShortList = new ArrayList<Decision<Transition>>();
                                for (Decision<Transition> decision : decisions) {
                                    if (!eliminatedTransitions.contains(decision.getOutcome())) {
                                        decisionShortList.add(decision);
                                    } else {
                                        LOG.trace("Eliminating decision: " + decision.toString());
                                    }
                                }
                                if (decisionShortList.size() > 0) {
                                    decisions = decisionShortList;
                                } else {
                                    LOG.debug("All decisions eliminated! Restoring original decisions.");
                                }
                            }
                        } finally {
                            MONITOR.endTask("check negative rules");
                        }
                    }
                } // has a positive rule been applied?

                boolean transitionApplied = false;
                // add new configuration to the heap, one for each valid transition
                MONITOR.startTask("heap sort");
                try {
                    // Why apply all decisions here? Why not just the top N (where N = beamwidth)?
                    // Answer: because we're not always adding solutions to the same heap
                    // And yet: a decision here can only do one of two things: process a token (heap+1000), or add a non-processing transition (heap+1)
                    // So, if we've already applied N decisions of each type, we should be able to stop
                    for (Decision<Transition> decision : decisions) {
                        Transition transition = decision.getOutcome();
                        if (LOG.isTraceEnabled())
                            LOG.trace("Outcome: " + transition.getCode() + ", " + decision.getProbability());

                        if (transition.checkPreconditions(history)) {
                            transitionApplied = true;
                            ParseConfiguration configuration = this.parserServiceInternal
                                    .getConfiguration(history);
                            if (decision.isStatistical())
                                configuration.addDecision(decision);
                            transition.apply(configuration);

                            int nextHeapIndex = parseComparisonStrategy.getComparisonIndex(configuration)
                                    * 1000;
                            if (configuration.isTerminal()) {
                                nextHeapIndex = Integer.MAX_VALUE;
                            } else {
                                while (nextHeapIndex <= currentHeapIndex)
                                    nextHeapIndex++;
                            }

                            PriorityQueue<ParseConfiguration> nextHeap = heaps.get(nextHeapIndex);
                            if (nextHeap == null) {
                                if (configuration.isTerminal())
                                    nextHeap = terminalHeap;
                                else
                                    nextHeap = new PriorityQueue<ParseConfiguration>();
                                heaps.put(nextHeapIndex, nextHeap);
                                if (LOG.isTraceEnabled())
                                    LOG.trace("Created heap with index: " + nextHeapIndex);
                            }
                            nextHeap.add(configuration);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Added configuration with score " + configuration.getScore()
                                        + " to heap: " + nextHeapIndex + ", total size: " + nextHeap.size());
                            }

                            configuration.clearMemory();
                        } else {
                            if (LOG.isTraceEnabled())
                                LOG.trace("Cannot apply transition: doesn't meet pre-conditions");
                            // just in case the we run out of both heaps and analyses, we build this backup heap
                            backupHeap.add(history);
                        } // does transition meet pre-conditions?
                    } // next transition
                } finally {
                    MONITOR.endTask("heap sort");
                }

                if (transitionApplied) {
                    j++;
                } else {
                    LOG.trace("No transitions could be applied: not counting this history as part of the beam");
                }

                // beam width test
                if (j == maxSequences)
                    break;
            } // next history   
        } // next atomic index

        // return the best sequences on the heap
        List<ParseConfiguration> bestConfigurations = new ArrayList<ParseConfiguration>();
        int i = 0;

        if (finalHeap.isEmpty())
            finalHeap = backupHeap;

        while (!finalHeap.isEmpty()) {
            bestConfigurations.add(finalHeap.poll());
            i++;
            if (i >= this.getBeamWidth())
                break;
        }
        if (LOG.isDebugEnabled()) {
            for (ParseConfiguration finalConfiguration : bestConfigurations) {
                LOG.debug(df.format(finalConfiguration.getScore()) + ": " + finalConfiguration.toString());
                LOG.debug("Pos tag sequence: " + finalConfiguration.getPosTagSequence());
                LOG.debug("Transitions: " + finalConfiguration.getTransitions());
                LOG.debug("Decisions: " + finalConfiguration.getDecisions());
                if (LOG.isTraceEnabled()) {
                    StringBuilder sb = new StringBuilder();
                    for (Decision<Transition> decision : finalConfiguration.getDecisions()) {
                        sb.append(" * ");
                        sb.append(df.format(decision.getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getTransitions().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * PosTag sequence score ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getScore()));
                    sb.append(" = ");
                    for (PosTaggedToken posTaggedToken : finalConfiguration.getPosTagSequence()) {
                        sb.append(" * ");
                        sb.append(df.format(posTaggedToken.getDecision().getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getPosTagSequence().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * Token sequence score = ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getTokenSequence().getScore()));
                    LOG.trace(sb.toString());

                }
            }
        }
        return bestConfigurations;
    } finally {
        MONITOR.endTask("parseSentence");
    }
}

From source file:com.joliciel.talismane.parser.TransitionBasedGlobalLearningParser.java

public List<ParseConfiguration> parseSentence(List<PosTagSequence> posTagSequences,
        FeatureWeightVector weightVector, RankingSolution correctSolution) {
    MONITOR.startTask("parseSentence");
    try {//from www . ja v a  2 s . c o  m
        long startTime = (new Date()).getTime();
        int maxAnalysisTimeMilliseconds = maxAnalysisTimePerSentence * 1000;
        int minFreeMemoryBytes = minFreeMemory * KILOBYTE;

        TokenSequence tokenSequence = posTagSequences.get(0).getTokenSequence();

        TreeMap<Integer, TreeSet<ParseConfiguration>> heaps = new TreeMap<Integer, TreeSet<ParseConfiguration>>();

        TreeSet<ParseConfiguration> heap0 = new TreeSet<ParseConfiguration>();
        for (PosTagSequence posTagSequence : posTagSequences) {
            // add an initial ParseConfiguration for each postag sequence
            ParseConfiguration initialConfiguration = this.getParserServiceInternal()
                    .getInitialConfiguration(posTagSequence);
            initialConfiguration.setScoringStrategy(new SimpleRankingScoringStrategy());
            initialConfiguration.setRankingScore(0.0);
            heap0.add(initialConfiguration);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding initial posTagSequence: " + posTagSequence);
            }
        }
        heaps.put(0, heap0);
        TreeSet<ParseConfiguration> backupHeap = null;

        TreeSet<ParseConfiguration> finalHeap = null;
        while (heaps.size() > 0) {
            Entry<Integer, TreeSet<ParseConfiguration>> heapEntry = heaps.firstEntry();
            TreeSet<ParseConfiguration> currentHeap = heapEntry.getValue();
            int currentHeapIndex = heapEntry.getKey();
            if (LOG.isTraceEnabled()) {
                LOG.trace("##### Polling next heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
            }

            boolean finished = false;
            // systematically set the final heap here, just in case we exit "naturally" with no more heaps
            finalHeap = heapEntry.getValue();
            backupHeap = new TreeSet<ParseConfiguration>();

            // we jump out when either (a) all tokens have been attached or (b) we go over the max alloted time
            ParseConfiguration topConf = currentHeap.first();
            if (topConf.isTerminal()) {
                LOG.trace("Exiting with terminal heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
                finished = true;
            }

            // check if we've gone over alloted time for this sentence
            long analysisTime = (new Date()).getTime() - startTime;
            if (maxAnalysisTimePerSentence > 0 && analysisTime > maxAnalysisTimeMilliseconds) {
                LOG.info("Parse tree analysis took too long for sentence: " + tokenSequence.getText());
                LOG.info("Breaking out after " + maxAnalysisTimePerSentence + " seconds.");
                finished = true;
            }

            // check if we've enough memory to process this sentence
            if (minFreeMemory > 0) {
                long freeMemory = Runtime.getRuntime().freeMemory();
                if (freeMemory < minFreeMemoryBytes) {
                    LOG.info("Not enough memory left to parse sentence: " + tokenSequence.getText());
                    LOG.info("Min free memory (bytes):" + minFreeMemoryBytes);
                    LOG.info("Current free memory (bytes): " + freeMemory);
                    finished = true;
                }
            }

            // check if any of the remaining top-N solutions on any heap can lead to the correct solution
            if (correctSolution != null) {
                boolean canReachCorrectSolution = false;
                for (TreeSet<ParseConfiguration> heap : heaps.values()) {
                    int j = 1;
                    for (ParseConfiguration solution : heap) {
                        if (j > beamWidth)
                            break;
                        if (solution.canReach(correctSolution)) {
                            canReachCorrectSolution = true;
                            break;
                        }
                        j++;
                    }
                    if (canReachCorrectSolution)
                        break;
                }
                if (!canReachCorrectSolution) {
                    LOG.debug("None of the solutions on the heap can reach the gold solution. Exiting.");
                    finished = true;
                }
            }

            if (finished) {
                // combine any remaining heaps
                for (TreeSet<ParseConfiguration> heap : heaps.values()) {
                    if (finalHeap != heap) {
                        finalHeap.addAll(heap);
                    }
                }
                break;
            }

            // remove heap from set of heaps
            heapEntry = heaps.pollFirstEntry();

            // limit the breadth to K
            int maxSolutions = currentHeap.size() > this.beamWidth ? this.beamWidth : currentHeap.size();

            int j = 0;
            while (currentHeap.size() > 0) {
                ParseConfiguration history = currentHeap.pollFirst();
                backupHeap.add(history);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("### Next configuration on heap " + heapEntry.getKey() + ":");
                    LOG.trace(history.toString());
                    LOG.trace("Score: " + df.format(history.getScore()));
                    LOG.trace(history.getPosTagSequence());
                }

                Set<Transition> transitions = new HashSet<Transition>();

                // test the positive rules on the current configuration
                boolean ruleApplied = false;
                if (parserPositiveRules != null) {
                    MONITOR.startTask("check rules");
                    try {
                        for (ParserRule rule : parserPositiveRules) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Checking rule: " + rule.getCondition().getName());
                            }
                            RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                            FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                            if (ruleResult != null && ruleResult.getOutcome()) {
                                transitions.add(rule.getTransition());
                                ruleApplied = true;
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Rule applies. Setting transition to: "
                                            + rule.getTransition().getCode());
                                }

                                if (!rule.getTransition().checkPreconditions(history)) {
                                    LOG.error("Cannot apply rule, preconditions not met.");
                                    ruleApplied = false;
                                }
                                break;
                            }
                        }
                    } finally {
                        MONITOR.endTask("check rules");
                    }
                }

                if (!ruleApplied) {
                    transitions = parsingConstrainer.getPossibleTransitions(history);

                    Set<Transition> eliminatedTransitions = new HashSet<Transition>();
                    for (Transition transition : transitions) {
                        if (!transition.checkPreconditions(history)) {
                            eliminatedTransitions.add(transition);
                        }
                    }
                    transitions.removeAll(eliminatedTransitions);

                    // apply the negative rules
                    eliminatedTransitions = new HashSet<Transition>();
                    if (parserNegativeRules != null) {
                        MONITOR.startTask("check negative rules");
                        try {
                            for (ParserRule rule : parserNegativeRules) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Checking negative rule: " + rule.getCondition().getName());
                                }
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                                if (ruleResult != null && ruleResult.getOutcome()) {
                                    eliminatedTransitions.add(rule.getTransition());
                                    if (LOG.isTraceEnabled()) {
                                        LOG.debug("Rule applies. Eliminating transition: "
                                                + rule.getTransition().getCode());
                                    }
                                }
                            }

                            if (eliminatedTransitions.size() == transitions.size()) {
                                LOG.debug("All transitions eliminated! Restoring original transitions.");
                            } else {
                                transitions.removeAll(eliminatedTransitions);
                            }
                        } finally {
                            MONITOR.endTask("check negative rules");
                        }
                    }
                } // has a positive rule been applied?

                if (transitions.size() == 0) {
                    // just in case the we run out of both heaps and analyses, we build this backup heap
                    backupHeap.add(history);
                    if (LOG.isTraceEnabled())
                        LOG.trace(
                                "No transitions could be applied: not counting this solution as part of the beam");
                } else {
                    // up the counter, since we will count this solution towards the heap
                    j++;
                    // add solutions to the heap, one per valid transition
                    MONITOR.startTask("heap sort");
                    try {
                        Map<Transition, Double> deltaScorePerTransition = new HashMap<Transition, Double>();
                        double absoluteMax = 1;

                        for (Transition transition : transitions) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Applying transition: " + transition.getCode());
                            }
                            ParseConfiguration configuration = this.parserServiceInternal
                                    .getConfiguration(history);
                            transition.apply(configuration);
                            configuration.setRankingScore(history.getRankingScore());
                            configuration.getIncrementalFeatureResults()
                                    .addAll(history.getIncrementalFeatureResults());

                            // test the features on the new configuration
                            double scoreDelta = 0.0;
                            MONITOR.startTask("feature analyse");
                            List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
                            try {
                                for (ParseConfigurationFeature<?> feature : this.parseFeatures) {
                                    MONITOR.startTask(feature.getName());
                                    try {
                                        RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                        FeatureResult<?> featureResult = feature.check(configuration, env);
                                        if (featureResult != null) {
                                            featureResults.add(featureResult);
                                            double weight = weightVector.getWeight(featureResult);
                                            scoreDelta += weight;
                                            if (LOG.isTraceEnabled()) {
                                                LOG.trace(featureResult.toString() + " = " + weight);
                                            }
                                        }
                                    } finally {
                                        MONITOR.endTask(feature.getName());
                                    }
                                }
                                configuration.getIncrementalFeatureResults().add(featureResults);
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Score = " + configuration.getRankingScore() + " + " + scoreDelta
                                            + " = " + (configuration.getRankingScore() + scoreDelta));
                                }
                                configuration.setRankingScore(configuration.getRankingScore() + scoreDelta);
                                deltaScorePerTransition.put(transition, scoreDelta);
                                if (Math.abs(scoreDelta) > absoluteMax)
                                    absoluteMax = Math.abs(scoreDelta);

                            } finally {
                                MONITOR.endTask("feature analyse");
                            }

                            int nextHeapIndex = parseComparisonStrategy.getComparisonIndex(configuration)
                                    * 1000;
                            while (nextHeapIndex <= currentHeapIndex)
                                nextHeapIndex++;

                            TreeSet<ParseConfiguration> nextHeap = heaps.get(nextHeapIndex);
                            if (nextHeap == null) {
                                nextHeap = new TreeSet<ParseConfiguration>();
                                heaps.put(nextHeapIndex, nextHeap);
                                if (LOG.isTraceEnabled())
                                    LOG.trace("Created heap with index: " + nextHeapIndex);
                            }
                            nextHeap.add(configuration);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Added configuration with score " + configuration.getScore()
                                        + " to heap: " + nextHeapIndex + ", total size: " + nextHeap.size());
                            }

                            configuration.clearMemory();
                        } // next transition

                        // Create a probability distribution of transitions
                        // normalise probabilities for each transition via normalised exponential
                        // e^(x/absmax)/sum(e^(x/absmax))
                        // where x/absmax is in [-1,1]
                        // e^(x/absmax) is in [1/e,e]

                        double total = 0.0;
                        for (Transition transition : deltaScorePerTransition.keySet()) {
                            double deltaScore = deltaScorePerTransition.get(transition);
                            deltaScore = Math.exp(deltaScore / absoluteMax);
                            deltaScorePerTransition.put(transition, deltaScore);
                            total += deltaScore;
                        }

                        for (Transition transition : deltaScorePerTransition.keySet()) {
                            double probability = deltaScorePerTransition.get(transition);
                            probability /= total;
                            Decision<Transition> decision = machineLearningService.createDecision(transition,
                                    probability);
                            transition.setDecision(decision);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Transition: " + transition.getCode() + ", Prob: " + probability);
                            }
                        }

                    } finally {
                        MONITOR.endTask("heap sort");
                    }
                } // have we any transitions?

                // beam width test
                if (j == maxSolutions)
                    break;
            } // next history   
        } // next atomic index

        // return the best sequences on the heap
        List<ParseConfiguration> bestConfigurations = new ArrayList<ParseConfiguration>();
        int i = 0;

        if (finalHeap.isEmpty())
            finalHeap = backupHeap;

        while (!finalHeap.isEmpty()) {
            bestConfigurations.add(finalHeap.pollFirst());
            i++;
            if (i >= this.getBeamWidth())
                break;
        }
        if (LOG.isDebugEnabled()) {
            if (correctSolution != null) {
                LOG.debug("Gold transitions: " + correctSolution.getIncrementalOutcomes());
            }
            for (ParseConfiguration finalConfiguration : bestConfigurations) {
                LOG.debug(df.format(finalConfiguration.getScore()) + ": " + finalConfiguration.toString());
                LOG.debug("Pos tag sequence: " + finalConfiguration.getPosTagSequence());
                LOG.debug("Transitions: " + finalConfiguration.getTransitions());
                if (LOG.isTraceEnabled()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(" * PosTag sequence score ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getScore()));
                    sb.append(" = ");
                    for (PosTaggedToken posTaggedToken : finalConfiguration.getPosTagSequence()) {
                        sb.append(" * ");
                        sb.append(df.format(posTaggedToken.getDecision().getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getPosTagSequence().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * Token sequence score = ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getTokenSequence().getScore()));
                    LOG.trace(sb.toString());

                }
            }
        }
        return bestConfigurations;
    } finally {
        MONITOR.endTask("parseSentence");
    }
}

From source file:org.opennms.features.vaadin.pmatrix.calculator.PmatrixDpdCalculatorRepository.java

/**
 * Causes the dataPointMap to be persisted to a file
 * @param file file definition to persist the data set to
 * @return true if dataPointMap persisted correctly, false if not
 *///w  w w.ja va  2  s .c  o  m
public boolean persist() {
    File currentArchiveFile = null;
    File tmpCurrentArchiveFile = null;
    Resource tmpResource = null;

    if (!persistHistoricData) {
        LOG.debug("not persisting data as persistHistoricData=false");
        return false;
    }

    if (archiveFileName == null || archiveFileDirectoryLocation == null) {
        LOG.error("cannot save historical data to file as incorrect file location:"
                + " archiveFileDirectoryLocation=" + archiveFileDirectoryLocation + " archiveFileName="
                + archiveFileName);
        return false;
    }

    // set the date on which this file was persisted
    datePersisted = new Date();

    // used to get file name suffix
    SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormatString);

    // persist data to temporary file <archiveFileName.tmp>
    String tmpArchiveFileName = archiveFileName + ".tmp";
    String tmpArchiveFileLocation = archiveFileDirectoryLocation + File.separator + tmpArchiveFileName;
    LOG.debug("historical data will be written to temporary file :" + tmpArchiveFileLocation);

    try {
        tmpResource = resourceLoader.getResource(tmpArchiveFileLocation);
        tmpCurrentArchiveFile = new File(tmpResource.getURL().getFile());
    } catch (IOException e) {
        LOG.error("cannot save historical data to file at archiveFileLocation='" + tmpArchiveFileLocation
                + "' due to error:", e);
        return false;
    }

    LOG.debug(
            "persisting historical data to temporary file location:" + tmpCurrentArchiveFile.getAbsolutePath());

    // marshal the data
    PrintWriter writer = null;
    boolean marshalledCorrectly = false;
    try {
        // create  directory if doesn't exist
        File directory = new File(tmpCurrentArchiveFile.getParentFile().getAbsolutePath());
        directory.mkdirs();
        // create file if doesn't exist
        writer = new PrintWriter(tmpCurrentArchiveFile, "UTF-8");
        writer.close();

        // see http://stackoverflow.com/questions/1043109/why-cant-jaxb-find-my-jaxb-index-when-running-inside-apache-felix
        // need to provide bundles class loader
        ClassLoader cl = org.opennms.features.vaadin.pmatrix.model.DataPointDefinition.class.getClassLoader();
        JAXBContext jaxbContext = JAXBContext.newInstance(
                "org.opennms.features.vaadin.pmatrix.model:org.opennms.features.vaadin.pmatrix.calculator", cl);

        //JAXBContext jaxbContext = JAXBContext.newInstance("org.opennms.features.vaadin.pmatrix.model:org.opennms.features.vaadin.pmatrix.calculator");

        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        // TODO CHANGE output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // marshal this Data Repository
        jaxbMarshaller.marshal(this, tmpCurrentArchiveFile);

        marshalledCorrectly = true;
    } catch (JAXBException e) {
        LOG.error("problem marshalling file: ", e);
    } catch (Exception e) {
        LOG.error("problem marshalling file: ", e);
    } finally {
        if (writer != null)
            writer.close();
    }
    if (marshalledCorrectly == false)
        return false;

    // marshaling succeeded so rename tmp file
    String archiveFileLocation = archiveFileDirectoryLocation + File.separator + archiveFileName;
    LOG.info("historical data will be written to:" + archiveFileLocation);

    Resource resource = resourceLoader.getResource(archiveFileLocation);

    if (resource.exists()) {
        String oldArchiveFileName = archiveFileName + "." + dateFormatter.format(datePersisted);
        String oldArchiveFileLocation = archiveFileDirectoryLocation + File.separator + oldArchiveFileName;
        LOG.info("previous historical file at archiveFileLocation='" + archiveFileLocation
                + "' exists so being renamed to " + oldArchiveFileLocation);
        Resource oldresource = resourceLoader.getResource(oldArchiveFileLocation);
        try {
            currentArchiveFile = new File(resource.getURL().getFile());
            File oldArchiveFile = new File(oldresource.getURL().getFile());
            // rename current archive file to old archive file name
            if (!currentArchiveFile.renameTo(oldArchiveFile)) {
                throw new IOException("cannot rename current archive file:"
                        + currentArchiveFile.getAbsolutePath() + " to " + oldArchiveFile.getAbsolutePath());
            }
            // rename temporary archive file to current archive file name
            if (!tmpCurrentArchiveFile.renameTo(currentArchiveFile)) {
                throw new IOException("cannot rename temporary current archive file:"
                        + tmpCurrentArchiveFile.getAbsolutePath() + " to "
                        + currentArchiveFile.getAbsolutePath());
            }
        } catch (IOException e) {
            LOG.error("Problem archiving old persistance file", e);
        }
        // remove excess files
        try {
            Resource directoryResource = resourceLoader.getResource(archiveFileDirectoryLocation);
            File archiveFolder = new File(directoryResource.getURL().getFile());
            File[] listOfFiles = archiveFolder.listFiles();

            String filename;
            //this will sort earliest to latest date
            TreeMap<Date, File> sortedFiles = new TreeMap<Date, File>();

            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    filename = listOfFiles[i].getName();
                    if ((!filename.equals(archiveFileName)) && (!filename.equals(tmpArchiveFileName))
                            && (filename.startsWith(archiveFileName))) {
                        String beforeTimeString = archiveFileName + ".";
                        String timeSuffix = filename.substring(beforeTimeString.length());
                        if (!"".equals(timeSuffix)) {
                            Date fileCreatedate = null;
                            try {
                                fileCreatedate = dateFormatter.parse(timeSuffix);
                            } catch (ParseException e) {
                                LOG.debug("cant parse file name suffix to time for filename:" + filename, e);
                            }
                            if (fileCreatedate != null) {
                                sortedFiles.put(fileCreatedate, listOfFiles[i]);
                            }
                        }
                    }
                }
            }

            while (sortedFiles.size() > archiveFileMaxNumber) {
                File removeFile = sortedFiles.remove(sortedFiles.firstKey());
                LOG.debug("deleting archive file:'" + removeFile.getName()
                        + "' so that number of archive files <=" + archiveFileMaxNumber);
                removeFile.delete();
            }
            for (File archivedFile : sortedFiles.values()) {
                LOG.debug("not deleting archive file:'" + archivedFile.getName()
                        + "' so that number of archive files <=" + archiveFileMaxNumber);
            }

            return true;
        } catch (IOException e) {
            LOG.error("Problem removing old persistance files", e);
        }
    } else {
        // if resource doesn't exist just rename the new tmp file to the archive file name
        try {
            currentArchiveFile = new File(resource.getURL().getFile());
            // rename temporary archive file to current archive file name
            if (!tmpCurrentArchiveFile.renameTo(currentArchiveFile)) {
                throw new IOException("cannot rename temporary current archive file:"
                        + tmpCurrentArchiveFile.getAbsolutePath() + " to "
                        + currentArchiveFile.getAbsolutePath());
            }
            return true;
        } catch (IOException e) {
            LOG.error("cannot rename temporary current archive ", e);
        }
    }

    return false;

}

From source file:com.sec.ose.osi.ui.frm.main.manage.dialog.JDlgProjectCreate.java

@SuppressWarnings("unchecked")
private void createCloneProject(String rootLocation, String projectName) {
    UIResponseObserver observer = null;/*from  w  ww. jav  a  2 s  . c o m*/
    if (getJCheckBoxClonedFrom().isSelected() == true) { // clone
        UEProjectClone uProjectClone = null;
        String clonedFromProjectName = getJComboBoxClonedFrom().getSelectedItem().toString();
        uProjectClone = new UEProjectClone(projectName, clonedFromProjectName, rootLocation, jPanManageMain);
        observer = UserRequestHandler.getInstance().handle(UserRequestHandler.PROJECT_CLONE, uProjectClone,
                true, // progress
                false // result
        );

    } else { // create
        TreeMap<String, ProjectSplitInfo> mapOfAnalyzeTarget = null;
        UEProjectCreate ueProjectCreate = null;

        if (rootLocation.length() == 0) { // one creation, no source path

            ueProjectCreate = new UEProjectCreate(projectName, mapOfAnalyzeTarget);
            observer = UserRequestHandler.getInstance().handle(UserRequestHandler.PROJECT_CREATE,
                    ueProjectCreate, true, // progress
                    false // result
            );

        } else { // split or no split

            File rootLocationFile = new File(rootLocation);
            ProjectSplitUtil projectSplitUtil = new ProjectSplitUtil(projectName, rootLocation);
            UEProtexSplit ue = new UEProtexSplit(rootLocationFile, projectSplitUtil);
            observer = UserRequestHandler.getInstance().handle(UserRequestHandler.PROJECT_SPLIT, ue, true,
                    false);
            if (observer.getResult() == UIResponseObserver.RESULT_SUCCESS) {
                mapOfAnalyzeTarget = ProjectSplitUtil.getAnalyzeTargetMap();
            } else {
                return;
            }

            int size = mapOfAnalyzeTarget.size();
            if (size > 1) {
                JDlgProjectSplitInfo.getInstance().showDialog();
                boolean isWorking = JDlgProjectSplitInfo.getInstance().isWorking();

                if (isWorking == false) {
                    return;
                }
            }

            ueProjectCreate = new UEProjectCreate(projectName, mapOfAnalyzeTarget);
            observer = UserRequestHandler.getInstance().handle(UserRequestHandler.PROJECT_CREATE,
                    ueProjectCreate, true, // progress
                    false // result
            );
        }
    }

    if (observer.getResult() == UIResponseObserver.RESULT_SUCCESS) {

        ArrayList<OSIProjectInfo> osiProjectInfoList = (ArrayList<OSIProjectInfo>) observer.getReturnValue();
        for (OSIProjectInfo pf : osiProjectInfoList) {
            jPanManageMain.addNewProject(pf);
        }

        jTextFieldNewProjectName.setText("");
        jTextFieldNewProjectName.setToolTipText("");
        jCheckBoxClonedFrom.setSelected(false);
        setVisible(false);
    } else {
        jTextFieldNewProjectName.setSelectionStart(0);
        jTextFieldNewProjectName.setSelectionEnd(jTextFieldNewProjectName.getText().length());
        jTextFieldNewProjectName.requestFocusInWindow();
    }

}

From source file:org.opendatakit.database.data.ColumnDefinitionTest.java

@SuppressWarnings("unchecked")
@Test/*  ww  w. java 2 s . c o m*/
public void testBuildExtendedJson() {
    List<Column> columns = new ArrayList<Column>();

    // arbitrary type derived from integer
    columns.add(new Column("col0", "col0", "myothertype:integer", "[]"));
    // primitive types
    columns.add(new Column("col1", "col1", "boolean", "[]"));
    columns.add(new Column("col2", "col2", "integer", "[]"));
    columns.add(new Column("col3", "col3", "number", "[]"));
    columns.add(new Column("col4", "col4", "string", "[]"));
    // string with 500 varchars allocated to it
    columns.add(new Column("col5", "col5", "string(500)", "[]"));
    columns.add(new Column("col6", "col6", "configpath", "[]"));
    columns.add(new Column("col7", "col7", "rowpath", "[]"));
    // object type (geopoint)
    columns.add(new Column("col8", "col8", "geopoint",
            "[\"col8_accuracy\",\"col8_altitude\",\"col8_latitude\",\"col8_longitude\"]"));
    columns.add(new Column("col8_accuracy", "accuracy", "number", "[]"));
    columns.add(new Column("col8_altitude", "altitude", "number", "[]"));
    columns.add(new Column("col8_latitude", "latitude", "number", "[]"));
    columns.add(new Column("col8_longitude", "longitude", "number", "[]"));
    // arbitrary type derived from string
    columns.add(new Column("col9", "col9", "mytype", "[]"));

    // arrays
    columns.add(new Column("col12", "col12", "array", "[\"col12_items\"]"));
    columns.add(new Column("col12_items", "items", "integer", "[]"));
    // array with 500 varchars allocated to it
    columns.add(new Column("col14", "col14", "array(400)", "[\"col14_items\"]"));
    columns.add(new Column("col14_items", "items", "string", "[]"));

    columns.add(new Column("col1a", "col1a", "geolist:array(500)", "[\"col1a_items\"]"));
    columns.add(new Column("col1a_items", "items", "geopoint",
            "[\"col1a_items_accuracy\",\"col1a_items_altitude\",\"col1a_items_latitude\","
                    + "\"col1a_items_longitude\"]"));
    columns.add(new Column("col1a_items_accuracy", "accuracy", "number", "[]"));
    columns.add(new Column("col1a_items_altitude", "altitude", "number", "[]"));
    columns.add(new Column("col1a_items_latitude", "latitude", "number", "[]"));
    columns.add(new Column("col1a_items_longitude", "longitude", "number", "[]"));

    ArrayList<ColumnDefinition> colDefs = ColumnDefinition.buildColumnDefinitions("appName", "testTable",
            columns);

    String equivalentXLSXConverterDataTableModel = "{" + "\"col0\": {" + "\"type\": \"integer\","
            + "\"_defn\": [" + "{" + "\"_row_num\": 2," + "\"section_name\": \"model\"" + "}" + "],"
            + "\"elementType\": \"myothertype:integer\"," + "\"elementKey\": \"col0\","
            + "\"elementName\": \"col0\"," + "\"elementSet\": \"data\"," + "\"elementPath\": \"col0\"" + "},"
            + "\"col1\": {" + "\"type\": \"boolean\"," + "\"_defn\": [" + "{" + "\"_row_num\": 3,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"elementKey\": \"col1\","
            + "\"elementName\": \"col1\"," + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1\"" + "},"
            + "\"col2\": {" + "\"type\": \"integer\"," + "\"_defn\": [" + "{" + "\"_row_num\": 4,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"elementKey\": \"col2\","
            + "\"elementName\": \"col2\"," + "\"elementSet\": \"data\"," + "\"elementPath\": \"col2\"" + "},"
            + "\"col3\": {" + "\"type\": \"number\"," + "\"_defn\": [" + "{" + "\"_row_num\": 5,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"elementKey\": \"col3\","
            + "\"elementName\": \"col3\"," + "\"elementSet\": \"data\"," + "\"elementPath\": \"col3\"" + "},"
            + "\"col4\": {" + "\"type\": \"string\"," + "\"_defn\": [" + "{" + "\"_row_num\": 6,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"elementKey\": \"col4\","
            + "\"elementName\": \"col4\"," + "\"elementSet\": \"data\"," + "\"elementPath\": \"col4\"" + "},"
            + "\"col5\": {" + "\"type\": \"string\"," + "\"_defn\": [" + "{" + "\"_row_num\": 7,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"elementType\": \"string(500)\","
            + "\"elementKey\": \"col5\"," + "\"elementName\": \"col5\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col5\"" + "}," + "\"col6\": {" + "\"type\": \"string\"," + "\"_defn\": ["
            + "{" + "\"_row_num\": 8," + "\"section_name\": \"model\"" + "}" + "],"
            + "\"elementType\": \"configpath\"," + "\"elementKey\": \"col6\"," + "\"elementName\": \"col6\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col6\"" + "}," + "\"col7\": {"
            + "\"type\": \"string\"," + "\"_defn\": [" + "{" + "\"_row_num\": 9,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"elementType\": \"rowpath\","
            + "\"elementKey\": \"col7\"," + "\"elementName\": \"col7\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col7\"" + "}," + "\"col8\": {" + "\"type\": \"object\","
            + "\"elementType\": \"geopoint\"," + "\"properties\": {" + "\"latitude\": {"
            + "\"type\": \"number\"," + "\"elementKey\": \"col8_latitude\"," + "\"elementName\": \"latitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col8.latitude\"" + "}," + "\"longitude\": {"
            + "\"type\": \"number\"," + "\"elementKey\": \"col8_longitude\","
            + "\"elementName\": \"longitude\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col8.longitude\"" + "}," + "\"altitude\": {" + "\"type\": \"number\","
            + "\"elementKey\": \"col8_altitude\"," + "\"elementName\": \"altitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col8.altitude\"" + "}," + "\"accuracy\": {"
            + "\"type\": \"number\"," + "\"elementKey\": \"col8_accuracy\"," + "\"elementName\": \"accuracy\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col8.accuracy\"" + "}" + "}," + "\"_defn\": ["
            + "{" + "\"_row_num\": 10," + "\"section_name\": \"model\"" + "}" + "],"
            + "\"elementKey\": \"col8\"," + "\"elementName\": \"col8\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col8\"," + "\"listChildElementKeys\": [" + "\"col8_accuracy\","
            + "\"col8_altitude\"," + "\"col8_latitude\"," + "\"col8_longitude\"" + "],"
            + "\"notUnitOfRetention\": true" + "}," + "\"col9\": {" + "\"type\": \"string\"," + "\"_defn\": ["
            + "{" + "\"_row_num\": 11," + "\"section_name\": \"model\"" + "}" + "],"
            + "\"elementType\": \"mytype\"," + "\"elementKey\": \"col9\"," + "\"elementName\": \"col9\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col9\"" + "}," + "\"col12\": {"
            + "\"type\": \"array\"," + "\"_defn\": [" + "{" + "\"_row_num\": 12,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"items\": {" + "\"type\": \"integer\","
            + "\"elementKey\": \"col12_items\"," + "\"elementName\": \"items\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col12.items\"," + "\"notUnitOfRetention\": true" + "},"
            + "\"elementKey\": \"col12\"," + "\"elementName\": \"col12\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col12\"," + "\"listChildElementKeys\": [" + "\"col12_items\"" + "]" + "},"
            + "\"col14\": {" + "\"type\": \"array\"," + "\"_defn\": [" + "{" + "\"_row_num\": 13,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"elementType\": \"array(400)\"," + "\"items\": {"
            + "\"type\": \"string\"," + "\"elementKey\": \"col14_items\"," + "\"elementName\": \"items\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col14.items\"," + "\"notUnitOfRetention\": true"
            + "}," + "\"elementKey\": \"col14\"," + "\"elementName\": \"col14\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col14\"," + "\"listChildElementKeys\": [" + "\"col14_items\"" + "]" + "},"
            + "\"col1a\": {" + "\"type\": \"array\"," + "\"_defn\": [" + "{" + "\"_row_num\": 14,"
            + "\"section_name\": \"model\"" + "}" + "]," + "\"elementType\": \"geolist:array(500)\","
            + "\"items\": {" + "\"type\": \"object\"," + "\"elementType\": \"geopoint\"," + "\"properties\": {"
            + "\"latitude\": {" + "\"type\": \"number\"," + "\"elementName\": \"latitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items.latitude\","
            + "\"elementKey\": \"col1a_items_latitude\"," + "\"notUnitOfRetention\": true" + "},"
            + "\"longitude\": {" + "\"type\": \"number\"," + "\"elementName\": \"longitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items.longitude\","
            + "\"elementKey\": \"col1a_items_longitude\"," + "\"notUnitOfRetention\": true" + "},"
            + "\"altitude\": {" + "\"type\": \"number\"," + "\"elementName\": \"altitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items.altitude\","
            + "\"elementKey\": \"col1a_items_altitude\"," + "\"notUnitOfRetention\": true" + "},"
            + "\"accuracy\": {" + "\"type\": \"number\"," + "\"elementName\": \"accuracy\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items.accuracy\","
            + "\"elementKey\": \"col1a_items_accuracy\"," + "\"notUnitOfRetention\": true" + "}" + "},"
            + "\"elementKey\": \"col1a_items\"," + "\"elementName\": \"items\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col1a.items\"," + "\"listChildElementKeys\": [" + "\"col1a_items_accuracy\","
            + "\"col1a_items_altitude\"," + "\"col1a_items_latitude\"," + "\"col1a_items_longitude\"" + "],"
            + "\"notUnitOfRetention\": true" + "}," + "\"elementKey\": \"col1a\","
            + "\"elementName\": \"col1a\"," + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a\","
            + "\"listChildElementKeys\": [" + "\"col1a_items\"" + "]" + "}," + "\"col8_latitude\": {"
            + "\"type\": \"number\"," + "\"elementKey\": \"col8_latitude\"," + "\"elementName\": \"latitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col8.latitude\"" + "},"
            + "\"col8_longitude\": {" + "\"type\": \"number\"," + "\"elementKey\": \"col8_longitude\","
            + "\"elementName\": \"longitude\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col8.longitude\"" + "}," + "\"col8_altitude\": {" + "\"type\": \"number\","
            + "\"elementKey\": \"col8_altitude\"," + "\"elementName\": \"altitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col8.altitude\"" + "}," + "\"col8_accuracy\": {"
            + "\"type\": \"number\"," + "\"elementKey\": \"col8_accuracy\"," + "\"elementName\": \"accuracy\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col8.accuracy\"" + "}," + "\"col12_items\": {"
            + "\"type\": \"integer\"," + "\"elementKey\": \"col12_items\"," + "\"elementName\": \"items\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col12.items\"," + "\"notUnitOfRetention\": true"
            + "}," + "\"col14_items\": {" + "\"type\": \"string\"," + "\"elementKey\": \"col14_items\","
            + "\"elementName\": \"items\"," + "\"elementSet\": \"data\"," + "\"elementPath\": \"col14.items\","
            + "\"notUnitOfRetention\": true" + "}," + "\"col1a_items\": {" + "\"type\": \"object\","
            + "\"elementType\": \"geopoint\"," + "\"properties\": {" + "\"latitude\": {"
            + "\"type\": \"number\"," + "\"elementName\": \"latitude\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col1a.items.latitude\"," + "\"elementKey\": \"col1a_items_latitude\","
            + "\"notUnitOfRetention\": true" + "}," + "\"longitude\": {" + "\"type\": \"number\","
            + "\"elementName\": \"longitude\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col1a.items.longitude\"," + "\"elementKey\": \"col1a_items_longitude\","
            + "\"notUnitOfRetention\": true" + "}," + "\"altitude\": {" + "\"type\": \"number\","
            + "\"elementName\": \"altitude\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col1a.items.altitude\"," + "\"elementKey\": \"col1a_items_altitude\","
            + "\"notUnitOfRetention\": true" + "}," + "\"accuracy\": {" + "\"type\": \"number\","
            + "\"elementName\": \"accuracy\"," + "\"elementSet\": \"data\","
            + "\"elementPath\": \"col1a.items.accuracy\"," + "\"elementKey\": \"col1a_items_accuracy\","
            + "\"notUnitOfRetention\": true" + "}" + "}," + "\"elementKey\": \"col1a_items\","
            + "\"elementName\": \"items\"," + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items\","
            + "\"listChildElementKeys\": [" + "\"col1a_items_accuracy\"," + "\"col1a_items_altitude\","
            + "\"col1a_items_latitude\"," + "\"col1a_items_longitude\"" + "]," + "\"notUnitOfRetention\": true"
            + "}," + "\"col1a_items_latitude\": {" + "\"type\": \"number\"," + "\"elementName\": \"latitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items.latitude\","
            + "\"elementKey\": \"col1a_items_latitude\"," + "\"notUnitOfRetention\": true" + "},"
            + "\"col1a_items_longitude\": {" + "\"type\": \"number\"," + "\"elementName\": \"longitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items.longitude\","
            + "\"elementKey\": \"col1a_items_longitude\"," + "\"notUnitOfRetention\": true" + "},"
            + "\"col1a_items_altitude\": {" + "\"type\": \"number\"," + "\"elementName\": \"altitude\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items.altitude\","
            + "\"elementKey\": \"col1a_items_altitude\"," + "\"notUnitOfRetention\": true" + "},"
            + "\"col1a_items_accuracy\": {" + "\"type\": \"number\"," + "\"elementName\": \"accuracy\","
            + "\"elementSet\": \"data\"," + "\"elementPath\": \"col1a.items.accuracy\","
            + "\"elementKey\": \"col1a_items_accuracy\"," + "\"notUnitOfRetention\": true" + "}," + "\"_id\": {"
            + "\"type\": \"string\"," + "\"isNotNullable\": true," + "\"elementKey\": \"_id\","
            + "\"elementName\": \"_id\"," + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_id\""
            + "}," + "\"_row_etag\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_row_etag\"," + "\"elementName\": \"_row_etag\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_row_etag\"" + "},"
            + "\"_sync_state\": {" + "\"type\": \"string\"," + "\"isNotNullable\": true,"
            + "\"elementKey\": \"_sync_state\"," + "\"elementName\": \"_sync_state\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_sync_state\"" + "},"
            + "\"_conflict_type\": {" + "\"type\": \"integer\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_conflict_type\"," + "\"elementName\": \"_conflict_type\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_conflict_type\"" + "},"
            + "\"_default_access\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_default_access\"," + "\"elementName\": \"_default_access\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_default_access\"" + "},"
            + "\"_row_owner\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_row_owner\"," + "\"elementName\": \"_row_owner\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_row_owner\"" + "},"
            + "\"_group_read_only\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_group_read_only\"," + "\"elementName\": \"_group_read_only\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_group_read_only\"" + "},"
            + "\"_group_modify\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_group_modify\"," + "\"elementName\": \"_group_modify\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_group_modify\"" + "},"
            + "\"_group_privileged\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_group_privileged\"," + "\"elementName\": \"_group_privileged\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_group_privileged\"" + "},"
            + "\"_form_id\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_form_id\"," + "\"elementName\": \"_form_id\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_form_id\"" + "},"
            + "\"_locale\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_locale\"," + "\"elementName\": \"_locale\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_locale\"" + "},"
            + "\"_savepoint_type\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_savepoint_type\"," + "\"elementName\": \"_savepoint_type\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_savepoint_type\"" + "},"
            + "\"_savepoint_timestamp\": {" + "\"type\": \"string\"," + "\"isNotNullable\": true,"
            + "\"elementKey\": \"_savepoint_timestamp\"," + "\"elementName\": \"_savepoint_timestamp\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_savepoint_timestamp\"" + "},"
            + "\"_savepoint_creator\": {" + "\"type\": \"string\"," + "\"isNotNullable\": false,"
            + "\"elementKey\": \"_savepoint_creator\"," + "\"elementName\": \"_savepoint_creator\","
            + "\"elementSet\": \"instanceMetadata\"," + "\"elementPath\": \"_savepoint_creator\"" + "}" + "}";
    int metaCount = 0;
    TreeMap<String, Object> model = ColumnDefinition.getExtendedDataModel(colDefs);
    TypeReference<TreeMap<String, Object>> refType = new TypeReference<TreeMap<String, Object>>() {
    };
    TreeMap<String, Object> xlsxModel;
    try {
        xlsxModel = ODKFileUtils.mapper.readValue(equivalentXLSXConverterDataTableModel, refType);
    } catch (IOException e) {
        assertFalse("failed to parse XLSXConverter version", true);
        return;
    }

    for (String elementKey : model.keySet()) {
        TreeMap<String, Object> value = (TreeMap<String, Object>) model.get(elementKey);
        assert (xlsxModel.containsKey(elementKey));
        Map<String, Object> xlsxValue = (Map<String, Object>) xlsxModel.get(elementKey);
        List<String> ignoredKeys = new ArrayList<String>();
        for (String key : xlsxValue.keySet()) {
            if (key.startsWith("_")) {
                ignoredKeys.add(key);
            }
            if (key.equals("prompt_type_name")) {
                ignoredKeys.add(key);
            }
        }
        for (String key : ignoredKeys) {
            xlsxValue.remove(key);
        }
        assertEquals("Investigating: " + elementKey, value.size(), xlsxValue.size());
        recursiveMatch(elementKey, value, xlsxValue);

        ColumnDefinition def = null;
        try {
            def = ColumnDefinition.find(colDefs, elementKey);
        } catch (IllegalArgumentException e) {
            // ignore
        }
        if (def == null) {
            assertEquals(value.get("elementSet"), "instanceMetadata");
            assertEquals(value.get("elementKey"), elementKey);
            assertEquals(value.get("elementName"), elementKey);
            assertEquals(value.get("elementPath"), elementKey);
            assertEquals(value.containsKey("notUnitOfRetention"), false);

            if (elementKey.equals(TableConstants.ID)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), true);
            } else if (elementKey.equals(TableConstants.ROW_ETAG)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.SYNC_STATE)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), true);
            } else if (elementKey.equals(TableConstants.CONFLICT_TYPE)) {
                metaCount++;
                assertEquals(value.get("type"), "integer");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.DEFAULT_ACCESS)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.ROW_OWNER)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.GROUP_READ_ONLY)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.GROUP_MODIFY)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.GROUP_PRIVILEGED)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.SAVEPOINT_TIMESTAMP)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), true);
            } else if (elementKey.equals(TableConstants.SAVEPOINT_TYPE)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.SAVEPOINT_CREATOR)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.FORM_ID)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else if (elementKey.equals(TableConstants.LOCALE)) {
                metaCount++;
                assertEquals(value.get("type"), "string");
                assertEquals(value.get("isNotNullable"), false);
            } else {
                throw new IllegalStateException("Unexpected non-user column");
            }
        } else {
            assertEquals(value.get("elementPath"), def.getElementName());
            assertEquals(value.get("elementSet"), "data");
            assertEquals(value.get("elementName"), def.getElementName());
            assertEquals(value.get("elementKey"), def.getElementKey());
            if (!def.isUnitOfRetention()) {
                assertEquals(value.get("notUnitOfRetention"), true);
            }
            // TODO: there are a lot more paths to verify here...
            assertEquals(value.containsKey("isNotNullable"), false);
        }
    }
    assertEquals(metaCount, 14);
}

From source file:com.jtstand.swing.StatsPanel.java

public JFreeChart getChartTime() {
    TreeMap<String, List<TestStepInstance>> s = getGroupedSteps(getFilteringIterator());
    if (s == null || s.size() == 0) {
        return null;
    }//ww  w.j  a  va  2s.  co  m
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    for (Iterator<String> en = s.keySet().iterator(); en.hasNext();) {
        String groupName = en.next();
        List<TestStepInstance> stps = s.get(groupName);
        //            TimeSeries pop = new TimeSeries(groupName, Millisecond.class);
        TimeSeries pop = new TimeSeries(groupName);
        for (Iterator<TestStepInstance> it = stps.iterator(); it.hasNext();) {
            TestStepInstance step = it.next();
            Number num = getNumber(step);
            if (num != null) {
                switch (chartMode) {
                case STEP_TIME:
                    pop.addOrUpdate(RegularTimePeriod.createInstance(Millisecond.class,
                            new Date(step.getStartTime()), TimeZone.getDefault()), num);
                    break;
                case SEQUENCE_TIME:
                    //                            pop.addOrUpdate(RegularTimePeriod.createInstance(Millisecond.class, new Date(step.getTestSequenceInstance().getStartTime()), RegularTimePeriod.DEFAULT_TIME_ZONE), num);
                    pop.addOrUpdate(RegularTimePeriod.createInstance(Millisecond.class,
                            new Date(step.getTestSequenceInstance().getCreateTime()), TimeZone.getDefault()),
                            num);
                    break;
                }
            }
        }
        dataset.addSeries(pop);
    }
    JFreeChart chart = null;
    switch (chartMode) {
    case STEP_TIME:
        chart = ChartFactory.createTimeSeriesChart(null, "Step Started Time", getValueString(), dataset,
                isGrouping(), true, false);
        break;
    case SEQUENCE_TIME:
        chart = ChartFactory.createTimeSeriesChart(null, "Sequence Started Time", getValueString(), dataset,
                isGrouping(), true, false);
        break;
    }
    chart.setBackgroundPaint((Paint) UIManager.get("Panel.background"));

    XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(Color.white);
    XYLineAndShapeRenderer renderer5 = new XYLineAndShapeRenderer();
    renderer5.setBaseSeriesVisibleInLegend(false);
    plot.setRenderer(renderer5);
    plot.setDomainGridlinesVisible(true);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeCrosshairVisible(true);
    plot.setDomainCrosshairVisible(true);
    //        chart.setTitle(valueName);
    placeLimitMarkers(plot, true);

    //renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
    renderer5.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());

    /* coloring */
    if (isCategorization()) {
        //            TreeMap<String, Color> cmap = new TreeMap<String, Color>();
        int i = 0;
        for (Iterator<String> it = catstats.keySet().iterator(); it.hasNext(); i++) {
            String groupName = it.next();
            Color c = ChartCategories.getColor(i);
            for (int j = 0; j < dataset.getSeriesCount(); j++) {
                TimeSeries ts = dataset.getSeries(j);
                if (ts.getKey().equals(groupName)) {
                    renderer5.setSeriesPaint(j, c);
                }
            }
        }
    } else {
        renderer5.setSeriesPaint(0, ChartCategories.getColor(0));
    }

    //        chart.addProgressListener(new ChartProgressListener() {
    //
    //            public void chartProgress(final ChartProgressEvent progress) {
    //                SwingUtilities.invokeLater(
    //                        new Runnable() {
    //
    //                            @Override
    //                            public void run() {
    //
    //                                System.out.println("progress:" + progress + " " + progress.getType());
    //                                if (progress.getType() == ChartProgressEvent.DRAWING_FINISHED) {
    //                                    if (plot != null) {
    //                                        if (plot.isDomainCrosshairVisible() && plot.isDomainCrosshairLockedOnData()) {
    ////                            System.out.println("getDomainCrosshairValue:" + plot.getDomainCrosshairValue());
    //                                            double xx = plot.getDomainCrosshairValue();
    //                                            if (xx != 0.0) {
    //                                                long x = (long) xx;
    //                                                System.out.println(new Date(x));
    //                                                for (TestStepInstance step : testStepInstances.getSteps()) {
    //                                                    if (step.getStartTime() != null && step.getStartTime().equals(x)) {
    //                                                        testStepInstances.selectStep(step);
    //                                                    }
    //                                                }
    //                                                System.out.println(new Date(x));
    //                                            }
    //                                        }
    ////                        if (plot.isRangeCrosshairVisible()) {
    ////                            System.out.println("getRangeCrosshairValue:" + plot.getRangeCrosshairValue());
    ////                        }
    //                                    }
    //                                }
    //                            }
    //                        });
    //            }
    //        });

    //        chart.addChangeListener(new ChartChangeListener() {
    //
    //            public void chartChanged(ChartChangeEvent event) {
    //                System.out.println("event:" + event);
    //                if (event != null) {
    ////                    JFreeChart chart = event.getChart();
    ////                    System.out.println("chart:" + chart);
    ////                    if (chart != null) {
    ////                        System.out.println("title:" + event.getChart().getTitle());
    ////                    }
    //                    System.out.println("type:" + event.getType());
    //                    if (plot != null) {
    //                        if (plot.isDomainCrosshairVisible()) {
    //                            System.out.println("getDomainCrosshairValue:" + plot.getDomainCrosshairValue());
    //                            long x = (long) plot.getDomainCrosshairValue();
    //                            for (TestStepInstance step : testStepInstances.getSteps()) {
    //                                if (step.getStartTime() != null && step.getStartTime().equals(x)) {
    //                                    testStepInstances.selectStep(step);
    //                                }
    //                            }
    //                            System.out.println(new Date(x));
    //                        }
    //                        if (plot.isRangeCrosshairVisible()) {
    //                            System.out.println("getRangeCrosshairValue:" + plot.getRangeCrosshairValue());
    //                        }
    //                    }
    //                }
    //            }
    //        });
    chart.setTextAntiAlias(false);
    return chart;
}