]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
3caae72cec844972cf5635e0347207c191133503
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.function;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.HashSet;\r
17 import java.util.Map;\r
18 import java.util.Set;\r
19 \r
20 import org.eclipse.core.runtime.IProgressMonitor;\r
21 import org.eclipse.ui.IWorkbenchPage;\r
22 import org.simantics.databoard.Bindings;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.common.utils.Logger;\r
26 import org.simantics.db.common.utils.NameUtils;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
29 import org.simantics.db.exception.ServiceException;\r
30 import org.simantics.db.layer0.genericrelation.Dependencies;\r
31 import org.simantics.diagram.stubs.DiagramResource;\r
32 import org.simantics.layer0.Layer0;\r
33 import org.simantics.modeling.ModelingResources;\r
34 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;\r
35 import org.simantics.operation.Layer0X;\r
36 import org.simantics.scl.runtime.function.FunctionImpl5;\r
37 import org.simantics.sysdyn.SysdynResource;\r
38 import org.simantics.sysdyn.ui.handlers.FindSearchTrim;\r
39 import org.simantics.sysdyn.ui.handlers.FindSearchTrim.Scope;\r
40 import org.simantics.sysdyn.ui.utils.SysdynWorkbenchUtils;\r
41 import org.simantics.workbench.search.NameAndTypeRow;\r
42 import org.simantics.workbench.search.NamedResource;\r
43 import org.simantics.workbench.search.SearchQuery;\r
44 import org.simantics.workbench.search.SearchResult;\r
45 import org.simantics.workbench.search.Searching;\r
46 \r
47 /**\r
48  * modulesSearchFunction:\r
49  *      (IProgressMonitor, ReadGraph, model : Resource, query : String, maxResults : Integer) -> QueryResult\r
50  * \r
51  * @author Tuukka Lehtonen\r
52  * @author Tuomas Miettinen\r
53  */\r
54 public class ModulesSearchFunction extends FunctionImpl5<IProgressMonitor, ReadGraph, Resource, SearchQuery, Integer, SearchResult> {\r
55 \r
56         private final int MAX_RESULTS = 50000;\r
57         \r
58     @Override\r
59     public SearchResult apply(IProgressMonitor monitor, ReadGraph graph, Resource model, SearchQuery query, Integer maxResults) {\r
60         try {\r
61                 // Check whether only currently open diagram is searched\r
62                 String filteredQuery = query.getQuery("Name","Types");\r
63                 Collection<Map<String, Object>> results = Searching.performSearch(graph, Layer0X.getInstance(graph).Dependencies, model, filteredQuery, MAX_RESULTS);\r
64 \r
65                 if (!query.getSearchParams().contains(FindSearchTrim.CURRENT_DIAGRAM_OPTION))\r
66                 return generateSearchResults(graph, results, Scope.ALL_MODELS);\r
67                 else\r
68                         return generateSearchResults(graph, results, Scope.CURRENT_DIAGRAM);\r
69         } catch (DatabaseException e) {\r
70             Logger.defaultLogError(e);\r
71         }\r
72         return null;\r
73     }\r
74     \r
75     private static boolean resourceFoundInCurrentDiagram(ReadGraph graph, Resource diagram) throws ManyObjectsForFunctionalRelationException, ServiceException {\r
76 \r
77         IWorkbenchPage page = SysdynWorkbenchUtils.getActivePageOfEditor();\r
78         DiagramEditor editor = (DiagramEditor)page.getActiveEditor();\r
79         if (editor != null) {\r
80                 Resource inputResource = editor.getInputResource();\r
81                 ModelingResources MOD = ModelingResources.getInstance(graph);\r
82                 Resource editorResource = graph.getPossibleObject(inputResource, MOD.DiagramToComposite);\r
83                 if (diagram.equalsResource(editorResource)) \r
84                         return true;\r
85         }\r
86         return false;\r
87     }\r
88 \r
89     private static final SearchResult generateSearchResults(ReadGraph graph,\r
90             Collection<Map<String, Object>> results, Scope scope) throws DatabaseException {\r
91         Layer0 L0 = Layer0.getInstance(graph);\r
92         DiagramResource DIA = DiagramResource.getInstance(graph);\r
93         SysdynResource SYSDYN = SysdynResource.getInstance(graph);\r
94 \r
95         SearchResult result = new SearchResult(NameAndTypeRow.columns);\r
96         Set<Resource> processed = new HashSet<Resource>();\r
97 \r
98         for (Map<String, Object> r : results) {\r
99             Resource resource = (Resource) r.get(Dependencies.FIELD_RESOURCE);\r
100 \r
101             // Only Auxiliary, Stock, Valve, Input, and Module \r
102             if (!(graph.isInstanceOf(resource, SYSDYN.IndependentVariable)\r
103                         || graph.isInstanceOf(resource, SYSDYN.Input)\r
104                         || graph.isInstanceOf(resource, SYSDYN.Module)))\r
105                 continue;\r
106 \r
107             // Prevent index corruption from producing duplicate results.\r
108             if (!processed.add(resource))\r
109                 continue;\r
110 \r
111             // Prevent diagram elements from being collected into the results.\r
112             Collection<Resource> typeResources = graph.getTypes(resource);\r
113             if (typeResources.contains(DIA.Element))\r
114                 continue;\r
115 \r
116             // Check if the result is found in the open diagram.  \r
117             Resource parent = (Resource) r.get(Dependencies.FIELD_PARENT);\r
118             if (scope == Scope.CURRENT_DIAGRAM &&\r
119                         !resourceFoundInCurrentDiagram(graph, parent))\r
120                 continue;\r
121             \r
122             String name = (String) r.get(Dependencies.FIELD_NAME);\r
123             NameAndTypeRow rst = new NameAndTypeRow();\r
124             rst.resource = NamedResource.of(graph, resource, name);\r
125             rst.parent = NamedResource.of(graph, parent);\r
126 \r
127             Collection<Resource> principalTypeResources = graph.getPrincipalTypes(resource);\r
128             if (!typeResources.isEmpty()) {\r
129                 rst.types = new ArrayList<NamedResource>(typeResources.size());\r
130                 rst.principalTypes = new ArrayList<NamedResource>(principalTypeResources.size());\r
131                 for (Resource t : typeResources) {\r
132                     String aprosName = graph.getPossibleRelatedValue(t, L0.HasName, Bindings.STRING);\r
133                     if (aprosName == null)\r
134                         aprosName = NameUtils.getSafeLabel(graph, t);\r
135                     NamedResource nr = NamedResource.of(graph, t, aprosName);\r
136                     rst.types.add(nr);\r
137                     if (principalTypeResources.contains(t))\r
138                         rst.principalTypes.add(nr);\r
139                 }\r
140             }\r
141   \r
142             result.addRow(rst);\r
143         }\r
144         return result;\r
145     }\r
146 \r
147 }\r