Search in sources :

Example 1 with DiscreteMappingEntry

use of org.cytoscape.io.internal.util.vizmap.model.DiscreteMappingEntry in project cytoscape-impl by cytoscape.

the class VisualStyleSerializer method createVisualProperties.

@SuppressWarnings("unchecked")
private <K, V> void createVisualProperties(VisualStyle vs, Class<? extends CyIdentifiable> targetType, List<org.cytoscape.io.internal.util.vizmap.model.VisualProperty> vpModelList) {
    for (org.cytoscape.io.internal.util.vizmap.model.VisualProperty vpModel : vpModelList) {
        String vpId = vpModel.getName();
        String defValue = vpModel.getDefault();
        VisualProperty<V> vp = (VisualProperty<V>) lexicon.lookup(targetType, vpId);
        if (vp != null) {
            // Default Value
            if (defValue != null) {
                V value = parseValue(defValue, vp);
                vs.setDefaultValue(vp, value);
            }
            // Any mapping?
            if (vpModel.getPassthroughMapping() != null) {
                org.cytoscape.io.internal.util.vizmap.model.PassthroughMapping pmModel = vpModel.getPassthroughMapping();
                final String attrName = pmModel.getAttributeName();
                final AttributeType attrType = pmModel.getAttributeType();
                final Class<?> columnDataType;
                if (attrType == AttributeType.BOOLEAN)
                    columnDataType = Boolean.class;
                else if (attrType == AttributeType.FLOAT)
                    columnDataType = Double.class;
                else if (attrType == AttributeType.INTEGER)
                    columnDataType = Integer.class;
                else if (attrType == AttributeType.LONG)
                    columnDataType = Long.class;
                else if (attrType == AttributeType.LIST)
                    columnDataType = List.class;
                else
                    columnDataType = String.class;
                try {
                    VisualMappingFunctionFactory pmFactory = serviceRegistrar.getService(VisualMappingFunctionFactory.class, "(mapping.type=passthrough)");
                    PassthroughMapping<K, V> pm = (PassthroughMapping<K, V>) pmFactory.createVisualMappingFunction(attrName, columnDataType, vp);
                    vs.addVisualMappingFunction(pm);
                } catch (Throwable e) {
                    logger.error("Cannot create PassthroughMapping (style=" + vs.getTitle() + ", property=" + vp.getIdString() + ")", e);
                }
            } else if (vpModel.getDiscreteMapping() != null) {
                org.cytoscape.io.internal.util.vizmap.model.DiscreteMapping dmModel = vpModel.getDiscreteMapping();
                String attrName = dmModel.getAttributeName();
                AttributeType attrType = dmModel.getAttributeType();
                try {
                    Class<?> attrClass = null;
                    // TODO refactor attr type assignment
                    switch(attrType) {
                        case BOOLEAN:
                            attrClass = Boolean.class;
                            break;
                        case FLOAT:
                            attrClass = Double.class;
                            break;
                        case INTEGER:
                            attrClass = Integer.class;
                            break;
                        case LONG:
                            attrClass = Long.class;
                            break;
                        default:
                            attrClass = String.class;
                            break;
                    }
                    VisualMappingFunctionFactory dmFactory = serviceRegistrar.getService(VisualMappingFunctionFactory.class, "(mapping.type=discrete)");
                    DiscreteMapping<K, V> dm = (DiscreteMapping<K, V>) dmFactory.createVisualMappingFunction(attrName, attrClass, vp);
                    for (DiscreteMappingEntry entryModel : dmModel.getDiscreteMappingEntry()) {
                        String sAttrValue = entryModel.getAttributeValue();
                        String sValue = entryModel.getValue();
                        if (sAttrValue != null && sValue != null) {
                            Object attrValue = null;
                            switch(attrType) {
                                case BOOLEAN:
                                    attrValue = Boolean.parseBoolean(sAttrValue);
                                    break;
                                case FLOAT:
                                    attrValue = Double.parseDouble(sAttrValue);
                                    break;
                                case INTEGER:
                                    attrValue = Integer.parseInt(sAttrValue);
                                    break;
                                case LONG:
                                    attrValue = Long.parseLong(sAttrValue);
                                    break;
                                default:
                                    // Note: Always handle List type as String!
                                    attrValue = sAttrValue;
                                    break;
                            }
                            V vpValue = parseValue(sValue, vp);
                            if (vpValue != null)
                                dm.putMapValue((K) attrValue, vpValue);
                        }
                    }
                    vs.addVisualMappingFunction(dm);
                } catch (Throwable e) {
                    logger.error("Cannot create DiscreteMapping (style=" + vs.getTitle() + ", property=" + vp.getIdString() + ")", e);
                }
            } else if (vpModel.getContinuousMapping() != null) {
                org.cytoscape.io.internal.util.vizmap.model.ContinuousMapping cmModel = vpModel.getContinuousMapping();
                String attrName = cmModel.getAttributeName();
                try {
                    VisualMappingFunctionFactory cmFactory = serviceRegistrar.getService(VisualMappingFunctionFactory.class, "(mapping.type=continuous)");
                    ContinuousMapping<K, V> cm = (ContinuousMapping<K, V>) cmFactory.createVisualMappingFunction(attrName, Number.class, vp);
                    for (org.cytoscape.io.internal.util.vizmap.model.ContinuousMappingPoint pModel : cmModel.getContinuousMappingPoint()) {
                        // Should be numbers or colors
                        V lesser = parseValue(pModel.getLesserValue(), vp);
                        V equal = parseValue(pModel.getEqualValue(), vp);
                        V greater = parseValue(pModel.getGreaterValue(), vp);
                        BoundaryRangeValues<V> brv = new BoundaryRangeValues<V>(lesser, equal, greater);
                        Double attrValue = pModel.getAttrValue().doubleValue();
                        cm.addPoint((K) attrValue, brv);
                    }
                    vs.addVisualMappingFunction(cm);
                } catch (Throwable e) {
                    logger.error("Cannot create ContinuousMapping (style=" + vs.getTitle() + ", property=" + vp.getIdString() + ")", e);
                }
            }
        }
    }
}
Also used : VisualMappingFunctionFactory(org.cytoscape.view.vizmap.VisualMappingFunctionFactory) AttributeType(org.cytoscape.io.internal.util.vizmap.model.AttributeType) BoundaryRangeValues(org.cytoscape.view.vizmap.mappings.BoundaryRangeValues) List(java.util.List) ContinuousMapping(org.cytoscape.view.vizmap.mappings.ContinuousMapping) DiscreteMapping(org.cytoscape.view.vizmap.mappings.DiscreteMapping) VisualProperty(org.cytoscape.view.model.VisualProperty) PassthroughMapping(org.cytoscape.view.vizmap.mappings.PassthroughMapping) DiscreteMappingEntry(org.cytoscape.io.internal.util.vizmap.model.DiscreteMappingEntry)

