package org.simantics.district.network.profile; import java.util.Map; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.network.visualisations.model.DynamicSizeContribution; import org.simantics.district.network.visualisations.model.DynamicSizeMap; import org.simantics.district.network.visualisations.model.DynamicVisualisation; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.scenegraph.INode; import org.simantics.scenegraph.g2d.G2DSceneGraph; import org.simantics.scenegraph.g2d.nodes.SingleElementNode; import org.simantics.scenegraph.profile.EvaluationContext; import org.simantics.scenegraph.profile.common.ProfileVariables; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Jani Simomaa */ public class DNElementSizeStyle extends ThrottledStyleBase { private static final Logger LOGGER = LoggerFactory.getLogger(DNElementSizeStyle.class); private static final boolean DEBUG = false; private static final Double PENDING = Double.NaN; private static final Double ONE = 1.0; @Override public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException { DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram), TransientCacheAsyncListener.instance()); // Prevent PendingVariableExceptions from coming through boolean wasSynchronous = graph.setSynchronous(true); try { if (dv != null) { Layer0 L0 = Layer0.getInstance(graph); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); ModelingResources MOD = ModelingResources.getInstance(graph); Resource mapping = graph.getSingleObject(groupItem, DN.HasMapping); Map sizeContributions = dv.getSizeContributions(); String mappingName = graph.getRelatedValue(mapping, L0.HasName); DynamicSizeContribution dsc = sizeContributions.get(mappingName); if (dsc != null && dsc.isUsed()) { Resource mappedComponent = graph.getPossibleObject(groupItem, DN.MappedComponent); if (mappedComponent != null) { Resource component = graph.getSingleObject(mappedComponent, MOD.ElementToComponent); Variable variable = Variables.getVariable(graph, component); Variable possibleActiveVariable = Variables.possibleActiveVariable(graph, variable); if (possibleActiveVariable != null) { Variable module = possibleActiveVariable.getPossibleChild(graph, dsc.getModuleName()); if (module != null) { Variable attribute = module.getPossibleProperty(graph, dsc.getAttributeName()); if (attribute != null) { Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE); if (possibleValue != null) { // here we do the adjusting according to spec in #15038 double adjustedValue = possibleValue.doubleValue() * dsc.getVariableGain() + dsc.getVariableBias(); DynamicSizeMap defaultSizeMap = dsc.getDefaultSizeMap(); double size = defaultSizeMap.getSize(adjustedValue, dv.getSizeBarOptions().isUseGradients(), dsc.getDefaultMin(), dsc.getDefaultMax()); return size; } else { LOGGER.warn("No value for {}", attribute.getURI(graph)); } } else { LOGGER.warn("Wrong attribute name {} for {} !!", dsc.getAttributeName(), module.getURI(graph)); } } else { LOGGER.warn("Wrong modulename {} for {} !!", dsc.getModuleName(), possibleActiveVariable.getURI(graph)); } } else { LOGGER.debug("No active experiment for {}", variable.getURI(graph)); } } else { LOGGER.debug("No mapped component for {} to calculate dynamic size style", groupItem); } } } } finally { graph.setSynchronous(wasSynchronous); } return 1.0; } @Override public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) { //System.out.println("apply: " + node + " : " + value); SingleElementNode n = (SingleElementNode) node; if (value == PENDING) { ((G2DSceneGraph)node.getRootNode()).setPending(node); } else { ((G2DSceneGraph)node.getRootNode()).clearPending(node); } if (value == null) value = ONE; for (INode nn : n.getNodes()) { ProfileVariables.claimNodeProperty(nn, "size", value, observer); ProfileVariables.claimNodeProperty(nn, "stroke", value, observer); } } @Override protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) { ((G2DSceneGraph)node.getRootNode()).clearPending(node); SingleElementNode n = (SingleElementNode) node; for (INode nn : n.getNodes()) { ProfileVariables.claimNodeProperty(nn, "size", ONE, evaluationContext); ProfileVariables.claimNodeProperty(nn, "stroke", ONE, evaluationContext); } } }