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

📄 owlsreader_1_0.java

📁 开发owl的API,提供了W3C规定标准接口,是目前比较少的API.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// The MIT License
//
// Copyright (c) 2004 Evren Sirin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

/*
 * Created on Dec 27, 2003
 *
 */
package org.mindswap.owls.io.impl;

import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.Util;
import org.mindswap.owls.OWLSFactory;
import org.mindswap.owls.grounding.AtomicGrounding;
import org.mindswap.owls.grounding.Grounding;
import org.mindswap.owls.grounding.MessageMap;
import org.mindswap.owls.grounding.MessageMapList;
import org.mindswap.owls.grounding.UPnPAtomicGrounding;
import org.mindswap.owls.grounding.WSDLAtomicGrounding;
import org.mindswap.owls.process.AtomicProcess;
import org.mindswap.owls.process.Choice;
import org.mindswap.owls.process.CompositeProcess;
import org.mindswap.owls.process.Condition;
import org.mindswap.owls.process.ConditionList;
import org.mindswap.owls.process.ControlConstruct;
import org.mindswap.owls.process.DataFlow;
import org.mindswap.owls.process.DataFlowElement;
import org.mindswap.owls.process.Effect;
import org.mindswap.owls.process.EffectList;
import org.mindswap.owls.process.Parameter;
import org.mindswap.owls.process.Process;
import org.mindswap.owls.process.ProcessComponent;
import org.mindswap.owls.process.ProcessComponentList;
import org.mindswap.owls.process.ProcessModel;
import org.mindswap.owls.process.Sequence;
import org.mindswap.owls.process.Split;
import org.mindswap.owls.process.SplitJoin;
import org.mindswap.owls.process.Unordered;
import org.mindswap.owls.profile.Profile;
import org.mindswap.owls.service.Service;
import org.mindswap.owls.vocabulary.jena.FLAServiceOnt;
import org.mindswap.owls.vocabulary.jena.OWLS_1_0;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;

/**
 * @author Evren Sirin
 *
 */
public class OWLSReader_1_0 extends OWLSReaderImpl {
	public static boolean DEBUG = false;
	
	/**
	 * 
	 */
	public OWLSReader_1_0() {
		version = "1.0";
	}
	
	protected List createService(URI fileURI, Model model) {
		StmtIterator si = model.listStatements(null, RDF.type, OWLS_1_0.Service.Service);
		List list = new ArrayList();
		
		while(si.hasNext()) {
			Resource serviceInfo = si.nextStatement().getSubject();		
			try {
				startService(serviceInfo.toString());				
				Resource profileInfo = serviceInfo.getProperty(OWLS_1_0.Service.presents).getResource();
				Resource processModelInfo = serviceInfo.getProperty(OWLS_1_0.Service.describedBy).getResource();
				Resource groundingInfo = serviceInfo.getProperty(OWLS_1_0.Service.supports).getResource();
				
				Service service = OWLSFactory.createService(serviceInfo);
				service.setFileURI(fileURI);
				service.setOWLSVersion(version);
				
				createProcessModel(service, processModelInfo);
				createProfile(service, profileInfo);		
				createGrounding(service, groundingInfo);
				
				if(!errorOccurred)
					list.add(service);
				
				finishService(serviceInfo.toString());				
			} catch (Exception e) {
				error("Invalid service description " + serviceInfo);
				e.printStackTrace();
			}
		}
		
		return list;		
	}

	private void createProcessModel(Service service, Resource processModelInfo) {	
		Resource processInfo = processModelInfo.getProperty(OWLS_1_0.Process.hasProcess).getResource();
		
		ProcessModel processModel = OWLSFactory.createProcessModel(processModelInfo);
		Process process = createProcess(service, processInfo);
		
		processModel.setProcess(process);
		processModel.setService(service);	
	}

	private boolean isProcess(Resource processInfo) {
		return processInfo.hasProperty(RDF.type, OWLS_1_0.Process.AtomicProcess) ||
			   processInfo.hasProperty(RDF.type, OWLS_1_0.Process.CompositeProcess) ||
			   processInfo.hasProperty(RDF.type, OWLS_1_0.Process.SimpleProcess);
	}
		
