]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/TypeGroup.java
No point computing ordered set if resource does not have any statements
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / TypeGroup.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.adapter;
13
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.RequestProcessor;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.primitiverequest.OrderedSet;
25 import org.simantics.db.common.procedure.wrapper.SetListenerToSingleSetListener;
26 import org.simantics.db.common.request.BinaryRead;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.procedure.SetListener;
29 import org.simantics.diagram.stubs.DiagramResource;
30 import org.simantics.layer0.Layer0;
31 import org.simantics.scenegraph.profile.Group;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * @author Tuukka Lehtonen
37  */
38 public class TypeGroup implements Group {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(TypeGroup.class);
41
42     private final Collection<Resource> types;
43     private final String               name;
44
45     public TypeGroup(String name, Resource type) {
46         this(name, new Resource[] { type });
47     }
48
49     public TypeGroup(String name, Resource... types) {
50         this.types = Arrays.asList(types);
51         this.name = name;
52     }
53
54     @Override
55     public int hashCode() {
56         final int prime = 31;
57         int result = 1;
58         result = prime * result + types.hashCode();
59         return result;
60     }
61
62     @Override
63     public boolean equals(Object obj) {
64         if (this == obj)
65             return true;
66         if (obj == null)
67             return false;
68         if (getClass() != obj.getClass())
69             return false;
70         TypeGroup other = (TypeGroup) obj;
71         return types.equals(other.types);
72     }
73
74     /**
75      * Initialize TypeGroup from a DIAGRAM.Group instance.
76      * 
77      * @param graph
78      * @param group
79      * @throws DatabaseException
80      */
81     public TypeGroup(ReadGraph graph, final Resource group) throws DatabaseException {
82         Layer0 L0 = Layer0.getInstance(graph);
83         DiagramResource DIA = DiagramResource.getInstance(graph);
84
85         this.name = graph.getPossibleRelatedValue(group, L0.HasName, Bindings.STRING);
86         this.types = graph.getObjects(group, DIA.TypeGroup_HasType);
87     }
88
89     @Override
90     public void trackItems(RequestProcessor processor, final Resource runtimeDiagram, final SetListener<Resource> listener) throws DatabaseException {
91         if (types.isEmpty()) {
92             System.out.println("TypeGroup has no types!");
93             return;
94         }
95
96         processor.syncRequest(new BinaryRead<Resource, Collection<Resource>, Collection<Resource>>(runtimeDiagram, types) {
97
98             @Override
99             public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
100                 HashSet<Resource> result = new HashSet<Resource>();
101
102                 Resource realDiagram = graph.getPossibleObject(runtimeDiagram, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
103                 if (realDiagram == null)
104                     return result;
105
106 //                System.out.println("looking for diagram elements of type:");
107 //                for (Resource t : types)
108 //                    System.out.println("\t" + NameUtils.getSafeName(graph, t, true));
109
110                 if (graph.hasStatement(realDiagram)) {
111                     Collection<Resource> elements = graph.syncRequest(new OrderedSet(realDiagram));
112                     for (Resource element : elements) {
113     //                    System.out.println("checking element " + NameUtils.getSafeName(graph, element, true));
114                         Collection<Resource> elementTypes = graph.getTypes(element);
115                         if (!Collections.disjoint(types, elementTypes))
116                             result.add(element);
117                     }
118                 } else {
119                     LOGGER.warn("Most likely after deleting a diagram or something therefore no ordered set can be found for {}", realDiagram);
120                 }
121                 return result;
122             }
123
124         }, new SetListenerToSingleSetListener<Resource>(listener));
125     }
126
127     @Override
128     public String toString() {
129         return "every '" + name + "'";
130     }
131
132 }