All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.ucm.fdi.gaia.jcolibri.connector.OntologyConnector Maven / Gradle / Ivy

Go to download

jCOLIBRI is a java framework for the development of Case-Based Reasoning systems.

There is a newer version: 3.2
Show newest version
/**
 * OntologyConnector.java
 * jCOLIBRI2 framework. 
 * @author Juan A. Recio-Garc�a.
 * GAIA - Group for Artificial Intelligence Applications
 * http://gaia.fdi.ucm.es
 * 07/06/2007
 */
package es.ucm.fdi.gaia.jcolibri.connector;

import java.io.FileWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.logging.log4j.LogManager;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseBaseFilter;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseComponent;
import es.ucm.fdi.gaia.jcolibri.cbrcore.Connector;
import es.ucm.fdi.gaia.jcolibri.connector.ontologyutils.OntologyInfo;
import es.ucm.fdi.gaia.jcolibri.connector.ontologyutils.OntologyMapping;
import es.ucm.fdi.gaia.jcolibri.datatypes.Instance;
import es.ucm.fdi.gaia.jcolibri.exception.InitializingException;
import es.ucm.fdi.gaia.jcolibri.util.FileIO;
import es.ucm.fdi.gaia.jcolibri.util.OntoBridgeSingleton;
import es.ucm.fdi.gaia.jcolibri.util.ProgressController;
import es.ucm.fdi.gaia.ontobridge.OntoBridge;
import es.ucm.fdi.gaia.ontobridge.OntologyDocument;

