Example usage for com.google.common.collect BiMap get

List of usage examples for com.google.common.collect BiMap get

Introduction

In this page you can find the example usage for com.google.common.collect BiMap get.

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:com.google.cloud.genomics.dataflow.functions.PCoAnalysis.java

private List<GraphResult> getPcaData(double[][] data, BiMap<Integer, String> dataNames) {
    int rows = data.length;
    int cols = data.length;

    // Center the similarity matrix.
    double matrixSum = 0;
    double[] rowSums = new double[rows];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            matrixSum += data[i][j];/*from ww  w. j  a  v a  2 s  .  c  om*/
            rowSums[i] += data[i][j];
        }
    }
    double matrixMean = matrixSum / rows / cols;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            double rowMean = rowSums[i] / rows;
            double colMean = rowSums[j] / rows;
            data[i][j] = data[i][j] - rowMean - colMean + matrixMean;
        }
    }

    // Determine the eigenvectors, and scale them so that their
    // sum of squares equals their associated eigenvalue.
    Matrix matrix = new Matrix(data);
    EigenvalueDecomposition eig = matrix.eig();
    Matrix eigenvectors = eig.getV();
    double[] realEigenvalues = eig.getRealEigenvalues();

    for (int j = 0; j < eigenvectors.getColumnDimension(); j++) {
        double sumSquares = 0;
        for (int i = 0; i < eigenvectors.getRowDimension(); i++) {
            sumSquares += eigenvectors.get(i, j) * eigenvectors.get(i, j);
        }
        for (int i = 0; i < eigenvectors.getRowDimension(); i++) {
            eigenvectors.set(i, j, eigenvectors.get(i, j) * Math.sqrt(realEigenvalues[j] / sumSquares));
        }
    }

    // Find the indices of the top two eigenvalues.
    int maxIndex = -1;
    int secondIndex = -1;
    double maxEigenvalue = 0;
    double secondEigenvalue = 0;

    for (int i = 0; i < realEigenvalues.length; i++) {
        double eigenvector = realEigenvalues[i];
        if (eigenvector > maxEigenvalue) {
            secondEigenvalue = maxEigenvalue;
            secondIndex = maxIndex;
            maxEigenvalue = eigenvector;
            maxIndex = i;
        } else if (eigenvector > secondEigenvalue) {
            secondEigenvalue = eigenvector;
            secondIndex = i;
        }
    }

    // Return projected data
    List<GraphResult> results = Lists.newArrayList();
    for (int i = 0; i < rows; i++) {
        results.add(new GraphResult(dataNames.get(i), eigenvectors.get(i, maxIndex),
                eigenvectors.get(i, secondIndex)));
    }

    return results;
}

From source file:org.apache.ctakes.ytex.uima.mapper.DocumentMapperServiceImpl.java

private void addAnnoLinks(JCas jcas, BiMap<Annotation, Integer> mapAnnoToId, List<AnnoLink> listAnnoLinks,
        AnnoMappingInfo mi) {/*from   w ww. ja v a2s .  c  o  m*/
    Type t = jcas.getTypeSystem().getType(mi.getAnnoClassName());
    if (t != null) {
        ColumnMappingInfo cip = mi.getMapField().get("parent_anno_base_id");
        ColumnMappingInfo cic = mi.getMapField().get("child_anno_base_id");
        // get the parent and child features
        Feature fp = t.getFeatureByBaseName(cip.getAnnoFieldName());
        Feature fc = t.getFeatureByBaseName(cic.getAnnoFieldName());
        // get all annotations
        FSIterator<FeatureStructure> iter = jcas.getFSIndexRepository().getAllIndexedFS(t);
        while (iter.hasNext()) {
            FeatureStructure fs = iter.next();
            // get parent and child feature values
            FeatureStructure fsp = fs.getFeatureValue(fp);
            FeatureStructure fsc = fs.getFeatureValue(fc);
            if (fsp != null && fsc != null) {
                // extract the parent annotation from the parent feature
                // value
                Object parentAnno = extractFeature(cip.getJxpath(), fsp);
                if (parentAnno instanceof Annotation) {
                    Integer parentId = mapAnnoToId.get((Annotation) parentAnno);
                    if (parentId != null) {
                        // parent is persisted, look for child(ren)
                        if (fsc instanceof FSList || fsc instanceof FSArray) {
                            // this is a one-to-many relationship
                            // iterate over children
                            List<FeatureStructure> children = extractList(fsc);
                            for (FeatureStructure child : children) {
                                addLink(mapAnnoToId, listAnnoLinks, t.getShortName(), cic.getJxpath(), parentId,
                                        child);
                            }
                        } else {
                            // this is a one-to-one relationship
                            addLink(mapAnnoToId, listAnnoLinks, t.getShortName(), cic.getJxpath(), parentId,
                                    fsc);
                        }
                    }
                }
            }
        }
    }
}

From source file:cpw.mods.fml.common.Loader.java

/**
 * Sort the mods into a sorted list, using dependency information from the
 * containers. The sorting is performed using a {@link TopologicalSort}
 * based on the pre- and post- dependency information provided by the mods.
 *///from www .  ja  va 2s. co  m
