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