All pastes #392224 Raw Edit

Something

public text v1 · immutable
·published 2007-03-12 19:18 UTC
rendered paste body
package oppronto;


import java.io.ByteArrayOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.shared.NotFoundException;
import com.hp.hpl.jena.util.*;
import com.hp.hpl.jena.vocabulary.*;

// add identityPredicate, and OnlineResource 'contains' (plus inverse), and Agent 'has' (+inv) relations...
// OnlineIdentity cannot hold more than one identity

public class OpprontoModel {
	public static final String NS = "http://cies.breijs.com/ns/oppronto/0.1/oppronto#";
	private List<URL> nextUrls = new ArrayList<URL>();
	private HashSet<URL> visitedUrls = new HashSet<URL>();
	private HashMap<Resource, Resource> resourceIdentityPredicates = new HashMap<Resource, Resource>();
	private InfModel infmodel;

	public OpprontoModel(String ontologyUrl, String projectUrl) {
		ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
		Model schema = FileManager.get().loadModel(ontologyUrl);
		Model data = FileManager.get().loadModel(projectUrl);
		Reasoner  reasoner = ReasonerRegistry.getOWLMicroReasoner();
		reasoner = reasoner.bindSchema(schema);
		infmodel = ModelFactory.createInfModel(reasoner, data);
		checkValidity();

		System.out.println("\nrdfs:seeAlsos:");
		seeAlsos();

		ListIterator<URL> iter = nextUrls.listIterator();
		while (iter.hasNext()) {
			URL url = iter.next();
			visitedUrls.add(url);
			try {
				data.add(FileManager.get().loadModel(url.toString()));
				System.out.println("Loaded: " + url);
			} catch (NotFoundException e) {
				System.out.println("Could not find: " + url);
			}
		}

		infmodel = ModelFactory.createInfModel(reasoner, data);

		checkValidity();

		seeAlsos();
		findIdentityPredicates();
		connectOnlineIdentitiesWithOnlineResources();
	}

	public InfModel fullModel() {
		return infmodel;
	}
	
	public String query(String q) {
		Query query = QueryFactory.create(q);
		QueryExecution qe = QueryExecutionFactory.create(query, infmodel);
		ResultSet results = qe.execSelect();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ResultSetFormatter.out(out, results, query);
		qe.close();
		return out.toString();
	}	

	private boolean checkValidity() {
		ValidityReport validity = infmodel.validate();
		if (validity.isValid()) {
			System.out.println("OK");
			return true;
		}
		System.out.println("Conflicts");
		for (Iterator i = validity.getReports(); i.hasNext();) {
			ValidityReport.Report report = (ValidityReport.Report) i.next();
			System.out.println(" - " + report);
		}
		return false;
	}

	private boolean checkStrictValidity() {
		// how to check for the use of undefined tags (like: <codeName>)
		// how to check for requirements (with cardinality? or with relax NG)
		// how to check for proper includes and legal URI's over more than one
		// file
		return false;
	}

	private void seeAlsos() {
		NodeIterator iter = infmodel.listObjectsOfProperty(RDFS.seeAlso);
		while (iter.hasNext()) {
			String url = iter.next().toString();
			System.out.println(" - " + url);  // it can be a literal or a resource
			try {
				addURL(new URL(url));
			} catch (MalformedURLException e) {
				System.out.println("Found malformed URL: " + url);
			}
		}
	}

	private void connectOnlineIdentitiesWithOnlineResources() {
		Property did = infmodel.getProperty(NS, "did");  // use much later but set here _once_
		ArrayList<Resource[]> relations = userResourcePartRelations();
		for (int i = 0; i < relations.size(); i++) {
			System.out.println(" === " + relations.get(i)[0].toString() + " " + did.toString() + " " + relations.get(i)[1].toString());
			infmodel.add(relations.get(i)[0], did, (RDFNode)relations.get(i)[1]);
		}
	}
	
	
	private ArrayList<Resource[]> userResourcePartRelations() {
		ArrayList<Resource[]> result = new ArrayList<Resource[]>();
		Property holdsOnlineIdentityPredicate = infmodel.getProperty(NS, "holdsOnlineIdentity");
		StmtIterator iter = infmodel.listStatements(null, holdsOnlineIdentityPredicate, (RDFNode)null);
		//listObjectsOfProperty(identityPredicate);
		while (iter.hasNext()) {
			Statement stmt = iter.nextStatement();
			System.out.println(" - " + PrintUtil.print(stmt));

			Property identityPredicate = infmodel.getProperty(NS, "identity");
			StmtIterator _iter = infmodel.listStatements((Resource)stmt.getObject(), identityPredicate, (RDFNode)null);
			String agentIdentifier = null;
			while (_iter.hasNext()) {
				Statement _stmt = _iter.nextStatement();
				agentIdentifier = _stmt.getString();
				System.out.println(" --+ " + agentIdentifier);
			}
			
			if (agentIdentifier != null) {
				Property onOnlineResourcePredicate = infmodel.getProperty(NS, "onOnlineResource");
				_iter = infmodel.listStatements((Resource)stmt.getObject(), onOnlineResourcePredicate, (RDFNode)null);
				while (_iter.hasNext()) {
					Statement _stmt = _iter.nextStatement();					
					Resource onlineResource = (Resource)_stmt.getObject();
					Resource identifyingPropertyResource = resourceIdentityPredicates.get(onlineResource);
					System.out.println(" --> " + onlineResource.toString());
					if (identifyingPropertyResource != null) {
						Property identifyingProperty = infmodel.getProperty(identifyingPropertyResource.getURI());
						StmtIterator __iter = infmodel.listStatements(null, identifyingProperty, (RDFNode)null);
						while (__iter.hasNext()) {
							Statement __stmt = __iter.nextStatement();
							System.out.println(" >>> " + PrintUtil.print(__iter));
							if (__stmt.getObject().toString().equals(agentIdentifier)) {
								Resource[] resourceList = new Resource[2];
								resourceList[0] = stmt.getSubject();
								resourceList[1] = __stmt.getSubject();
								result.add(resourceList);
							}
						}
					}
				}
			}
		}
		return result;
	}
	
	// some method that adds relations between all the statements in the seeAlso-linked
	// resources to the OnlineResource they belong to would be nice.
	private void findIdentityPredicates() {
		Property identityPredicate = infmodel.getProperty(NS, "identityPredicate");
		StmtIterator iter = infmodel.listStatements(null, identityPredicate, (RDFNode)null);
		//listObjectsOfProperty(identityPredicate);
		while (iter.hasNext()) {
			Statement stmt = iter.nextStatement();
			System.out.println(" - " + PrintUtil.print(stmt));
			resourceIdentityPredicates.put(stmt.getSubject(), (Resource)stmt.getObject());
		}
	}

	private void addURL(URL url) {
		if (!visitedUrls.contains(url)) {
			nextUrls.add(url);
			visitedUrls.add(url);
		} else {
			System.out.println("Already seen: " + url);
		}
	}
}