versionedontology.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,850 行 · 第 1/5 页

JAVA
1,850
字号
						if (before.equals(after) || !check) {
							// nothing has changed!
							System.out.println("Found redundant change: "+oc);
							swc.isRedundant = true;
							testSuccess = false;
							failCount++;
						}
						else swc.isRedundant = false;
					}
					catch (Exception ex) {
						swc.isRedundant = true;
						System.out.println("Change Error for: "+oc);
						testSuccess = false;
						failCount++;
						ex.printStackTrace();
					}
				}								
			}
			catch (Exception ex) {
				ex.printStackTrace();
				testSuccess = false;
			}
			
			if (testSuccess) statusBar.setText(status+"Test PASSED");
			else {
				statusBar.setText(status+"Test FAILED");
				JOptionPane.showMessageDialog(this, "Change Test FAILED: "+failCount+" change(s) is/are redundant or cause clashes. Please Fix/Remove them and try again.");
			}
			
			// refresh ontTreeTable
			this.refreshOntTreeTable();
			return testSuccess;	
		}
		
	}
	
	/*
	 * Perform an update on the Repository. This is the only place
	 * in the code which updates versions from the repository
	 * and saves them in the local data structure versionCommits
	 */
	private boolean updateRepChanges(boolean sync) {
		
		try {				
			// get repository header since we need count of versions 
			// i.e. headVersionNum
			String status = "Status: [ACTION - Update Repository]...";
			
			statusBar.setText(status+"Loading repository header to find headVersionNumber");
			boolean existsRep = this.loadRepositoryHeader();
			if (!existsRep) {
				if (DEBUG) System.out.println("NOT FOUND");
				// update from local Swoop log anyway if uri matches ontURI
				this.updateRepFromLog(new URI(this.repBaseOntFld.getText()));
				return false;
			}
			statusBar.setText(status+"HeadVersionNum="+this.headVersionNumber);
		
			// note: all version commits have been made to URLs:
			// repositoryURL+"#1"..."#headVersionNum"
			// so iterate through versionCommits and see if value is null, implying it hasn't been updated
			for (int ctr=1; ctr<=this.headVersionNumber; ctr++) {
				if (versionNodes[ctr]==null) {
					// form URI using ctr
					URI versionURI = new URI(this.repositoryURI+"#"+ctr);
					statusBar.setText(status+"Updating version at "+versionURI);
					Set commitSet = client.findAnnotations(versionURI);
					// get single Description (version) at URI
					Description version = (Description) commitSet.iterator().next();
					versionDescriptions[ctr] = version;
					TreeTableNode mainNode = this.parseSingleCommit(version);
					// set params on mainNode
					mainNode.swoopChange.isOnRepository = true;
					mainNode.location = version.getLocation();
					versionNodes[ctr] = mainNode;
				}
			}
			
			// also if advanced is off, update from local copy as well
//			if (!advanced) {
//				this.updateRepFromLog(new URI(this.repBaseOntFld.getText()));
//			}
			
			// resort all nodes under root
			this.refreshRepTreeTable(true);
			
			// update version repository cache
			swoopModel.updateVersionRepository(repositoryURI, versionDescriptions);
			
			// if sync is true, commit all changes to the synchronized ontology
			if (sync) {
				statusBar.setText(status+"Synching with Local Swoop Ontology");
				this.commitOntChanges(true);				
			}
			statusBar.setText(status+"DONE");
			return true;
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}
	
	/*
	 * Parse a single 'Commit' Description to obtain the main top node
	 * and all the SwoopChange nodes included in that commit 
	 * (put them as children of the main node)
	 */
	private TreeTableNode parseSingleCommit(Description commit) throws OWLException {

		// make the main annotation the top header
		SwoopChange main = new SwoopChange();
		main.setAuthor(commit.getAuthor());
		main.setCommitted(true); // not needed really
		main.isTopNode = true; /* this is key */
		main.setTimeStamp(commit.getCreated());
		main.setDescription(commit.getAnnotatedEntityDefinition());
		TreeTableNode mainNode = new TreeTableNode(main);
		
		// parse change set associated with single commit
		String changeSetString = commit.getBody();
		if (changeSetString!=null) {
			// decode CDATA stuff
			changeSetString = changeSetString.replaceAll("ENCODED-CDATA", "CDATA");
			changeSetString = changeSetString.replaceAll("]ENCODED]>", "]]>"); 
				
			// now split based on separator
			String[] changeStr = changeSetString.split(separator);
			
			// each split component is a separate change
			List repDesc = new ArrayList(); // list of Description (changes)
			for (int i=0; i<changeStr.length; i++) {
				String swcDesc = changeStr[i]; // SwoopChange mapped to a Description serialized in RDF/XML
				Description desc = new Description();
				desc.deserializeFromString(swcDesc, swoopModel);
				repDesc.add(desc);							
			}
			
			// transform Description list to SwoopChange list
			List swcChangeList = this.transformChangeList(repDesc);
			
			// add each swoopChange as a child of mainNode
			this.sortChanges(swcChangeList);
			for (Iterator iter2 = swcChangeList.iterator(); iter2.hasNext();) {
				SwoopChange swc = (SwoopChange) iter2.next();
				TreeTableNode swcNode = new TreeTableNode(swc);
				swcNode.swoopChange.isOnRepository = true;
				mainNode.addChild(swcNode);
			}
		}
		
		return mainNode;
	}
	
	/*
	 * Load repository header annotation
	 * and parse annotation to obtain repository information such as
	 * author, date, baseOntologyURI, and no of version commits
	 */
	private boolean loadRepositoryHeader() {
		try {
			// annotation with URI: repositoryURL + "#header"
			repositoryURI = new URI(repURLFld.getText());
			URI repHeaderURI = new URI(repositoryURI+"#header");
			
			if (client==null) this.initAnnoteaClient();
			
			// get annotation using Annotea
			Set headerSet = client.findAnnotations(repHeaderURI);
			if (headerSet.size()>0) {
				Description header = (Description) headerSet.iterator().next();
				versionDescriptions[0] = header;
				
				// parse header description to get author, date, baseOntologyURI
				this.repositoryAuthor = header.getAuthor();
				this.reposityCreatedDate = header.getCreated();
				this.baseOntologyURI = new URI(header.getAnnotatedEntityDefinition());
				// and headVersionNumber
				this.headVersionNumber = Integer.parseInt(header.getBody());
				
				// also get actual URL location of annotation
				this.repHeaderLoc = header.getLocation();
				
				// set UI accordingly
				this.repAuthorFld.setText(this.repositoryAuthor);
				this.repDateFld.setText(this.reposityCreatedDate);
				this.repBaseOntFld.setText(this.baseOntologyURI.toString());
				this.toggleRepOptions(false);
				System.out.println("Ontology Repository header exists at "+repHeaderURI);
				return true;
			}	
			
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}
	
	/*
	 * Transform a list of changes from SwoopChange -> Description objects
	 * or vice versa
	 */
	private List transformChangeList(List source) {
		
		List target = new ArrayList();
		for (int i=0; i<source.size(); i++) {
			Object change = source.get(i);
			if (change instanceof SwoopChange) {
				SwoopChange swc = (SwoopChange) change;
				// change SwoopChange to a Description object
				Description desc = new Description();
				desc.setAuthor(swc.getAuthor());
				desc.setCreated(swc.getTimeStamp());
				desc.setBody(swc.getDescription());
				desc.setBodyType("text/html");
				desc.setAnnotatedEntityDefinition(swc.comment);
				// create annotates URI from
				// 1. repository URL
				// 2. owlObjectURI
				// 3. extra URIs
				URI[] uris = new URI[2+swc.getExtraSubjects().size()];
				uris[0] = repositoryURI;
				uris[1] = swc.getOwlObjectURI();
				for (int j=0; j<swc.getExtraSubjects().size(); j++) {
					uris[j+2] = (URI) swc.getExtraSubjects().get(j);
				}
				desc.setAnnotates(uris);
				// attach single ontology change to description
				List chngList = new ArrayList();
				chngList.add(swc.getChange());
				desc.setOntologyChangeSet(chngList);				
				target.add(desc);
			}
			else if (change instanceof Description) {
				Description desc = (Description) change;
				// change Description to SwoopChange object
				SwoopChange swc = new SwoopChange();
				swc.setAuthor(desc.getAuthor());
				swc.setTimeStamp(desc.getCreated());
				swc.setDescription(desc.getBody());
				swc.comment = desc.getAnnotatedEntityDefinition();
				swc.setCommitted(true); // set committed
				// get URIs from desc for swc
				URI[] uris = desc.getAnnotates();
				swc.setOwlObjectURI(uris[1]);
				List extraSubjects = new ArrayList(); 
				for (int j=2; j<uris.length; j++) {
					extraSubjects.add(uris[j]);
				}
				swc.setExtraSubjects(extraSubjects);
				// get single ontology change object
				List chngList = desc.getOntologyChangeSet();
				swc.setChange((OntologyChange) chngList.iterator().next());
				target.add(swc);
			}
		}
		return target;		
	}
	
	/*
	 * Toggle UI enable/disable of Repository Information fields
	 */
	private void toggleRepOptions(boolean enable) {
		Color color = Color.WHITE;
		if (!enable) color = Color.LIGHT_GRAY;
		this.repAuthorFld.setBackground(color);
		this.repBaseOntFld.setBackground(color);
		this.repDateFld.setBackground(color);
		this.repAuthorFld.setEditable(enable);
		this.repBaseOntFld.setEditable(enable);
		this.repDateFld.setEditable(enable);
	}

	public void keyTyped(KeyEvent e) {
		if (e.getSource() == repURLFld) this.toggleRepOptions(true);
	}

	public void keyPressed(KeyEvent arg0) {
		
	}

	public void keyReleased(KeyEvent arg0) {
	}
	
	private void initAnnoteaClient() {
		if (Annotea.INSTANCE==null) Annotea.initializeAnnotea();
		try {
			client = new AnnoteaClient(new URL(serverURL), swoopModel);
			// also set annotType for all Description objects created here
			// to the Change type in Annotea
			Set annotTypes = Annotea.annotationTypes;
			for (Iterator iter = annotTypes.iterator(); iter.hasNext();) {
				OWLClass type = (OWLClass) iter.next();
				if (type.getURI().toString().toLowerCase().indexOf("change")>=0) {
					this.annotType = type;
				}
			}			
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * Used only for TESTING Purposes. Delete contents of repository
	 */
	private void deleteRepository() {
		try {
			
			int opt = JOptionPane.showConfirmDialog(this, "Delete entire Repository (all change sets) at "+ repURLFld.getText()+" ?", "Delete All", JOptionPane.YES_NO_OPTION);
			if (opt == JOptionPane.NO_OPTION) return;
			
			// delete repository header
			if (repHeaderLoc!=null) {
				client.delete(this.repHeaderLoc);
				System.out.println("Deleted Repository Header at " + repHeaderLoc);
			}
			else System.out.println("URL location of Repository Header not known");
			
			// delete all commits at Version Controlled Repository
			for (int ctr=1; ctr<=this.headVersionNumber; ctr++) {
				URL loc = null;
				if (versionNodes[ctr]!=null) {
					loc = versionNodes[ctr].location;										
				}
				else {
					URI versionURI = new URI(this.repositoryURI.toString()+"#"+String.valueOf(ctr));
					Set versionSet = client.findAnnotations(versionURI);
					loc = ((Description) versionSet.iterator().next()).getLocation();					
				}
				client.delete(loc);
				System.out.println("Deleted Version at "+loc);
			}			
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	/*
	 * Sort all changes in a changeList based on timestamp
	 */
	private void sortChanges(List changeList) {
		SortedSet changeSet = new TreeSet(ChangeComparator.INSTANCE);
		changeSet.addAll(changeList);
		changeList.clear();
		for (Iterator iter = changeSet.iterator(); iter.hasNext();) {
			changeList.add(iter.next());
		}
	}
	
	/*
	 * Refresh all topNodes (version commits) under (children of) repRoot	 * 
	 */
	private void refreshRepTreeTable(boolean refreshRoot) {
		
		if (refreshRoot) {
			repRoot.children = new Vector();
			for (int ctr=1; ctr<=this.headVersionNumber; ctr++) {
				repRoot.addChild(this.versionNodes[ctr]);
			}
			// add new commit node at the end
			repRoot.addChild(newCommitNode);
		}
		
		// expand the newCommit node
		JTree changeTree = repChangeTT.getTree();
		int rows = changeTree.getRowCount();
		changeTree.expandRow(rows-1);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?