/**
 * Implements a generic Ontology connector.
 * It uses OntoBridge to manage the ontologies and the reasoner.
 * To configure this connector create a configuration xml file following this schema:
 * /doc/configfilesSchemas/OntologyConnector.xsd:

* *

* This connector only maps case structures without compound attributes. * All attributes must be Instance typed. * * For a complete example see Test 10. * * @author Juan A. Recio-Garcia * @version 2.0 */ public class OntologyConnector implements Connector { private Class descriptionClass; private Class solutionClass; private Class justOfSolutionClass; private Class resultClass; private OntologyInfo mainOntologyInfo; private ArrayList subOntologiesInfo; private String CaseMainConcept; private ArrayList descriptionMappings; private ArrayList solutionMappings; private ArrayList justOfSolutionMappings; private ArrayList resultMappings; private boolean modified; private OntologyInfo getOntologyInfo(Node node) { OntologyInfo oi = new OntologyInfo(); NodeList ontologyNodes = node.getChildNodes(); for(int i=0; i descriptionMappings) { NodeList mappingNodes = mappings.getChildNodes(); for(int i=0; i(); NodeList subOntologiesNodes = doc.getElementsByTagName("SubOntology"); for(int i=0; i(); getOntologyMappings(mappings, descriptionMappings); /* Solution mapping */ try{ this.solutionClass = Class.forName(doc.getElementsByTagName("SolutionClassName").item(0).getTextContent()); mappings = doc.getElementsByTagName("SolutionMappings").item(0); this.solutionMappings = new ArrayList(); getOntologyMappings(mappings, solutionMappings); }catch(Exception e) {} /* JustOfSolution mapping */ try{ this.justOfSolutionClass = Class.forName(doc.getElementsByTagName("JustificationOfSolutionClassName").item(0).getTextContent()); mappings = doc.getElementsByTagName("JustificationOfSolutionMappings").item(0); this.justOfSolutionMappings = new ArrayList(); getOntologyMappings(mappings, justOfSolutionMappings); }catch(Exception e) {} /* result mapping */ try{ this.resultClass = Class.forName(doc.getElementsByTagName("ResultClassName").item(0).getTextContent()); mappings = doc.getElementsByTagName("ResultMappings").item(0); this.resultMappings = new ArrayList(); getOntologyMappings(mappings, resultMappings); }catch(Exception e) {} // Now let's initialize Ontobridge // Obtain a reference to OntoBridge OntoBridge ob = es.ucm.fdi.gaia.jcolibri.util.OntoBridgeSingleton.getOntoBridge(); // Configure it to work with the Pellet reasoner ob.initWithPelletReasoner(); // Setup the main ontology OntologyDocument mainOnto = new OntologyDocument(this.mainOntologyInfo.getUrl(), FileIO.findFile(this.mainOntologyInfo.getLocalCopy()).toExternalForm()); // Setup subontologies ArrayList subOntologies = new ArrayList(); for(OntologyInfo oi : this.subOntologiesInfo) { OntologyDocument subOnto = new OntologyDocument(oi.getUrl(), FileIO.findFile(oi.getLocalCopy()).toURI().toString()); subOntologies.add(subOnto); } // Load the ontology ob.loadOntology(mainOnto, subOntologies, false); // Set modified to false this.modified = false; } catch (Exception e) { throw new InitializingException(e); } } /* (non-Javadoc) * @see jcolibri.cbrcore.Connector#retrieveAllCases() */ public List retrieveAllCases() { //Result list ArrayList cases = new ArrayList(); //Obtain OntoBridge OntoBridge ob = OntoBridgeSingleton.getOntoBridge(); ProgressController.init(this.getClass(), "Loading concepts", ProgressController.UNKNOWN_STEPS); //Obtain instances Iterator caseInstances = ob.listInstances(this.CaseMainConcept); while(caseInstances.hasNext()) { String caseInstance = caseInstances.next(); CBRCase _case = new CBRCase(); try { //Map description CaseComponent description = (CaseComponent)this.descriptionClass.newInstance(); retrieveCaseComponent(ob, description, caseInstance, this.descriptionMappings); _case.setDescription(description); //Map solution if(this.solutionClass != null) { CaseComponent cc = (CaseComponent)this.solutionClass.newInstance(); retrieveCaseComponent(ob, cc, caseInstance, this.solutionMappings); _case.setSolution(cc); } //Map justification of solution if(this.justOfSolutionClass != null) { CaseComponent cc = (CaseComponent)this.justOfSolutionClass.newInstance(); retrieveCaseComponent(ob, cc, caseInstance, this.justOfSolutionMappings); _case.setJustificationOfSolution(cc); } //Map result solution if(this.resultClass != null) { CaseComponent cc = (CaseComponent)this.resultClass.newInstance(); retrieveCaseComponent(ob, cc, caseInstance, this.resultMappings); _case.setResult(cc); } // If everything ok add the case to the list cases.add(_case); } catch (Exception e) { LogManager.getLogger(this.getClass()).error(e); } ProgressController.step(this.getClass()); } ProgressController.finish(this.getClass()); return cases; } private void retrieveCaseComponent(OntoBridge ob, CaseComponent cc, String mainInstanceName, ArrayList mappings) throws Exception { //Id Instance id = new Instance(mainInstanceName); cc.getIdAttribute().setValue(cc, id); //Other attributes for(OntologyMapping om: mappings) { // Obtain CaseComponent attribute Attribute at = new Attribute(om.getAttribute(), cc.getClass()); // Find values of the property. It could have several values. Iterator values = ob.listPropertyValue(mainInstanceName, om.getProperty()); // Find which value is instance of the concept boolean found = false; while(values.hasNext() && !found) { String valueInstance = values.next(); if(ob.isInstanceOf(valueInstance, om.getConcept())) { found = true; Instance concept = new Instance(valueInstance); at.setValue(cc, concept); } } } } /** * UnImplemented. * @see Connector#retrieveSomeCases(CaseBaseFilter) */ public List retrieveSomeCases(CaseBaseFilter filter) { LogManager.getLogger(this.getClass()).error("retrieveSomeCases(CaseBaseFilter) method is not yet implemented"); return null; } /** * Stores cases into the ontology. * @see Connector#storeCases(Collection) */ public void storeCases(Collection cases) { if(cases.isEmpty()) return; else modified = true; //Obtain OntoBridge OntoBridge ob = OntoBridgeSingleton.getOntoBridge(); ProgressController.init(this.getClass(), "Storing concepts/cases", cases.size()); for(CBRCase _case: cases) { try { if(!ob.existsInstance(_case.getID().toString(),this.CaseMainConcept)) ob.createInstance(this.CaseMainConcept, _case.getID().toString()); createCaseComponent(_case.getDescription(), this.descriptionMappings); createCaseComponent(_case.getSolution(), this.solutionMappings); createCaseComponent(_case.getJustificationOfSolution(), this.justOfSolutionMappings); createCaseComponent(_case.getResult(), this.resultMappings); } catch (Exception e) { LogManager.getLogger(this.getClass()).error("Error storing case: "+_case+". Cause: "+ e.getMessage()); } ProgressController.step(this.getClass()); } ProgressController.finish(this.getClass()); } private void createCaseComponent(CaseComponent cc, ArrayList maps) throws Exception { if((cc == null)||(maps==null)) return; OntoBridge ob = OntoBridgeSingleton.getOntoBridge(); String mainInstance = cc.getIdAttribute().getValue(cc).toString(); for(OntologyMapping om: maps) { Attribute at = new Attribute(om.getAttribute(), cc.getClass()); String instance = at.getValue(cc).toString(); if(!ob.existsInstance(instance,om.getConcept())) ob.createInstance(om.getConcept(), instance); ob.createOntProperty(mainInstance, om.getProperty(), instance); } } /** * If there was any modification to the ontology, the owl file is replaced with a new one that contains the changes. * The new owl file is completely regenerated from scrach with the current content of the reasoner (not including the inferred model). * OntoBridge uses the RDF/XML-ABBREV syntax for the owl files. * * @see Connector#close() */ public void close() { if(!modified) return; OntoBridge ob = OntoBridgeSingleton.getOntoBridge(); try { ob.save(new FileWriter(FileIO.findFile(this.mainOntologyInfo.getLocalCopy()).getFile())); } catch (Exception e) { LogManager.getLogger(this.getClass()).error(e); } } /** * Deletes cases in the ontology. Only the main instance (case id mapped instance) is removed, so the instances mapped to attributes are keep. * @see Connector#deleteCases(Collection) */ public void deleteCases(Collection cases) { if(cases.isEmpty()) return; else modified = true; OntoBridge ob = OntoBridgeSingleton.getOntoBridge(); ProgressController.init(this.getClass(), "Deleting concepts/cases", cases.size()); for(CBRCase _case: cases) { ob.delete(_case.getID().toString()); ProgressController.step(this.getClass()); } ProgressController.finish(this.getClass()); } /**************************************************************/ /*********** Public API for the connector configuration */ /**************************************************************/ /** * @return Returns the caseMainConcept. */ public String getCaseMainConcept() { return CaseMainConcept; } /** * @return Returns the descriptionMappings. */ public ArrayList getDescriptionMappings() { return descriptionMappings; } /** * @return Returns the justOfSolutionMappings. */ public ArrayList getJustOfSolutionMappings() { return justOfSolutionMappings; } /** * @return Returns the mainOntologyInfo. */ public OntologyInfo getMainOntologyInfo() { return mainOntologyInfo; } /** * @return Returns the resultMappings. */ public ArrayList getResultMappings() { return resultMappings; } /** * @return Returns the solutionMappings. */ public ArrayList getSolutionMappings() { return solutionMappings; } /** * @return Returns the subOntologiesInfo. */ public ArrayList getSubOntologiesInfo() { return subOntologiesInfo; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy