LuceneMemoryOntologyIndex implements Closeable { public LuceneMemoryOntologyIndex(OWLGraphWrapper ontology, List<String> roots, String dlQuery, List<BranchDetails> branches, ReasonerFactory reasonerFactory) throws IOException { super(); if (logger.isInfoEnabled()) { StringBuilder message = new StringBuilder(); message.append("Start creating lucene memory index for: "); message.append(ontology.getOntologyId()); if (roots != null) { message.append(" Root: "); message.append(roots); } if (branches != null && !branches.isEmpty()) { message.append(" Branches: "); for (BranchDetails branch : branches) { message.append(" ("); message.append(branch.getName()); if (branch.getRoots() != null) { message.append(","); message.append(branch.getRoots()); } else if (branch.getDlQuery() != null) { message.append(","); message.append(branch.getDlQuery()); } message.append(") "); } } logger.info(message.toString()); } Map<String, Analyzer> alternatives = new HashMap<String, Analyzer>(); WhitespaceAnalyzer whitespaceAnalyzer = new WhitespaceAnalyzer(version); alternatives.put(ID_FIELD, whitespaceAnalyzer); if (branches != null && !branches.isEmpty()) { alternatives.put(BRANCH_FIELD, whitespaceAnalyzer); } analyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer(version), alternatives); RAMDirectory directory = new RAMDirectory(); IndexWriterConfig conf = new IndexWriterConfig(version, analyzer); IndexWriter writer = new IndexWriter(directory, conf); Set<OWLObject> allOWLObjects; SharedReasoner reasoner = reasonerFactory.getSharedReasoner(ontology); if (dlQuery != null) { allOWLObjects = reasoner.executeDLQuery(dlQuery, ontology); } else { if (roots == null || roots.isEmpty()) { allOWLObjects = ontology.getAllOWLObjects(); } else { allOWLObjects = getDecendantsReflexive(roots, ontology, reasoner); } } BranchInfos branchInfos = null; if (branches != null) { branchInfos = new BranchInfos(); for (BranchDetails branch : branches) { String name = replaceWhitespaces(branch.getName()); String branchDLQuery = branch.getDlQuery(); List<String> ids = branch.getRoots(); if (ids != null && !ids.isEmpty()) { branchInfos.add(name, getDecendantsReflexive(ids, ontology, reasoner)); } else if (branchDLQuery != null) { branchInfos.add(name, reasoner.executeDLQuery(branchDLQuery, ontology)); } } branchInfos.setup(); } int npeCounter = 0; int obsoleteCounter = 0; for (OWLObject owlObject : allOWLObjects) { boolean isObsolete = ontology.getIsObsolete(owlObject); if (isObsolete) { obsoleteCounter += 1; continue; } String value = ontology.getLabel(owlObject); if (value != null) { Document doc = new Document(); doc.add(new Field(LABEL_FIELD, value, Store.YES, Index.ANALYZED)); doc.add(new Field(LENGTH_FIELD, Integer.toString(value.length()), Store.YES, Index.NO)); List<ISynonym> synonyms = ontology.getOBOSynonyms(owlObject); if (synonyms != null && !synonyms.isEmpty()) { for (ISynonym synonym : synonyms) { String label = synonym.getLabel(); String scope = synonym.getScope(); if (label != null && label.length() > 1) { if (scope == null || OboFormatTag.TAG_RELATED.getTag().equals(scope)) { doc.add(new Field(SYNOYM_FIELD_RELATED, label, Store.YES, Index.NO)); } else if (OboFormatTag.TAG_NARROW.getTag().equals(scope)) { doc.add(new Field(SYNOYM_FIELD_NARROW, label, Store.YES, Index.NO)); } else if (OboFormatTag.TAG_EXACT.getTag().equals(scope)) { doc.add(new Field(SYNOYM_FIELD_EXACT, label, Store.YES, Index.NO)); } else if (OboFormatTag.TAG_BROAD.getTag().equals(scope)) { doc.add(new Field(SYNOYM_FIELD_BROAD, label, Store.YES, Index.NO)); } } } } String def = ontology.getDef(owlObject); if (def != null && def.length() > 1) { doc.add(new Field(DESCRIPTION_FIELD, def, Store.YES, Index.NO)); } try { String identifier = ontology.getIdentifier(owlObject); doc.add(new Field(ID_FIELD, identifier, Store.YES, Index.NOT_ANALYZED)); if (branchInfos != null && branchInfos.isValid()) { List<String> brancheNames = branchInfos.getBranches(owlObject); if (!brancheNames.isEmpty()) { for (String branchName : brancheNames) { doc.add(new Field(BRANCH_FIELD, branchName, Store.NO, Index.NOT_ANALYZED)); } } } writer.addDocument(doc); } catch (NullPointerException exception) { npeCounter += 1; logger.error("NPE for getting an ID for: " + owlObject); } } } writer.close(); searcher = new IndexSearcher(IndexReader.open(directory)); if (logger.isInfoEnabled()) { logger.info("Finished creating index for: " + ontology.getOntologyId()); if (branchInfos != null && branchInfos.isValid()) { logger.info(branchInfos.createSummary()); } if (obsoleteCounter > 0) { logger.info("Skipped " + obsoleteCounter + " obsolete terms during index creation"); } if (npeCounter > 0) { logger.info("During the index creation there were " + npeCounter + " NPEs"); } } } LuceneMemoryOntologyIndex(OWLGraphWrapper ontology,
			List<String> roots,
			String dlQuery,
			List<BranchDetails> branches,
			ReasonerFactory reasonerFactory); Collection<SearchResult> search(String queryString, int maxCount, String branch); @Override void close();  }
@Test public void testLuceneMemoryOntologyIndex() throws Exception { ParserWrapper pw = new ParserWrapper(); OWLGraphWrapper g = pw.parseToOWLGraph("http: ReasonerFactory factory = new ReasonerFactoryImpl(); LuceneMemoryOntologyIndex index = new LuceneMemoryOntologyIndex(g, null, null, null, factory); Collection<SearchResult> results = index.search(" me pigmentation ", 5, null); for (SearchResult searchResult : results) { TermSuggestion suggestion = searchResult.term; String id = suggestion.getIdentifier(); assertNotNull(suggestion.getLabel()); assertNotNull(suggestion.getDescription()); OWLObject owlObject = g.getOWLObjectByIdentifier(id); String label = g.getLabel(owlObject); System.out.println(id + " " + searchResult.score + " " + label); } index.close(); assertEquals(2, results.size()); }