Search in sources :

Example 1 with JavelinExtractionRule

use of com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule in project convertigo by convertigo.

the class JavelinTransaction method applyExtractionRules.

public void applyExtractionRules(JavelinScreenClass screenClass, BlockFactory blockFactory, iJavelin javelin, boolean bNotFirstLoop) throws EngineException {
    String t = context.statistics.start(EngineStatistics.APPLY_EXTRACTION_RULES);
    try {
        // We apply the extraction rules for this screen class to
        // the words list.
        Block block = null;
        JavelinExtractionRuleResult extractionRuleResult;
        int extractionRuleInitReason;
        boolean panelExtractionRuleFound = false;
        List<ExtractionRule> vExtractionRules = screenClass.getExtractionRules();
        for (ExtractionRule extractionRule : vExtractionRules) {
            JavelinExtractionRule javelinExtractionRule = (JavelinExtractionRule) extractionRule;
            if (!runningThread.bContinue)
                break;
            /*
    			if ((Engine.objectsProvider == null) && !javelin.isConnected()) {
    				throw new ConnectionException("The emulator has been disconnected! See the emulator logs for more details...");
    			}
    			*/
            blockFactory.moveToFirstBlock();
            if (!extractionRule.isEnabled()) {
                // if extraction rule is disabled
                Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because it has been disabled.");
                continue;
            }
            if (blockFactory instanceof PanelBlockFactory && !panelExtractionRuleFound) {
                // if we apply the rules in a panel and the rule is before the rule which has created the panel
                Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because it has been applied before the panel creation.");
                if (extractionRule instanceof Nptui)
                    panelExtractionRuleFound = true;
                continue;
            }
            if (blockFactory instanceof PanelBlockFactory && extractionRule instanceof TabBox) {
                // if we try to apply tabbox rule in a panel
                Block panel = ((PanelBlockFactory) blockFactory).getPanel();
                XMLRectangle zone = ((TabBox) extractionRule).getSelectionScreenZone();
                if (zone.contains(panel.column, panel.line, Integer.parseInt(panel.getOptionalAttribute("width")), Integer.parseInt(panel.getOptionalAttribute("height")))) {
                    // if the tabbox screen zone is larger than the panel
                    Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because the screen zone is larger than the panel.");
                    continue;
                }
            }
            Engine.logContext.debug("(JavelinTransaction) Applying the extraction rule \"" + extractionRule.getName() + "\" on blocks");
            String extractionRuleQName = extractionRule.getQName();
            if (vExtractionRulesInited.contains(extractionRuleQName)) {
                extractionRuleInitReason = ExtractionRule.ACCUMULATING;
            } else {
                extractionRuleInitReason = ExtractionRule.INITIALIZING;
                vExtractionRulesInited.add(extractionRuleQName);
            }
            Engine.logContext.trace("(JavelinTransaction) Initializing extraction rule (reason = " + extractionRuleInitReason + ")...");
            extractionRule.init(extractionRuleInitReason);
            // We fire engine events only in studio mode.
            if (Engine.isStudioMode()) {
                Engine.theApp.fireObjectDetected(new EngineEvent(extractionRule));
            }
            // We try to apply the current extraction rule on each block.
            while (runningThread.bContinue && ((block = blockFactory.getNextBlock(block)) != null)) {
                Engine.logContext.trace("(JavelinTransaction) Analyzing block \"" + block.getText() + "\"");
                // We skip final blocks.
                if (block.bFinal) {
                    Engine.logContext.trace("(JavelinTransaction) The block has been marked as final; skipping it.");
                    continue;
                }
                extractionRuleResult = javelinExtractionRule.apply(javelin, block, blockFactory, context.outputDocument);
                if (extractionRuleResult.hasMatched) {
                    // extraction rules on this block and pass on the next block.
                    if (javelinExtractionRule.isFinal()) {
                        block.bFinal = true;
                        Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': matching and final");
                    } else {
                        Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': matching");
                    }
                    // We need to update the current block only if the
                    // rule has matched.
                    block = extractionRuleResult.newCurrentBlock;
                } else {
                    Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': not matching");
                }
            }
            // We fire engine events only in studio mode.
            if (Engine.isStudioMode()) {
                Engine.theApp.fireBlocksChanged(new EngineEvent(blockFactory));
                Engine.logContext.debug("(JavelinTransaction) Step reached after having applied the extraction rule \"" + extractionRule.getName() + "\".");
                Engine.theApp.fireStepReached(new EngineEvent(extractionRule));
            }
        }
        vExtractionRules = null;
        extractionRuleResult = null;
    } finally {
        context.statistics.stop(t, bNotFirstLoop);
    }
}
Also used : PanelBlockFactory(com.twinsoft.convertigo.beans.common.PanelBlockFactory) JavelinExtractionRule(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule) ExtractionRule(com.twinsoft.convertigo.beans.core.ExtractionRule) JavelinExtractionRule(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule) TabBox(com.twinsoft.convertigo.beans.common.TabBox) JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) XMLRectangle(com.twinsoft.convertigo.beans.common.XMLRectangle) Block(com.twinsoft.convertigo.beans.core.Block) Nptui(com.twinsoft.convertigo.beans.sna.Nptui) EngineEvent(com.twinsoft.convertigo.engine.EngineEvent)

Aggregations

PanelBlockFactory (com.twinsoft.convertigo.beans.common.PanelBlockFactory)1 TabBox (com.twinsoft.convertigo.beans.common.TabBox)1 XMLRectangle (com.twinsoft.convertigo.beans.common.XMLRectangle)1 Block (com.twinsoft.convertigo.beans.core.Block)1 ExtractionRule (com.twinsoft.convertigo.beans.core.ExtractionRule)1 JavelinExtractionRule (com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule)1 JavelinExtractionRuleResult (com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult)1 Nptui (com.twinsoft.convertigo.beans.sna.Nptui)1 EngineEvent (com.twinsoft.convertigo.engine.EngineEvent)1