📄 swoopmodel.java
字号:
public String getTimeStamp() { Calendar cal = Calendar.getInstance(TimeZone.getDefault()); String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT); sdf.setTimeZone(TimeZone.getDefault()); String ts = sdf.format(cal.getTime()).toString(); if (ts.equals(previousTimeStamp)) { // incr. ts by 1s int s = Integer.parseInt(ts.substring(ts.lastIndexOf(":")+1, ts.length())); s++; ts = ts.substring(0, ts.lastIndexOf(":")) + ":" + String.valueOf(s); } previousTimeStamp = ts; return ts; } /** * Load RDF/XML from a reader directly with the given uri used as * the xml:base. Calls overriden method with the default setting for * 'importing' - true. i.e. Imported ontologies are parsed in as well. * @param reader * @param uri * @return */ public OWLOntology loadOntologyInRDF(Reader reader, URI uri) throws OWLException { return this.loadOntologyInRDF(reader, uri, true); } /** * Load RDF/XML from a reader directly with the given uri used as * the xml:base * * @param reader Source of the ontology * @param uri Logical URI used as xml:base * @return Loaded ontology */ public OWLOntology loadOntologyInRDF(Reader reader, URI uri, boolean importing) throws OWLException { OWLOntology ontology = null; OWLRDFParser parser = new OWLRDFParser(); parser.setImporting(importing); parser.setOWLRDFErrorHandler(new OWLRDFErrorHandler() { public void owlFullConstruct(int code, String message) throws SAXException { } public void error(String message) throws SAXException { throw new SAXException(message.toString()); } public void warning(String message) throws SAXException { System.out.println("RDFParser: " + message.toString()); } public void owlFullConstruct(int code, String message, Object obj) throws SAXException { } }); OWLConnection connection = new OWLConnectionImpl(); parser.setConnection(connection); // logging parse time // System.out.println("parsing ontology in RDF: start time-- // "+Calendar.getInstance().get(Calendar.HOUR)+":"+Calendar.getInstance().get(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND)+":"+Calendar.getInstance().get(Calendar.MILLISECOND)); // PARSE THE ONTOLOGY! if (reader != null) ontology = parser.parseOntology(reader, uri); else ontology = parser.parseOntology(uri); // System.out.println("parsing ontology in RDF: end time-- // "+Calendar.getInstance().get(Calendar.HOUR)+":"+Calendar.getInstance().get(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND)+":"+Calendar.getInstance().get(Calendar.MILLISECOND)); return ontology; } /** * Remove all changes in SwoopModel (uncommitted and committed lists) * that are associated with the ontology object passed to it * @param reloadOnt - ontology whose changes are removed from SwoopModel */ public void removeChanges(OWLOntology reloadOnt) { try { // also remove any changes in ontologyChanges and allChangeLog pertaining to reloadOnt List newOntologyChanges = new ArrayList(); Iterator iter = uncommittedChanges.iterator(); while (iter.hasNext()) { OntologyChange change = (OntologyChange) iter.next(); if (!change.getOntology().getURI().equals(reloadOnt.getURI())) { newOntologyChanges.add(change); } } uncommittedChanges.clear(); uncommittedChanges.addAll(newOntologyChanges); List newAllChangeLog = new ArrayList(); iter = committedChanges.iterator(); while (iter.hasNext()) { OntologyChange change = (OntologyChange) iter.next(); URI changeURI = null; if (change.getOntology()!=null) changeURI = change.getOntology().getURI(); else { if (change instanceof SaveCheckpointChange) changeURI = ((SaveCheckpointChange) change).getOntologyURI(); else if (change instanceof RevertCheckpointChange) changeURI = ((RevertCheckpointChange) change).getCheckpointChange().getOntologyURI(); } if (!changeURI.equals(reloadOnt.getURI())) { newAllChangeLog.add(change); } } committedChanges.clear(); committedChanges.addAll(newAllChangeLog); notifyListeners(new ModelChangeEvent(this, ModelChangeEvent.RESET_CHANGE, reloadOnt)); } catch (OWLException e) { e.printStackTrace(); } } /** * Reload an OWL Ontology in Swoop - replace the current entry in swoopModel * with the argument passed to the method. * * @param reloadOnt - new ontology to be reloaded * @param resetChanges - if true, remove all changes corresponding to new ontology */ public void reloadOntology(OWLOntology reloadOnt, boolean resetChanges) { try { // remove entry from hashtable OWLOntology existingOnt = (OWLOntology) ontologies.get(reloadOnt.getURI()); //System.out.println("found existing "+existingOnt.getURI()); ontologies.remove(existingOnt); // make it the selected ontology only if the one it replaced was selected if (selectedOntology==existingOnt) selectedOntology = reloadOnt; // put new ontology in hashtable ontologies.put(reloadOnt.getURI(), reloadOnt); // need to reclassify ontology in reasoner reasoner.setOntology(reloadOnt); if (resetChanges) { this.removeChanges(reloadOnt); } notifyListeners(new ModelChangeEvent(this, ModelChangeEvent.ONTOLOGY_RELOADED, reloadOnt)); } catch (OWLException e) { e.printStackTrace(); } } /** * @return Returns the changeSharingMethod. */ public int getChangeSharingMethod() { return changeSharingMethod; } /** * @param changeSharingMethod The changeSharingMethod to set. */ public void setChangeSharingMethod(int changeSharingMethod) { this.changeSharingMethod = changeSharingMethod; } /** * Replace an entity in the ontology with a new version represented by its RDF/XML code definition * This is also a String operation since the OWL-API has no clean way of doing this. * It serializes the entire ontology in RDF/XML except for the entity definition, then inserts * the new entity definition at the end and parses the RDF/XML back into a new ontology * @param ont * @param entity * @param newEntityRDFCode * @return */ public boolean replaceEntityRDF(OWLOntology ont, OWLEntity entity, String newEntityRDFCode) { try { // get rdf for oldEntity StringWriter st = new StringWriter(); RDFXMLEntityRenderer rdfRend = new RDFXMLEntityRenderer(); rdfRend.setEditorEnabled(true); rdfRend.render(entity, this, st); String oldEntity = st.toString(); // parse both oldEntity and newEntity rdf into two separate OWLOntologies StringReader reader = new StringReader(oldEntity); OWLOntology source = this.loadOntologyInRDF(reader, ont.getURI()); StringReader reader2 = new StringReader(newEntityRDFCode); OWLOntology destination = this.loadOntologyInRDF(reader2, ont.getURI()); if (source==null || destination==null) return false; List diff = FilteredDiff.getChanges(source, destination, ont); this.addUncommittedChanges(diff); this.applyOntologyChanges(); return true; } catch (OWLException ex) { ex.printStackTrace(); return false; } } /* * clone an OWL Ontology object (by serializing & parsing RDF) */ public OWLOntology cloneOntology(OWLOntology source) { OWLOntology copy = null; try { CorrectedRDFRenderer rdfRend = new CorrectedRDFRenderer(); StringWriter st = new StringWriter(); rdfRend.renderOntology(source, st); copy = this.loadOntologyInRDF(new StringReader(st.toString()), source.getURI(), true); } catch (Exception ex) { ex.printStackTrace(); } return copy; } public boolean replaceEntityAS(OWLOntology ont, String newEntityASCode) { try {// get rdf for oldEntity StringWriter st = new StringWriter(); AbstractSyntaxEntityRenderer rdfRend = new AbstractSyntaxEntityRenderer(); rdfRend.setEditorEnabled(true); rdfRend.render(this.selectedEntity, this, st); String oldEntity = st.toString(); System.out.println("newEntityASCode:\n"+newEntityASCode); System.out.println("oldEntity:\n"+oldEntity); // parse both oldEntity and newEntity AS into two separate OWLOntologies StringReader reader = new StringReader(oldEntity); OWLOntology ont1 = this.loadOntologyInAbstractSyntax(reader, ont.getURI()); StringReader reader2 = new StringReader(newEntityASCode); OWLOntology ont2 = this.loadOntologyInAbstractSyntax(reader2, ont.getURI()); if (ont2==null) return false; List diff = FilteredDiff.getChanges(ont1, ont2, ont); //this.useOWLDifferentiator(ont, this.selectedEntity, ont, ont2); this.addUncommittedChanges(diff); this.applyOntologyChanges(); return true; } catch (OWLException ex) { ex.printStackTrace(); } return false; } public String getFontSize() { return this.fontSize; } public void setFontSize(String size) { this.fontSize = size; // also refresh display to show effect notifyListeners(new ModelChangeEvent(this, ModelChangeEvent.FONT_CHANGED, selectedOntology)); } public String getTreeThreshold() { return this.treeThreshold; } public void setTreeThreshold(String threshold) { this.treeThreshold = threshold; } public boolean isHighlightCode() { return highlightCode; } public void setHighlightCode(boolean highlightCode) { this.highlightCode = highlightCode; } /** * Tries to load bookmarks for three different places: * 1) From the file 'favorites.txt' in the CWD * 2) From the Preferences API * 3) From the file 'favorites.txt' in the directory relative to SwoopModel.class * Return the bookmarks from the preferences as a reader. * If the bookmarks aren't stored there yet, return * the book marks store relative to this class ('favorites.txt') * @return */ public BufferedReader getBookmarks() { BufferedReader reader = null; File file = new File("favorites.txt"); if (file.exists()) { try { reader = new BufferedReader(new FileReader(file)); } catch (IOException ex) { System.out.println("Can't load local favorites.txt"); ex.printStackTrace(); } } if (reader == null) { String bookmarks = preferences.get("bookmarks", null); if (bookmarks == null) { InputStream is = SwoopModel.class.getResourceAsStream("favorites.txt"); reader = new BufferedReader(new InputStreamReader(is)); } else { reader = new BufferedReader(new StringReader(bookmarks)); } } return reader; } public void saveBookmarks(String bookmarks) { preferences.put("bookmarks", bookmarks); File file = new File("favorites.txt"); if (file.exists()) { try { FileWriter writer = new FileWriter(file); writer.write(bookmarks); writer.close(); } catch (IOException e) { System.out.println("Couldn't save favorites in the current working directory."); e.printStackTrace(); } } } /** * Load Swoop Preferences that have been saved to File "preferences" * Do this at startup. Call setPreferences(..) after reading the local file * and creating the necessary preferences HashMap */ public void loadPreferences() { File prefFile = new File("preferences"); try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(prefFile)); Map prefs = (HashMap) in.readObject(); this.setPreferences(prefs); // also load ontology settings (imports/qnames/reasoner) this.ontologySettings = (HashMap) in.readObject(); // finally load last saved works
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -