Java tutorial
/* * Copyright 2014 JBoss, by Red Hat, Inc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.drools.workbench.screens.drltext.backend.server.indexing; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopScoreDocCollector; import org.drools.workbench.screens.drltext.type.DRLResourceTypeDefinition; import org.junit.Test; import org.kie.workbench.common.services.refactoring.backend.server.BaseIndexingTest; import org.kie.workbench.common.services.refactoring.backend.server.TestIndexer; import org.kie.workbench.common.services.refactoring.backend.server.indexing.RuleAttributeNameAnalyzer; import org.kie.workbench.common.services.refactoring.model.index.terms.RuleAttributeIndexTerm; import org.kie.workbench.common.services.refactoring.model.index.terms.RuleAttributeValueIndexTerm; import org.uberfire.ext.metadata.backend.lucene.index.LuceneIndex; import org.uberfire.ext.metadata.engine.Index; import org.uberfire.ext.metadata.io.KObjectUtil; import org.uberfire.java.nio.file.Path; import static org.apache.lucene.util.Version.*; import static org.junit.Assert.*; public class IndexRuleAttributeNameAndValueTest extends BaseIndexingTest<DRLResourceTypeDefinition> { @Test public void testIndexDrlRuleAttributeNameAndValue() throws IOException, InterruptedException { //Add test files final Path path = basePath.resolve("drl1.drl"); final String drl = loadText("drl1.drl"); ioService().write(path, drl); Thread.sleep(5000); //wait for events to be consumed from jgit -> (notify changes -> watcher -> index) -> lucene index final Index index = getConfig().getIndexManager().get(KObjectUtil.toKCluster(basePath.getFileSystem())); { final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher(); final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true); final BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term(RuleAttributeIndexTerm.TERM, "ruleflow-group")), BooleanClause.Occur.MUST); query.add(new TermQuery(new Term(RuleAttributeValueIndexTerm.TERM, "nonexistent")), BooleanClause.Occur.MUST); searcher.search(query, collector); final ScoreDoc[] hits = collector.topDocs().scoreDocs; assertEquals(0, hits.length); ((LuceneIndex) index).nrtRelease(searcher); } //This simply checks whether there is a Rule Attribute "ruleflow-group" and a Rule Attribute Value "myRuleflowGroup" //The specific query does not check that the Rule Attribute Value corresponds to the Rule Attribute, so it is possible //that the value relates to a different Rule Attribute. { final IndexSearcher searcher = ((LuceneIndex) index).nrtSearcher(); final TopScoreDocCollector collector = TopScoreDocCollector.create(10, true); final BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term(RuleAttributeIndexTerm.TERM, "ruleflow-group")), BooleanClause.Occur.MUST); query.add(new TermQuery(new Term(RuleAttributeValueIndexTerm.TERM, "myruleflowgroup")), BooleanClause.Occur.MUST); searcher.search(query, collector); final ScoreDoc[] hits = collector.topDocs().scoreDocs; assertEquals(1, hits.length); ((LuceneIndex) index).nrtRelease(searcher); } } @Override protected TestIndexer getIndexer() { return new TestDrlFileIndexer(); } @Override public Map<String, Analyzer> getAnalyzers() { return new HashMap<String, Analyzer>() { { put(RuleAttributeIndexTerm.TERM, new RuleAttributeNameAnalyzer(LUCENE_40)); } }; } @Override protected DRLResourceTypeDefinition getResourceTypeDefinition() { return new DRLResourceTypeDefinition(); } @Override protected String getRepositoryName() { return this.getClass().getSimpleName(); } }