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

📄 owlswriter_0_9.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.io.Writer;
import java.net.URI;
import java.util.Iterator;
import java.util.Map;

import org.mindswap.owl.OWLResource;
import org.mindswap.owl.Util;
import org.mindswap.owl.vocabulary.OWL;
import org.mindswap.owl.vocabulary.RDF;
import org.mindswap.owl.vocabulary.RDFS;
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.CompositeProcess;
import org.mindswap.owls.process.ControlConstruct;
import org.mindswap.owls.process.DataFlow;
import org.mindswap.owls.process.DataFlowElement;
import org.mindswap.owls.process.Input;
import org.mindswap.owls.process.Parameter;
import org.mindswap.owls.process.ParameterList;
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.ValueMap;
import org.mindswap.owls.profile.Profile;
import org.mindswap.owls.service.Service;
import org.mindswap.owls.vocabulary.FLAServiceOnt;
import org.mindswap.owls.vocabulary.OWLS;
import org.mindswap.owls.vocabulary.OWLS_0_9;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.RDFWriter;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.vocabulary.RDFSyntax;


/**
 * @author Evren Sirin
 *
 */
public class OWLSWriter_0_9 extends OWLSWriterImpl {
	public static boolean DEBUG = false;
	
	private Model model = null;
	private URI base = null;
	
	private OWLS_0_9 OWLS = OWLS_0_9.instance;

	/**
	 * 
	 */
	public OWLSWriter_0_9() {
		version = "0.9";
	}

	/* (non-Javadoc)
	 * @see org.mindswap.owls.io.OWLSWriter#write(org.mindswap.owls.service.Service, java.io.Writer)
	 */
	public void write(Service service, Writer out) {
		model = ModelFactory.createDefaultModel();
		model.setNsPrefix("service", OWLS.Service.URI);
		model.setNsPrefix("profile", OWLS.Profile.URI);
		model.setNsPrefix("process", OWLS.Process.URI);
		model.setNsPrefix("grounding", OWLS.Grounding.URI);
		
		base = service.getFileURI();
		
		RDFWriter writer = model.getWriter("RDF/XML-ABBREV");
		// Jena does not let to use relative URI's if we don't disable this check
		writer.setProperty("allowBadURIs", "true");
		// we don't want literals to b eused as attributes
		writer.setProperty("blockRules", new Resource[] {RDFSyntax.propertyAttr});
		// show xml header <?xml version="..." ...?>
		writer.setProperty("showXmlDeclaration", "true");
		
		writeService(service);
		
		writer.write(model, out, "");
	}

	private Resource toRDF(URI u) {
		if(base != null) u = base.relativize(u);
		return Util.toResource(u);
	}
	
	private Resource toRDF(OWLResource r) {
		return toRDF(r.getURI());
	}
	
	private RDFNode toRDF(String o) {
		return toRDF(o, false);
	}

	private RDFNode toRDF(String o, boolean wellFormed) {
		return model.createLiteral(o, wellFormed);
	}

	private void addStatement(Object s, Object p, Object o) {
		Resource subj = null;
		Property prop = null;
		RDFNode  obj  = null;
		
		if(s instanceof Resource)
			subj = (Resource) s;
		else if(s instanceof OWLResource)
			subj = toRDF((OWLResource) s);
		else if(s instanceof URI)
			subj = toRDF((URI) s);
		else
			throw new RuntimeException("Invalid subject " + s + " " + s.getClass());
		
		if(p instanceof Property)
			prop = (Property) p;
		else if(p instanceof URI)
			prop = Util.toProperty((URI) p);
		else
			throw new RuntimeException("Invalid property " + p + " " + p.getClass());
		
		if(o instanceof RDFNode)
			obj = (RDFNode) o;
		else if(o instanceof OWLResource)
			obj = toRDF((OWLResource) o);
		else if(o instanceof URI)
			obj = toRDF((URI) o);
		else if(o instanceof String)
			obj = toRDF((String) o);
		else
			throw new RuntimeException("Invalid object " + o + " " + o.getClass());
		
		model.add(subj, prop, obj);
	}
	
	private void writeService(Service service) {
		addStatement(service, RDF.type, OWLS.Service.Service);
		addStatement(service, OWLS.Service.presents, service.getProfile());
		addStatement(service, OWLS.Service.describedBy, service.getProcessModel());
		addStatement(service, OWLS.Service.supports, service.getGrounding());
		
		writeProfile(service.getProfile());
		writeProcessModel(service.getProcessModel());
		writeGrounding(service.getGrounding());
	}

	private void writeProfile(Profile profile) {
		URI profileType = profile.getType();
				
		for(int i = 0; i < OWLSFactory.supportedVersions.length; i++) {
			String version = OWLSFactory.supportedVersions[i];
			OWLS vocabulary = OWLSFactory.getVocabulary(version);
			if(profileType.equals(vocabulary.getProfile().Profile)) {
				profileType = OWLS.Profile.Profile;
				break;
			}
		}
		
		addStatement(profile, RDF.type, profileType);
		addStatement(profile, OWLS.Service.presentedBy, profile.getService());
		
		if(profile.getLabel() != null) {
			addStatement(profile, OWLS.Profile.serviceName, profile.getLabel());
			addStatement(profile, RDFS.label, profile.getLabel());
		}		
		if(profile.getTextDescription() != null)
			addStatement(profile, OWLS.Profile.textDescription, profile.getTextDescription());
		
		writeProfileParams(profile, profile.getInputs());
		writeProfileParams(profile, profile.getOutputs());
	}

	private void writeProfileParams(Profile profile, ParameterList params) {
		for(int i = 0; i < params.size(); i++) {
			Parameter param = params.parameterAt(i);

			Resource paramDesc = ResourceFactory.createResource();
			if(param instanceof Input)
				addStatement(profile, OWLS.Profile.input, paramDesc);			
			else
				addStatement(profile, OWLS.Profile.output, paramDesc);
			
			addStatement(paramDesc, RDF.type, OWLS.Profile.ParameterDescription);
			addStatement(paramDesc, OWLS.Profile.parameterName, param.getLabel());
			addStatement(paramDesc, RDFS.label, param.getLabel());		
			addStatement(paramDesc, OWLS.Profile.restrictedTo, param.getType());
			addStatement(paramDesc, OWLS.Profile.refersTo, param);
		}
	}

	private void writeProcessModel(ProcessModel processModel) {
		addStatement(processModel, RDF.type, OWLS.Process.ProcessModel);
		addStatement(processModel, OWLS.Service.describes, OWLS.Process.AtomicProcess);
		addStatement(processModel, OWLS.Process.hasProcess, processModel.getProcess());
		
		writeProcess(processModel.getProcess());
	}
	
	private void writeProcess(Process process) {
		if(process instanceof AtomicProcess) 
			writeAtomicProcess((AtomicProcess) process);
		else if(process instanceof CompositeProcess) 
			writeCompositeProcess((CompositeProcess) process);

		writeProcessParams(process, process.getInputs());
		writeProcessParams(process, process.getOutputs());
		
		writeDefaultValues(process);
		writeDataFlow(process);			
	}
	
	private void writeAtomicProcess(AtomicProcess process) {			
		addStatement(process, RDF.type, OWL.Class);
		addStatement(process, RDFS.subClassOf, OWLS.Process.AtomicProcess);
	}

⌨️ 快捷键说明

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