⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owlsreader_1_0dl.java

📁 开发owl的API,提供了W3C规定标准接口,是目前比较少的API.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
							type = type.getModel().createResource(lit.getDatatypeURI());
						}
						else { 
							Resource res = (Resource) first;
							value = res.getURI();
							if(res.hasProperty(RDF.type))
								type = res.getProperty(RDF.type).getResource();
						}
						
						process.getDefaultValues().setValue(param, value);						
					}
				}
						
				if(type != null) {					
					URI uri = URI.create(type.getURI());
					if(uri == null)
						error("Invalid parameter type for the process parameter (" + 
								"\n      process: " + process + ", " +
								"\n    parameter: " + param + ", " + 
								"\n         type: " + type + ")");		
					else if(!uri.isAbsolute())
						error("Parameter type for the process parameter is not absolute (" + 
								"\n      process: " + process + ", " +
								"\n    parameter: " + param + ", " + 
								"\n         type: " + type + ")");		
					else
						param.setType(OWLFactory.createOWLResource(type));
				}
			}
			else
				error("Cannot find the type for the process parameter (" + 
						"\n      process: " + process + ", " +
						"\n    parameter: " + param + ")");			
								
			if(DEBUG) {
				System.out.println("  Process   " + process.getURI() + "\n" + 
								   (isInput ? "  Input     ":"  Output    " ) +
								   param.getURI() + "\n" + 
								   "  Type      " + param.getType() + "\n");
			}			
		}
	}	
	
	public Profile createProfile(Service service, Resource profileInfo) {
		try {
			Profile profile = OWLSFactory.createProfile(profileInfo);
			Process process = service.getProcess();
			
			createProfileParams(profile, process, true, profileInfo);
			createProfileParams(profile, process, false, profileInfo);
			
			createConditions(profile.getPreconditions(), profileInfo.listProperties(OWLS_1_0DL.Profile.hasPrecondition));
			createEffects(profile.getEffects(), profileInfo.listProperties(OWLS_1_0DL.Profile.hasEffect));			
			
			profile.setType(Util.toURI(profileInfo.getProperty(RDF.type).getResource()));		
			copyPropertyValues(profileInfo, OWLS_1_0DL.Profile.serviceName, RDFS.label);
			
			profile.setService(service);
			
			return profile;
		} catch (RuntimeException e) {
			error("Invalid profile description");
			e.printStackTrace();
			return null;
		}		
	}
	
	private void createProfileParams(Profile profile, Process process, boolean isInput, Resource profileInfo) {
		Property prop = isInput ? OWLS_1_0DL.Profile.hasInput : OWLS_1_0DL.Profile.hasOutput;
	    	
		StmtIterator i = profileInfo.getModel().listStatements(profileInfo, prop, (Resource)null);
		while(i.hasNext()) {
			Resource p = (Resource) i.nextStatement().getObject();
			
			String paramURI   = p.getURI();			
			Parameter refersTo = process.getParameter(URI.create(paramURI));
			ProcessComponentList processList = process.getAllProcesses();
			String paramProcessURI = null;
			
			if(refersTo == null ) {
				try {
					StmtIterator s = profileInfo.getModel().listStatements(null, isInput?OWLS_1_0DL.Process.hasInput:OWLS_1_0DL.Process.hasOutput, p);
					paramProcessURI = s.nextStatement().getSubject().getURI();					
					Process paramProcess = (Process) processList.getProcessComponent(URI.create(paramProcessURI));							
					refersTo = paramProcess.getParameter(URI.create(paramURI));
				} catch (Exception e) {
				}
			}
						
			if(refersTo == null) {
				error("The parameter defined in profile does not exist in the process model (" + 
					"\n    parameter: " + paramURI + ", " +
					"\n   in profile: " + profile.getURI() + ", " +
					(paramProcessURI  == null ? "\n does not belong to a process)" :
					"\n   belongs to: " + paramProcessURI + " \n   which is not in the process model)"));
			}	
			else {
				if(isInput) 
					profile.getInputs().add(refersTo);
				else
					profile.getOutputs().add(refersTo);
				
				if(DEBUG) {
					System.out.println("  Profile   " + profile.getURI() + "\n" + 
							(isInput ? "  Input     ":"  Output    " ) +
							paramURI + "\n" + 
							"  Refers to " + refersTo + "\n");
				}				
			}
		}	
	}
	
	private void createConditions(ConditionList conds, StmtIterator i) {
		while(i.hasNext()) {
			Resource p = (Resource) i.nextStatement().getObject();			
						
			conds.add(createCondition(p));
		}				
	}
		
	private void createEffects(EffectList effects, StmtIterator i) {
		while(i.hasNext()) {
			Resource p = (Resource) i.nextStatement().getObject();			
			
			effects.add(createEffect(p));
		}				
	}	
	
	private Condition createCondition(Resource condition) {
		Condition cond = OWLSFactory.createCondition(condition);
		
		return cond;
	}
	
	private Effect createEffect(Resource effect) {
		Effect eff = OWLSFactory.createEffect(effect);
		
		return eff;
	}
	
	public Grounding createGrounding(Service service, Resource groundingInfo) {
		Grounding grounding = OWLSFactory.createGrounding(groundingInfo);

		StmtIterator i = groundingInfo.listProperties(OWLS_1_0DL.Grounding.hasAtomicProcessGrounding); 
		while(i.hasNext()) {
			Resource apGroundingInfo = i.nextStatement().getResource();
			
			AtomicGrounding apGrounding = createAPGrounding(service, null, apGroundingInfo);
			if(apGrounding != null)
				grounding.addGrounding(apGrounding);
			else
				error("Invalid AtomicProcess grounding " + apGroundingInfo);
		}
		i = groundingInfo.listProperties(FLAServiceOnt.hasUPnPAtomicProcessGrounding); 
		while(i.hasNext()) {
			Resource apGroundingInfo = i.nextStatement().getResource();
			
			AtomicGrounding apGrounding = createAPGrounding(service, null, apGroundingInfo);
			if(apGrounding != null)
				grounding.addGrounding(apGrounding);
			else
				error("Invalid AtomicProcess grounding " + apGroundingInfo);
		}			
		
		if(grounding.getAtomicGroundings().size() == 0)
			warning("The grounding of the service is empty (" + 
					"\n    service " + service.getURI() +
					"\n  grounding " + grounding.getURI() + ")");
		
		grounding.setService(service);	
		
		return grounding;
	}	
		
	private AtomicGrounding createAPGrounding(Service service, AtomicProcess process, Resource groundingInfo) {
		try {
			AtomicGrounding grounding = null;
			
			if(groundingInfo.hasProperty(RDF.type, OWLS_1_0DL.Grounding.WsdlAtomicProcessGrounding))		
				grounding =  createWSDLGrounding(service, process, groundingInfo);

			if(groundingInfo.hasProperty(RDF.type, FLAServiceOnt.UPnPAtomicProcessGrounding))
				grounding =  createUPnPGrounding(service, process, groundingInfo);
			
			if(grounding != null)
				grounding.getProcess().setGrounding(grounding);
			
			return grounding;
		} catch (RuntimeException e) {
			e.printStackTrace();
		}

		return null;	
	}	

	private AtomicGrounding createWSDLGrounding(Service service, AtomicProcess process, Resource groundingInfo) {
		URI processURI; 
		String wsdlLoc, opName, portType = null;
		Resource operationInfo;
		
		if(!groundingInfo.hasProperty(OWLS_1_0DL.Grounding.damlsProcess)) {
			error(groundingInfo + " does not have a grounding:owlsProcess property");
			return null;			
		}
		else
			processURI = Util.toURI(groundingInfo.getProperty(OWLS_1_0DL.Grounding.damlsProcess).getResource());

		if(!groundingInfo.hasProperty(OWLS_1_0DL.Grounding.wsdlDocument)) {
			error(groundingInfo + " does not have a grounding:wsdlDocument property");
			return null;
		}		
		else if(groundingInfo.getProperty(OWLS_1_0DL.Grounding.wsdlDocument).getObject() instanceof Resource) {
			error("The value of grounding:wsdlDocument property in " + groundingInfo + " is not a literal");
			return null;			
		}
		else
			wsdlLoc = groundingInfo.getProperty(OWLS_1_0DL.Grounding.wsdlDocument).getObject().toString();
		
		if(!groundingInfo.hasProperty(OWLS_1_0DL.Grounding.wsdlOperation)) {
			error(groundingInfo + " does not have a grounding:wsdlOperation propertye");
			return null;			
		}		
		else
			operationInfo = groundingInfo.getProperty(OWLS_1_0DL.Grounding.wsdlOperation).getResource();
		
		if(!operationInfo.hasProperty(OWLS_1_0DL.Grounding.operation)) {
			error("WsdlOperationRef in " + groundingInfo + " does not have a grounding:operation property");
			return null;			
		}
		else if(operationInfo.getProperty(OWLS_1_0DL.Grounding.operation).getObject() instanceof Resource) {
			error("The value of grounding:operation property in WsdlOperationRef of " + groundingInfo + " is not a literal");
			return null;			
		}
		else
			opName = operationInfo.getProperty(OWLS_1_0DL.Grounding.operation).getObject().toString();		
		
		if(!operationInfo.hasProperty(OWLS_1_0DL.Grounding.portType))
			warning("WsdlOperationRef in " + groundingInfo + " does not have a grounding:operation property");			
		else if(operationInfo.getProperty(OWLS_1_0DL.Grounding.portType).getObject() instanceof Resource) {
			error("The value of grounding:portType property in WsdlOperationRef of " + groundingInfo + " is not a literal");
			return null;			
		}
		else
			portType = operationInfo.getProperty(OWLS_1_0DL.Grounding.portType).getObject().toString();	
		
		if(process == null)
			process = (AtomicProcess) findProcess(service.getProcess(), processURI);
		
  		if(process == null) {
  			error("The process specified in the grounding cannot be found (" + 
  					"\n    grounding: " + groundingInfo + ", " +
					"\n      process: " + processURI + ")");
  			return null;
  		}
  		
		WSDLAtomicGrounding g = OWLSFactory.createWSDLAtomicGrounding(groundingInfo); 
		g.setProcess(process);
		g.setWSDL(wsdlLoc);
		g.setOperation(opName);
		g.setPortType(portType);

		if(groundingInfo.hasProperty(OWLS_1_0DL.Grounding.wsdlInputMessage))
			g.setInputMessage(groundingInfo.getProperty(OWLS_1_0DL.Grounding.wsdlInputMessage).getObject().toString());		

		if(groundingInfo.hasProperty(OWLS_1_0DL.Grounding.wsdlOutputMessage))
			g.setOutputMessage(groundingInfo.getProperty(OWLS_1_0DL.Grounding.wsdlOutputMessage).getObject().toString());
		
		createMessageMapList(g, groundingInfo, true);
		createMessageMapList(g, groundingInfo, false);

		if(DEBUG) {
			System.out.println("  Process   " + process.getURI() + "\n" + 
							   "  WSDL file " + wsdlLoc + "\n" + 
							   "  Operation " + opName + "\n");
		}
		
		return g;
	}
	
	private AtomicGrounding createUPnPGrounding(Service service, AtomicProcess process, Resource groundingInfo) {
		URI processURI = Util.toURI(groundingInfo.getProperty(OWLS_1_0DL.Grounding.damlsProcess).getResource());
		String upnpDevice = groundingInfo.getProperty(FLAServiceOnt.upnpDeviceURL).getObject().toString();
		String upnpService = groundingInfo.getProperty(FLAServiceOnt.upnpServiceID).getObject().toString();
		String upnpAction = groundingInfo.getProperty(FLAServiceOnt.upnpCommand).getObject().toString();
		
		if(process == null)
			process = (AtomicProcess) findProcess(service.getProcess(), processURI);
		
		UPnPAtomicGrounding g = OWLSFactory.createUPnPAtomicGrounding(groundingInfo); 
		g.setProcess(process);
		g.setUPnPDescription(upnpDevice);
		g.setUPnPService(upnpService);
		g.setUPnPAction(upnpAction);
		
		createMessageMapList(g, groundingInfo, true);
		createMessageMapList(g, groundingInfo, false);

		if(DEBUG) {
			System.out.println("  Process " + process.getURI() + "\n" + 
							   "  Device  " + upnpDevice + "\n" + 
							   "  Service " + upnpService + "\n" +
							   "  Action  " + upnpAction + "\n");
		}
		
		return g;
	}
		
	private void createMessageMapList(AtomicGrounding g, Resource groundingInfo, boolean isInput) {
		Process process = g.getProcess();	
		Property messageParts = null;
		Property messagePart = null;
		
		if(g instanceof UPnPAtomicGrounding) {
			messageParts = isInput ? 
				FLAServiceOnt.UPnPInputMapping: 
				FLAServiceOnt.UPnPOutputMapping;
			messagePart = FLAServiceOnt.upnpParameter;
										
		}
		else if(g instanceof WSDLAtomicGrounding) {
			messageParts = isInput ? 
				OWLS_1_0DL.Grounding.wsdlInputMessageParts: 
				OWLS_1_0DL.Grounding.wsdlOutputMessageParts;
			messagePart = OWLS_1_0DL.Grounding.wsdlMessagePart;
		}
		
		if(!groundingInfo.hasProperty(messageParts))
			return;
			
		Resource messageMapInfo = (Resource) groundingInfo.getProperty(messageParts).getObject();
		List messageMapList = Util.createList(messageMapInfo, ObjList.first, ObjList.rest, ObjList.nil);

		MessageMapList mapList = isInput ? g.getInputMap() : g.getOutputMap();
		for(int i = 0; i < messageMapList.size(); i++) {
			Resource messageMap = (Resource) messageMapList.get(i);

			MessageMap map = OWLSFactory.createMessageMap(messageMap);

			URI owlsParameterInfo = Util.toURI(messageMap.getProperty(OWLS_1_0DL.Grounding.damlsParameter).getResource());
			Parameter owlsParameter = isInput ?
				process.getInputs().getParameter(owlsParameterInfo):
				process.getOutputs().getParameter(owlsParameterInfo);
			map.setOWLSParameter(owlsParameter);
			
			String wsdlMessagePartInfo = messageMap.getProperty(messagePart).getObject().toString();
			if(messageMap.getProperty(messagePart).getObject() instanceof Resource) {
				error("The value of grounding:wsdlMessagePart property in " + groundingInfo + " is not a literal");
				continue;			
			}						
			map.setGroundingParameter(wsdlMessagePartInfo);

			String transformation = null;
			if(messageMap.hasProperty(OWLS_1_0DL.Grounding.xsltTranformation)) {
				transformation = messageMap.getProperty(OWLS_1_0DL.Grounding.xsltTranformation).getString();
				map.setTransformation(transformation);
			}
			
			if(owlsParameter == null) {
				error("Cannot find the target of message map for " + (isInput?"input":"output") + " parameter (" +
					"\n   wsdl parameter: " + wsdlMessagePartInfo + ", " +
					"\n       in process: " + process.getURI() + ", " +
					"\n        mapped to: " + owlsParameterInfo + ")");
			}				

			if(DEBUG) {
				System.out.println("  Process   " + process.getURI() + "\n" + 
								   "  Param     " + owlsParameterInfo + "\n" + 
								   "  Grounding " + wsdlMessagePartInfo + "\n" + 
								   "  Transform " + transformation + "\n");
			}
			
			mapList.add(map);			
		}
	}	

	private Process findProcess(ProcessComponent process, URI processURI) {
		if(process == null)
			return null;
		else if(process.getURI() != null && process.getURI().equals(processURI)){
			if(process instanceof Process) return (Process) process;
		}
		else if(process instanceof CompositeProcess) {
			return findProcess(((CompositeProcess) process).getComposedOf(), processURI);
		}
		else if(process instanceof ControlConstruct) {	
			ProcessComponentList list = ((ControlConstruct)process).getComponents();
			
			for(int i = 0; i < list.size(); i++) {
				ProcessComponent pc = list.processComponentAt(i);
				Process p = findProcess(pc, processURI);
				if(p != null) return p;
			}
		}
			
		return null;	
	}
	
	private void copyPropertyValues(Resource subj, Property srcProp, Property targetProp) {
		List values = new ArrayList();
		StmtIterator si = subj.listProperties(srcProp);
		while(si.hasNext())
			values.add(si.nextStatement().getObject());
		Iterator i = values.iterator(); 
		while(i.hasNext())
			subj.addProperty(targetProp, (RDFNode) i.next());
			
	}
}

⌨️ 快捷键说明

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