1 package org.simantics.db.impl.query;
4 import java.io.IOException;
5 import java.io.UnsupportedEncodingException;
6 import java.net.MalformedURLException;
8 import java.net.URLDecoder;
10 import org.simantics.utils.FileUtils;
12 public class CodeGen {
18 String[] signatureR1RelationInfo = { "int r", "r", "keyR", "long", "InternalProcedure<RelationInfo>", "entry.id" };
19 String[] signatureR1Bytes = { "int r", "r", "keyR", "long", "InternalProcedure<byte[]>", "entry.id" };
20 String[] signatureR1IntSet = { "int r", "r", "keyR", "long", "InternalProcedure<IntSet>", "entry.id" };
21 String[] signatureR1IP = { "int r", "r", "keyR", "long", "IntProcedure", "entry.id" };
22 String[] signatureR2IP = { "int r1, int r2", "r1,r2", "keyR2", "long", "IntProcedure", "entry.id" };
23 String[] signatureR2TIP = { "int r1, int r2", "r1,r2", "keyR2", "long", "TripleIntProcedure", "entry.id" };
24 String[] signatureID1 = { "String id", "id", "keyID", "String", "InternalProcedure<Integer>", "entry.id" };
25 String[] signatureID2 = { "String id", "id", "keyID", "String", "InternalProcedure<TObjectIntHashMap<String>>", "entry.id" };
26 String[] signatureRead = { "Read<?> r", "r", "id", "long", "AsyncProcedure", "entry.request" };
27 String[] signatureAsyncRead = { "AsyncRead<?> r", "r", "id", "long", "AsyncProcedure", "entry.request" };
28 String[] signatureMultiRead = { "MultiRead<?> r", "r", "id", "long", "AsyncMultiProcedure", "entry.request" };
29 String[] signatureAsyncMultiRead = { "AsyncMultiRead<?> r", "r", "id", "long", "AsyncMultiProcedure", "entry.request" };
30 String[] signatureExternalRead = { "ExternalRead<?> r", "r", "id", "long", "AsyncProcedure", "entry.request" };
32 private void line(StringBuilder content, String line) {
33 for(int i=0;i<indent;i++)
39 public void generateQuery(StringBuilder content, String clazz, String[] signature, boolean runnerShortcut) {
40 generateGetOrCreate(content, clazz, signature);
41 generateRemove(content, clazz, signature);
42 generateRunner(content, clazz, signature, runnerShortcut);
45 public void generateRunner(StringBuilder content, String clazz, String[] signature, boolean shortcut) {
47 line(content, "public static void runner" + clazz + "(ReadGraphImpl graph, " + signature[0] + ", CacheEntry parent, ListenerBase listener, " + signature[4] + " procedure) throws DatabaseException {");
49 line(content, " if(parent == null && listener == null) {");
50 line(content, " " + clazz + ".computeForEach(graph, " + signature[1] + ", null, procedure);");
51 line(content, " return;");
54 line(content, " QueryCache cache = graph.processor.cache;");
55 line(content, " if(procedure == null) procedure = emptyProcedure" + clazz + ";");
56 line(content, " " + clazz + " entry = (" + clazz + ")cache.getOrCreate" + clazz + "(" + signature[1] + ");");
57 line(content, " ListenerEntry listenerEntry = cache.registerDependencies(graph, entry, parent, listener, procedure, false);");
58 line(content, " if(entry.isReady()) entry.performFromCache(graph, procedure);");
59 line(content, " else {");
60 if(shortcut) line(content, " " + clazz + ".computeForEach(graph, " + signature[1] + ", entry, procedure);");
61 else line(content, " entry.compute(graph, procedure);");
62 line(content, " if(listenerEntry != null) cache.primeListenerEntry(listenerEntry, entry.getResult());");
69 public void generateRemove(StringBuilder content, String clazz, String[] signature) {
71 String lower = Character.toLowerCase(clazz.charAt(0)) + clazz.substring(1);
73 line(content, "void remove(" + clazz + " entry) {");
74 line(content, " synchronized(" + lower + "Map) {");
75 line(content, " " + lower + "Map.remove(" + signature[5] + ");");
82 public void generateGetOrCreate(StringBuilder content, String clazz, String[] signature) {
84 String lower = Character.toLowerCase(clazz.charAt(0)) + clazz.substring(1);
86 line(content, "" + clazz + " getOrCreate" + clazz + "(" + signature[0] + ") throws DatabaseException {");
87 line(content, " " + clazz + " existing = null;");
88 line(content, " synchronized(" + lower + "Map) {");
89 line(content, " existing = (" + clazz + ")" + lower + "Map.get(" + signature[1] + ");");
90 line(content, " if(existing == null) {");
91 line(content, " existing = new " + clazz + "(" + signature[1] + ");");
92 line(content, " existing.clearResult(querySupport);");
93 line(content, " existing.setPending();");
94 line(content, " " + lower + "Map.put(" + signature[2] + "(" + signature[1] + "), existing);");
95 line(content, " return existing;");
97 line(content, " if(existing.requiresComputation()) {");
98 line(content, " existing.setPending();");
99 line(content, " return existing;");
102 line(content, " if(existing.isPending()) waitPending(existing);");
103 line(content, " return existing;");
109 public void generate() {
111 URL classLocation = CodeGen.class.getResource(".");
112 if (classLocation != null) {
113 if (classLocation.getProtocol().equals("file")) {
115 URL resource = new URL(classLocation, ".");
116 File path = new File(URLDecoder.decode(resource.getPath(), "UTF-8"));
117 String target = path.getAbsolutePath().replace("\\", "/");
118 target = target.replace("/bin/", "/src/") + "/QueryCache.java";
119 System.err.println("target=" + target);
120 File source = new File(target);
121 StringBuilder content = new StringBuilder();
122 content.append("package org.simantics.db.impl.query;\n");
123 content.append("\n");
124 content.append("import org.simantics.db.RelationInfo;\n");
125 content.append("import org.simantics.db.exception.DatabaseException;\n");
126 content.append("import org.simantics.db.impl.graph.ReadGraphImpl;\n");
127 content.append("import org.simantics.db.impl.procedure.InternalProcedure;\n");
128 content.append("import org.simantics.db.procedure.AsyncMultiProcedure;\n");
129 content.append("import org.simantics.db.procedure.AsyncProcedure;\n");
130 content.append("import org.simantics.db.procedure.ListenerBase;\n");
131 content.append("import org.simantics.db.request.AsyncMultiRead;\n");
132 content.append("import org.simantics.db.request.AsyncRead;\n");
133 content.append("import org.simantics.db.request.ExternalRead;\n");
134 content.append("import org.simantics.db.request.MultiRead;\n");
135 content.append("import org.simantics.db.request.Read;\n");
136 content.append("\n");
137 content.append("import gnu.trove.map.hash.TObjectIntHashMap;\n");
138 content.append("\n");
140 content.append("public class QueryCache extends QueryCacheBase {\n");
141 content.append("\n");
142 line(content,"public QueryCache(QuerySupport querySupport, int threads) {");
143 line(content," super(querySupport, threads);");
145 content.append("\n");
147 generateQuery(content, "Objects", signatureR2IP, true);
148 generateQuery(content, "Statements", signatureR2TIP, true);
149 generateQuery(content, "DirectObjects", signatureR2IP, true);
150 generateQuery(content, "RelationInfoQuery", signatureR1RelationInfo, true);
151 generateQuery(content, "URIToResource", signatureID1, true);
152 generateQuery(content, "ValueQuery", signatureR1Bytes, true);
153 generateQuery(content, "OrderedSet", signatureR1IP, true);
154 generateQuery(content, "PrincipalTypes", signatureR1IP, true);
155 generateQuery(content, "DirectPredicates", signatureR1IntSet, true);
156 generateQuery(content, "Predicates", signatureR1IntSet, true);
157 generateQuery(content, "ReadEntry", signatureRead, true);
158 generateQuery(content, "AsyncReadEntry", signatureAsyncRead, true);
159 generateQuery(content, "Types", signatureR1IntSet, true);
160 generateQuery(content, "NamespaceIndex", signatureID2, true);
162 generateQuery(content, "AssertedStatements", signatureR2TIP, false);
163 generateQuery(content, "AssertedPredicates", signatureR1IP, false);
164 generateQuery(content, "DirectSuperRelations", signatureR1IP, false);
165 generateQuery(content, "SuperTypes", signatureR1IntSet, false);
166 generateQuery(content, "TypeHierarchy", signatureR1IntSet, false);
167 generateQuery(content, "SuperRelations", signatureR1IntSet, false);
168 generateQuery(content, "MultiReadEntry", signatureMultiRead, false);
169 generateQuery(content, "AsyncMultiReadEntry", signatureAsyncMultiRead, false);
170 generateQuery(content, "ExternalReadEntry", signatureExternalRead, false);
171 content.append("}\n");
172 FileUtils.writeFile(source, content.toString().getBytes());
173 } catch (MalformedURLException e) {
175 } catch (UnsupportedEncodingException e) {
177 } catch (IOException e) {
185 public static void main(String[] args) {
186 new CodeGen().generate();