Example 2 with DiscreteMappingEntry

use of org.cytoscape.io.internal.util.vizmap.model.DiscreteMappingEntry in project cytoscape-impl by cytoscape.

the class CalculatorConverter method getMappingFunction.

/**
 * @param props
 *            All the visual properties
 * @param mapperName
 *            Example: "MyStyle-Edge Color-Discrete Mapper"
 * @return A {@link org.cytoscape.io.internal.util.vizmap.model.DiscreteMapping},
 *           {@link org.cytoscape.io.internal.util.vizmap.model.ContinuousMapping} or
 *           {@link org.cytoscape.io.internal.util.vizmap.model.PassThroughMapping} object
 */
private Object getMappingFunction(Properties props, String mapperName, VisualProperty vp) {
    String baseKey = null;
    String functionType = null;
    while (functionType == null) {
        // e.g. "edgeColorCalculator.MyStyle-Edge Color-Discrete Mapper.mapping."
        baseKey = (legacyKey != null ? legacyKey : key) + "." + mapperName + ".mapping.";
        functionType = props.getProperty(baseKey + "type");
        if (functionType == null) {
            // Try with Cytoscape v2.3 calculator names...
            if (baseKey.startsWith("nodeFillColorCalculator.")) {
                legacyKey = "nodeColorCalculator";
            } else if (baseKey.startsWith("edgeSourceArrowCalculator.") || baseKey.startsWith("edgeTargetArrowCalculator.")) {
                legacyKey = "edgeArrowCalculator";
            } else {
                return null;
            }
        }
    }
    String attrName = props.getProperty(baseKey + "controller");
    if (attrName == null)
        return null;
    // "ID" is actually the "name" column!!!
    if ("ID".equalsIgnoreCase(attrName))
        attrName = "name";
    if ("DiscreteMapping".equalsIgnoreCase(functionType)) {
        String controllerTypeProp = props.getProperty(baseKey + "controllerType");
        byte controllerType = controllerTypeProp != null ? Byte.parseByte(controllerTypeProp) : TYPE_STRING;
        AttributeType attrType = null;
        if (attrName.equals("has_nested_network")) {
            // Force to boolean (in Cy2, this attribute is a "yes\no" string type)
            attrType = AttributeType.BOOLEAN;
        } else {
            switch(controllerType) {
                case TYPE_BOOLEAN:
                    attrType = AttributeType.BOOLEAN;
                    break;
                case TYPE_FLOATING_POINT:
                    attrType = AttributeType.FLOAT;
                    break;
                case TYPE_INTEGER:
                    attrType = AttributeType.INTEGER;
                    break;
                default:
                    attrType = AttributeType.STRING;
                    break;
            }
        }
        DiscreteMapping dm = new DiscreteMapping();
        dm.setAttributeName(attrName);
        dm.setAttributeType(attrType);
        String entryKey = baseKey + "map.";
        for (String sk : props.stringPropertyNames()) {
            if (sk.contains(entryKey)) {
                String attrValue = sk.replace(entryKey, "");
                String sv = props.getProperty(sk);
                String value = getValue(sv);
                if (attrName.equals("has_nested_network"))
                    attrValue = attrValue.matches("(?i)yes|true") ? "true" : "false";
                DiscreteMappingEntry entry = new DiscreteMappingEntry();
                entry.setAttributeValue(attrValue);
                entry.setValue(value);
                dm.getDiscreteMappingEntry().add(entry);
            }
        }
        return dm;
    } else if ("ContinuousMapping".equalsIgnoreCase(functionType)) {
        ContinuousMapping cm = new ContinuousMapping();
        cm.setAttributeName(attrName);
        cm.setAttributeType(AttributeType.FLOAT);
        int boundaryValues = 0;
        try {
            String s = props.getProperty(baseKey + "boundaryvalues");
            boundaryValues = Integer.parseInt(s);
        } catch (NumberFormatException nfe) {
        // TODO: warning
        }
        for (int i = 0; i < boundaryValues; i++) {
            try {
                BigDecimal value = new BigDecimal(props.getProperty(baseKey + "bv" + i + ".domainvalue"));
                String lesser = getValue(props.getProperty(baseKey + "bv" + i + ".lesser"));
                String equal = getValue(props.getProperty(baseKey + "bv" + i + ".equal"));
                String greater = getValue(props.getProperty(baseKey + "bv" + i + ".greater"));
                ContinuousMappingPoint point = new ContinuousMappingPoint();
                point.setAttrValue(value);
                point.setLesserValue(lesser);
                point.setEqualValue(equal);
                point.setGreaterValue(greater);
                cm.getContinuousMappingPoint().add(point);
            } catch (Exception e) {
            // TODO: warning
            }
        }
        return cm;
    } else if ("PassThroughMapping".equalsIgnoreCase(functionType)) {
        PassthroughMapping pm = new PassthroughMapping();
        pm.setAttributeName(attrName);
        // Cy2 doesn't write the "controllerType" property for PassThroughMappings, so just set STRING
        // (it should work, anyway).
        pm.setAttributeType(AttributeType.STRING);
        return pm;
    }
    return null;
}
Also used : ContinuousMapping(org.cytoscape.io.internal.util.vizmap.model.ContinuousMapping) AttributeType(org.cytoscape.io.internal.util.vizmap.model.AttributeType) DiscreteMapping(org.cytoscape.io.internal.util.vizmap.model.DiscreteMapping) PassthroughMapping(org.cytoscape.io.internal.util.vizmap.model.PassthroughMapping) ContinuousMappingPoint(org.cytoscape.io.internal.util.vizmap.model.ContinuousMappingPoint) DiscreteMappingEntry(org.cytoscape.io.internal.util.vizmap.model.DiscreteMappingEntry) BigDecimal(java.math.BigDecimal)

