comparison src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java @ 78:b32b176a8aad

make normalizations configurable (static).
author casties
date Thu, 02 Mar 2017 20:31:32 +0100
parents a59984fd3c3f
children b0aebac0780a
comparison
equal deleted inserted replaced
77:a59984fd3c3f 78:b32b176a8aad
33 private static Logger logger = Logger.getLogger(OM4StreamWriter.class); 33 private static Logger logger = Logger.getLogger(OM4StreamWriter.class);
34 34
35 private static int itemsPerPage = 500; 35 private static int itemsPerPage = 500;
36 36
37 /** Include normalized own-values. */ 37 /** Include normalized own-values. */
38 public static boolean includeNorm = true; 38 public static boolean includeNormalizations = true;
39 39
40 /** 40 /**
41 * Return the object's string representation or "null" if its null. 41 * Return the object's string representation or "null" if its null.
42 * 42 *
43 * @param s 43 * @param s
57 * 57 *
58 * @param fileName 58 * @param fileName
59 * @param ps 59 * @param ps
60 */ 60 */
61 public static void backupEntities(String fileName, PersistenceService ps) { 61 public static void backupEntities(String fileName, PersistenceService ps) {
62 writeEntsAndRels(fileName, ps, Node.TYPE_ABOX); 62 writeEntsAndRels(fileName, ps, Node.TYPE_ABOX, includeNormalizations);
63 } 63 }
64 64
65 /** 65 /**
66 * Saves all definitions in a XML file with the given fileName. 66 * Saves all definitions in a XML file with the given fileName.
67 * 67 *
68 * @param fileName 68 * @param fileName
69 * @param ps 69 * @param ps
70 */ 70 */
71 public static void backupDefinitions(String fileName, PersistenceService ps) { 71 public static void backupDefinitions(String fileName, PersistenceService ps) {
72 writeEntsAndRels(fileName, ps, Node.TYPE_TBOX); 72 writeEntsAndRels(fileName, ps, Node.TYPE_TBOX, false);
73 } 73 }
74 74
75 /** 75 /**
76 * Writes all entities of the given type and their relations to the XML file at fileName. 76 * Writes all entities of the given type and their relations to the XML file at fileName.
77 * 77 *
79 * 79 *
80 * @param fileName 80 * @param fileName
81 * @param ps 81 * @param ps
82 * @param type 82 * @param type
83 */ 83 */
84 private static void writeEntsAndRels(String fileName, PersistenceService ps, String type) { 84 private static void writeEntsAndRels(String fileName, PersistenceService ps, String type, boolean includeNorm) {
85 OutputStreamWriter out; 85 OutputStreamWriter out;
86 try { 86 try {
87 FileOutputStream fileOut = new FileOutputStream(fileName); 87 FileOutputStream fileOut = new FileOutputStream(fileName);
88 out = new OutputStreamWriter(fileOut, "UTF-8"); 88 out = new OutputStreamWriter(fileOut, "UTF-8");
89 XMLOutputFactory factory = XMLOutputFactory.newInstance(); 89 XMLOutputFactory factory = XMLOutputFactory.newInstance();
132 entities = ps.getEntityPage(Node.TYPE_TBOX, startRecord, itemsPerPage); 132 entities = ps.getEntityPage(Node.TYPE_TBOX, startRecord, itemsPerPage);
133 } 133 }
134 134
135 for (Entity ent : entities) { 135 for (Entity ent : entities) {
136 // write entity to XML 136 // write entity to XML
137 writeEntity(ent, writer, ps); 137 writeEntity(ent, writer, ps, includeNorm);
138 // add (source)relations to list 138 // add (source)relations to list
139 relList.addAll(ent.getSourceRelations()); 139 relList.addAll(ent.getSourceRelations());
140 140
141 counter++; 141 counter++;
142 /* if ((counter % 50) == 0) { 142 /* if ((counter % 50) == 0) {
156 * write relations (from list) 156 * write relations (from list)
157 */ 157 */
158 writer.writeStartElement(XMLUtil.RELATIONS); 158 writer.writeStartElement(XMLUtil.RELATIONS);
159 writer.writeAttribute("number", Integer.toString(relList.size())); 159 writer.writeAttribute("number", Integer.toString(relList.size()));
160 for (Relation rel : relList) { 160 for (Relation rel : relList) {
161 writeRelation(rel, writer); 161 writeRelation(rel, writer, includeNorm);
162 } 162 }
163 writer.writeEndElement(); 163 writer.writeEndElement();
164 164
165 // end file. 165 // end file.
166 writer.writeEndElement(); 166 writer.writeEndElement();
181 * 181 *
182 * @param rel 182 * @param rel
183 * @param writer 183 * @param writer
184 * @throws XMLStreamException 184 * @throws XMLStreamException
185 */ 185 */
186 private static void writeRelation(Relation rel, XMLStreamWriter writer) throws XMLStreamException { 186 private static void writeRelation(Relation rel, XMLStreamWriter writer, boolean includeNorm) throws XMLStreamException {
187 writer.writeStartElement(XMLUtil.RELATION); 187 writer.writeStartElement(XMLUtil.RELATION);
188 188
189 /* 189 /*
190 * write XML-attributes 190 * write XML-attributes
191 */ 191 */
210 * write OpenMind attributes of this relation as XML tags 210 * write OpenMind attributes of this relation as XML tags
211 */ 211 */
212 if (rel.getAttributes().size() > 0) { 212 if (rel.getAttributes().size() > 0) {
213 writer.writeStartElement(XMLUtil.ATTRIBUTES); 213 writer.writeStartElement(XMLUtil.ATTRIBUTES);
214 for (Attribute att : rel.getAttributes()) { 214 for (Attribute att : rel.getAttributes()) {
215 writeAttribute(att, writer); 215 writeAttribute(att, writer, includeNorm);
216 } 216 }
217 writer.writeEndElement(); 217 writer.writeEndElement();
218 } 218 }
219 219
220 /* 220 /*
233 * @param entity 233 * @param entity
234 * @param writer 234 * @param writer
235 * @param ps 235 * @param ps
236 * @throws XMLStreamException 236 * @throws XMLStreamException
237 */ 237 */
238 private static void writeEntity(Entity entity, XMLStreamWriter writer, PersistenceService ps) 238 private static void writeEntity(Entity entity, XMLStreamWriter writer, PersistenceService ps, boolean includeNorm)
239 throws XMLStreamException { 239 throws XMLStreamException {
240 240
241 writer.writeStartElement((entity.getType().equals(Node.TYPE_TBOX)) ? XMLUtil.DEFINITION : XMLUtil.ENTITY); 241 writer.writeStartElement((entity.getType().equals(Node.TYPE_TBOX)) ? XMLUtil.DEFINITION : XMLUtil.ENTITY);
242 242
243 if (entity.isLightweight()) { 243 if (entity.isLightweight()) {
266 * write OpenMind attributes of this entity as XML tags 266 * write OpenMind attributes of this entity as XML tags
267 */ 267 */
268 if (entity.getAttributes().size() > 0) { 268 if (entity.getAttributes().size() > 0) {
269 writer.writeStartElement(XMLUtil.ATTRIBUTES); 269 writer.writeStartElement(XMLUtil.ATTRIBUTES);
270 for (Attribute att : entity.getAttributes()) { 270 for (Attribute att : entity.getAttributes()) {
271 writeAttribute(att, writer); 271 writeAttribute(att, writer, includeNorm);
272 } 272 }
273 writer.writeEndElement(); 273 writer.writeEndElement();
274 } 274 }
275 275
276 /* 276 /*
289 } 289 }
290 290
291 writer.writeEndElement(); 291 writer.writeEndElement();
292 } 292 }
293 293
294 private static void writeAttribute(Attribute att, XMLStreamWriter writer) throws XMLStreamException { 294 private static void writeAttribute(Attribute att, XMLStreamWriter writer, boolean includeNorm) throws XMLStreamException {
295 writer.writeStartElement(XMLUtil.ATTRIBUTE); 295 writer.writeStartElement(XMLUtil.ATTRIBUTE);
296 296
297 /* 297 /*
298 * write XML attributes 298 * write XML attributes
299 */ 299 */