1 package org.simantics.district.network.profile;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
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;
25 * @author Tuukka Lehtonen
27 public class DistrictNodeGroup implements Group {
29 private final String name;
30 private final Collection<Resource> types;
31 private final Set<String> mappedComponentTypeNames;
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);
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) );
48 public int hashCode() {
51 result = prime * result + name.hashCode();
52 result = prime * result + types.hashCode();
53 result = prime * result + mappedComponentTypeNames.hashCode();
58 public boolean equals(Object obj) {
63 if (getClass() != obj.getClass())
65 DistrictNodeGroup other = (DistrictNodeGroup) obj;
66 return name.equals(other.name) && types.equals(other.types) && mappedComponentTypeNames.equals(other.mappedComponentTypeNames);
70 public void trackItems(RequestProcessor processor, final Resource runtimeDiagram, final SetListener<Resource> listener) throws DatabaseException {
71 if (types.isEmpty()) {
72 System.out.println("DistrictNodeGroup has no types!");
75 processor.syncRequest(new TernaryRead<Resource, Collection<Resource>, Set<String>, Collection<Resource>>(runtimeDiagram, types, mappedComponentTypeNames) {
77 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
78 return graph.syncRequest(new ElementsMappedToComponentTypes(parameter, parameter2, parameter3));
80 }, new SetListenerToSingleSetListener<Resource>(listener));
84 public String toString() {
85 return "every '" + name + "' of mapped type " + mappedComponentTypeNames;
88 private static class ElementsMappedToComponentTypes extends TernaryRead<Resource, Collection<Resource>, Set<String>, Set<Resource>> {
90 public ElementsMappedToComponentTypes(Resource runtimeDiagram, Collection<Resource> elementTypes, Set<String> componentTypeNames) {
91 super(runtimeDiagram, elementTypes, componentTypeNames);
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);
104 if (parameter3.contains(ct)) {
105 //System.out.println("GROUP: added element " + NameUtils.getSafeName(graph, e) + " of type " + ct);
116 private static class ElementsOfType extends BinaryRead<Resource, Collection<Resource>, Set<Resource>> {
118 public ElementsOfType(Resource runtimeDiagram, Collection<Resource> types) {
119 super(runtimeDiagram, types);
123 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
124 HashSet<Resource> result = new HashSet<>();
126 Resource realDiagram = graph.getPossibleObject(parameter, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
127 if (realDiagram == null)
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)) {