private void sortModList() {
    FMLLog.finer("Verifying mod requirements are satisfied");
    try {
        BiMap<String, ArtifactVersion> modVersions = HashBiMap.create();
        for (ModContainer mod : getActiveModList()) {
            modVersions.put(mod.getModId(), mod.getProcessedVersion());
        }

        ArrayListMultimap<String, String> reqList = ArrayListMultimap.create();
        for (ModContainer mod : getActiveModList()) {
            if (!mod.acceptableMinecraftVersionRange().containsVersion(minecraft.getProcessedVersion())) {
                FMLLog.severe(
                        "The mod %s does not wish to run in Minecraft version %s. You will have to remove it to play.",
                        mod.getModId(), getMCVersionString());
                throw new WrongMinecraftVersionException(mod);
            }
            Map<String, ArtifactVersion> names = Maps.uniqueIndex(mod.getRequirements(),
                    new ArtifactVersionNameFunction());
            Set<ArtifactVersion> versionMissingMods = Sets.newHashSet();

            Set<String> missingMods = Sets.difference(names.keySet(), modVersions.keySet());
            if (!missingMods.isEmpty()) {
                FMLLog.severe("The mod %s (%s) requires mods %s to be available", mod.getModId(), mod.getName(),
                        missingMods);
                for (String modid : missingMods) {
                    versionMissingMods.add(names.get(modid));
                }
                throw new MissingModsException(versionMissingMods);
            }
            reqList.putAll(mod.getModId(), names.keySet());
            ImmutableList<ArtifactVersion> allDeps = ImmutableList.<ArtifactVersion>builder()
                    .addAll(mod.getDependants()).addAll(mod.getDependencies()).build();
            for (ArtifactVersion v : allDeps) {
                if (modVersions.containsKey(v.getLabel())) {
                    if (!v.containsVersion(modVersions.get(v.getLabel()))) {
                        versionMissingMods.add(v);
                    }
                }
            }
            if (!versionMissingMods.isEmpty()) {
                FMLLog.severe("The mod %s (%s) requires mod versions %s to be available", mod.getModId(),
                        mod.getName(), versionMissingMods);
                throw new MissingModsException(versionMissingMods);
            }
        }

        FMLLog.finer("All mod requirements are satisfied");

        reverseDependencies = Multimaps.invertFrom(reqList, ArrayListMultimap.<String, String>create());
        ModSorter sorter = new ModSorter(getActiveModList(), namedMods);

        try {
            FMLLog.finer("Sorting mods into an ordered list");
            List<ModContainer> sortedMods = sorter.sort();
            // Reset active list to the sorted list
            modController.getActiveModList().clear();
            modController.getActiveModList().addAll(sortedMods);
            // And inject the sorted list into the overall list
            mods.removeAll(sortedMods);
            sortedMods.addAll(mods);
            mods = sortedMods;
            FMLLog.finer("Mod sorting completed successfully");
        } catch (ModSortingException sortException) {
            FMLLog.severe(
                    "A dependency cycle was detected in the input mod set so an ordering cannot be determined");
            SortingExceptionData<ModContainer> exceptionData = sortException.getExceptionData();
            FMLLog.severe("The first mod in the cycle is %s", exceptionData.getFirstBadNode());
            FMLLog.severe("The mod cycle involves");
            for (ModContainer mc : exceptionData.getVisitedNodes()) {
                FMLLog.severe("%s : before: %s, after: %s", mc.toString(), mc.getDependants(),
                        mc.getDependencies());
            }
            FMLLog.log(Level.ERROR, sortException, "The full error");
            throw sortException;
        }
    } finally {
        FMLLog.fine("Mod sorting data");
        int unprintedMods = mods.size();
        for (ModContainer mod : getActiveModList()) {
            if (!mod.isImmutable()) {
                FMLLog.fine("\t%s(%s:%s): %s (%s)", mod.getModId(), mod.getName(), mod.getVersion(),
                        mod.getSource().getName(), mod.getSortingRules());
                unprintedMods--;
            }
        }
        if (unprintedMods == mods.size()) {
            FMLLog.fine("No user mods found to sort");
        }
    }

}

From source file:net.minecraftforge.fml.common.Loader.java

/**
 * Sort the mods into a sorted list, using dependency information from the
 * containers. The sorting is performed using a {@link TopologicalSort}
 * based on the pre- and post- dependency information provided by the mods.
 *//*w  w w  .j  a va 2 s  .c  om*/