	public Process createProcess(Service service, Resource processInfo) {	
		if(!isProcess(processInfo))
			throw new RuntimeException("The process is neither atomic nor composite: " + processInfo.getURI());
			
		Process process = null;	
		if(processInfo.hasProperty(RDF.type, OWLS_1_0.Process.AtomicProcess))
			process = createAtomicProcess(service, processInfo);
		if(processInfo.hasProperty(RDF.type, OWLS_1_0.Process.CompositeProcess))
			process = createCompositeProcess(service, processInfo);
	
		if(process == null)
			return null;
		
		createProcessParams(process, true, processInfo);		
		createProcessParams(process, false, processInfo);
		
		createConditions(process.getPreconditions(), processInfo.listProperties(OWLS_1_0.Profile.hasPrecondition));
		createEffects(process.getEffects(), processInfo.listProperties(OWLS_1_0.Profile.hasEffect));			
		
		createDataFlow(process, processInfo);	
				
		
		
		if(process instanceof AtomicProcess) {
			Model model = processInfo.getModel();
			StmtIterator s = model.listStatements(null, OWLS_1_0.Grounding.damlsProcess, processInfo);
			if(s.hasNext()) {
				Resource groundingInfo = s.nextStatement().getSubject();
				createAPGrounding(null, (AtomicProcess) process, groundingInfo);							
			}
		}
		
		process.setService(service);
		
		return process;
	}
	
	private AtomicProcess createAtomicProcess(Service service, Resource processInfo) {	
		AtomicProcess process =	OWLSFactory.createAtomicProcess(processInfo);
		
		return process;
	}

	private CompositeProcess createCompositeProcess(Service service, Resource processInfo) {
		Statement stmt = processInfo.getProperty(OWLS_1_0.Process.composedOf);
		
		if(stmt == null) {
			error("Cannot find the components for composite process (" + 
					"\n      process: " + processInfo + ")");	
			return null;
		}
		
		Resource composedInfo = stmt.getResource();
		
		ControlConstruct controlConstruct = createControlConstruct(service, composedInfo);
		
		CompositeProcess process = OWLSFactory.createCompositeProcess(processInfo);
		process.setComposedOf(controlConstruct);
		
		return process;   
	}	
	
	private ProcessComponent createProcessComponent(Service service, Resource processComponentInfo) {
		ProcessComponent processComponent = null;
		
		if(isProcess(processComponentInfo))
			processComponent = createProcess(service, processComponentInfo);
		else
			processComponent = createControlConstruct(service, processComponentInfo);

		return processComponent;
	}	
		
	private ControlConstruct createControlConstruct(Service service, Resource controlConstructInfo) {
		Resource ccType = controlConstructInfo.getProperty(RDF.type).getResource();
		Resource componentsInfo = controlConstructInfo.getProperty(OWLS_1_0.Process.components).getResource();
		
		ControlConstruct cc = null;
		if(ccType.equals(OWLS_1_0.Process.Sequence))
			cc = createSequence(service, componentsInfo);
		else if(ccType.equals(OWLS_1_0.Process.Choice))
			cc = createChoice(service, componentsInfo);
		else if(ccType.equals(OWLS_1_0.Process.Split))
			cc = createSplit(service, componentsInfo);
		else if(ccType.equals(OWLS_1_0.Process.Unordered))
			cc = createUnordered(service, componentsInfo);
		else
			error("Don't know how to read the control construct " + ccType);
		
		return cc;
	}	
	
	private Sequence createSequence(Service service, Resource sequenceInfo) {
		Sequence sequence = OWLSFactory.createSequence(sequenceInfo);

		createComponents(service, sequence, sequenceInfo);		
		
		return sequence;
	}	
	
	private Split createSplit(Service service, Resource splitInfo) {
		Split split = OWLSFactory.createSplit(splitInfo);

		createComponents(service, split, splitInfo);		
		
		return split;
	}	
	
	private SplitJoin createSplitJoin(Service service, Resource splitInfo) {
		SplitJoin split = OWLSFactory.createSplitJoin(splitInfo);

		createComponents(service, split, splitInfo);		
		
		return split;
	}	
		
	private Unordered createUnordered(Service service, Resource unorderedInfo) {
		Unordered unordered = OWLSFactory.createUnordered(unorderedInfo);

		createComponents(service,unordered, unorderedInfo);		
		
		return unordered;
	}		

	private Choice createChoice(Service service, Resource choiceInfo) {
		Choice choice = OWLSFactory.createChoice(choiceInfo);

		createComponents(service, choice, choiceInfo);		
		
		return choice;
	}
		
	private void createComponents(Service service, ControlConstruct cc, Resource ccInfo) {		
		List list = Util.createList(ccInfo);
		for(int i = 0; i < list.size(); i++) {
			Resource processComponentInfo = (Resource) list.get(i);
			ProcessComponent processComponent = createProcessComponent(service, processComponentInfo);
			
			if(processComponent == null)
				error("Invalid ProcessComponent description " + processComponentInfo);
			else
				cc.getComponents().add(processComponent);
		}
	}	

