Package enzyme.ontology

Source Code of enzyme.ontology.OntologyDataSource

package enzyme.ontology;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

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.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;

import enzyme.database.Doc;

//��������Դ
public class OntologyDataSource {
  //��������
  private OntModel model;
  //�����·��
  private String ontologyPath;
  //Ȩֵ�ļ�·��
  private String weightsPath;
  //������������
  private IndexSearcher searcher;
  //������ȡ����
  private IndexReader reader;
 
  //����Ŀ¼��ַ
  private String indexPath;
  //������
  private Analyzer analyzer;

  public OntologyDataSource() {
  }

  //���ر����
  public void loadOntology(String absolutePath) {
    ontologyPath = absolutePath;
    try {

      FileInputStream fin = new FileInputStream(new File(ontologyPath));
      InputStreamReader fr = new InputStreamReader(fin, "utf-8");
      getOntModel().read(fr, "");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }

  private OntModel getOntModel() {
    if (model == null) {
      model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
    }
    return model;
  }

  //����ǰ׺�����ļ�
  public void loadPrefixConfiguration(String absolutePath) {
    Properties properties = new Properties();
    try {
      properties.load(new FileInputStream(new File(absolutePath)));
      for (Object key : properties.keySet()) {

        getOntModel().setNsPrefix(key.toString(),
            properties.getProperty(key.toString()));
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  public void setWeightsPath(String weightsPath) {
    this.weightsPath = weightsPath;
  }

  //��������
  public void loadIndex(String absolutePath) {
    indexPath = absolutePath;
    try {
      reader = IndexReader.open(indexPath);

      searcher = new IndexSearcher(reader);
//      analyzer = new ChineseOntologyAnalyzer(getOntModel(), weightsPath);
      analyzer = new WhitespaceAnalyzer();
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  //���¼�������
  private void reloadIndex() {
    loadIndex(indexPath);
    System.out.println("Index reloaded!");
  }

  //���ڱ�����ĵ�ȫ�ļ���
  public SearchResult search(String keywords, int startIndex, int endIndex) {
   
    //�ж��Ƿ���Ҫ���¼��ر����
    try {
      boolean isCurrent = this.reader.isCurrent();
      if (!isCurrent) {
        reloadIndex();
      }
    } catch (IOException e1) {
      e1.printStackTrace();
      return null;
    }

    //����������������
    QueryParser parser = new QueryParser("docContent", analyzer);
    try {
      //��������
      Query query = parser.parse(keywords);
     
      //��ü������
      Hits hits = searcher.search(query);
     
      //�����������ҳ
      List<Doc> docs = this.processHits(hits, startIndex, endIndex);
     
      //���ؼ������
      return new SearchResult(hits.length(), docs);

    } catch (ParseException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  //��չǰ׺
  private String expandPrefix(String shortForm) {
    if (shortForm.indexOf(":") < 0) {
      shortForm = ":" + shortForm;
    }
    return this.getOntModel().expandPrefix(shortForm);
  }

  //��������
  public QueryResult query(String s, String p) {
   
    //����
    String subject = this.expandPrefix(s);
    //ν��
    String predicate = this.expandPrefix(p);

    //�����������ڵ���
    if (RDF.type.getURI().equals(predicate) || "��".equals(p)) {
      if (this.getOntModel().getOntClass(subject) != null) {
        return null;
      }
      Individual s_i = this.getOntModel().getIndividual(subject);
      if (s_i != null) {
        QueryResult qr = new QueryResult(OntologyElement.ONTCLASS);
        qr.add(wrap(s_i.getOntClass()));
        return qr;
      }
      return null;
    }
    //������ĸ���
    else if (RDFS.subClassOf.getURI().equals(predicate) || "����".equals(p)) {
      OntClass s_c = this.getOntModel().getOntClass(subject);
      if (s_c != null) {
        QueryResult qr = new QueryResult(OntologyElement.ONTCLASS);
        OntClass sp = s_c.getSuperClass();
        if (sp != null)
          qr.add(wrap(sp));
        return qr;
      }
      return null;
    }
    //�����������
    else if ("����".equals(p)) {

      OntClass s_c = this.getOntModel().getOntClass(subject);
      if (s_c != null) {
        QueryResult qr = new QueryResult(OntologyElement.ONTCLASS);
        Iterator it = s_c.listSubClasses();
        while (it.hasNext()) {
          OntClass sp = (OntClass) it.next();
          if (sp != null) {
            qr.add(wrap(sp));
          }
        }
        return qr;
      }
      return null;
    }
   
    //�������ԵĶ�����
    else if (RDFS.domain.getURI().equals(predicate) || "������".equals(p)
        || "domain".equals(p)) {
      OntProperty s_p = this.getOntModel().getOntProperty(subject);
      if (s_p != null) {
        QueryResult qr = new QueryResult(OntologyElement.ONTCLASS);
        qr.add(wrap(s_p.getDomain().asClass()));
        return qr;
      }
      return null;
    }
   
    //�������Ե�ֵ��
    else if (RDFS.range.getURI().equals(predicate) || "ֵ��".equals(p)
        || "range".equals(p)) {
      OntProperty s_p = this.getOntModel().getOntProperty(subject);
      if (s_p != null) {
        QueryResult qr = new QueryResult(OntologyElement.ONTCLASS);
        OntResource range = s_p.getRange();
        if (range != null) {
          qr.add(wrap(range.asClass()));
          return qr;
        }
        return null;
      }
      return null;
    }
   
    //�������ʵ��
    else if ("ʵ��".equals(p)) {
      OntClass s_c = this.getOntModel().getOntClass(subject);
      if (s_c != null) {
        QueryResult qr = new QueryResult(OntologyElement.INDIVIDUAL);
        Iterator it = s_c.listInstances();
        while (it.hasNext()) {
          Individual ind = (Individual) it.next();
          if (ind != null) {
            qr.add(wrap(ind));
          }
        }
        return qr;
      }
      return null;
    }
   
    //��������ı���
    Individual s_i = this.getOntModel().getIndividual(subject);
    if (s_i != null) {
      OntProperty p_p = this.getOntModel().getOntProperty(predicate);
      if (p_p != null) {
        QueryResult qr;
        if (p_p.isObjectProperty()) {
          qr = new QueryResult(OntologyElement.INDIVIDUAL);
        } else if (p_p.isDatatypeProperty()) {
          qr = new QueryResult(OntologyElement.LITERAL);
        } else {
          return null;
        }

        NodeIterator it = s_i.listPropertyValues(p_p);
        while (it.hasNext()) {
          RDFNode node = it.nextNode();
          if (node.isLiteral()
              && OntologyElement.LITERAL.equals(qr.getType())) {
            qr.add(wrap((Literal) node));
          } else if (node.canAs(Individual.class)
              && OntologyElement.INDIVIDUAL.equals(qr.getType())) {
            qr.add(wrap((Individual) node.as(Individual.class)));
          }
        }
        return qr;
      }
      return null;
    }

    return null;
  }

  //��װһ������
  private IndividualOE wrap(Individual indv) {
    IndividualOE wp = new IndividualOE(indv.getURI(), indv.getLocalName());
    wp.setTheOntClass(wrap(indv.getOntClass(), false));
    return wp;
  }

  //��װһ����������
  private LiteralOE wrap(Literal literal) {
    return new LiteralOE(literal.getString());
  }

  //��װһ����
  private OntClassOE wrap(OntClass cls) {
    return wrap(cls, true);
  }

  //��װһ���࣬���丸��
  private OntClassOE wrap(OntClass cls, boolean withSuperClass) {
    OntClassOE wp = new OntClassOE(cls.getURI(), cls.getLocalName());
    OntClass sp = cls.getSuperClass();
    if (withSuperClass && sp != null) {
      wp.setSuperClass(wrap(sp, false));
    }
    return wp;
  }

  //���������ҳ
  private List<Doc> processHits(Hits hits, int startIndex, int endIndex)
      throws IOException {
    if (endIndex >= hits.length())
      endIndex = hits.length() - 1;
    List<Doc> docs = new ArrayList<Doc>();
    for (int i = startIndex; i <= endIndex; i++) {
      Document d = hits.doc(i);
      docs.add(createDoc(d));
    }
    return docs;
  }

  //����һ���ĵ�����
  private Doc createDoc(Document d) {
    Doc doc = new Doc();
    doc.setDocAuthor(d.getField("docAuthor").stringValue());
    doc.setDocFilePath(d.getField("docFilePath").stringValue());
    doc.setDocKey(d.getField("docKey").stringValue());
    doc.setDocPublishDate(d.getField("docPublishDate").stringValue());
    doc.setDocTitle(d.getField("docTitle").stringValue());
    doc.setDocTypeId(Long.parseLong(d.getField("docTypeId").stringValue()));
    doc.setId(Long.parseLong(d.getField("id").stringValue()));
    return doc;

  }

  public static void main(String[] args) {

    OntologyDataSource ods = new OntologyDataSource();
    String ontology = "c:\\Enzyme.owl";
    String prefix =  "c:\\prefix.properties";
    String index= "c:\\index";
    String weights =  "c:\\Weights.properties";
   
    ods.loadOntology(ontology);
    ods.loadPrefixConfiguration(prefix);
   
    ods.setWeightsPath(weights);
    ods.loadIndex(index);

  }
}
TOP

Related Classes of enzyme.ontology.OntologyDataSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.