Example usage for org.apache.commons.collections15.bag HashBag HashBag

List of usage examples for org.apache.commons.collections15.bag HashBag HashBag

Introduction

In this page you can find the example usage for org.apache.commons.collections15.bag HashBag HashBag.

Prototype

public HashBag() 

Source Link

Document

Constructs an empty HashBag.

Usage

From source file:com.diversityarrays.kdxplore.KDXploreFrame.java

private void initialiseKdxApps() throws IOException {

    String[] classNames = KdxploreConfig.getInstance().getMainPluginClassNames();
    if (classNames != null && classNames.length > 0) {
        List<String> classNamesToLoad = new ArrayList<>();
        Collections.addAll(classNamesToLoad, classNames);
        if (!classNamesToLoad.contains(OFFLINE_DATA_APP_SERVICE_CLASS_NAME)) {
            classNamesToLoad.add(0, OFFLINE_DATA_APP_SERVICE_CLASS_NAME);
            classNames = classNamesToLoad.toArray(new String[classNamesToLoad.size()]);
        }//from w ww. java 2  s  .c  om
    }

    Map<KdxApp, Component> componentByApp = collectKdxApps(classNames);

    appByComponent.clear();
    for (KdxApp app : componentByApp.keySet()) {
        Component comp = componentByApp.get(app);
        if (comp != null) {
            appByComponent.put(comp, app);
        }
    }

    allKdxApps.clear();
    allKdxApps.addAll(componentByApp.keySet());

    // Initialise the apps in initialisation order.
    allKdxApps.sort(Comparator.comparing(KdxApp::getInitialisationOrder));

    // And while we're initialising them we collect
    // those that can perform a databaseBackup (i.e. have a BackupProvider).
    backupProviders.clear();
    List<KdxApp> wantedAppsWithUi = new ArrayList<>();

    for (KdxApp app : allKdxApps) {
        BackupProvider bp = app.getBackupProvider();
        if (bp != null) {
            backupProviders.add(bp);
        }

        /**
         * See {@link com.diversityarrays.kdxplore.prefs.KdxplorePreferences#SHOW_ALL_APPS}
         */
        if (appIsWanted(app)) {
            try {
                app.initialiseAppBeforeUpdateCheck(appInitContext);
            } catch (Exception e) {
                String msg = Msg.MSG_KDXAPP_INIT_PROBLEM(app.getAppName());
                Shared.Log.w(TAG, msg, e);
                messagesPanel.println(msg);
                messagesPanel.println(e.getMessage());
            }
        }

        if (appIsWanted(app) && null != componentByApp.get(app)) {
            wantedAppsWithUi.add(app);
        }
    }

    // - - - - - - - - - - - - - - - - - - - - -
    // Display the apps in display order.
    wantedAppsWithUi.sort(Comparator.comparing(KdxApp::getDisplayOrder));
    backupProviders.sort(Comparator.comparing(BackupProvider::getDisplayOrder));

    switch (wantedAppsWithUi.size()) {
    case 0:
        JLabel label = new JLabel(Msg.MSG_NO_KDXPLORE_APPS_AVAILABLE());
        label.setHorizontalAlignment(JLabel.CENTER);
        cardPanel.add(label, CARD_KDXAPPS);
        break;
    case 1:
        KdxApp kdxApp = wantedAppsWithUi.get(0);
        Component uiComponent = componentByApp.get(kdxApp);

        Component appComponent = makeComponentForTab(kdxApp, uiComponent);
        cardPanel.add(appComponent, CARD_KDXAPPS);

        getRootPane().setDefaultButton(kdxApp.getDefaultButton());

        String msg = Msg.MSG_SHOWING_KDXAPP(kdxApp.getAppName());
        messagesPanel.println(msg);
        System.err.println(msg + " uiClass=" //$NON-NLS-1$
                + uiComponent.getClass().getName());
        break;
    default:
        kdxAppTabs = new JTabbedPane(JTabbedPane.LEFT);
        cardPanel.add(kdxAppTabs, CARD_KDXAPPS);
        Bag<String> tabsSeen = new HashBag<>();
        for (KdxApp app : wantedAppsWithUi) {
            Component ui = componentByApp.get(app);

            String tabName = app.getAppName();
            DevelopmentState devState = app.getDevelopmentState();
            switch (devState) {
            case ALPHA:
                tabName = tabName + " (\u03b1)"; // TODO move to UnicodeChars
                break;
            case BETA:
                tabName = tabName + " (\u03b2)"; // TODO move to UnicodeChars
                break;
            case PRODUCTION:
                break;
            default:
                tabName = tabName + " " + devState.name();
                break;
            }
            tabsSeen.add(tabName);
            int count = tabsSeen.getCount(tabName);
            if (count > 1) {
                tabName = tabName + "_" + count; //$NON-NLS-1$
            }

            Component tabComponent = makeComponentForTab(app, ui);
            kdxAppTabs.addTab(tabName, tabComponent);
            if (macapp == null) {
                int index = kdxAppTabs.indexOfTab(tabName);
                if (index >= 0) {
                    JLabel tabLabel = new JLabel(tabName);
                    tabLabel.setBorder(new EmptyBorder(2, 2, 2, 2));
                    tabLabel.setUI(new VerticalLabelUI(VerticalLabelUI.UPWARDS));
                    kdxAppTabs.setTabComponentAt(index, tabLabel);
                }
            }
            messagesPanel.println(Msg.MSG_SHOWING_KDXAPP(tabName));
        }

        kdxAppTabs.addChangeListener(kdxAppTabsChangeListener);
        kdxAppTabs.setSelectedIndex(0);
        break;
    }
}

