Source code: org/bdgp/apps/dagedit/dataadapter/DefaultIDAdapter.java
1 package org.bdgp.apps.dagedit.dataadapter;
2
3 import org.bdgp.apps.dagedit.datamodel.*;
4 import java.util.*;
5 import java.lang.reflect.*;
6 import org.bdgp.io.*;
7 import org.bdgp.util.RangeHash;
8
9 public class DefaultIDAdapter implements DEDataAdapterI {
10
11 Term currentRoot = null;
12 protected static Vector types;
13 protected static Vector categories;
14
15 protected static TermRelationshipType ISA_TYPE =
16 new TermRelationshipType("ISA", "is a");
17 protected static TermRelationshipType PARTOF_TYPE =
18 new TermRelationshipType("PARTOF", "part of");
19
20 public Vector getRelationshipTypes() {
21 return (Vector) types.clone();
22 }
23
24 public Vector getTermCategories() throws DataAdapterException {
25 return categories;
26 }
27
28 public DefaultIDAdapter() {
29 types = new Vector();
30 types.addElement(ISA_TYPE);
31 types.addElement(PARTOF_TYPE);
32 categories = new Vector();
33 }
34
35 public void delegateToIDAdapter(DEDataAdapterI idAdapter) {
36 // meaningless for this adapter
37 }
38
39 public String [] getIDs(Term root, Term term, String prefix, int length,
40 int count)
41 throws DataAdapterException {
42 return getIDs(root, term, prefix, 0, Integer.MAX_VALUE, length, count);
43 }
44
45 public String [] getIDs(Term root, Term term, String prefix,
46 int minID, int maxID, int length, int count)
47 throws DataAdapterException {
48
49 Enumeration e = root.getAllDescendants(true);
50 Vector idsV = new Vector();
51 boolean idGenerated = false;
52 while(e.hasMoreElements()) {
53 Term t = (Term) e.nextElement();
54 int id;
55 try {
56 id = Integer.parseInt(t.getIDSuffix());
57 } catch (Exception ex) {
58 id = -1;
59 }
60 if (id >= minID && id <= maxID)
61 idsV.addElement(new Integer(id));
62
63 for(int i=0; i < t.getSynonyms().size(); i++) {
64 Synonym s = (Synonym) t.getSynonyms().elementAt(i);
65 if (s.getID() != null) {
66 int colonLoc = s.getID().indexOf(":");
67 if (colonLoc >= 0) {
68 String trailingID = s.getID().substring(colonLoc+1);
69 id = Integer.parseInt(trailingID);
70 if (id >= minID && id <= maxID)
71 idsV.addElement(new Integer(id));
72 }
73 }
74 }
75
76 for(int i=0; i < t.getDanglingParents().size(); i++) {
77 DanglingRelationship dr = (DanglingRelationship)
78 t.getDanglingParents().elementAt(i);
79 int colonLoc = dr.getID().indexOf(":");
80 if (colonLoc >= 0) {
81 String trailingID = dr.getID().substring(colonLoc+1);
82 id = Integer.parseInt(trailingID);
83 if (id >= minID && id <= maxID)
84 idsV.addElement(new Integer(id));
85 }
86 }
87 }
88
89 org.bdgp.util.VectorUtil.sort(idsV, new org.bdgp.util.Comparator() {
90 public int compare(Object a, Object b) {
91 Integer ai = (Integer) a;
92 Integer bi = (Integer) b;
93 int compVal = ai.compareTo(bi);
94 if (compVal < 0)
95 return org.bdgp.util.Comparator.LESS_THAN;
96 else if (compVal > 0)
97 return org.bdgp.util.Comparator.GREATER_THAN;
98 else
99 return org.bdgp.util.Comparator.EQUAL_TO;
100 }
101 });
102
103 int firstID = minID;
104 int createdCount = 0;
105 Vector out = new Vector();
106 for(int i=0; i < idsV.size(); i++) {
107 int id = ((Integer) idsV.elementAt(i)).intValue();
108 int slots = id - firstID;
109 for(int j=0; j < slots; j++) {
110 out.addElement(Term.formatID(prefix, firstID, length));
111 createdCount++;
112 firstID++;
113 if (createdCount >= count)
114 break;
115 }
116 if (createdCount >= count)
117 break;
118 firstID = id+1;
119 }
120
121 for( ; firstID <= maxID && createdCount < count;
122 firstID++, createdCount++)
123 out.addElement(Term.formatID(prefix, firstID, length));
124
125 String [] ids = new String[out.size()];
126 for(int i=0; i < out.size(); i++)
127 ids[i] = (String) out.elementAt(i);
128 return ids;
129 }
130
131 public String getName() {
132 return "GO Default ID Adapter";
133 }
134
135 public String getType() {
136 return "GO IDs";
137 }
138
139 public IOOperation [] getSupportedOperations () {
140 IOOperation [] supported = new IOOperation[1];
141 supported[0] = DEDataAdapterI.GET_ID;
142 return supported;
143 }
144
145 public void init() {
146 // do nothing
147 }
148
149 // ----------------------------------------------------------------
150 // Filling out the interface
151
152 public DEEditHistory write(DEEditHistory term)
153 throws DataAdapterException {
154 throw new DataAdapterException("Write operation not supported");
155 }
156
157 public DEEditHistory getRoot() throws DataAdapterException {
158 throw new DataAdapterException("Write operation not supported");
159 }
160
161 public Vector getHistories() throws DataAdapterException {
162 throw new DataAdapterException("Not supported");
163 }
164
165 public Properties getStateInformation() {
166 return null;
167 }
168
169 public void setStateInformation(Properties props) {
170 // do nothing
171 }
172 }