]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/writer/AbstractDelayedGraphWriter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / writer / AbstractDelayedGraphWriter.java
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.layer0.utils.writer;\r
13 \r
14 import gnu.trove.list.array.TIntArrayList;\r
15 import gnu.trove.map.hash.TIntObjectHashMap;\r
16 import gnu.trove.map.hash.TObjectIntHashMap;\r
17 \r
18 import java.util.ArrayList;\r
19 \r
20 import org.eclipse.core.runtime.IProgressMonitor;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.Session;\r
24 import org.simantics.db.WriteOnlyGraph;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 \r
27 public abstract class AbstractDelayedGraphWriter extends GraphWriterPartial {\r
28 \r
29         protected int current = 0;\r
30         int externalCount = 0;\r
31         protected int internalCount = 0;\r
32         ArrayList<Resource> externals = new ArrayList<Resource>();\r
33         protected TObjectIntHashMap<Resource> externalsInv = new TObjectIntHashMap<Resource>();\r
34         protected TIntObjectHashMap<Resource> inverses = new TIntObjectHashMap<Resource>();\r
35         TIntArrayList timestamps = new TIntArrayList();\r
36         long[] resourceIds;\r
37         int time = 0;\r
38         \r
39         public long getResourceId(Session session, Resource r) {\r
40                 if(r instanceof InternalResource) {\r
41                         if(resourceIds == null) {\r
42                                 System.out.println("ERROR. Requesting resource id for new resource in writer before ids have been assigned.");\r
43                                 return 0;\r
44                         }\r
45                         return resourceIds[((InternalResource)r).id-1];\r
46                 } else {\r
47                         return r.getResourceId();\r
48                 }\r
49         }\r
50 \r
51         protected static class InternalResource implements Resource {\r
52         \r
53                         int id;\r
54                         \r
55                         public InternalResource(int id) {\r
56                                 this.id = id;\r
57                         }\r
58         \r
59                         @Override\r
60                         public long getResourceId() {\r
61                                 return (long)id;\r
62                         }\r
63         \r
64                         @Override\r
65                         public Resource get() {\r
66                                 return this;\r
67                         }\r
68                         \r
69                         @Override\r
70                         public boolean isPersistent() {\r
71                                 return false;\r
72                         }\r
73                         \r
74                         @Override\r
75                         public boolean equals(Object obj) {\r
76                                 return this==obj || \r
77                                         (obj instanceof InternalResource && \r
78                                           ((InternalResource)obj).id == id);\r
79                         }\r
80 \r
81                         @Override\r
82                         public boolean equalsResource(Resource other) {\r
83                                 return equals(other);\r
84                         }\r
85                         \r
86                         @Override\r
87                         public int hashCode() {\r
88                                 return id;\r
89                         }\r
90                         \r
91                         @Override\r
92                         public int getThreadHash() {\r
93                                 return id>>>16;\r
94                         }\r
95 \r
96                         @Override\r
97                         public int compareTo(Resource o) {\r
98                                 if(this==o)\r
99                                         return 0;\r
100                                 if(o instanceof InternalResource)\r
101                                         return id - ((InternalResource)o).id;\r
102                                 else\r
103                                         return -1;\r
104                         }\r
105                         \r
106                 }\r
107 \r
108         public AbstractDelayedGraphWriter(ReadGraph graph) {\r
109                 super(graph);\r
110         }\r
111 \r
112         @Override\r
113         public GraphWriter create() {\r
114                 current = ++internalCount;\r
115                 timestamps.add(0);\r
116                 return this;\r
117         }\r
118 \r
119         @Override\r
120         public Resource get() {\r
121                 if(current > 0)\r
122                         return new InternalResource(current);\r
123                 else if(current < 0)\r
124                         return externals.get(-1-current);\r
125                 else\r
126                         return null;\r
127         }\r
128 \r
129         protected int getId(Resource r) {\r
130                 if(r instanceof InternalResource)\r
131                         return ((InternalResource)r).id;\r
132                 int id = externalsInv.get(r);\r
133                 if(id==0) {\r
134                         id = -(++externalCount);\r
135                         externals.add(r);\r
136                         externalsInv.put(r, id);\r
137                 }\r
138                 return id;                      \r
139         }       \r
140 \r
141         @Override\r
142         public GraphWriter handle(Resource s) {\r
143                 current = getId(s);\r
144                 return this;\r
145         }\r
146 \r
147         protected int getPredicateId(Resource p) throws DatabaseException {\r
148                 int pId = getId(p);\r
149                 if(!inverses.contains(pId))\r
150                         if(!(p instanceof InternalResource))\r
151                                 inverses.put(pId, graph.getInverse(p));\r
152                 return pId;\r
153         }\r
154         \r
155         public abstract void commit(IProgressMonitor monitor, WriteOnlyGraph wg) throws DatabaseException;\r
156 \r
157 }