/* * $Id$ * * Copyright (C) INRIA, 2011 * * Modifications to the initial code base are copyright of their * respective authors, or their employers as appropriate. Authorship * of the modifications may be determined from the ChangeLog placed at * the end of this file. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */ package org.ontologymatching.book; // Alignment API classes import org.semanticweb.owl.align.Cell; import org.semanticweb.owl.align.Alignment; import org.semanticweb.owl.align.AlignmentException; import org.semanticweb.owl.align.AlignmentProcess; // Alignment API implementation classes import fr.inrialpes.exmo.align.parser.AlignmentParser; import fr.inrialpes.exmo.align.impl.ObjectAlignment; import fr.inrialpes.exmo.align.impl.URIAlignment; import fr.inrialpes.exmo.ontowrap.OntologyFactory; // Jena import com.hp.hpl.jena.util.iterator.ExtendedIterator; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.SimpleSelector; import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.ontology.OntClass; import com.hp.hpl.jena.ontology.OntProperty; import com.hp.hpl.jena.ontology.Individual; // Java standard classes import java.util.Hashtable; import java.util.Properties; import java.util.Set; import java.util.HashSet; import java.io.FileInputStream; import java.io.InputStream; import java.io.File; import java.net.URI; import java.net.URLEncoder; /** * ComposedAlignment * * Solves the C.16 exercise of the book by importing instances of o in o' */ // Beware currently online files are NOT complete enough public class Translate { /** Creation **/ public Translate() {} // Main (static) public static void main (String args[]) throws Exception { OntologyFactory.setDefaultFactory( "fr.inrialpes.exmo.ontowrap.jena25.JENAOntologyFactory" ); new Translate().run(); } public void run () throws Exception { // Loads the alignment and the ontologies under Jena Alignment al = new AlignmentParser(0).parse( new URI( "file:refalign.rdf" ) ); ObjectAlignment oal = ObjectAlignment.toObjectAlignment( (URIAlignment)al ); OntModel onto1 = (OntModel)oal.getOntology1(); OntModel onto2 = (OntModel)oal.getOntology2(); // Transform transform( onto1, onto2, oal ); } public void transform( OntModel onto1, OntModel onto2, Alignment al ) { try { Hashtable ht = new Hashtable(); URI base = al.getOntology2URI(); // For each correspondence involving a class, transform individuals for ( Cell c : al ) { OntClass cl = onto1.getOntClass( c.getObject1AsURI( al ).toString() ); if ( cl != null && ( c.getRelation().getRelation().equals("=") || c.getRelation().getRelation().equals("<") ) ) { OntClass cl2 = onto2.getOntClass( c.getObject2AsURI( al ).toString() ); for ( Individual i : listInstances( cl ) ) { String id = base.getScheme()+base.getSchemeSpecificPart()+URLEncoder.encode( i.getLabel(""), "UTF-8" ); Individual im = onto2.createIndividual( id, cl2 ); ht.put( i.getURI(), im ); System.out.println( im.getURI()+" rdf:type "+im.getOntClass()+" ." ); } } } // For each correspondence involving a property, transform statements for ( Cell c : al ) { OntProperty pr = onto1.getOntProperty( c.getObject1AsURI( al ).toString() );; if ( pr != null ) { OntProperty pr2 = onto2.getOntProperty( c.getObject2AsURI( al ).toString() ); for ( StmtIterator stmtit = onto1.listStatements( (Resource)null, pr, (RDFNode)null ); stmtit.hasNext() ; ) { Statement st = stmtit.nextStatement(); Individual subj = (Individual)ht.get( st.getSubject().getURI() ); if ( subj != null ) { RDFNode obj = st.getObject(); if ( obj.isResource() ) { obj = (Individual)ht.get( obj.asResource().getURI() ); } if ( obj != null && !obj.isAnon() ) { // do not translate onto2.createStatement( subj, pr2, obj ); System.out.println( subj+" "+pr2+" "+obj+" ." ); } } } } } } catch ( Exception ex ) { ex.printStackTrace(); } } // Apparently, I am not able to make listInstances work in Jena // So I wrote mine public Set listInstances( OntClass cl ) { HashSet result = new HashSet(); for ( ExtendedIterator intit = cl.listInstances( true ); intit.hasNext() ; ) { result.add( (Individual)intit.next() ); } for ( ExtendedIterator clit = cl.listSubClasses( true ); clit.hasNext() ; ) { result.addAll( listInstances( (OntClass)clit.next() ) ); } return result; } }