]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DistrictNodeGroup.java
First draft of vertex size adjusting district network diagram profiles
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / DistrictNodeGroup.java
1 package org.simantics.district.network.profile;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Set;
7
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.RequestProcessor;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.primitiverequest.OrderedSet;
13 import org.simantics.db.common.procedure.wrapper.SetListenerToSingleSetListener;
14 import org.simantics.db.common.request.BinaryRead;
15 import org.simantics.db.common.request.TernaryRead;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.procedure.SetListener;
18 import org.simantics.diagram.stubs.DiagramResource;
19 import org.simantics.district.network.ontology.DistrictNetworkResource;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.scenegraph.profile.Group;
22 import org.simantics.utils.strings.StringUtils;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class DistrictNodeGroup implements Group {
28
29     private final String               name;
30     private final Collection<Resource> types;
31     private final Set<String>          mappedComponentTypeNames;
32
33     public DistrictNodeGroup(ReadGraph graph, Resource group) throws DatabaseException {
34         Layer0 L0 = Layer0.getInstance(graph);
35         DiagramResource DIA = DiagramResource.getInstance(graph);
36         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
37
38         this.name = StringUtils.safeString( graph.getPossibleRelatedValue(group, L0.HasName, Bindings.STRING) );
39         this.types = graph.getObjects(group, DIA.TypeGroup_HasType);
40         Collection<Resource> names = graph.getObjects(group, DN.DistrictNodeGroup_hasComponentTypeName);
41         this.mappedComponentTypeNames = new HashSet<>(names.size());
42         for (Resource name : names) {
43             mappedComponentTypeNames.add( graph.getValue(name, Bindings.STRING) );
44         }
45     }
46
47     @Override
48     public int hashCode() {
49         final int prime = 31;
50         int result = 1;
51         result = prime * result + name.hashCode();
52         result = prime * result + types.hashCode();
53         result = prime * result + mappedComponentTypeNames.hashCode();
54         return result;
55     }
56
57     @Override
58     public boolean equals(Object obj) {
59         if (this == obj)
60             return true;
61         if (obj == null)
62             return false;
63         if (getClass() != obj.getClass())
64             return false;
65         DistrictNodeGroup other = (DistrictNodeGroup) obj;
66         return name.equals(other.name) && types.equals(other.types) && mappedComponentTypeNames.equals(other.mappedComponentTypeNames);
67     }
68
69     @Override
70     public void trackItems(RequestProcessor processor, final Resource runtimeDiagram, final SetListener<Resource> listener) {
71         if (types.isEmpty()) {
72             System.out.println("DistrictNodeGroup has no types!");
73             return;
74         }
75         processor.asyncRequest(new TernaryRead<Resource, Collection<Resource>, Set<String>, Collection<Resource>>(runtimeDiagram, types, mappedComponentTypeNames) {
76             @Override
77             public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
78                 return graph.syncRequest(new ElementsMappedToComponentTypes(parameter, parameter2, parameter3));
79             }
80         }, new SetListenerToSingleSetListener<Resource>(listener));
81     }
82
83     @Override
84     public String toString() {
85         return "every '" + name + "' of mapped type " + mappedComponentTypeNames;
86     }
87
88     private static class ElementsMappedToComponentTypes extends TernaryRead<Resource, Collection<Resource>, Set<String>, Set<Resource>> {
89
90         public ElementsMappedToComponentTypes(Resource runtimeDiagram, Collection<Resource> elementTypes, Set<String> componentTypeNames) {
91             super(runtimeDiagram, elementTypes, componentTypeNames);
92         }
93
94         @Override
95         public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
96             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
97             Set<Resource> result = new HashSet<>();
98             Set<Resource> elementsOfType = graph.syncRequest(new ElementsOfType(parameter, parameter2));
99             for (Resource e : elementsOfType) {
100                 Resource mapping = graph.getPossibleObject(e, DN.HasMapping);
101                 if (mapping != null) {
102                     String ct = graph.getPossibleRelatedValue(mapping, DN.Mapping_ComponentType, Bindings.STRING);
103                     if (ct != null) {
104                         if (parameter3.contains(ct)) {
105                             //System.out.println("GROUP: added element " + NameUtils.getSafeName(graph, e) + " of type " + ct);
106                             result.add(e);
107                         }
108                     }
109                 }
110             }
111             return result;
112         }
113
114     }
115
116     private static class ElementsOfType extends BinaryRead<Resource, Collection<Resource>, Set<Resource>> {
117
118         public ElementsOfType(Resource runtimeDiagram, Collection<Resource> types) {
119             super(runtimeDiagram, types);
120         }
121
122         @Override
123         public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
124             HashSet<Resource> result = new HashSet<>();
125
126             Resource realDiagram = graph.getPossibleObject(parameter, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
127             if (realDiagram == null)
128                 return result;
129
130             Collection<Resource> elements = graph.syncRequest(new OrderedSet(realDiagram));
131             for (Resource element : elements) {
132                 Collection<Resource> elementTypes = graph.getTypes(element);
133                 if (!Collections.disjoint(parameter2, elementTypes)) {
134                     result.add(element);
135                 }
136             }
137
138             return result;
139         }
140
141     }
142
143 }