private void sortModList() {
    FMLLog.finer("Verifying mod requirements are satisfied");
    try {
        BiMap<String, ArtifactVersion> modVersions = HashBiMap.create();
        for (ModContainer mod : Iterables.concat(getActiveModList(), ModAPIManager.INSTANCE.getAPIList())) {
            modVersions.put(mod.getModId(), mod.getProcessedVersion());
        }

        ArrayListMultimap<String, String> reqList = ArrayListMultimap.create();
        for (ModContainer mod : getActiveModList()) {
            if (!mod.acceptableMinecraftVersionRange().containsVersion(minecraft.getProcessedVersion())) {
                FMLLog.severe(
                        "The mod %s does not wish to run in Minecraft version %s. You will have to remove it to play.",
                        mod.getModId(), getMCVersionString());
                throw new WrongMinecraftVersionException(mod);
            }
            Map<String, ArtifactVersion> names = Maps.uniqueIndex(mod.getRequirements(),
                    new ArtifactVersionNameFunction());
            Set<ArtifactVersion> versionMissingMods = Sets.newHashSet();

            Set<String> missingMods = Sets.difference(names.keySet(), modVersions.keySet());
            if (!missingMods.isEmpty()) {
                FMLLog.severe("The mod %s (%s) requires mods %s to be available", mod.getModId(), mod.getName(),
                        missingMods);
                for (String modid : missingMods) {
                    versionMissingMods.add(names.get(modid));
                }
                throw new MissingModsException(versionMissingMods, mod.getModId(), mod.getName());
            }
            reqList.putAll(mod.getModId(), names.keySet());
            ImmutableList<ArtifactVersion> allDeps = ImmutableList.<ArtifactVersion>builder()
                    .addAll(mod.getDependants()).addAll(mod.getDependencies()).build();
            for (ArtifactVersion v : allDeps) {
                if (modVersions.containsKey(v.getLabel())) {
                    if (!v.containsVersion(modVersions.get(v.getLabel()))) {
                        versionMissingMods.add(v);
                    }
                }
            }
            if (!versionMissingMods.isEmpty()) {
                FMLLog.severe("The mod %s (%s) requires mod versions %s to be available", mod.getModId(),
                        mod.getName(), versionMissingMods);
                throw new MissingModsException(versionMissingMods, mod.getModId(), mod.getName());
            }
        }

        FMLLog.finer("All mod requirements are satisfied");

        reverseDependencies = Multimaps.invertFrom(reqList, ArrayListMultimap.<String, String>create());
        ModSorter sorter = new ModSorter(getActiveModList(), namedMods);

        try {
            FMLLog.finer("Sorting mods into an ordered list");
            List<ModContainer> sortedMods = sorter.sort();
            // Reset active list to the sorted list
            modController.getActiveModList().clear();
            modController.getActiveModList().addAll(sortedMods);
            // And inject the sorted list into the overall list
            mods.removeAll(sortedMods);
            sortedMods.addAll(mods);
            mods = sortedMods;
            FMLLog.finer("Mod sorting completed successfully");
        } catch (ModSortingException sortException) {
            FMLLog.severe(
                    "A dependency cycle was detected in the input mod set so an ordering cannot be determined");
            SortingExceptionData<ModContainer> exceptionData = sortException.getExceptionData();
            FMLLog.severe("The first mod in the cycle is %s", exceptionData.getFirstBadNode());
            FMLLog.severe("The mod cycle involves");
            for (ModContainer mc : exceptionData.getVisitedNodes()) {
                FMLLog.severe("%s : before: %s, after: %s", mc.toString(), mc.getDependants(),
                        mc.getDependencies());
            }
            FMLLog.log(Level.ERROR, sortException, "The full error");
            throw sortException;
        }
    } finally {
        FMLLog.fine("Mod sorting data");
        int unprintedMods = mods.size();
        for (ModContainer mod : getActiveModList()) {
            if (!mod.isImmutable()) {
                FMLLog.fine("\t%s(%s:%s): %s (%s)", mod.getModId(), mod.getName(), mod.getVersion(),
                        mod.getSource().getName(), mod.getSortingRules());
                unprintedMods--;
            }
        }
        if (unprintedMods == mods.size()) {
            FMLLog.fine("No user mods found to sort");
        }
    }

}

From source file:org.wrml.runtime.schema.DefaultSchemaLoader.java

@Override
public final String getNativeTypeName(final URI typeUri) {

    final BiMap<URI, String> uriToNativeTypeNameBiMapView = _NativeTypeNameToUriBiMap.inverse();
    if (!uriToNativeTypeNameBiMapView.containsKey(typeUri)) {

        final UniqueName uniqueName = getTypeUniqueName(typeUri);
        if (uniqueName == null) {
            throw new SchemaLoaderException("The type's uniqueName could not be determined from: " + typeUri,
                    null, this);
        }/*from ww  w  .  j a  v a2s  . c o m*/

        String localName = uniqueName.getLocalName();
        if (localName != null) {
            int indexOfLastDot = localName.lastIndexOf(".");

            if (indexOfLastDot > 0) {
                localName = localName.substring(0, indexOfLastDot);
            } else if (indexOfLastDot == 0) {
                localName = localName.substring(1);
            }
        }

        String namespace = uniqueName.getNamespace();
        if (namespace != null) {
            namespace = StringUtils.replaceChars(namespace, ".", "_");
        }

        String internalTypeName;
        if (namespace != null && localName != null) {
            String suffix = localName.trim();
            if (!suffix.isEmpty()) {
                suffix = UniqueName.NAME_SEPARATOR + suffix;
            }

            internalTypeName = namespace + suffix;
        } else if (namespace == null && localName == null) {
            internalTypeName = "unnamed";
        } else if (namespace == null && localName != null) {
            internalTypeName = localName;
        } else {
            internalTypeName = namespace;
        }

        internalTypeName = StringUtils.replaceChars(internalTypeName, ".", "_");
        final String schemaInterfaceName = SchemaGenerator.internalTypeNameToExternalTypeName(internalTypeName);

        _NativeTypeNameToUriBiMap.put(schemaInterfaceName, typeUri);
    }

    return uriToNativeTypeNameBiMapView.get(typeUri);

}

From source file:org.apache.beam.runners.flink.FlinkBatchPortablePipelineTranslator.java