From source file:com.diversityarrays.kdxplore.trials.TrialOverviewPanel.java

private void doAddTraitInstances(Trial selectedTrial, Map<Trait, List<Integer>> instanceNumbersByTrait)
        throws IOException {

    List<Trait> unusedTraits = offlineData.getKdxploreDatabase().getAllTraits();

    Map<Integer, Trait> traitById = unusedTraits.stream()
            .collect(Collectors.toMap(Trait::getTraitId, Function.identity()));

    List<TraitInstance> instances = this.getAllTraitInstances(selectedTrial);

    Map<Trait, Integer> instancesByTrait = new HashMap<>();
    for (TraitInstance ti : instances) {
        Trait trait = traitById.get(ti.getTraitId());

        Integer inst = instancesByTrait.get(trait);
        if (inst == null) {
            inst = ti.getInstanceNumber();
        } else {//from w  w w  .  j  a  v  a  2 s. c  o m
            inst++;
        }
        instancesByTrait.put(trait, inst);
    }

    for (Trait trait : instanceNumbersByTrait.keySet()) {
        List<Integer> newInstances = instanceNumbersByTrait.get(trait);

        if (!Check.isEmpty(newInstances)) {
            Integer currentCount = instancesByTrait.get(trait);

            if (currentCount != null) {
                List<Integer> toUse = new ArrayList<>();
                newInstances.stream().forEach(i -> toUse.add(i += currentCount));
                instanceNumbersByTrait.put(trait, toUse);
            }
        }
    }

    if (instanceNumbersByTrait.isEmpty()) {
        return;
    }

    int trialId = selectedTrial.getTrialId();

    int maxSso = 0;
    Bag<Integer> ssoUseCount = new HashBag<>();

    Map<Trait, Integer> traitSsoMap = trialTraitsTableModel.getTraitsSsoForTrial(selectedTrial);
    if (!Check.isEmpty(traitSsoMap)) {
        for (Integer sso : traitSsoMap.values()) {
            if (sso != null) {
                maxSso = Math.max(maxSso, sso);
                ssoUseCount.add(sso);
            }
        }
    }

    TraitNameStyle traitNameStyle = selectedTrial.getTraitNameStyle();

    final int firstInstanceNumber = traitNameStyle.getFirstInstanceNumber();

    int maxSsoIncrement = 1;
    if (ssoUseCount.uniqueSet().size() == 1 && maxSso == 0 && ssoUseCount.getCount(maxSso) > 1) {
        // Special hack for Trials that haven't yet been "sorted".
        // (i.e. all the SSOs are the same value
        maxSsoIncrement = 0;
    }

    List<Trait> ignored = new ArrayList<>();
    Map<TraitLevel, List<TraitInstance>> instancesByLevel = new HashMap<>();
    boolean multipleTraitInstances = false;

    Set<TraitInstance> traitInstances = new LinkedHashSet<>();
    for (Trait trait : instanceNumbersByTrait.keySet()) {

        switch (trait.getTraitLevel()) {
        case PLOT:
        case SPECIMEN:
            break;
        default:
            ignored.add(trait);
            continue;
        }
        maxSso += maxSsoIncrement;

        List<Integer> instancesNumbersWanted = instanceNumbersByTrait.get(trait);
        if (instancesNumbersWanted.size() > 1) {
            multipleTraitInstances = true;
        }

        for (Integer instanceNumber : instancesNumbersWanted) {
            TraitInstance ti = new TraitInstance();
            //                if (firstInstanceNumber <= 0) {
            //                    ti.setInstanceNumber(instanceNumber - 1);
            //                }
            //                else {
            ti.setInstanceNumber(instanceNumber);
            //                }
            ti.setScoringSortOrder(maxSso);
            ti.setTraitId(trait.getTraitId());
            ti.setTrialId(trialId);
            ti.setUsedForScoring(true);

            traitInstances.add(ti);

            List<TraitInstance> list = instancesByLevel.get(trait.getTraitLevel());
            if (list == null) {
                list = new ArrayList<>();
                instancesByLevel.put(trait.getTraitLevel(), list);
            }
            list.add(ti);
        }
    }

    KdxploreDatabase kdxdb = offlineData.getKdxploreDatabase();
    KDSmartDatabase kdsdb = kdxdb.getKDXploreKSmartDatabase();

    Map<Integer, DeviceIdentifier> devidMap = kdxdb.getDeviceIdentifierMap();
    Optional<DeviceIdentifier> opt_devid = devidMap.values().stream()
            .filter(devid -> DeviceType.EDITED.equals(devid.getDeviceType())).findFirst();

    List<KdxSample> samples;
    if (opt_devid.isPresent()) {
        DeviceIdentifier curated = opt_devid.get();
        List<SampleGroup> sampleGroups = kdxdb.getSampleGroups(trialId,
                KdxploreDatabase.WithSamplesOption.WITH_SAMPLES);
        int curatedSampleGroupId = curated.getDeviceIdentifierId();

        Optional<SampleGroup> opt_sg = sampleGroups.stream()
                .filter(sg -> curatedSampleGroupId == sg.getDeviceIdentifierId()).findFirst();
        if (opt_sg.isPresent()) {
            samples = new ArrayList<>();

            List<TraitInstance> plotInstances = instancesByLevel.get(TraitLevel.PLOT);
            List<TraitInstance> subPlotInstances = instancesByLevel.get(TraitLevel.SPECIMEN);

            Consumer<KdxSample> sampleConsumer = new Consumer<KdxSample>() {
                @Override
                public void accept(KdxSample sample) {
                    samples.add(sample);
                }
            };
            TrialItemVisitor<Plot> plotVisitor = new TrialItemVisitor<Plot>() {
                @Override
                public void setExpectedItemCount(int count) {
                }

                @Override
                public boolean consumeItem(Plot plot) {
                    DatabaseUtil.createSamples(trialId, plot, curatedSampleGroupId, null, // previousSamplesByKey
                            plotInstances, subPlotInstances, sampleConsumer);
                    return true;
                }
            };

            kdsdb.visitPlotsForTrial(trialId, SampleGroupChoice.NO_TAGS_SAMPLE_GROUP,
                    KDSmartDatabase.WithPlotAttributesOption.WITHOUT_PLOT_ATTRIBUTES, plotVisitor);
        } else {
            samples = null;
        }
    } else {
        samples = null;
    }

    BatchHandler<Void> batchHandler = new BatchHandler<Void>() {
        @Override
        public Void call() throws Exception {
            kdsdb.saveTraitInstances(traitInstances);
            if (!Check.isEmpty(samples)) {
                kdsdb.saveMultipleSamples(samples, false);
            }
            return null;
        }

        @Override
        public boolean checkSuccess(Void t) {
            return true;
        }
    };

    boolean saved = true;
    Either<Exception, Void> either = kdsdb.doBatch(batchHandler);
    if (either.isLeft()) {
        saved = false;
        MsgBox.error(TrialOverviewPanel.this, either.left(), "Database Error");
    }
    refreshTrialTableModel(offlineData.getKdxploreDatabase());

    trialTraitsTableModel.setSelectedTrial(selectedTrial);

    if (saved) {
        String prefix = multipleTraitInstances ? "Trait Instances Added:\n" : "Traits Added:\n";

        Function<Trait, String> nameFactory = new Function<Trait, String>() {
            @Override
            public String apply(Trait trait) {
                List<Integer> instanceNumbers = instanceNumbersByTrait.get(trait);
                if (instanceNumbers == null) {
                    return null;
                } else {
                    return instanceNumbers.stream().map(inum -> inum.toString())
                            .collect(Collectors.joining(",", trait.getTraitName() + ": ", ""));
                }
            }
        };

        String selection = instanceNumbersByTrait.keySet().stream().map(nameFactory).filter(n -> n != null)
                .collect(Collectors.joining("\n", prefix, "\n------")); //$NON-NLS-1$//$NON-NLS-2$

        messagePrinter.println(selection);

        if (samples != null) {
            messagePrinter.println("Samples Added: " + samples.size());
        }
    }
}