Example 3 with DiscreteMappingEntry

use of org.cytoscape.io.internal.util.vizmap.model.DiscreteMappingEntry in project cytoscape-impl by cytoscape.

the class VisualStyleSerializer method createVizmapProperties.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void createVizmapProperties(final VisualStyle vs, final VisualProperty<Visualizable> root, final List<org.cytoscape.io.internal.util.vizmap.model.VisualProperty> vpModelList) {
    final Collection<VisualProperty<?>> vpList = lexicon.getAllDescendants(root);
    final Iterator<VisualProperty<?>> iter = vpList.iterator();
    while (iter.hasNext()) {
        final VisualProperty vp = iter.next();
        try {
            // NETWORK root includes NODES and EDGES, but we want to separate the CyNetwork properties!
            if (root == BasicVisualLexicon.NETWORK && vp.getTargetDataType() != CyNetwork.class)
                continue;
            Object defValue = vs.getDefaultValue(vp);
            final VisualMappingFunction<?, ?> mapping = vs.getVisualMappingFunction(vp);
            if (defValue != null || mapping != null) {
                org.cytoscape.io.internal.util.vizmap.model.VisualProperty vpModel = new org.cytoscape.io.internal.util.vizmap.model.VisualProperty();
                vpModel.setName(vp.getIdString());
                vpModelList.add(vpModel);
                try {
                    if (defValue != null) {
                        String sValue = vp.toSerializableString(defValue);
                        if (sValue != null)
                            vpModel.setDefault(sValue);
                    }
                } catch (final Exception e) {
                    System.out.println("CCE in VisualStyleSerielizer");
                }
                if (mapping instanceof PassthroughMapping<?, ?>) {
                    PassthroughMapping<?, ?> pm = (PassthroughMapping<?, ?>) mapping;
                    AttributeType attrType = toAttributeType(pm.getMappingColumnType());
                    org.cytoscape.io.internal.util.vizmap.model.PassthroughMapping pmModel = new org.cytoscape.io.internal.util.vizmap.model.PassthroughMapping();
                    pmModel.setAttributeName(pm.getMappingColumnName());
                    pmModel.setAttributeType(attrType);
                    vpModel.setPassthroughMapping(pmModel);
                } else if (mapping instanceof DiscreteMapping<?, ?>) {
                    DiscreteMapping<?, ?> dm = (DiscreteMapping<?, ?>) mapping;
                    AttributeType attrType = toAttributeType(dm.getMappingColumnType());
                    org.cytoscape.io.internal.util.vizmap.model.DiscreteMapping dmModel = new org.cytoscape.io.internal.util.vizmap.model.DiscreteMapping();
                    dmModel.setAttributeName(dm.getMappingColumnName());
                    dmModel.setAttributeType(attrType);
                    Map<?, ?> map = dm.getAll();
                    for (Map.Entry<?, ?> entry : map.entrySet()) {
                        final Object value = entry.getValue();
                        if (value == null)
                            continue;
                        try {
                            DiscreteMappingEntry entryModel = new DiscreteMappingEntry();
                            entryModel.setAttributeValue(entry.getKey().toString());
                            // System.out.println("DiscreteMapEntry: " + entry.getKey().toString() + " = " + value);
                            entryModel.setValue(vp.toSerializableString(value));
                            dmModel.getDiscreteMappingEntry().add(entryModel);
                        } catch (Exception e) {
                            logger.warn("Could not add Discrete Mapping entry: " + value, e);
                        }
                    }
                    vpModel.setDiscreteMapping(dmModel);
                } else if (mapping instanceof ContinuousMapping<?, ?>) {
                    final ContinuousMapping<?, ?> cm = (ContinuousMapping<?, ?>) mapping;
                    AttributeType attrType = toAttributeType(cm.getMappingColumnType());
                    org.cytoscape.io.internal.util.vizmap.model.ContinuousMapping cmModel = new org.cytoscape.io.internal.util.vizmap.model.ContinuousMapping();
                    cmModel.setAttributeName(cm.getMappingColumnName());
                    cmModel.setAttributeType(attrType);
                    List<?> points = cm.getAllPoints();
                    for (Object point : points) {
                        ContinuousMappingPoint<?, ?> continuousPoint = (ContinuousMappingPoint<?, ?>) point;
                        org.cytoscape.io.internal.util.vizmap.model.ContinuousMappingPoint pModel = new org.cytoscape.io.internal.util.vizmap.model.ContinuousMappingPoint();
                        Object originalValue = continuousPoint.getValue();
                        final String sValue = originalValue.toString();
                        final BigDecimal value = new BigDecimal(sValue);
                        pModel.setAttrValue(value);
                        Object lesser = continuousPoint.getRange().lesserValue;
                        Object equal = continuousPoint.getRange().equalValue;
                        Object greater = continuousPoint.getRange().greaterValue;
                        pModel.setLesserValue(vp.toSerializableString(lesser));
                        pModel.setEqualValue(vp.toSerializableString(equal));
                        pModel.setGreaterValue(vp.toSerializableString(greater));
                        cmModel.getContinuousMappingPoint().add(pModel);
                    }
                    vpModel.setContinuousMapping(cmModel);
                }
            }
        } catch (final Exception e) {
            logger.error("Cannot save visual property: " + (vp != null ? vp.getDisplayName() : ""), e);
        }
    }
}
Also used : CyNetwork(org.cytoscape.model.CyNetwork) ContinuousMappingPoint(org.cytoscape.view.vizmap.mappings.ContinuousMappingPoint) DiscreteMappingEntry(org.cytoscape.io.internal.util.vizmap.model.DiscreteMappingEntry) Entry(java.util.Map.Entry) AttributeType(org.cytoscape.io.internal.util.vizmap.model.AttributeType) ContinuousMapping(org.cytoscape.view.vizmap.mappings.ContinuousMapping) DiscreteMapping(org.cytoscape.view.vizmap.mappings.DiscreteMapping) BigDecimal(java.math.BigDecimal) VisualProperty(org.cytoscape.view.model.VisualProperty) PassthroughMapping(org.cytoscape.view.vizmap.mappings.PassthroughMapping) HashMap(java.util.HashMap) Map(java.util.Map) DiscreteMappingEntry(org.cytoscape.io.internal.util.vizmap.model.DiscreteMappingEntry)

