]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java
Generate parts of db client query code
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / TypeHierarchy.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.db.impl.query;
13
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.impl.graph.ReadGraphImpl;
16 import org.simantics.db.impl.procedure.InternalProcedure;
17 import org.simantics.db.procedure.ListenerBase;
18
19 import gnu.trove.procedure.TIntProcedure;
20
21 final public class TypeHierarchy extends UnaryQuery<InternalProcedure<IntSet>> {
22     
23     TypeHierarchy(final int resource) {
24         super(resource);
25     }
26     
27     final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure<IntSet> procedure) throws DatabaseException {
28         QueryCache.runnerTypeHierarchy(graph, r, parent, listener, procedure);
29     }
30
31         @Override
32         final public void removeEntry(QueryProcessor provider) {
33                 provider.cache.remove(this);
34         }
35
36         @Override
37         public IntSet compute(ReadGraphImpl graph, final InternalProcedure<IntSet> procedure) throws DatabaseException {
38
39                 QueryProcessor processor = graph.processor;
40                 
41         final IntSet result = new IntSet(processor.querySupport, id);
42         
43         final TIntProcedure addToResult = new TIntProcedure() {
44             @Override
45             public boolean execute(int r) {
46                 result.add(r);
47                 return true;
48             }
49         };
50         
51         QueryCache.runnerSuperTypes(graph, id, TypeHierarchy.this, null, new InternalProcedure<IntSet>() {
52
53                 @Override
54                 public void execute(ReadGraphImpl graph, IntSet types) throws DatabaseException {
55
56                         types.forEach(addToResult);
57                         addOrSet(graph, result, processor);
58                         procedure.execute(graph, result); 
59
60                 }
61
62                 @Override
63                 public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException {
64                         procedure.exception(graph, t);
65                 }
66
67         });
68         
69         return result;
70                         
71     }
72     
73     @Override
74     public String toString() {
75         return "TypeHierarchy[" + id + "]";
76     }
77
78     private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) {
79         
80         assert(!isReady());
81
82         synchronized(this) {
83         
84             value.trim();
85             setResult(value);
86             setReady();
87         
88         }
89         
90     }
91
92     @Override
93     public Object performFromCache(ReadGraphImpl graph, InternalProcedure<IntSet> procedure) throws DatabaseException {
94         
95         assert(isReady());
96
97         if(handleException(graph, procedure)) return null;
98         
99         IntSet result = getResult();
100         
101         procedure.execute(graph, result);
102         
103         return result;
104         
105     }
106     
107     @Override
108     public void recompute(ReadGraphImpl graph) throws DatabaseException {
109     
110         compute(graph, new InternalProcedure<IntSet>() {
111
112                 @Override
113                 public void execute(ReadGraphImpl graph, IntSet result) {
114                 }
115
116             @Override
117             public void exception(ReadGraphImpl graph, Throwable t) {
118                 new Error("Error in recompute.", t).printStackTrace();
119             }
120
121         });
122         
123     }
124     
125     @Override
126     boolean isImmutable(ReadGraphImpl graph) {
127         return graph.processor.isImmutable(id);
128     }
129
130 }