1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.openrdf.elmo.dynabean.helpers;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.concurrent.ConcurrentMap;
39
40 import org.apache.commons.beanutils.DynaProperty;
41 import org.openrdf.model.URI;
42
43
44
45
46
47
48
49
50 public class DynaClassInfo implements OntologyDigester {
51
52 private Collection<URI> rdfTypes;
53
54 private ConcurrentMap<String, URI> predicates = new ConcurrentHashMap<String, URI>();
55
56 private ConcurrentMap<String, Boolean> functional = new ConcurrentHashMap<String, Boolean>();
57
58 private ConcurrentMap<String, List<URI>> predicateRdfTypes = new ConcurrentHashMap<String, List<URI>>();
59
60 private ConcurrentMap<String, DynaProperty> properties = new ConcurrentHashMap<String, DynaProperty>();
61
62 public DynaClassInfo(Collection<URI> rdfTypes) {
63 this.rdfTypes = rdfTypes;
64 }
65
66 public String getName() {
67 return toString();
68 }
69
70 public Collection<URI> getRdfTypes() {
71 return rdfTypes;
72 }
73
74 public Collection<URI> getPredicates() {
75 return predicates.values();
76 }
77
78 public void addPredicate(URI pred) {
79 predicates.put(pred.getLocalName(), pred);
80 }
81
82 public void setFunctional(URI pred, boolean functional) {
83 if (functional)
84 this.functional.put(pred.getLocalName(), Boolean.TRUE);
85 }
86
87 public List<URI> getRdfTypes(URI pred) {
88 return predicateRdfTypes.get(pred.getLocalName());
89 }
90
91 public void setRdfTypes(URI pred, List<URI> rdfTypes) {
92 predicateRdfTypes.put(pred.getLocalName(), rdfTypes);
93 }
94
95 public void addRdfType(URI pred, URI rdfType) {
96 String key = pred.getLocalName();
97 List<URI> rdfTypes = new ArrayList<URI>();
98 List<URI> o = predicateRdfTypes.putIfAbsent(key, rdfTypes);
99 if (o != null) {
100 rdfTypes = o;
101 }
102 rdfTypes.add(rdfType);
103 }
104
105 public void setIndexed(URI pred) {
106 if (isFunctional(pred.getLocalName())) {
107 DynaProperty dp = new DynaProperty(pred.getLocalName(), List.class);
108 properties.put(pred.getLocalName(), dp);
109 }
110 }
111
112 public void setMapped(URI pred) {
113 if (isFunctional(pred.getLocalName())) {
114 DynaProperty dp = new DynaProperty(pred.getLocalName(), Map.class);
115 properties.put(pred.getLocalName(), dp);
116 }
117 }
118
119 public void end() {
120 for (String property : predicates.keySet()) {
121 if (!properties.containsKey(property)) {
122 DynaProperty dp;
123 if (isFunctional(property))
124 dp = new DynaProperty(property);
125 else
126 dp = new DynaProperty(property, Set.class);
127 properties.put(property, dp);
128 }
129 }
130 }
131
132 public DynaProperty getDynaProperty(String property) {
133 if (properties.containsKey(property))
134 return properties.get(property);
135 throw new IllegalArgumentException("Invalid property: " + property
136 + ". Valid properties are "
137 + new ArrayList<String>(properties.keySet()).toString());
138 }
139
140 public DynaProperty[] getDynaProperties() {
141 return properties.values().toArray(new DynaProperty[properties.size()]);
142 }
143
144 public URI getPredicate(String property) {
145 if (predicates.containsKey(property))
146 return predicates.get(property);
147 throw new IllegalArgumentException("Invalid property: " + property
148 + ". Valid properties are "
149 + new ArrayList<String>(properties.keySet()).toString());
150 }
151
152 public boolean isFunctional(String property) {
153 return functional.containsKey(property);
154 }
155
156 public Collection<URI> getRdfTypes(String property) {
157 return predicateRdfTypes.get(property);
158 }
159
160 @Override
161 public boolean equals(Object obj) {
162 if (this == obj)
163 return true;
164 if (!(obj instanceof DynaClassInfo))
165 return false;
166 DynaClassInfo o = (DynaClassInfo) obj;
167 return rdfTypes.equals(o.rdfTypes);
168 }
169
170 @Override
171 public int hashCode() {
172 return rdfTypes.hashCode();
173 }
174
175 @Override
176 public String toString() {
177 Iterator<URI> i = rdfTypes.iterator();
178 if (rdfTypes.size() == 1)
179 return i.next().toString();
180 StringBuilder buf = new StringBuilder();
181 boolean hasNext = i.hasNext();
182 while (hasNext) {
183 String o = i.next().toString();
184 buf.append(String.valueOf(o));
185 hasNext = i.hasNext();
186 if (hasNext)
187 buf.append(";");
188 }
189 return buf.toString();
190 }
191
192 }