Aggregations

AttributeType (org.cytoscape.io.internal.util.vizmap.model.AttributeType)3 DiscreteMappingEntry (org.cytoscape.io.internal.util.vizmap.model.DiscreteMappingEntry)3 BigDecimal (java.math.BigDecimal)2 VisualProperty (org.cytoscape.view.model.VisualProperty)2 ContinuousMapping (org.cytoscape.view.vizmap.mappings.ContinuousMapping)2 DiscreteMapping (org.cytoscape.view.vizmap.mappings.DiscreteMapping)2 PassthroughMapping (org.cytoscape.view.vizmap.mappings.PassthroughMapping)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ContinuousMapping (org.cytoscape.io.internal.util.vizmap.model.ContinuousMapping)1 ContinuousMappingPoint (org.cytoscape.io.internal.util.vizmap.model.ContinuousMappingPoint)1 DiscreteMapping (org.cytoscape.io.internal.util.vizmap.model.DiscreteMapping)1 PassthroughMapping (org.cytoscape.io.internal.util.vizmap.model.PassthroughMapping)1 CyNetwork (org.cytoscape.model.CyNetwork)1 VisualMappingFunctionFactory (org.cytoscape.view.vizmap.VisualMappingFunctionFactory)1 BoundaryRangeValues (org.cytoscape.view.vizmap.mappings.BoundaryRangeValues)1 ContinuousMappingPoint (org.cytoscape.view.vizmap.mappings.ContinuousMappingPoint)1