result.add(new Action(stateExactMatch.value, new TermMatches(new TermMatch(term)), 0, 1)); new Action(stateForPrefix.value, new TermMatches( new TermMatch(term, matches.add(new TermMatch((Term) element)); result.add(new Action( stateExactMatch.value, matches, pos - matches.size() + ofs, pos + ofs)); result.add(new Action( stateForPrefix.value, matches, pos - matches.size() + ofs, pos + ofs)); result.add(new Action( stateExactMatch.value, new TermMatches(new TermMatch((Term) element)), pos, pos + 1)); if (isTerm) { final Term term = (Term) element; result.add(new Action( stateForPrefix.value, new TermMatches(new TermMatch(term, true, term.subSequence(stateForPrefix.index + 1, term.length()))), pos, pos + 1));
protected void applySequence(PositionSequence<Term> sequence, boolean addBoundaries) { PositionSequence<InputSequenceElement> sequenceForLookUp = addBoundaries ? addBoundaries(sequence) : termSequenceToInputSequence(sequence); boolean isDebug = Boolean.TRUE.equals(context.get(CONTEXT_KEY_DEBUG_ENABLED)); List<String> actionsDebugInfo = (List<String>) context.get(CONTEXT_KEY_DEBUG_DATA); // prepare debug info context object if requested if (isDebug && actionsDebugInfo == null) { actionsDebugInfo = new LinkedList<>(); context.put(CONTEXT_KEY_DEBUG_DATA, actionsDebugInfo); } for (Action action : rules.getRewriteActions(sequenceForLookUp)) { if (isDebug) { actionsDebugInfo.add(action.toString()); } for (Instructions instructions : action.getInstructions()) { for (Instruction instruction : instructions) { instruction.apply(sequence, action.getTermMatches(), action.getStartPosition(), action.getEndPosition(), expandedQuery, context); } } } }
@Test public void testMultipleTermsPerPosition() throws Exception { RulesCollectionBuilder builder = new TrieMapRulesCollectionBuilder(false); String s1 = "test1"; String s2 = "test2"; Input input1 = new Input(inputTerms(null, s1), false, false); Input input2 = new Input(inputTerms(null, s2), false, false); Instructions instructions1 = instructions("instruction1"); builder.addRule(input1, instructions1); Instructions instructions2 = instructions("instruction2"); builder.addRule(input2, instructions2); // Input is just s1 RulesCollection rulesCollection = builder.build(); PositionSequence<InputSequenceElement> sequence = new PositionSequence<>(); sequence.nextPosition(); sequence.addElement(new Term(null, s1)); List<Action> actions = rulesCollection.getRewriteActions(sequence); assertThat(actions, contains(new Action(Arrays.asList(instructions1), termMatches(s1), 0, 1))); sequence.addElement(new Term(null, s2)); actions = rulesCollection.getRewriteActions(sequence); assertThat(actions, contains(new Action(Arrays.asList(instructions1), termMatches(s1), 0, 1), new Action(Arrays.asList(instructions2), termMatches(s2), 0, 1))); }
assertThat(actions, contains(new Action(Arrays.asList(instructions1), termMatches(s1), 0, 1))); assertThat(actions, contains(new Action(Arrays.asList(instructions2), termMatches(s2), 0, 1))); new Action(Arrays.asList(instructions2), termMatches(s2), 0, 1), new Action(Arrays.asList(instructions1), termMatches(s1), 1, 2))
List<Action> actions = rules.getRewriteActions(seq); assertThat(actions, contains( new Action( Arrays.asList( new Instructions(
@Test public void testSingeInputSingleInstruction() { RulesCollectionBuilder builder = new TrieMapRulesCollectionBuilder(false); String s1 = "test"; Input input = new Input(inputTerms(null, s1), false, false); Instructions instructions = instructions("instruction1"); builder.addRule(input, instructions); RulesCollection rulesCollection = builder.build(); PositionSequence<InputSequenceElement> sequence = new PositionSequence<>(); sequence.nextPosition(); sequence.addElement(new Term(null, s1)); List<Action> actions = rulesCollection.getRewriteActions(sequence); assertThat(actions, contains(new Action(Arrays.asList(instructions), termMatches(s1), 0, 1))); }
@Test public void testSingleInputTwoInstructionsFromSameRule() { RulesCollectionBuilder builder = new TrieMapRulesCollectionBuilder(false); String s1 = "test"; Input input = new Input(inputTerms(null, s1), false, false); Instructions instructions = instructions("instruction1", "instruction2"); builder.addRule(input, instructions); RulesCollection rulesCollection = builder.build(); PositionSequence<InputSequenceElement> sequence = new PositionSequence<>(); sequence.nextPosition(); sequence.addElement(new Term(null, s1)); List<Action> actions = rulesCollection.getRewriteActions(sequence); assertThat(actions, contains(new Action(Arrays.asList(instructions), termMatches(s1), 0, 1))); }
@Test public void testTwoMatchingInputsOnePartial() throws Exception { RulesCollectionBuilder builder = new TrieMapRulesCollectionBuilder(false); String s1 = "test1"; String s2 = "test2"; Input input1 = new Input(inputTerms(null, s1, s2), false, false); Input input2 = new Input(inputTerms(null, s2), false, false); Instructions instructions1 = instructions("instruction1"); Instructions instructions2 = instructions("instruction2"); builder.addRule(input2, instructions2); builder.addRule(input1, instructions1); // Input is s1 s2 RulesCollection rulesCollection = builder.build(); PositionSequence<InputSequenceElement> sequence = new PositionSequence<>(); sequence.nextPosition(); sequence.addElement(new Term(null, s1)); sequence.nextPosition(); sequence.addElement(new Term(null, s2)); List<Action> actions = rulesCollection.getRewriteActions(sequence); assertThat(actions, contains( new Action(Arrays.asList(instructions1), termMatches(s1, s2), 0, 2), new Action(Arrays.asList(instructions2), termMatches(s2), 1, 2) ) ); }
@Test public void test05() throws Exception { RulesCollection rules = createRulesFromResource("rules-test.txt", false); Term t1 = new Term(null, "tf2"); PositionSequence<InputSequenceElement> seq = new PositionSequence<>(); seq.nextPosition(); seq.addElement(t1); List<Action> actions = rules.getRewriteActions(seq); assertThat(actions, contains( new Action( Arrays.asList( new Instructions( Arrays.asList( (Instruction) new FilterInstruction(makeQueryUsingFactory("flt2 flt3"))) )), new TermMatches(new TermMatch(t1)), 0, 1) )); }
@Test public void test01() throws Exception { RulesCollection rules = createRulesFromResource("rules-test.txt", false); Term t1 = new Term(null, "aa"); Term t2 = new Term(null, "l"); PositionSequence<InputSequenceElement> seq = new PositionSequence<>(); seq.nextPosition(); seq.addElement(t1); seq.nextPosition(); seq.addElement(t2); List<Action> actions = rules.getRewriteActions(seq); assertThat(actions, contains( new Action( Arrays.asList( new Instructions(Arrays.asList((Instruction) new DeleteInstruction(Arrays.asList(mkTerm("aa")))))), new TermMatches(new TermMatch(t1)), 0, 1))); }
@Test public void testSameInputTwoInstructionsFromDiffentRules() { RulesCollectionBuilder builder = new TrieMapRulesCollectionBuilder(false); String s1 = "test"; Input input = new Input(inputTerms(null, s1), false, false); Instructions instructions1 = instructions("instruction1"); builder.addRule(input, instructions1); Instructions instructions2 = instructions("instruction2"); builder.addRule(input, instructions2); RulesCollection rulesCollection = builder.build(); PositionSequence<InputSequenceElement> sequence = new PositionSequence<>(); sequence.nextPosition(); sequence.addElement(new Term(null, s1)); List<Action> actions = rulesCollection.getRewriteActions(sequence); assertThat(actions, contains(new Action(Arrays.asList(instructions1, instructions2), termMatches(s1), 0, 1))); }
actions = rulesCollection.getRewriteActions(sequence); assertThat(actions, contains(new Action(Arrays.asList(instructions1), new TermMatches(Arrays.asList(new TermMatch(term11), new TermMatch(term22))), 0, 2))); sequence.clear(); actions = rulesCollection.getRewriteActions(sequence); assertThat(actions, contains(new Action(Arrays.asList(instructions1), new TermMatches(Arrays.asList(new TermMatch(term12), new TermMatch(term21))), 0, 2)));
List<Action> actions = rules.getRewriteActions(seq); assertThat(actions, contains( new Action( Arrays.asList( new Instructions(
new Action( Arrays.asList( new Instructions(
@Test public void test04() throws Exception { RulesCollection rules = createRulesFromResource("rules-test.txt", false); Term t1 = new Term(null, "t1"); Term t2 = new Term(null, "t2"); PositionSequence<InputSequenceElement> seq = new PositionSequence<>(); seq.nextPosition(); seq.addElement(t1); seq.nextPosition(); seq.addElement(t2); List<Action> actions = rules.getRewriteActions(seq); assertThat(actions, contains( new Action( Arrays.asList( new Instructions( Arrays.asList( (Instruction) new BoostInstruction(makeQueryUsingFactory("tboost tb2"), BoostDirection.UP, 3.5f)) )), new TermMatches(Arrays.asList(new TermMatch(t1), new TermMatch(t2))), 0, 2) )); }
new Action( Arrays.asList( new Instructions(
new Action( Arrays.asList( new Instructions(
List<Action> actions = rules.getRewriteActions(seq); assertThat(actions, contains( new Action( Arrays.asList( new Instructions( new RawQuery(null, "color:x", Occur.SHOULD, false), BoostDirection.DOWN ,2f)))), new TermMatches(new TermMatch(t1)), 0, 1), new Action( Arrays.asList( new Instructions( new TermMatches(Arrays.asList(new TermMatch(t1), new TermMatch(t2))), 0, 2), new Action( Arrays.asList( new Instructions(
new Action( Arrays.asList( new Instructions(