private static <InputT> void translateExecutableStage(PTransformNode transform, RunnerApi.Pipeline pipeline,
        BatchTranslationContext context) {
    // TODO: Fail on splittable DoFns.
    // TODO: Special-case single outputs to avoid multiplexing PCollections.

    RunnerApi.Components components = pipeline.getComponents();
    Map<String, String> outputs = transform.getTransform().getOutputsMap();
    // Mapping from PCollection id to coder tag id.
    BiMap<String, Integer> outputMap = createOutputMap(outputs.values());
    // Collect all output Coders and create a UnionCoder for our tagged outputs.
    List<Coder<?>> unionCoders = Lists.newArrayList();
    // Enforce tuple tag sorting by union tag index.
    Map<String, Coder<WindowedValue<?>>> outputCoders = Maps.newHashMap();
    for (String collectionId : new TreeMap<>(outputMap.inverse()).values()) {
        PCollectionNode collectionNode = PipelineNode.pCollection(collectionId,
                components.getPcollectionsOrThrow(collectionId));
        Coder<WindowedValue<?>> coder;
        try {//from   w w w  . ja va2s.c o  m
            coder = (Coder) WireCoders.instantiateRunnerWireCoder(collectionNode, components);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        outputCoders.put(collectionId, coder);
        unionCoders.add(coder);
    }
    UnionCoder unionCoder = UnionCoder.of(unionCoders);
    TypeInformation<RawUnionValue> typeInformation = new CoderTypeInformation<>(unionCoder);

    RunnerApi.ExecutableStagePayload stagePayload;
    try {
        stagePayload = RunnerApi.ExecutableStagePayload
                .parseFrom(transform.getTransform().getSpec().getPayload());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    String inputPCollectionId = stagePayload.getInput();
    Coder<WindowedValue<InputT>> windowedInputCoder = instantiateCoder(inputPCollectionId, components);

    DataSet<WindowedValue<InputT>> inputDataSet = context.getDataSetOrThrow(inputPCollectionId);

    final FlinkExecutableStageFunction<InputT> function = new FlinkExecutableStageFunction<>(stagePayload,
            context.getJobInfo(), outputMap, FlinkExecutableStageContext.factory(context.getPipelineOptions()),
            getWindowingStrategy(inputPCollectionId, components).getWindowFn().windowCoder());

    final String operatorName = generateNameFromStagePayload(stagePayload);

    final SingleInputUdfOperator taggedDataset;
    if (stagePayload.getUserStatesCount() > 0 || stagePayload.getTimersCount() > 0) {

        Coder valueCoder = ((WindowedValue.FullWindowedValueCoder) windowedInputCoder).getValueCoder();
        // Stateful stages are only allowed of KV input to be able to group on the key
        if (!(valueCoder instanceof KvCoder)) {
            throw new IllegalStateException(String.format(Locale.ENGLISH,
                    "The element coder for stateful DoFn '%s' must be KvCoder but is: %s", inputPCollectionId,
                    valueCoder.getClass().getSimpleName()));
        }
        Coder keyCoder = ((KvCoder) valueCoder).getKeyCoder();

        Grouping<WindowedValue<InputT>> groupedInput = inputDataSet.groupBy(new KvKeySelector<>(keyCoder));
        taggedDataset = new GroupReduceOperator<>(groupedInput, typeInformation, function, operatorName);
    } else {
        taggedDataset = new MapPartitionOperator<>(inputDataSet, typeInformation, function, operatorName);
    }

    for (SideInputId sideInputId : stagePayload.getSideInputsList()) {
        String collectionId = components.getTransformsOrThrow(sideInputId.getTransformId())
                .getInputsOrThrow(sideInputId.getLocalName());
        // Register under the global PCollection name. Only ExecutableStageFunction needs to know the
        // mapping from local name to global name and how to translate the broadcast data to a state
        // API view.
        taggedDataset.withBroadcastSet(context.getDataSetOrThrow(collectionId), collectionId);
    }

    for (String collectionId : outputs.values()) {
        pruneOutput(taggedDataset, context, outputMap.get(collectionId), outputCoders.get(collectionId),
                transform.getTransform().getUniqueName(), collectionId);
    }
    if (outputs.isEmpty()) {
        // NOTE: After pipeline translation, we traverse the set of unconsumed PCollections and add a
        // no-op sink to each to make sure they are materialized by Flink. However, some SDK-executed
        // stages have no runner-visible output after fusion. We handle this case by adding a sink
        // here.
        taggedDataset.output(new DiscardingOutputFormat<>()).name("DiscardingOutput");
    }
}

From source file:com.cloudera.director.aws.ec2.EC2Provider.java

/**
 * Returns a map from virtual instance IDs to instance state for the specified batch of virtual
 * instance IDs.// ww w  .j a  va  2  s.co  m
 *
 * @param virtualInstanceIds batch of virtual instance IDs
 * @return the map from instance IDs to instance state for the specified batch of virtual
 * instance IDs
 */
private Map<String, InstanceState> getBatchInstanceState(Collection<String> virtualInstanceIds) {
    Map<String, InstanceState> instanceStateByVirtualInstanceId = Maps
            .newHashMapWithExpectedSize(virtualInstanceIds.size());

    BiMap<String, String> virtualInstanceIdsByEC2InstanceId = getEC2InstanceIdsByVirtualInstanceId(
            virtualInstanceIds).inverse();

    int page = 0;
    LOG.info(">> Fetching page {}", page);

    if (virtualInstanceIdsByEC2InstanceId.isEmpty()) {
        // No EC2 instances are found, which means these id's are already terminated and gone.
        // In practice, this is possible when no instances were provisioned to begin with.
        // For example, when a deployment fails due to tagging error.
        return instanceStateByVirtualInstanceId;
    }

    DescribeInstanceStatusResult result = client.describeInstanceStatus(new DescribeInstanceStatusRequest()
            // Note that sending in an empty set will result in fetching _all_ instance Ids.
            // It requires you to send one or more EC2 Ids
            .withInstanceIds(virtualInstanceIdsByEC2InstanceId.keySet()).withIncludeAllInstances(true));
    LOG.info("<< Result: {}", result);

    while (!result.getInstanceStatuses().isEmpty()) {
        for (InstanceStatus status : result.getInstanceStatuses()) {

            InstanceStateName currentState = InstanceStateName.fromValue(status.getInstanceState().getName());
            String ec2InstanceId = status.getInstanceId();
            String virtualInstanceId = virtualInstanceIdsByEC2InstanceId.get(ec2InstanceId);
            InstanceState instanceState = EC2InstanceState.fromInstanceStateName(currentState);
            instanceStateByVirtualInstanceId.put(virtualInstanceId, instanceState);
        }

        String nextToken = result.getNextToken();
        if (nextToken != null) {
            page++;
            LOG.info(">> Fetching page {} using token {}", page, nextToken);
            result = client
                    .describeInstanceStatus(new DescribeInstanceStatusRequest().withNextToken(nextToken));
            LOG.info("<< Result: {}", result);
        } else {
            break;
        }
    }

    return instanceStateByVirtualInstanceId;
}

From source file:org.apache.ctakes.ytex.uima.mapper.DocumentMapperServiceImpl.java

/**
 * bind the variables to the prepared statement
 * /*from w w  w . ja va  2s.  c om*/
 * @param type
 * @param mapInfo
 * @param ps
 * @param annoId
 * @param anno
 * @throws SQLException
 */
private void saveAnnoBindVariables(final Type type, final AnnoMappingInfo mapInfo, PreparedStatement ps,
        int annoId, FeatureStructure anno, final BiMap<Annotation, Integer> mapAnnoToId) throws SQLException {
    // set anno_base_id
    int argIdx = 1;
    ps.setInt(argIdx++, annoId);
    if (mapInfo.getCoveredTextColumn() != null) {
        String trunc = null;
        if (anno instanceof Annotation) {
            trunc = truncateString(((Annotation) anno).getCoveredText(),
                    mapInfo.getCoveredTextColumn().getSize());
        }
        ps.setString(argIdx++, trunc);
    }
    if (!Strings.isNullOrEmpty(mapInfo.getUimaTypeIdColumnName())) {
        ps.setInt(argIdx++, mapInfo.getUimaTypeId());
    }
    // iterate over fields
    for (Map.Entry<String, ColumnMappingInfo> fieldEntry : mapInfo.getMapField().entrySet()) {
        ColumnMappingInfo fieldMapInfo = fieldEntry.getValue();
        String fieldName = fieldMapInfo.getAnnoFieldName();
        Feature feat = type.getFeatureByBaseName(fieldName);
        if (fieldMapInfo.getConverter() != null) {
            try {
                String prop = anno.getFeatureValueAsString(feat);
                ps.setObject(argIdx, fieldMapInfo.getConverter().convert(fieldMapInfo.getTargetType(), prop));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else if (!feat.getRange().isPrimitive()) {
            // feature is a structure/annotation
            FeatureStructure fs = anno.getFeatureValue(feat);
            if (fs == null) {
                // feature is null - set the column to null
                ps.setNull(argIdx, fieldMapInfo.getSqlType());
            } else {
                if (fieldMapInfo.getJxpath() != null) {
                    // jxpath to pull out feature attribute
                    Object o = this.extractFeature(fieldMapInfo.getJxpath(), fs);
                    if (o == null) {
                        // extracted value null - set column to null
                        ps.setNull(argIdx, fieldMapInfo.getSqlType());
                    } else if (o instanceof String) {
                        // string - truncate as needed
                        String trunc = truncateString((String) o, fieldMapInfo.getSize());
                        ps.setString(argIdx, trunc);
                    } else {
                        // set value
                        ps.setObject(argIdx, o);
                    }
                } else {
                    // reference to another annotation - get the other
                    // anno's id
                    Integer refAnnoId = null;
                    if (fs instanceof Annotation) {
                        refAnnoId = mapAnnoToId.get(fs);
                    }
                    if (refAnnoId != null) {
                        ps.setInt(argIdx, refAnnoId);
                    } else {
                        ps.setNull(argIdx, Types.INTEGER);
                    }
                }
            }
        } else {
            if ("uima.cas.Integer".equals(feat.getRange().getName())) {
                ps.setInt(argIdx, anno.getIntValue(feat));
            } else if ("uima.cas.Short".equals(feat.getRange().getName())) {
                ps.setShort(argIdx, anno.getShortValue(feat));
            } else if ("uima.cas.Long".equals(feat.getRange().getName())) {
                ps.setLong(argIdx, anno.getLongValue(feat));
            } else if ("uima.cas.Float".equals(feat.getRange().getName())) {
                ps.setFloat(argIdx, anno.getFloatValue(feat));
            } else if ("uima.cas.Double".equals(feat.getRange().getName())) {
                ps.setDouble(argIdx, anno.getDoubleValue(feat));
            } else if ("uima.cas.Byte".equals(feat.getRange().getName())) {
                ps.setByte(argIdx, anno.getByteValue(feat));
            } else if ("uima.cas.Boolean".equals(feat.getRange().getName())) {
                ps.setBoolean(argIdx, anno.getBooleanValue(feat));
            } else if ("uima.cas.String".equals(feat.getRange().getName())) {
                String trunc = truncateString(anno.getStringValue(feat), fieldMapInfo.getSize());
                ps.setString(argIdx, trunc);
            }
        }
        argIdx++;
    }
}

From source file:com.oodrive.nuage.vvr.persistence.repository.NrsRepository.java

private final void initNrsTree() throws NrsException {
    // Initialize Nrs
    nrsFileJanitor.setClientStartpoint(getMsgClientStartpoint(), nrsMsgEnhancer);
    nrsFileJanitor.init();/*from w  w  w.jav a  2  s.co  m*/

    // Reset previous objects
    rootItem = null;
    nrsToSnapshot = null;
    snapshots = null;
    parents = null;
    devices = null;

    // Load the NrsFile headers
    final AtomicReference<NrsSnapshot> rootItemNew = new AtomicReference<NrsSnapshot>();
    final BiMap<UuidT<NrsFile>, UUID> nrsToSnapshotNew = HashBiMap.create();
    final ConcurrentHashMap<UUID, NrsSnapshot> snapshotsNew = new ConcurrentHashMap<>();
    final ConcurrentHashMap<UuidT<NrsFile>, UuidT<NrsFile>> parentsNew = new ConcurrentHashMap<>();
    final ConcurrentHashMap<UUID, NrsDevice> devicesNew = new ConcurrentHashMap<>();
    try {

        // Load NrsFile<>NrsSnapshot associations
        Files.walkFileTree(snapshotDir.toPath(), new SimpleFileVisitor<Path>() {
            @Override
            public final FileVisitResult visitFile(final Path headerPath, final BasicFileAttributes attrs)
                    throws IOException {
                final File snapshotFile = headerPath.toFile();
                assert snapshotFile.isFile();

                // Should be a property file, containing a snapshot or its user preferences (if any)
                final Properties properties = new Properties();
                try (FileInputStream fis = new FileInputStream(snapshotFile)) {
                    properties.load(fis);
                }
                final String uuidStr = properties.getProperty(NrsVvrItem.UUID_KEY);
                final String nrsFileUuidStr = properties.getProperty(NrsSnapshot.NRSFILE_UUID_KEY);

                assert (uuidStr == null && nrsFileUuidStr == null)
                        || (uuidStr != null && nrsFileUuidStr != null);

                if (uuidStr == null && nrsFileUuidStr == null) {
                    // Not a snapshot: ignored
                    return FileVisitResult.CONTINUE;
                }

                // New snapshot
                final boolean deleted = Boolean.valueOf(properties.getProperty(NrsSnapshot.DELETED_KEY))
                        .booleanValue();
                if (deleted) {
                    // Ignored
                    return FileVisitResult.CONTINUE;
                }
                final UUID uuid = UUID.fromString(uuidStr);
                final UuidT<NrsFile> nrsFileUuid = SimpleIdentifierProvider.fromString(nrsFileUuidStr);
                nrsToSnapshotNew.put(nrsFileUuid, uuid);

                return FileVisitResult.CONTINUE;
            }
        });

        // Visit images
        nrsFileJanitor.visitImages(new SimpleFileVisitor<Path>() {

            @Override
            public final FileVisitResult visitFile(final Path headerPath, final BasicFileAttributes attrs)
                    throws IOException {
                final File headerFile = headerPath.toFile();
                assert headerFile.isFile();

                final NrsFileHeader<NrsFile> header = nrsFileJanitor.loadNrsFileHeader(headerPath);
                final UuidT<NrsFile> nrsFileId = header.getFileId();
                final UUID snapshotId = nrsToSnapshotNew.get(nrsFileId);

                // Stores hierarchy
                parentsNew.put(nrsFileId, header.getParentId());

                // Is root?
                if (header.isRoot()) {
                    assert header.getParentId().equals(nrsFileId);
                    assert snapshotId != null;
                    if (rootItemNew.get() == null) {
                        final NrsSnapshot rootTmp = loadSnapshot(header, snapshotId, headerPath);
                        if (rootTmp == null) {
                            throw new NrsException("Persistence of root not found '" + snapshotId + "'");
                        }
                        assert rootTmp.isRoot();

                        // Check the UUID of the node is correct
                        if (!getNodeId().equals(rootTmp.getNodeId())) {
                            throw new NrsException("Persistence of wrong node found: '" + rootTmp.getNodeId()
                                    + "' instead of '" + getNodeId() + "'");
                        }
                        rootItemNew.set(rootTmp);
                        snapshotsNew.put(snapshotId, rootTmp);
                    } else {
                        throw new NrsException("Duplicate root '" + rootItemNew.get().getUuid() + "' and '"
                                + snapshotId + "'");
                    }
                } else if (nrsFileJanitor.isSealed(headerPath)) {
                    // Update file: set is read-only if was not done previously
                    if (headerFile.canWrite()) {
                        headerFile.setReadOnly();
                    }

                    if (snapshotId == null) {
                        // Should be an isolated item
                        LOGGER.debug("No snapshot found for '" + nrsFileId + "'");
                    } else {
                        final NrsSnapshot snapshot = loadSnapshot(header, snapshotId, headerPath);
                        assert !snapshot.isRoot();
                        if (snapshot == null) {
                            // Should not happen
                            LOGGER.warn("Persistence of snapshot not found '" + snapshotId + "'");
                        } else if (snapshot.isDeleted()) {
                            LOGGER.debug("Snapshot deleted '" + snapshotId + "'");
                        } else {
                            snapshotsNew.put(snapshotId, snapshot);
                        }
                    }
                } else {
                    // TODO store item (current snapshot) ID in device persistence
                    final NrsDevice device = loadDevice(header, headerPath);
                    if (device == null) {
                        throw new NrsException(
                                "Persistence of device not found '" + header.getDeviceId() + "'");
                    }
                    devicesNew.put(device.getDeviceId(), device);
                }

                return FileVisitResult.CONTINUE;
            }
        });
    } catch (final NrsException e) {
        throw e;
    } catch (final IOException e) {
        throw new NrsException(e);
    }

    // Sanity check
    rootItem = rootItemNew.get();
    if (rootItem == null) {
        throw new NrsException("root not found");
    }

    // New values loaded
    nrsToSnapshot = nrsToSnapshotNew;
    snapshots = snapshotsNew;
    parents = parentsNew;
    devices = devicesNew;
}

From source file:org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.viewers.LamiScatterViewer.java

/**
 * Constructor//from w  w  w . j  av a 2s  . co m
 *
 * @param parent
 *            parent
 * @param resultTable
 *            Result table populating this chart
 * @param graphModel
 *            Model of this chart
 */
public LamiScatterViewer(Composite parent, LamiResultTable resultTable, LamiChartModel graphModel) {
    super(parent, resultTable, graphModel);
    if (getChartModel().getChartType() != ChartType.XY_SCATTER) {
        throw new IllegalStateException(
                "Chart type not a Scatter Chart " + getChartModel().getChartType().toString()); //$NON-NLS-1$
    }

    /* Inspect X series */
    fIndexMapping = new HashMap<>();

    fHoveringCrossDataPoint = new Point(-1, -1);

    List<LamiTableEntryAspect> xAxisAspects = getXAxisAspects();
    if (xAxisAspects.stream().distinct().count() == 1) {
        LamiTableEntryAspect singleXAspect = xAxisAspects.get(0);
        xAxisAspects.clear();
        xAxisAspects.add(singleXAspect);
    }

    BiMap<@Nullable String, Integer> xMap = checkNotNull(HashBiMap.create());
    boolean xIsLog = graphModel.xAxisIsLog();

    boolean areXAspectsContinuous = areAspectsContinuous(xAxisAspects);
    boolean areXAspectsTimeStamp = areAspectsTimeStamp(xAxisAspects);

    /* Check all aspect are the same type */
    for (LamiTableEntryAspect aspect : xAxisAspects) {
        if (aspect.isContinuous() != areXAspectsContinuous) {
            throw new IllegalStateException("Some X aspects are continuous and some are not"); //$NON-NLS-1$
        }
        if (aspect.isTimeStamp() != areXAspectsTimeStamp) {
            throw new IllegalStateException("Some X aspects are time based and some are not"); //$NON-NLS-1$
        }
    }

    /*
     * When xAxisAspects are discrete create a map for all values of all
     * series
     */
    if (!areXAspectsContinuous) {
        generateLabelMap(xAxisAspects, checkNotNull(xMap));
    } else {
        /*
         * Always clamp the range to min and max
         *
         * TODO: in the future this could be based on the result of the
         * delta between max and min multiplied by a ratio like it is done in
         * LibreOffice Calc
         */
        fXExternalRange = getRange(xAxisAspects, false);
    }

    /*
     * Create Y series
     */
    List<LamiTableEntryAspect> yAxisAspects = getYAxisAspects();
    BiMap<@Nullable String, Integer> yMap = checkNotNull(HashBiMap.create());
    boolean yIsLog = graphModel.yAxisIsLog();

    boolean areYAspectsContinuous = areAspectsContinuous(yAxisAspects);
    boolean areYAspectsTimeStamp = areAspectsTimeStamp(yAxisAspects);

    /* Check all aspect are the same type */
    for (LamiTableEntryAspect aspect : yAxisAspects) {
        if (aspect.isContinuous() != areYAspectsContinuous) {
            throw new IllegalStateException("Some Y aspects are continuous and some are not"); //$NON-NLS-1$
        }
        if (aspect.isTimeStamp() != areYAspectsTimeStamp) {
            throw new IllegalStateException("Some Y aspects are time based and some are not"); //$NON-NLS-1$
        }
    }

    /*
     * When yAspects are discrete create a map for all values of all series
     */
    if (!areYAspectsContinuous) {
        generateLabelMap(yAxisAspects, yMap);
    } else {
        /*
         * Only clamp the range to the minimum value if it is a time stamp since
         * plotting from 1970 would make little sense.
         */
        fYExternalRange = getRange(yAxisAspects, areYAspectsTimeStamp);
    }

    /* Plot the series */
    int index = 0;
    for (LamiTableEntryAspect yAspect : getYAxisAspects()) {
        String name;
        LamiTableEntryAspect xAspect;
        if (xAxisAspects.size() == 1) {
            /* Always map to the same x series */
            xAspect = xAxisAspects.get(0);
            name = yAspect.getLabel();
        } else {
            xAspect = xAxisAspects.get(index);
            name = (yAspect.getName() + ' ' + Messages.LamiScatterViewer_by + ' ' + xAspect.getName());
        }

        List<@Nullable Double> xDoubleSeries;
        List<@Nullable Double> yDoubleSeries;

        if (xAspect.isContinuous()) {
            xDoubleSeries = getResultTable().getEntries().stream().map(entry -> {
                Number number = xAspect.resolveNumber(entry);
                if (number != null && fXExternalRange != null) {
                    return getInternalDoubleValue(number, fXInternalRange, fXExternalRange);
                }
                return null;
            }).collect(Collectors.toList());
        } else {
            xDoubleSeries = getResultTable().getEntries().stream().map(entry -> {
                String string = xAspect.resolveString(entry);
                Integer value = xMap.get(string);
                if (value != null) {
                    return Double.valueOf(value.doubleValue());
                }
                return null;
            }).collect(Collectors.toList());
        }

        if (yAspect.isContinuous()) {
            yDoubleSeries = getResultTable().getEntries().stream().map(entry -> {
                Number number = yAspect.resolveNumber(entry);
                if (number != null && fYExternalRange != null) {
                    return getInternalDoubleValue(number, fYInternalRange, fYExternalRange);
                }
                return null;
            }).collect(Collectors.toList());
        } else {
            yDoubleSeries = getResultTable().getEntries().stream().map(entry -> {
                String string = yAspect.resolveString(entry);
                Integer value = yMap.get(string);
                if (value != null) {
                    return Double.valueOf(value.doubleValue());
                }
                return null;
            }).collect(Collectors.toList());
        }

        List<@Nullable Double> validXDoubleSeries = new ArrayList<>();
        List<@Nullable Double> validYDoubleSeries = new ArrayList<>();
        List<Integer> indexSeriesCorrespondance = new ArrayList<>();

        if (xDoubleSeries.size() != yDoubleSeries.size()) {
            throw new IllegalStateException("Series sizes don't match!"); //$NON-NLS-1$
        }

        /* Check for invalid tuple value. Any null elements are invalid */
        for (int i = 0; i < xDoubleSeries.size(); i++) {
            Double xValue = xDoubleSeries.get(i);
            Double yValue = yDoubleSeries.get(i);
            if (xValue == null || yValue == null) {
                /* Reject this tuple */
                continue;
            }
            if ((xIsLog && xValue <= ZERO_DOUBLE) || (yIsLog && yValue <= ZERO_DOUBLE)) {
                /*
                 * Equal or less than 0 values can't be plotted on log scale
                 */
                continue;
            }
            validXDoubleSeries.add(xValue);
            validYDoubleSeries.add(yValue);
            indexSeriesCorrespondance.add(i);
        }

        if (validXDoubleSeries.isEmpty() || validXDoubleSeries.isEmpty()) {
            /* No need to plot an empty series */
            index++;
            continue;
        }

        ILineSeries scatterSeries = (ILineSeries) getChart().getSeriesSet().createSeries(SeriesType.LINE, name);
        scatterSeries.setLineStyle(LineStyle.NONE);

        double[] xserie = validXDoubleSeries.stream().mapToDouble(elem -> checkNotNull(elem).doubleValue())
                .toArray();
        double[] yserie = validYDoubleSeries.stream().mapToDouble(elem -> checkNotNull(elem).doubleValue())
                .toArray();
        scatterSeries.setXSeries(xserie);
        scatterSeries.setYSeries(yserie);
        fIndexMapping.put(scatterSeries, indexSeriesCorrespondance);
        index++;
    }

    /* Modify x axis related chart styling */
    IAxisTick xTick = getChart().getAxisSet().getXAxis(0).getTick();
    if (areXAspectsContinuous) {
        Format xAxisFormat = getContinuousAxisFormatter(xAxisAspects, getResultTable().getEntries(),
                fXInternalRange, fXExternalRange);

        xTick.setFormat(xAxisFormat);

        if (xAxisFormat instanceof LamiTimeStampFormat) {
            setXUnits(((LamiTimeStampFormat) xAxisFormat).getPattern());
        }
    } else {
        xTick.setFormat(new LamiLabelFormat(checkNotNull(xMap)));
        updateTickMark(checkNotNull(xMap), xTick, getChart().getPlotArea().getSize().x);

        /* Remove vertical grid line */
        getChart().getAxisSet().getXAxis(0).getGrid().setStyle(LineStyle.NONE);
    }

    /* Modify Y axis related chart styling */
    IAxisTick yTick = getChart().getAxisSet().getYAxis(0).getTick();
    if (areYAspectsContinuous) {
        Format yAxisFormat = getContinuousAxisFormatter(yAxisAspects, getResultTable().getEntries(),
                fYInternalRange, fYExternalRange);

        yTick.setFormat(yAxisFormat);

        if (yAxisFormat instanceof LamiTimeStampFormat) {
            setYUnits(((LamiTimeStampFormat) yAxisFormat).getPattern());
        }
    } else {
        yTick.setFormat(new LamiLabelFormat(checkNotNull(yMap)));
        updateTickMark(checkNotNull(yMap), yTick, getChart().getPlotArea().getSize().y);

        /* Remove horizontal grid line */
        getChart().getAxisSet().getYAxis(0).getGrid().setStyle(LineStyle.NONE);
    }

    /*
     * SWTChart workaround: SWTChart fiddles with tick mark visibility based
     * on the fact that it can parse the label to double or not.
     *
     * If the label happens to be a double, it checks for the presence of
     * that value in its own tick labels to decide if it should add it or
     * not. If it happens that the parsed value is already present in its
     * map, the tick gets a visibility of false.
     *
     * The X axis does not have this problem since SWTCHART checks on label
     * angle, and if it is != 0 simply does no logic regarding visibility.
     * So simply set a label angle of 1 to the axis.
     */
    yTick.setTickLabelAngle(1);

    setLineSeriesColor();

    /* Put log scale if necessary */
    if (xIsLog && areXAspectsContinuous && !areXAspectsTimeStamp) {
        Stream.of(getChart().getAxisSet().getXAxes()).forEach(axis -> axis.enableLogScale(xIsLog));
    }

    if (yIsLog && areYAspectsContinuous && !areYAspectsTimeStamp) {
        /* Set the axis as logscale */
        Stream.of(getChart().getAxisSet().getYAxes()).forEach(axis -> axis.enableLogScale(yIsLog));
    }
    getChart().getAxisSet().adjustRange();

    /*
     * Selection listener
     */
    getChart().getPlotArea().addMouseListener(new LamiScatterMouseDownListener());

    /*
     * Hovering cross listener
     */
    getChart().getPlotArea().addMouseMoveListener(new HoveringCrossListener());

    /*
     * Mouse exit listener: reset state of hovering cross on mouse exit.
     */
    getChart().getPlotArea().addListener(SWT.MouseExit, new Listener() {

        @Override
        public void handleEvent(@Nullable Event event) {
            if (event != null) {
                fHoveringCrossDataPoint.x = -1;
                fHoveringCrossDataPoint.y = -1;
                redraw();
            }
        }
    });

    /*
     * Selections and hovering cross painting
     */
    getChart().getPlotArea().addPaintListener(new LamiScatterPainterListener());

    /* On resize check for axis tick updating */
    getChart().addListener(SWT.Resize, new Listener() {
        @Override
        public void handleEvent(@Nullable Event event) {
            if (yTick.getFormat() instanceof LamiLabelFormat) {
                updateTickMark(checkNotNull(yMap), yTick, getChart().getPlotArea().getSize().y);
            }
            if (xTick.getFormat() instanceof LamiLabelFormat) {
                updateTickMark(checkNotNull(xMap), xTick, getChart().getPlotArea().getSize().x);
            }
        }
    });
}