ontologychangerenderer.java

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

JAVA
1,066
字号
							OWLSameIndividualsAxiom sameAxiom = df.getOWLSameIndividualsAxiom(sameInds);
							RemoveIndividualAxiom change = new RemoveIndividualAxiom(changeOnt, sameAxiom, null); 
							changes.add(change);
						}
						catch (Exception ex) {
							ex.printStackTrace();
						}
					}
					else if (uri.equals(owlapiLocation+"#AddDifferentIndividuals")) {
						try {
							Set diffInds = new HashSet();
							int i = 1;
							while (arguments[i]!=null) {
								diffInds.add((OWLIndividual) swoopModel.getEntity(changeOnt, new URI(arguments[i]), true, swoopModel.INDIVIDUALS));
								i++;
							}
							OWLDifferentIndividualsAxiom diffAxiom = df.getOWLDifferentIndividualsAxiom(diffInds);
							AddIndividualAxiom change = new AddIndividualAxiom(changeOnt, diffAxiom, null); 
							changes.add(change);
						}
						catch (Exception ex) {
							ex.printStackTrace();
						}
					}
					else if (uri.equals(owlapiLocation+"#RemoveDifferentIndividuals")) {
						try {
							Set diffInds = new HashSet();
							int i = 1;
							while (arguments[i]!=null) {
								diffInds.add((OWLIndividual) swoopModel.getEntity(changeOnt, new URI(arguments[i]), true, swoopModel.INDIVIDUALS));
								i++;
							}
							OWLDifferentIndividualsAxiom diffAxiom = df.getOWLDifferentIndividualsAxiom(diffInds);
							RemoveIndividualAxiom change = new RemoveIndividualAxiom(changeOnt, diffAxiom, null); 
							changes.add(change);
						}
						catch (Exception ex) {
							ex.printStackTrace();
						}
					}
					else if (uri.equals(owlapiLocation+"#AddObjectPropertyInstance")) {
						try {
							OWLIndividual subj = (OWLIndividual) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.INDIVIDUALS);
							OWLObjectProperty prop = (OWLObjectProperty) swoopModel.getEntity(changeOnt, new URI(arguments[2]), true, swoopModel.OBJPROPERTIES);
							OWLIndividual obj = changeOnt.getIndividual(new URI(arguments[3]));
							AddObjectPropertyInstance change = new AddObjectPropertyInstance(changeOnt, subj, prop, obj, null);
							changes.add(change);
						}
						catch (Exception ex) {
							ex.printStackTrace();
						}
						 
					}
					else if (uri.equals(owlapiLocation+"#RemoveObjectPropertyInstance")) {
						try {
							OWLIndividual subj = (OWLIndividual) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.INDIVIDUALS);
							OWLObjectProperty prop = (OWLObjectProperty) swoopModel.getEntity(changeOnt, new URI(arguments[2]), true, swoopModel.OBJPROPERTIES);
							OWLIndividual obj = changeOnt.getIndividual(new URI(arguments[3]));
							RemoveObjectPropertyInstance change = new RemoveObjectPropertyInstance(changeOnt, subj, prop, obj, null);
							changes.add(change);
						}
						catch (Exception ex) {
							ex.printStackTrace();
						}
						 
					}
					else if (uri.equals(owlapiLocation+"#AddDataPropertyInstance")) {
						try {
							OWLIndividual subj = (OWLIndividual) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.INDIVIDUALS);
							OWLDataProperty prop = (OWLDataProperty) swoopModel.getEntity(changeOnt, new URI(arguments[2]), true, swoopModel.DATAPROPERTIES);
							String dValStr = origArguments[3];
							String dTypeURI = dValStr.substring(dValStr.lastIndexOf("[")+1, dValStr.length());
							if (dTypeURI.endsWith("@EN")) dTypeURI = dTypeURI.substring(0, dTypeURI.length()-3);
							if (dTypeURI.indexOf("^")>=0) dTypeURI = dTypeURI.substring(0, dTypeURI.indexOf("^"));
							arguments[3] = arguments[3].substring(0, arguments[3].indexOf("["));
							OWLDataValue dVal = df.getOWLConcreteData(new URI(dTypeURI), "EN", arguments[3]);
							AddDataPropertyInstance change = new AddDataPropertyInstance(changeOnt, subj, prop, dVal, null);
							changes.add(change);
						}
						catch (Exception ex) {
							ex.printStackTrace();
						}
					}
					else if (uri.equals(owlapiLocation+"#RemoveDataPropertyInstance")) {
						try {
							OWLIndividual subj = (OWLIndividual) swoopModel.getEntity(changeOnt, new URI(arguments[1]), true, swoopModel.INDIVIDUALS);
							OWLDataProperty prop = (OWLDataProperty) swoopModel.getEntity(changeOnt, new URI(arguments[2]), true, swoopModel.DATAPROPERTIES);
							String dValStr = origArguments[3];
							String dTypeURI = dValStr.substring(dValStr.lastIndexOf("[")+1, dValStr.length());
							if (dTypeURI.endsWith("@EN")) dTypeURI = dTypeURI.substring(0, dTypeURI.length()-3);
							if (dTypeURI.indexOf("^")>=0) dTypeURI = dTypeURI.substring(0, dTypeURI.indexOf("^"));
							arguments[3] = arguments[3].substring(0, arguments[3].indexOf("["));
							OWLDataValue dVal = df.getOWLConcreteData(new URI(dTypeURI), "EN", arguments[3]);
							RemoveDataPropertyInstance change = new RemoveDataPropertyInstance(changeOnt, subj, prop, dVal, null);
							changes.add(change);
						}
						catch (Exception ex) {
							ex.printStackTrace();
						}
					}
				}
			}
		}
	
		return changes;		
	}

	/**
	 * Check if argument string which is a URI pointer in annotation ontology
	 * points to an existing entity in current ontology, or a bnode (OWL Description)
	 * which is captured as the type of an individual with URI = "#RDFbnodeHASHCODE" 
	 * @param annotOnt - annotation ontology
	 * @param arg - string argument pointing to a URI 
	 * @param currOnt - current selected ontology in Swoop
	 * @return
	 */
	protected OWLDescription getOWLObjectNode(OWLOntology annotOnt, String arg, OWLOntology currOnt) {
		
		OWLDescription owlObj = null;
		try {
			URI argURI = new URI(arg);
			
			if (arg.indexOf("#RDFbnode")>=0) {
				//*** argument points to an OWL Description
				// hence get individual with URI = bnode (from ont)
				// and gets it type as the required OWL Description ***/
				/*** Does not matter if currOntology exists or not since it gets
				 * bnode description from annotOnt!
				 */
				OWLIndividual ind = annotOnt.getIndividual(argURI);
				owlObj = (OWLDescription) ind.getTypes(annotOnt).iterator().next();				
			}
			else {
				// argument points to existing owl class in currOnt
				owlObj = (OWLClass) changeLog.swoopModel.getEntity(currOnt, argURI, true, changeLog.swoopModel.CLASSES);				
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return owlObj;
	}

	/**
	 * Add an instance to the OWL-API (change characterization) ontology
	 * @param changeType - string representing ontology change type
	 * @param changeArguments - set of arguments of the ontology change 
	 * @return current owlapiOntology
	 */
	public OWLOntology addtoOWLAPIOntology(String changeType, List changeArguments) {
		
		try {
			// initialize owlapi ontology
			OWLDataFactory df = owlapiOntology.getOWLDataFactory();
			OWLDataProperty[] arguments = new OWLDataProperty[argLimit];
			for (int i=0; i<argLimit; i++) {
				arguments[i] = owlapiOntology.getDataProperty(new URI(owlapiLocation+"#argument"+String.valueOf(i+1)));
			}
			
			OWLClass changeClass = owlapiOntology.getClass(new URI(owlapiLocation+"#"+changeType));
			
			if (changeClass==null) {
				System.out.println("Unable to find class "+changeType+" in OWL-API Ontology..");
				return owlapiOntology;
			}
			
			OWLIndividual changeInst = df.getOWLIndividual(new URI("#"+changeType+changeArguments.hashCode()));
			AddEntity ae = new AddEntity(owlapiOntology, changeInst, null);
			ae.accept((ChangeVisitor) owlapiOntology);
			
			AddIndividualClass aic = new AddIndividualClass(owlapiOntology, changeInst, changeClass, null);
			aic.accept((ChangeVisitor) owlapiOntology);						
			
			Iterator iter = changeArguments.iterator();
			int argCtr = 0;
			while (iter.hasNext()) {
				
				Object arg = iter.next();
				// if argument is an entity, it has a URI
				// so add URI directly as value of dataProperty - "argument"
				if (arg instanceof OWLNamedObject) {
					OWLNamedObject namedObj = (OWLNamedObject) arg;
					String uri = namedObj.getURI().toString();
					OWLDataValue dVal = df.getOWLConcreteData(new URI(xsdString), "EN", uri);
					AddDataPropertyInstance dop = new AddDataPropertyInstance(owlapiOntology, changeInst, arguments[argCtr], dVal, null);
					dop.accept((ChangeVisitor) owlapiOntology);
				}
				// else if argument is an OWL Description (Anonymous)
				// create new individual (URI-hashcode of desc) which is of
				// type description :)
				else if (arg instanceof OWLDescription) {
					OWLDescription desc = (OWLDescription) arg;
					String descHash = String.valueOf(desc.hashCode());
					OWLIndividual descInst = df.getOWLIndividual(new URI(owlapiLocation+"#"+"RDFbnode"+descHash));
					ae = new AddEntity(owlapiOntology, descInst, null);
					ae.accept((ChangeVisitor) owlapiOntology);
					aic = new AddIndividualClass(owlapiOntology, descInst, desc, null);
					aic.accept((ChangeVisitor) owlapiOntology);
					String uri = descInst.getURI().toString();
					OWLDataValue dVal = df.getOWLConcreteData(new URI(xsdString), "EN", uri);
					AddDataPropertyInstance dop = new AddDataPropertyInstance(owlapiOntology, changeInst, arguments[argCtr], dVal, null);
					dop.accept((ChangeVisitor) owlapiOntology);
				}
				// else if argument is datatype,
				// add URI directly as value of property
				else if (arg instanceof OWLDataType) {
					OWLDataType dt = (OWLDataType) arg;
					String uri = dt.getURI().toString();
					OWLDataValue dVal = df.getOWLConcreteData(new URI(xsdString), "EN", uri);
					AddDataPropertyInstance dop = new AddDataPropertyInstance(owlapiOntology, changeInst, arguments[argCtr], dVal, null);
					dop.accept((ChangeVisitor) owlapiOntology);
				}
				// dataValue is more direct!
				else if (arg instanceof OWLDataValue) {
					String dValStr = arg.toString();
					dValStr = dValStr.substring(0, dValStr.indexOf("^")) + "[" + dValStr.substring(dValStr.lastIndexOf("^")+1, dValStr.length());
					OWLDataValue dVal = df.getOWLConcreteData(new URI(xsdString), "EN", dValStr);
					AddDataPropertyInstance dop = new AddDataPropertyInstance(owlapiOntology, changeInst, arguments[argCtr], dVal, null);
					dop.accept((ChangeVisitor) owlapiOntology);
				}
				// otherwise treat it as a string and directly add it as the value (w/o parsing)
				else if (arg instanceof String) {
					OWLDataValue dVal = df.getOWLConcreteData(new URI(xsdString), "EN", arg.toString());
					AddDataPropertyInstance dop = new AddDataPropertyInstance(owlapiOntology, changeInst, arguments[argCtr], dVal, null);
					dop.accept((ChangeVisitor) owlapiOntology);
				}
				
				argCtr++;
			}
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return owlapiOntology;
	}
	
	private void reloadOWLAPIOntology() throws OWLException, FileNotFoundException, URISyntaxException {
		OWLRDFParser parser = new OWLRDFParser();
		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 {
			}
		});
		// load and parse OWL-API Ontology (from local copy)
		InputStreamReader in = new InputStreamReader(OntologyChangeRenderer.class.getResourceAsStream("owlapi.owl")); 
		parser.setConnection(new OWLConnectionImpl());
		owlapiOntology = parser.parseOntology(in, new URI(owlapiLocation));
		
		in = new InputStreamReader(OntologyChangeRenderer.class.getResourceAsStream("owlapi.owl"));
		parser.setConnection(new OWLConnectionImpl());
		owlapiOnly = parser.parseOntology(in, new URI(owlapiLocation));
	}
	
	public OWLOntology getOWLAPIOntology() {
		return this.owlapiOntology;
	}
	
	/**
	 * Add or Remove the elements from the OWLAPI-only ontology
	 * from the serialized version of the changeset ontology 
	 * @param add
	 */
	protected void removeRedundantOWLAPI() {
		try {
			// add/remove classes
			for (Iterator iter = owlapiOnly.getClasses().iterator(); iter.hasNext();) {
				OWLClass cla = (OWLClass) iter.next();
				cla = owlapiOntology.getClass(cla.getURI());
				Set usage = OntologyHelper.entityUsage(owlapiOntology, cla);
				if (usage.size()==0) {
					OntologyChange oc = new RemoveEntity(owlapiOntology, cla, null);
					oc.accept((ChangeVisitor) owlapiOntology);
				}				
			}
			// add/remove data props
			for (Iterator iter = owlapiOnly.getDataProperties().iterator(); iter.hasNext();) {
				OWLDataProperty prop = (OWLDataProperty) iter.next();
				OntologyChange oc = new RemoveEntity(owlapiOntology, owlapiOntology.getDataProperty(prop.getURI()), null);
				oc.accept((ChangeVisitor) owlapiOntology);
			}
			// add/remove obj props
			for (Iterator iter = owlapiOnly.getObjectProperties().iterator(); iter.hasNext();) {
				OWLObjectProperty prop = (OWLObjectProperty) iter.next();
				OntologyChange oc = new RemoveEntity(owlapiOntology, owlapiOntology.getObjectProperty(prop.getURI()), null);
				oc.accept((ChangeVisitor) owlapiOntology);
			}
			
			// finally add/remove imports of OWLAPI-only
			// DONT ADD THIS BECAUSE IT BREAKS PARSER
//			if (add) {
//				// remove imports				
//				RemoveImport ri = new RemoveImport(owlapiOntology, owlapiOnly, null);
//				ri.accept((ChangeVisitor) owlapiOntology);
//			}
//			else {
//				// add imports
//				AddImport ai = new AddImport(owlapiOntology, owlapiOnly, null);
//				ai.accept((ChangeVisitor) owlapiOntology);
//			}
		}
		catch (OWLException ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * Create a New OWL Object (Ontology/Entity)
	 * @param cla - Java Class of OWL Object
	 * @param refURI - OWL Object URI
	 * @param refOnt - reference OWL Ontology used to create new entity 
	 * @return
	 */
	private OWLObject createNewOWLObject(Class cla, URI refURI, OWLOntology refOnt) {
 		OWLObject obj = null;
 		try {
			if (cla == OWLOntology.class) {
	 			// create new ontology
				OWLBuilder builder = new OWLBuilder();
				builder.createOntology(refURI, refURI);
				obj = builder.getOntology();
	 		}
			else if (cla == OWLClass.class) {
				// create new class
				obj = refOnt.getOWLDataFactory().getOWLClass(refURI);
			}
			else if (cla == OWLDataProperty.class) {
				// create new data property
				obj = refOnt.getOWLDataFactory().getOWLDataProperty(refURI);
			}
			else if (cla == OWLObjectProperty.class) {
				// create new object property
				obj = refOnt.getOWLDataFactory().getOWLObjectProperty(refURI);
			}
			else if (cla == OWLIndividual.class) {
				// create new individual
				obj = refOnt.getOWLDataFactory().getOWLIndividual(refURI);
			}
 		}
 		catch (OWLException ex) {
 			ex.printStackTrace();
 		}
		return obj;
	}
}

⌨️ 快捷键说明

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