From source file:com.diversityarrays.kdxplore.curate.TrialDataEditor.java

private void checkForInvalidTraits() {
    SampleGroup db_smdata = curationData.getDatabaseSampleGroup();
    if (db_smdata == null) {
        return;//from   w w w.  j a v a2  s  .  c om
    }
    Bag<Integer> traitIdCountsFromDatabase = new HashBag<>();
    for (KdxSample sm : db_smdata.getSamples()) {
        traitIdCountsFromDatabase.add(sm.getTraitId());
    }

    Set<Integer> errorTraitIds = new HashSet<>(traitIdCountsFromDatabase.uniqueSet());

    Set<Integer> traitIds = CurationData.getTraitIds(curationData);
    errorTraitIds.removeAll(traitIds);

    if (!errorTraitIds.isEmpty()) {
        for (Integer id : errorTraitIds) {
            if (id == null) {
                continue;
            }
            Trait trait = this.traitProvider.apply(id);
            if (trait == null) {
                trait = new Trait();
                trait.setTraitId(id);
                trait.setTraitName("Unknown Trait#" + id);
            }
            errorTraitsById.put(id, trait);
        }
    }
}

From source file:net.sqs2.util.PathUtil.java

public static Set<File> getSharedSuperDirectorySet(Iterable<File> files) {

    Bag<File> bag = new HashBag<File>();
    for (File file : files) {
        File superdir = file;/*from   w ww.  ja v a2s. c  om*/
        do {
            // System.err.print("SRC:"+file);
            bag.add(superdir);
            // System.err.print("\t");
            // System.err.print(superdir);
        } while ((superdir = superdir.getParentFile()) != null);
    }

    Set<File> ret = new HashSet<File>();
    for (File file : bag.uniqueSet()) {
        if (1 < bag.getCount(file)) {
            ret.add(file);
        }
    }
    return ret;
}

