1
2
16
17 package gate.creole;
18
19 import java.io.FileWriter;
20 import java.io.Writer;
21 import java.net.URL;
22 import java.util.*;
23
24 import junit.framework.Assert;
25
26 import com.hp.hpl.jena.daml.*;
27 import com.hp.hpl.jena.daml.common.DAMLModelImpl;
28 import com.hp.hpl.mesa.rdf.jena.common.RDFWriterFImpl;
29 import com.hp.hpl.mesa.rdf.jena.model.RDFWriter;
30
31 import gate.*;
32
33
34 public class RDFFormatExporter extends AbstractLanguageAnalyser {
35
36 private static final int DAML_EXPORT = 0;
37 private static final int RDF_EXPORT = 1;
38
39 private static final String[] EXPORT_FORMATS = {"DAML+OIL","RDF"};
40 private static final String[] EXPORT_EXTS = {"daml","rdf"};
41
42 private static final String ONTOGAZ_CLASS_FEATURE = "class";
43 private static final String ONTOGAZ_ONTOLOGY_FEATURE = "ontology";
44
45
46 private static final boolean DEBUG = false;
47
48 private int exportFormat;
49
50
51 private List exportedTypes = null;
52
53 private URL exportFilePath = null;
54
55 private URL ontologyLocation = null;
56
57 private String annotationSetName = null;
58
59 public RDFFormatExporter() {
60 }
61
62
63 public void setExportedTypes(List anExportedTypesList){
64 exportedTypes = anExportedTypesList;
65 }
67
68
69 public List getExportedTypes(){
70 return exportedTypes;
71 }
73
74 public void setExportFormat(String format){
75
76 Assert.assertTrue(format.equalsIgnoreCase(EXPORT_FORMATS[DAML_EXPORT]) ||
77 format.equalsIgnoreCase(EXPORT_FORMATS[RDF_EXPORT]));
78
79 if (format.equalsIgnoreCase(EXPORT_FORMATS[DAML_EXPORT])) {
80 this.exportFormat = DAML_EXPORT;
81 }
82 else if (format.equalsIgnoreCase(EXPORT_FORMATS[RDF_EXPORT])) {
83 this.exportFormat = RDF_EXPORT;
84 }
85 else {
86 Assert.fail();
87 }
88
89 }
91
92 public String getExportFormat() {
93 return EXPORT_FORMATS[this.exportFormat];
94 }
96
97 public void setExportFilePath(URL anExportFilePath){
98 exportFilePath = anExportFilePath;
99 }
101
102 public URL getExportFilePath(){
103 return exportFilePath;
104 }
106
107 public void setOntology(URL _ontologyLocation){
108 ontologyLocation = _ontologyLocation;
109 }
111
112 public URL getOntology(){
113 return ontologyLocation;
114 }
116
117 public String getAnnotationSetName() {
118 return annotationSetName;
119 }
121
122
123 public void setAnnotationSetName(String annotationSetName) {
124 this.annotationSetName = annotationSetName;
125 }
126
127
128 public gate.Resource init() throws ResourceInstantiationException {
129 return this;
130 }
132
133
134 public void execute() throws ExecutionException{
135
136 if(document == null) {
138 throw new ExecutionException("No document found to export in APF format!");
139 }
140
141
146
147
149 String exportFilePathStr = null;
150
151 if (exportFilePath == null) {
152 exportFilePathStr = new String(document.getSourceUrl().getFile() + "." +
153 EXPORT_EXTS[this.exportFormat]);
154 }
155 else {
156 exportFilePathStr = new String(exportFilePath.getPath()+
157 "/" +
158 document.getName() + "." +
159 EXPORT_EXTS[this.exportFormat]);
160 }
161 FileWriter writer = null;
164 try{
165 writer = new FileWriter(exportFilePathStr,false);
166 annotations2ontology(writer);
167 writer.flush();
168 writer.close();
169 }catch (Exception e){
170 throw new ExecutionException(e);
171 }
173 }
175 private void annotations2ontology(Writer output) throws Exception {
176
177 DAMLModel ontologyModel, instanceModel;
178 HashMap ontologies = new HashMap();
179 HashMap instanceMatches = new HashMap();
180 HashSet instanceNames = new HashSet();
181
182 ontologyModel = new DAMLModelImpl();
183 instanceModel = new DAMLModelImpl();
184
185 Assert.assertNotNull(ontologyModel);
186 Assert.assertNotNull(instanceModel);
187
188 DAMLOntology onto = instanceModel.createDAMLOntology("");
190 onto.prop_comment().addValue("autogenerated from GATE RDFFormatExporter");
191 onto.prop_versionInfo().addValue("1.0");
192
193 Assert.assertNotNull(this.ontologyLocation);
194 ontologyModel.read(this.ontologyLocation.toString());
195
196 HashMap ontologyMap = ontology2hashmap(ontologyModel);
198 Assert.assertNotNull(ontologyMap);
199
200 ontologies.put(this.ontologyLocation.toString(),ontologyMap);
203
204 if (null == ontologyModel) {
205 throw new ExecutionException("cannot read ontology");
206 }
207
208 HashMap defaultClasses = new HashMap((int)ontologyModel.size()/5);
209 Iterator itClasses = ontologyModel.listDAMLClasses();
210 while (itClasses.hasNext()) {
211 DAMLClass cls = (DAMLClass)itClasses.next();
212 String className = cls.getLocalName();
213 if (null != className) {
214 defaultClasses.put(className.toLowerCase(),cls);
215 }
216 }
217
218 AnnotationSet inputAs = (annotationSetName == null ||
220 annotationSetName.length() == 0) ?
221 document.getAnnotations() :
222 document.getAnnotations(annotationSetName);
223
224 Iterator itTypes = (exportedTypes == null || exportedTypes.size() == 0) ?
226 inputAs.getAllTypes().iterator() : exportedTypes.iterator();
227
228
231 while (itTypes.hasNext()) {
232
233 String type = (String)itTypes.next();
234 AnnotationSet as = this.document.getAnnotations().get(type);
235
236 if (null == as || true == as.isEmpty()) {
237 continue;
238 }
239
240 Iterator itAnnotations = as.iterator();
241 while (itAnnotations.hasNext()) {
242
243 Annotation ann = (Annotation)itAnnotations.next();
244 Assert.assertTrue(ann.getType().equals(type));
245
246 FeatureMap features = ann.getFeatures();
247 String annClass = (String)features.get(ONTOGAZ_CLASS_FEATURE);
248 String annOntology = (String)features.get(ONTOGAZ_ONTOLOGY_FEATURE);
249 DAMLClass damlClass = null;
250
251 if (null == annClass) {
252 if (defaultClasses.containsKey(ann.getType().toLowerCase())) {
255 damlClass = (DAMLClass)defaultClasses.get(ann.getType().toLowerCase());
258 Assert.assertNotNull(damlClass);
259 }
260 else {
261 continue;
262 }
263 }
264 else {
265 if (false == ontologies.containsKey(annOntology)) {
268
273 DAMLModel model = new DAMLModelImpl();
275 model.read(annOntology);
276
277 HashMap name2class = ontology2hashmap(model);
280 Assert.assertNotNull(name2class);
281
282 ontologies.put(annOntology,model);
284 }
285
286 damlClass = (DAMLClass)((HashMap)ontologies.get(annOntology)).get(annClass);
288 Assert.assertNotNull(damlClass);
289 }
290
291 String instanceName = this.document.getContent().getContent(
292 ann.getStartNode().getOffset(),
293 ann.getEndNode().getOffset())
294 .toString();
295 Assert.assertNotNull(instanceName);
296
297 if (instanceNames.contains(instanceName)) {
299 continue;
300 }
301
302 DAMLInstance annInstance = instanceModel.createDAMLInstance(damlClass,instanceName);
303 instanceNames.add(instanceName);
304
305 List matches = (List)ann.getFeatures().get("matches");
307 if (null != matches) {
308 if (instanceMatches.containsKey(matches)) {
310 DAMLInstance equivInstance = (DAMLInstance)instanceMatches.get(matches);
311
312 annInstance.prop_sameIndividualAs().add(equivInstance);
314 }
315 else {
316 instanceMatches.put(matches,annInstance);
318 }
319 }
320
321
322 } }
325 RDFWriter rdfWriter = new RDFWriterFImpl().getWriter("RDF/XML-ABBREV");
327 rdfWriter.setNsPrefix("gate",this.ontologyLocation.toString()+"#");
328 rdfWriter.write(instanceModel,output,null);
329 }
330
331 private HashMap ontology2hashmap(DAMLModel ontology) throws Exception {
332
333 HashMap result = null;
334
335 Assert.assertNotNull(ontology);
337
338
339 result = new HashMap((int)ontology.size()/5);
340
341 Iterator itClasses = ontology.listDAMLClasses();
343 while (itClasses.hasNext()) {
344 DAMLClass clazz = (DAMLClass)itClasses.next();
345 if (null != clazz.getLocalName()) {
347 result.put(clazz.getLocalName(),clazz);
348 }
349
350 }
351
352 return result;
353 }
354 }