gt: 		OWLClass cl = new OWLClass();
context: /*******************************************************************************
   * Copyright (c) 2005, Kobrix Software, Inc.
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the GNU Lesser Public License v2.1
   * which accompanies this distribution, and is available at
   * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
   * 
   * Contributors:
   *     Borislav Iordanov - initial API and implementation
   *     Murilo Saraiva de Queiroz - initial API and implementation
   ******************************************************************************/
  package disko.data.owl;
  
  import java.io.File;
  import java.text.SimpleDateFormat;
  import java.util.HashMap;
  import java.util.HashSet;
  
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.hypergraphdb.HGHandle;
  import org.hypergraphdb.HGQuery.hg;
  import org.hypergraphdb.atom.HGSubsumes;
  import org.hypergraphdb.HGTypeSystem;
  import org.hypergraphdb.HyperGraph;
  
  import com.hp.hpl.jena.ontology.Individual;
  import com.hp.hpl.jena.ontology.OntClass;
  import com.hp.hpl.jena.ontology.OntModel;
  import com.hp.hpl.jena.ontology.OntModelSpec;
  import com.hp.hpl.jena.ontology.OntProperty;
  import com.hp.hpl.jena.rdf.model.ModelFactory;
  import com.hp.hpl.jena.rdf.model.Statement;
  
  import disko.flow.analyzers.hgdb.HGDBSaver;
  
  /**
   * 
   * <p>
   * Use the Jena Semantic Framework to import a OWL file in HGDB.
   * </p>
   *
   * @author Borislav Iordanov
   *
   */
  public class OWLImporter
  {
  	private static Log log = LogFactory.getLog(HGDBSaver.class);
  	
  	private HyperGraph graph;
  	private String namespace;
  	HGHandle ontologyHandle = null;	
  	
  	private static final String TYPE_PREDICATE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
  	
  	private void prepare()
  	{		
  		log.debug("Preparing OWL import, creating types etc.");
  		OWLClassConstructor owlClassConstructor = graph.get(OWLClassConstructor.OWL_CLASS_CONSTRUCTOR_HANDLE);
  		if (owlClassConstructor == null)
  		{
  			owlClassConstructor = new OWLClassConstructor();
  			owlClassConstructor.setHyperGraph(graph);
  			graph.getTypeSystem().addPredefinedType(OWLClassConstructor.OWL_CLASS_CONSTRUCTOR_HANDLE, 
  												    owlClassConstructor, 
  												    OWLClass.class);
  		}
  		graph.getTypeSystem().getTypeHandle(OWLOntology.class);
  		graph.getTypeSystem().getTypeHandle(OntologyMember.class);
  		graph.getTypeSystem().getTypeHandle(OWLDomainLink.class);
  		graph.getTypeSystem().getTypeHandle(OWLProperty.class);		
  		log.debug("Done with OWL import preparation.");
  	}
  	
  	private HGHandle getPrimitiveType(String url)
  	{
  		HGTypeSystem ts = graph.getTypeSystem();
  		if (url.indexOf("XMLSchema") < 0)
  			return null;
  		else if (url.endsWith("#string"))
  			return ts.getTypeHandle(String.class);
  		else if (url.endsWith("#int"))
  			return ts.getTypeHandle(Integer.class);
  		else if (url.endsWith("#boolean"))
  			return ts.getTypeHandle(Boolean.class);
  		else if (url.endsWith("#float"))
  			return ts.getTypeHandle(Float.class);
  		else if (url.endsWith("#double"))
  			return ts.getTypeHandle(Double.class);
  		else if (url.endsWith("#date") || url.endsWith("#dateTime") || url.endsWith("#time"))
  			return ts.getTypeHandle(java.util.Date.class);
  		else if (url.endsWith("#anyURI"))
  			return ts.getTypeHandle(java.net.URI.class);		
  		else
  			throw new IllegalArgumentException("Unkown primitive XML Schema type: " + url);
  	}
  	
  	private java.util.Date parseDate(String s) throws Exception
  	{
  		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  		return sdf.parse(s);
  	}
  	
  	private HGHandle getLiteralAtom(String literal)
  	{
  		String [] parts = literal.split("\\^\\^");		
  		HGHandle type = getPrimitiveType(parts[1]);
  		if (type == null)
  			return null;		
  		
  		Object value = null;
  		if (parts[1].endsWith("#string"))
  			value = parts[0];
  		else if (parts[1].endsWith("#int"))
  			try { value = Integer.parseInt(parts[0]); } catch (Exception ex) { throw new RuntimeException(ex); }
  		else if (parts[1].endsWith("#boolean"))
  			value = Boolean.valueOf(parts[0]);
  		else if (parts[1].endsWith("#float"))
  			try { value = Float.parseFloat(parts[0]); } catch (Exception ex) { throw new RuntimeException(ex); }
  		else if (parts[1].endsWith("#double"))
  			try { value = Double.parseDouble(parts[0]); } catch (Exception ex) { throw new RuntimeException(ex); }
  		else if (parts[1].endsWith("#date") || parts[1].endsWith("#dateTime") || parts[1].endsWith("#time"))
  			try { value = parseDate(parts[0]); } catch (Exception ex) { throw new RuntimeException(ex); }
  		else if (parts[1].endsWith("#anyURI"))
  			try { value = new java.net.URI(parts[0]); } catch (Exception ex) { throw new RuntimeException(ex); }		
  		
  		if (value == null)
  			return null;
  		
  		HGHandle result = null;		
  		result = hg.findOne(graph, hg.and(hg.type(type), hg.eq(value)));
  		if (result == null)
  			result = graph.add(value);		
  		return result;
  	}
  	
  	private void getSimpleOntClassSet(Iterator<?> ontClassIterator, Set<HGHandle> set)
  	{
  		while (ontClassIterator.hasNext())
  		{
  			OntClass dom = (OntClass)ontClassIterator.next();			
  			if (dom.isUnionClass())
  				getSimpleOntClassSet(dom.asUnionClass().listOperands(), set);
  			else if (!dom.isAnon())
  			{
  				HGHandle clHandle = hg.findOne(graph, hg.and(hg.type(OWLClass.class), 
  						 hg.eq("localName", dom.getLocalName())));
  				if (clHandle == null)
  					throw new RuntimeException("Could not find handle for domain " + dom.getLocalName());
  				log.debug("Found non-anonymous domain : " + dom);
  				set.add(clHandle);				
  			}				
  		}
  	}
  	
  	private void importProperty(OntProperty p)
  	{
  		log.debug("Importing property " + p);
  		if (p.getRange() == null && p.isDatatypeProperty())
  			throw new RuntimeException("WTF? datatype property '" + p.getLocalName() +  "' has no range!");
  		Set<HGHandle> domain = new HashSet<HGHandle>();
  		getSimpleOntClassSet(p.listDomain(), domain);
  		if (domain.isEmpty())
  			domain.add(graph.getTypeSystem().getTop());
  		HGHandle rangeHandle = null;
  		if (p.isObjectProperty())
  			if (p.getRange() != null && !p.getRange().isAnon())
  			{
  				rangeHandle = hg.findOne(graph, hg.and(hg.type(OWLClass.class), 
  													   hg.eq("localName", p.getRange().getLocalName())));
  				log.debug("Property range is an OWL object: " + p.getRange().getLocalName());
  			}
  			else
  			{
  				log.debug("Object property range is top level OWL::Thing");
  				rangeHandle = graph.getTypeSystem().getTop();
  			}
  		else
  		{
  			rangeHandle = getPrimitiveType(p.getRange().toString());
  			log.debug("Property range is a primitive " + p.getRange());
  		}
  		if (rangeHandle == null)
  			throw new RuntimeException("Could not find handle for range " + p.getRange() + " of property " + p);
  		OWLProperty owlProp = new OWLProperty();
  		owlProp.setLocalName(p.getLocalName());
  		owlProp.setHyperGraph(graph);
  		owlProp.setRangeType(rangeHandle);
  		HGHandle propHandle = graph.add(owlProp);		
  		graph.getTypeSystem().addAlias(propHandle, p.getURI());
  		graph.add(new OntologyMember(ontologyHandle, propHandle));
  		log.debug("Added property " + p.getURI() + " at handle " + propHandle);		
  		for (HGHandle dh : domain)
  		{			
  			graph.add(new OWLDomainLink(propHandle, dh));
  			log.debug("Domain linked property " + propHandle + " and " + dh);
  		}
  	}
  	
  	private HGHandle importClass(OntClass ontcl, Set<OntClass> occurs)
  	{
  		occurs.add(ontcl);
  		log.debug("Importing class " + ontcl);
  