	private void createDataFlow(Process process, Resource processComponentInfo) {
		try {	
			DataFlow dataFlow = process.getDataFlow();
			StmtIterator i = processComponentInfo.listProperties(OWLS_1_0.Process.sameValues);
			while(i.hasNext()) {
				Resource sameValuesList = i.nextStatement().getResource();
				Resource value1 = sameValuesList.getProperty(RDF.first).getResource();
				Resource value2 = sameValuesList.getProperty(RDF.rest).getResource().getProperty(RDF.first).getResource();
	
				Statement stmt;
				
				Resource processName1, paramName1, processName2, paramName2;
				
				stmt = value1.getProperty(OWLS_1_0.Process.theProperty);
				if(stmt == null) {
					error("ValueOf is missing a value for process:theParameter");
					continue;
				}
				if(stmt.getObject() instanceof Resource)
					paramName1 = stmt.getResource();
				else
					paramName1 = Util.toResource(stmt.getObject().toString());
	
				stmt = value1.getProperty(OWLS_1_0.Process.atClass);
				if(stmt.getObject() instanceof Resource)
					processName1 = stmt.getResource();
				else
					processName1 = Util.toResource(stmt.getObject().toString());
				
				stmt = value2.getProperty(OWLS_1_0.Process.theProperty);
				if(stmt.getObject() instanceof Resource)
					paramName2 = stmt.getResource();
				else
					paramName2 = Util.toResource(stmt.getObject().toString());
	
				stmt = value2.getProperty(OWLS_1_0.Process.atClass);
				if(stmt.getObject() instanceof Resource)
					processName2 = stmt.getResource();
				else
					processName2 = Util.toResource(stmt.getObject().toString());
							
				Process p1 = findProcess(process, Util.toURI(processName1));
				Process p2 = findProcess(process, Util.toURI(processName2));
				Parameter param1 = null, param2 = null;
				
				if(p1 == null) {
					error("Cannot find the process data flow refers to (" + 
							"\n      process: " + processName1 + ", " +
							"\n    parameter: " + paramName1 + ", " +
							"\n data flow in: " + process.getURI() + ")");
				}
				else {
					param1 = p1.getParameter(Util.toURI(paramName1));
					if(param1 == null) {
						error("Cannot find the parameter data flow refers to (" + 
								"\n      process: " + processName1 + ", " +
								"\n    parameter: " + paramName1 + ", " +
								"\n data flow in: " + process.getURI() + ")");					
					}
				}
	
				if(p2 == null) {
					error("Cannot find the process data flow refers to (" + 
							"\n      process: " + processName2 + ", " +
							"\n    parameter: " + paramName2 + ", " +
							"\n data flow in: " + process.getURI() + ")");
				}
				else {
					param2 = p2.getParameter(Util.toURI(paramName2));
					if(param2 == null) {
						error("Cannot find the parameter data flow refers to (" + 
								"\n      process: " + processName2 + ", " +
								"\n    parameter: " + paramName2 + ", " +
								"\n data flow in: " + process.getURI() + ")");					
					}
				}
				
				if(param1 == null || param2 == null)
					continue;
				
				DataFlowElement dfe = OWLSFactory.createDataFlowElement();
				dfe.add(param1);
				dfe.add(param2);
				
				dataFlow.add(dfe);
			}
		} catch(Exception e) {
			error("Invalid data flow specification ");
			e.printStackTrace();
		}
	}	

	private void createProcessParams(Process process, boolean isInput, Resource processInfo) {	
		Property prop = isInput ? OWLS_1_0.Process.hasInput : OWLS_1_0.Process.hasOutput;		
		StmtIterator i = processInfo.listProperties(prop);		
		while(i.hasNext()) {
			Resource p = i.nextStatement().getResource();

			Parameter param = null;
			if(isInput) {
				param = OWLSFactory.createInput(p);
				process.getInputs().add(param);
			}
			else {
				param = OWLSFactory.createOutput(p);
				process.getOutputs().add(param);
			}
				
			param.setProcess(process);	
			
			if(p.hasProperty(OWLS_1_0.Process.parameterType)) {
				Resource type = p.getProperty(OWLS_1_0.Process.parameterType).getResource();
				
				if(type.hasProperty(OWL.oneOf)) {
					Resource list = type.getProperty(OWL.oneOf).getResource();
					RDFNode first = list.getProperty(RDF.first).getObject();
					RDFNode rest = list.getProperty(RDF.rest).getObject();
					
					if(rest.equals(RDF.nil)) {
						Object value = null;
						if(first instanceof Literal) {
							Literal lit = (Literal) first;
							value = lit.getValue();
							type = type.getModel().createResource(lit.getDatatypeURI());

⌨️ 快捷键说明

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