+ Simantics.getSession().asyncRequest(new WriteResultRequest<Resource>() {
+
+ @Override
+ public Resource perform(WriteGraph graph) throws DatabaseException {
+ return DistrictNetworkUtil.createNetworkDiagram(graph, target, compositeType, defaultName,
+ dialog.getDefaultEdgeMapping(),
+ dialog.getDefaultVertexMapping(),
+ dialog.getRightClickVertexMapping(),
+ dialog.getLeftClickVertexMapping(),
+ dialog.getCRS()
+ );
+ }
+
+
+ }, new Procedure<Resource>() {
+
+ @Override
+ public void execute(Resource composite) {
+ DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true);
+ procedure.execute(composite);
+ }
+
+ @Override
+ public void exception(Throwable t) {
+ LOGGER.error("Failed to create composite, see exception for details.", t);
+ procedure.exception(t);
+ }
+ });
+ }
+
+ public static Collection<Resource> getDistrictDiagrams(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ Collection<Resource> indexRoots = graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, L0.IndexRoot));
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ Set<Resource> results = new HashSet<>();
+ for (Resource indexRoot : indexRoots) {
+ Collection<Resource> diagrams = QueryIndexUtils.searchByType(graph, indexRoot, DN.Diagram);
+ results.addAll(diagrams);
+ }
+ return results;
+ }
+
+ private static List<String> listInstanceNames(ReadGraph graph, Variable context, Resource type) throws DatabaseException {
+ Resource indexRoot = Variables.getIndexRoot(graph, context);
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ List<Resource> properties = Collections.emptyList();//QueryIndexUtils.searchByType(graph, indexRoot, DN.Vertex_ScaleProperty);
+ return properties.stream()
+ .map(m -> createEnumeratedValue(graph, m))
+ .map(EnumeratedValue::getName)
+ .collect(Collectors.toList());
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object arrowLengthPropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ return listInstanceNames(graph, context, DN.Edge_ArrowLengthProperty);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object arrowLengthPropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ Resource diagram = resolveElement(graph, context);
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ return baseMappingModifier(graph, diagram, DN.Diagram_arrowLengthProperty, DN.Edge_ArrowLengthProperty, context);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> hasDiameterValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasDiameter, 0);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> hasNominalMassFlowValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasNominalMassFlow, 0);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> hasNominalSupplyPressure(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasSupplyPressure, 0);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> hasElevation(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Vertex_HasElevation, 0);
+ }
+
+ private static final Function1<Resource, Double> ONE = new FunctionImpl1<Resource, Double>() {
+ private final Double ONE = 1.0;
+ @Override
+ public Double apply(Resource edge) {
+ return ONE;
+ }
+ @Override
+ public String toString() {
+ return "1";
+ }
+ };
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Function1<Resource, Double> constantOne(ReadGraph graph, Resource resource, Variable context) throws DatabaseException {
+ return ONE;
+ }
+
+ private static Function1<Resource, Double> directPropertyValueFunction(Resource property, double defaultValue) throws DatabaseException {
+ Double def = defaultValue;
+ return new FunctionImpl1<Resource, Double>() {
+ @Override
+ public Double apply(Resource edge) {
+ ReadGraph graph = (ReadGraph) SCLContext.getCurrent().get("graph");
+ try {
+ Double d = graph.getPossibleRelatedValue(edge, property, Bindings.DOUBLE);
+ return d != null ? d : def;
+ } catch (DatabaseException e) {
+ LOGGER.error("Failed to evaluate property value", e);
+ return def;
+ }
+ }
+ };
+ }
+
+ private static class RangeValidator implements Function1<String, String> {
+ private double min;
+ private double max;
+ public RangeValidator(double min, double max) {
+ this.min = min;
+ this.max = max;
+ }
+ @Override
+ public String apply(String s) {
+ try {
+ double d = Double.parseDouble(s);
+ if (d < min)
+ return "Value must be greater than or equal to " + min;
+ if (d > max)
+ return "Value must be less than or equal to " + max;
+ return null;
+ } catch (NumberFormatException e) {
+ return "Specified value is not a number";
+ }
+ }
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object hueValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+ return new RangeValidator(0, 360);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object saturationValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+ return new RangeValidator(0, 100);
+ }
+
+ @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
+ public static Object brightnessValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
+ String importEntry = null;
+ Resource root = Variables.getPossibleIndexRoot(graph, context);
+ if (root != null) {
+ Resource sclmain = Layer0Utils.getPossibleChild(graph, root, "SCLMain");
+ if (sclmain != null) {
+ importEntry = graph.getPossibleURI(sclmain);
+ }
+ }
+ SCLContext ctx = SCLContext.getCurrent();
+ Object oldGraph = ctx.put("graph", graph);
+ try {
+ return new BrightnessExpressionValidator(
+ importEntry != null
+ ? Arrays.asList(importEntry)
+ : Collections.emptyList());
+ } finally {
+ ctx.put("graph", oldGraph);
+ }