From source file:trendanalisis.main.tools.weka.CoreWekaTFIDF.java

/**
 * Converts the instance w/o normalization.
 * /*from  w ww  .j  a v a  2s .  co  m*/
 * @oaram instance the instance to convert
 * @param v
 * @return the conerted instance
 */
private int convertInstancewoDocNorm(Instance instance, ArrayList<Instance> v, int indexInstance) {

    // Convert the instance into a sorted set of indexes
    TreeMap<Integer, Double> contained = new TreeMap<Integer, Double>();

    // Copy all non-converted attributes from input to output
    int firstCopy = 0;
    for (int i = 0; i < getInputFormat().numAttributes(); i++) {
        if (!m_SelectedRange.isInRange(i)) {
            if (getInputFormat().attribute(i).type() != Attribute.STRING
                    && getInputFormat().attribute(i).type() != Attribute.RELATIONAL) {
                // Add simple nominal and numeric attributes directly
                if (instance.value(i) != 0.0) {
                    contained.put(new Integer(firstCopy), new Double(instance.value(i)));
                }
            } else {
                if (instance.isMissing(i)) {
                    contained.put(new Integer(firstCopy), new Double(Utils.missingValue()));
                } else if (getInputFormat().attribute(i).type() == Attribute.STRING) {

                    // If this is a string attribute, we have to first add
                    // this value to the range of possible values, then add
                    // its new internal index.
                    if (outputFormatPeek().attribute(firstCopy).numValues() == 0) {
                        // Note that the first string value in a
                        // SparseInstance doesn't get printed.
                        outputFormatPeek().attribute(firstCopy)
                                .addStringValue("Hack to defeat SparseInstance bug");
                    }
                    int newIndex = outputFormatPeek().attribute(firstCopy)
                            .addStringValue(instance.stringValue(i));
                    contained.put(new Integer(firstCopy), new Double(newIndex));
                } else {
                    // relational
                    if (outputFormatPeek().attribute(firstCopy).numValues() == 0) {
                        Instances relationalHeader = outputFormatPeek().attribute(firstCopy).relation();

                        // hack to defeat sparse instances bug
                        outputFormatPeek().attribute(firstCopy).addRelation(relationalHeader);
                    }
                    int newIndex = outputFormatPeek().attribute(firstCopy)
                            .addRelation(instance.relationalValue(i));
                    contained.put(new Integer(firstCopy), new Double(newIndex));
                }
            }
            firstCopy++;
        }
    }

    Map<Integer, Integer> posMap = new HashMap<>();
    Bag<String> contents = new HashBag();

    for (int j = 0; j < instance.numAttributes(); j++) {
        // if ((getInputFormat().attribute(j).type() == Attribute.STRING)
        if (m_SelectedRange.isInRange(j) && (instance.isMissing(j) == false)) {

            m_Tokenizer.tokenize(instance.stringValue(j));
            int posWord = 1;

            while (m_Tokenizer.hasMoreElements()) {
                String word = m_Tokenizer.nextElement();
                if (this.m_lowerCaseTokens == true) {
                    word = word.toLowerCase();
                }
                word = m_Stemmer.stem(word);
                //   System.out.println(posWord +":"+ word);

                Integer index = m_Dictionary.get(word);

                Words.put(index, word);
                contents.add(word);
                if (index != null) {
                    if (m_OutputCounts) { // Separate if here rather than two lines down
                        // to avoid hashtable lookup
                        Double count = contained.get(index);
                        if (count != null) {
                            contained.put(index, new Double(count.doubleValue() + 1.0));
                        } else {
                            contained.put(index, new Double(1));
                        }
                    } else {
                        contained.put(index, new Double(1));
                    }

                    if (!posMap.containsKey(index)) {
                        posMap.put(index, posWord);
                        //  posWord++;
                    }
                }

                posWord++;
            }
        }
    }

    // Doing TFTransform
    //   ArrayList<Integer> posList= new ArrayList<>(posMap.values());
    //   System.out.println(posList);

    /*
     * Fitur Subtittle
     * instance attribut ke 0 adalah judul
     *                      1 adalah lokasi
     *                      3 adalah tanggal    
     */
    String subtittle = "";
    ArrayList<String> subtittles = null;
    if (ins_fitur_subtitle != null) {
        subtittle = ins_fitur_subtitle.get(indexInstance).stringValue(0);
        subtittles = new ArrayList<>(Arrays.asList(subtittle.split(" ")));

        // subtittles.retainAll(contents);

    } else {

        fitur_subtitle = false;
    }

    if (m_TFTransform == true) {
        Iterator<Integer> it = contained.keySet().iterator();
        int set = 0;
        for (; it.hasNext();) {
            Integer index = it.next();
            if (index.intValue() >= firstCopy) {
                double val = contained.get(index).doubleValue();

                if (isBinary_transform()) {
                    val = 1;
                }

                //  val = (1 + Math.log(val));
                contained.put(index, new Double(val));
                set++;
            }
        }
    }

    // Doing IDFTransform
    double sum = 0;
    if (m_IDFTransform == true) {
        Iterator<Integer> it = contained.keySet().iterator();
        int i = 0;

        for (; it.hasNext();) {
            Integer index = it.next();
            if (index.intValue() >= firstCopy) {
                double val = contained.get(index).doubleValue();
                double valIG = (FeatureSelection.InformationGain(m_DocsCounts[index.intValue()],
                        m_NumInstances));// Math.abs(valIG);

                global_tf[index.intValue()] += val;
                df_prob[index.intValue()] = FeatureSelection.IdfProbability((double) m_NumInstances,
                        m_DocsCounts[index.intValue()]);
                IG[index.intValue()] = valIG;

                if (isBinary_transform()) {
                    val = 1;
                    val = (Math.log(val));
                }

                val = val * (1 + Math.log((double) m_NumInstances / (double) m_DocsCounts[index.intValue()]));

                if (isStat_pos_word()) {
                    val = val + (1 / Math.sqrt(posMap.get(index)));
                }

                contained.put(index, new Double(val));

                // global_tf[index.intValue()]= (1+ Math.log(m_NumInstances / (double) m_DocsCounts[index.intValue()]));
                i++;
            }

        }

    }

    //  System.out.println("-------------------");

    // Convert the set to structures needed to create a sparse instance.
    double[] values = new double[contained.size()];
    int[] indices = new int[contained.size()];
    Iterator<Integer> it = contained.keySet().iterator();
    for (int i = 0; it.hasNext(); i++) {
        Integer index = it.next();
        Double value = contained.get(index);
        values[i] = value.doubleValue();
        indices[i] = index.intValue();

        //df_prob[index.intValue()] = df_prob[index.intValue()]/m_DocsCounts[index.intValue()];
    }

    Instance inst = new SparseInstance(instance.weight(), values, indices, outputFormatPeek().numAttributes());
    inst.setDataset(outputFormatPeek());

    v.add(inst);

    return firstCopy;
}