versionedontology.java

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

JAVA
1,850
字号
	
	/**
	 * transfer selected changes from Swoop Ont list to Repository List
	 * or vice versa
	 */
	public void transferChanges(JTreeTable source, JTreeTable target) {
		
		// add to changes in target based on source
		JEditorPane changePane = null;
		List changes = null;
		boolean refreshOntTT = true;
		if (target == ontChangeTT) {
			changes = ontChanges;
			changePane = ontChangeEdPane;
		}
		else {
			refreshOntTT = false;
			changes = repChanges;
			changePane= repChangeEdPane;
		}
		
		// get selected changes in source
		if (source.getTree().getSelectionPath()==null) return;
		TreePath[] paths = source.getTree().getSelectionPaths();
		for (int i=0; i<paths.length; i++) {
			TreePath path = paths[i];
			TreeTableNode selNode = (TreeTableNode) path.getLastPathComponent();
			if (selNode.swoopChange.isTopNode) {
				// skip version top nodes, but transfer its children
				for (Iterator iter = selNode.children.iterator(); iter.hasNext();) {
					TreeTableNode childNode = (TreeTableNode) iter.next();
					SwoopChange swc = (SwoopChange) childNode.swoopChange;
					// check if it already exists in changes (prevent duplicates) 
					if (!isPresent(changes, swc)) changes.add(swc.clone());
				}
			}
			else { 
				SwoopChange swc = (SwoopChange) selNode.swoopChange;
				// check if it already exists in changes (prevent duplicates) 
				if (!isPresent(changes, swc)) changes.add(swc.clone());
			}
		}
		
		this.sortChanges(changes);
		
		// refresh UI of target after adding changes
		if (refreshOntTT) this.refreshOntTreeTable();
		else {
			// add (new) repChanges to newCommit node directly
			newCommitNode.children = new Vector();
			for (Iterator iter = changes.iterator(); iter.hasNext();) {
				SwoopChange swc = (SwoopChange) iter.next();
				TreeTableNode swcNode = new TreeTableNode(swc);
				newCommitNode.addChild(swcNode);	
			}
			this.refreshRepTreeTable(true);
		}
	}
	
	/*
	 * Check if a swoopChange is present in the changes list.
	 * Check if the author, ontologychange and timestamp matches
	 * since these 3 params determine uniqueness
	 */
	private boolean isPresent(List changes, SwoopChange check) {
		for (Iterator iter = changes.iterator(); iter.hasNext();) {
			SwoopChange swc = (SwoopChange) iter.next();
			if (swc.getAuthor().equals(check.getAuthor()) &&
				swc.getDescription().equals(check.getDescription()) && 
//				swc.getChange().equals(check.getChange()) &&
				swc.getTimeStamp().equals(check.getTimeStamp()))
				return true;
		}
		return false;
	}
	
	/***
	 * Make a commit to the selected repository
	 * 1. Check if the repository already exists
	 * 2. Check if all changes on the repository are in Swoop
	 * 3. Check that there are no clashes involving changes (testChanges(..))
	 * 4. Commit new version alone!
	 */
	private void commitRepChanges() {
		
		// get repository info
		String repURL = repURLFld.getText();
		String boURI = repBaseOntFld.getText();
		this.repositoryAuthor = repAuthorFld.getText();
		this.reposityCreatedDate = repDateFld.getText();
		
		String status = "Status: [ACTION - Commit to Repository]...";
		try {
			this.repositoryURI = new URI(repURL);
			this.baseOntologyURI = new URI(boURI);
		}
		catch (URISyntaxException ex) {
			statusBar.setText(status+"Invalid Repository URL and/or Base Ontology URI");
			return;
		}
		
		try {
			// check if repository already exists
			boolean repExists = this.loadRepositoryHeader();
			
			if (!repExists) {
				// creating new repository
				// form repository header
				URI[] headerURI = new URI[1];
				headerURI[0] = new URI(this.repositoryURI+"#header");
				
				if (this.repositoryAuthor.equals("") || this.reposityCreatedDate.equals("")) {
					int opt = JOptionPane.showConfirmDialog(null, "Repository Author and/or Date not specified. Continue?", "Creating Repository", JOptionPane.YES_NO_OPTION);
					if (opt == JOptionPane.NO_OPTION) return;
				}
				// create annotea description for header
				Description header = new Description();
				header.setAnnotates(headerURI);
				header.setAuthor(this.repositoryAuthor);
				header.setCreated(this.reposityCreatedDate);
				header.setAnnotatedEntityDefinition(this.baseOntologyURI.toString());
				this.headVersionNumber = 0;
				header.setBody(String.valueOf(this.headVersionNumber));
				header.setBodyType("text/html");
				header.setAnnotationType(this.annotType);
				client.post(header);
				statusBar.setText(status+"Ontology Repository Header posted at "+headerURI[0]);
				toggleRepOptions(false);
				versionDescriptions[0] = header;
				swoopModel.updateVersionRepository(repositoryURI, versionDescriptions);
			}
			
			// test changes
			boolean success = this.testChanges(true);
			if (success){
				// prompt user for comment on commit
				String commitComment = JOptionPane.showInputDialog(this, "COMMIT New Version (Details):");
				
				if (commitComment==null) {
					statusBar.setText(status+"CANCELLED");
					return;
				}
				// transform SwoopChange list to Description list (to save author, date, uris etc)
				List descList = transformChangeList(repChanges);
				// serialize each description into RDF/XML
				// and make it one large string with separator ("[SWOOP-ANNOTATED-CHANGE-SET]") in the middle
				String largeChangeSetString = "";
				for (Iterator iter = descList.iterator(); iter.hasNext();) {
					Description desc = (Description) iter.next();
					String descStr = desc.serializeIntoString(swoopModel);					
					largeChangeSetString += descStr + separator;
				}
				
				// ENCODE CDATA
				largeChangeSetString = largeChangeSetString.replaceAll("CDATA", "ENCODED-CDATA");
				largeChangeSetString = largeChangeSetString.replaceAll("]]>", "]ENCODED]>");
				
				// finally commit a single annotation with the entire changeset string in the body
				// also allow author to make annotation on commit
				Description commit = new Description();
				commit.setAuthor(swoopModel.getUserName());
				commit.setCreated(swoopModel.getTimeStamp());
				commit.setBody(largeChangeSetString);
				commit.setBodyType("text/html");
				commit.setAnnotatedEntityDefinition(commitComment);
				commit.setAnnotationType(this.annotType);
				
				// increment headVersionNum and write new commit
				// at repURL+"#"+headVersionNum
				this.headVersionNumber++;
				URI[] annotates = new URI[1];
				annotates[0] = new URI(repositoryURI.toString()+"#"+String.valueOf(this.headVersionNumber));
				commit.setAnnotates(annotates);
				// COMMIT!
				client.post(commit);
				
				// post-process:
				// 1. rewrite rep header added newly incremented headVersionNumber 
				this.rewriteRepHeader();
				// 2. set newCommitNode params to current commit
				newCommitNode.swoopChange.setAuthor(commit.getAuthor());
				newCommitNode.swoopChange.setTimeStamp(commit.getCreated());
				newCommitNode.swoopChange.setDescription(commit.getAnnotatedEntityDefinition());
				// 3. save newCommitNode in versionCommits array and Description in versionDescriptions array
				versionNodes[this.headVersionNumber] = newCommitNode;
				versionDescriptions[this.headVersionNumber] = commit;
				swoopModel.updateVersionRepository(repositoryURI, versionDescriptions);
				// 4. for each child of newCommitNode, set its swoopChange.onRepository value to true
				for (Iterator iter3=newCommitNode.children.iterator(); iter3.hasNext();) {
					TreeTableNode child = (TreeTableNode) iter3.next();
					child.swoopChange.isOnRepository = true;
				}
				// 5. create newCommitNode and add to root at the end
				newCommitNode = new TreeTableNode(new SwoopChange(swoopModel.getUserName(), null, null, swoopModel.getTimeStamp(), "New Version Commit", true, false));
				newCommitNode.swoopChange.isTopNode = true;
				repRoot.addChild(newCommitNode);
				repChanges = new ArrayList(); // also clear this
				statusBar.setText(status+"Committed New Version "+this.headVersionNumber);
			}			
		}
		catch (Exception ex) {
			ex.printStackTrace();
			statusBar.setText(status+"FAILED");
		}
		this.refreshRepTreeTable(false);
	}
	
	/*
	 * Test the changes in the ontChange list or repChange list
	 * i.e. Take the initial base ontology, apply changes,
	 * see if clash occurs, and remove all redundant changes (that do not cause change in ontology) 
	 * @return
	 */
	private boolean testChanges(boolean testRepository) {
		
		if (testRepository) {
			
			String status = "Status: [ACTION - Testing Repository Changes]...";
			
			// test changes in repository for clashes etc.
			statusBar.setText(status);
			boolean testSuccess = true;
			int failCount = 0;
			
			try {
				// do an update first to make sure rep-side changes are all there
				boolean updateSuccess = this.updateRepChanges(false);
				
				if (!updateSuccess) {
					statusBar.setText(status+"Update FAILED");
					return false;
				}
				
				// load baseOntology using URI
				statusBar.setText(status+"Loading Base Ontology");
				OWLOntology baseOntology = swoopModel.loadOntology(baseOntologyURI);
				if (baseOntology == null) {
					// there is no base ontology, changes start from scratch
					// create new ontology using OWLBuilder
					OWLBuilder builder = new OWLBuilder();
					builder.createOntology(baseOntologyURI, baseOntologyURI);
					baseOntology = builder.getOntology();
				}
				
				// get all repository side changes: under repRoot
				List allRepChanges = this.getDescendantChanges(repRoot);
				statusBar.setText(status+"Testing "+allRepChanges.size()+" repository-side changes");
				
				// apply allRepChanges to baseOntology
				for (int i=0; i<allRepChanges.size(); i++) {
					SwoopChange swc = (SwoopChange) allRepChanges.get(i);
					OntologyChange oc = swc.getChange();
					//*** need to align changes with ontology! ***/
					try {
						OntologyChange alignOC = (OntologyChange) controlHandler.swoopHandler.changeLog.getChangeInformation(oc, ChangeLog.CHANGE_ALIGN, null, new ArrayList(), baseOntology);
						
						// save state of baseOntology and see if it changes
						// use Abstract Syntax since its more concise than rdf/xml?
						String before = controlHandler.swoopHandler.changeLog.getOntSnapshot(baseOntology);
						
						// APPLY CHANGE
						boolean check = true;
						if (alignOC instanceof BooleanElementChange) { 
		    				check = swoopModel.applyBooleanElementChange(alignOC);
		    			}
		    			else if (alignOC instanceof EnumElementChange) { 
		    				check = swoopModel.applyEnumElementChange(alignOC);
		    			}
						else alignOC.accept((ChangeVisitor) baseOntology);
						
						// need to see if any change in the ontology occured
						String after = controlHandler.swoopHandler.changeLog.getOntSnapshot(baseOntology);
						
						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 repTreeTable
			this.refreshRepTreeTable(false);
			return testSuccess;
		}
		else {
			// test changes in Swoop Ontology for clashes etc.
			String status = "Status: [ACTION - Testing LOCAL Ontology Changes]...";
			
			statusBar.setText(status);
			boolean testSuccess = true;
			int failCount = 0;
			
			// get base ontology
			statusBar.setText(status+"Loading Base Ontology");
			OWLOntology baseOntology = null;
			if (existingOntRadio.isSelected()) {
				// get existing ontology from swoopModel
				baseOntology = (OWLOntology) ontBox.getSelectedItem();
				// but clone it to prevent applying changes on *it*
				baseOntology = this.cloneOntology(baseOntology);
			}
			else {
				URI ontURI = null;
				try {
					// load ontology with URI
					ontURI = new URI(swoopOntFld.getText());
					baseOntology = swoopModel.loadOntology(ontURI);
				}
				catch (Exception ex) {
					// create new ontology
					OWLBuilder builder = new OWLBuilder();
					try {
						builder.createOntology(ontURI, ontURI);
					} catch (OWLException e) {
						e.printStackTrace();						
					}
					baseOntology = builder.getOntology();
				}
			}
			
			if (baseOntology == null) {
				statusBar.setText(status+"Unable to load base ontology");
				return false;
			}
			
			try {
				for (int i=0; i<ontChanges.size(); i++) {
					SwoopChange swc = (SwoopChange) ontChanges.get(i);
					OntologyChange oc = swc.getChange();
					//*** need to align changes with ontology! ***/
					try {
						OntologyChange alignOC = (OntologyChange) controlHandler.swoopHandler.changeLog.getChangeInformation(oc, ChangeLog.CHANGE_ALIGN, null, new ArrayList(), baseOntology);
						
						// save state of baseOntology and see if it changes
						// use Abstract Syntax since its more concise than rdf/xml?
						String before = controlHandler.swoopHandler.changeLog.getOntSnapshot(baseOntology);
						
						// APPLY CHANGE
						boolean check = true;
						if (alignOC instanceof BooleanElementChange) { 
		    				check = swoopModel.applyBooleanElementChange(alignOC);
		    			}
		    			else if (alignOC instanceof EnumElementChange) { 
		    				check = swoopModel.applyEnumElementChange(alignOC);
		    			}
						else alignOC.accept((ChangeVisitor) baseOntology);
						
						// need to see if any change in the ontology occured
						String after = controlHandler.swoopHandler.changeLog.getOntSnapshot(baseOntology);
						

⌨️ 快捷键说明

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