public virtual void SetExtractRules(SequenceMatchRules.IExtractRule <ICoreMap, T> basicExtractRule, SequenceMatchRules.IExtractRule <IList <ICoreMap>, T> compositeExtractRule, IPredicate <T> filterRule)
 {
     CoreMapExpressionExtractor.Stage <T> stage = new CoreMapExpressionExtractor.Stage <T>();
     stage.basicExtractRule     = basicExtractRule;
     stage.compositeExtractRule = compositeExtractRule;
     stage.filterRule           = filterRule;
     this.stages.Clear();
     this.stages[1] = stage;
 }
        private Pair <IList <ICoreMap>, IList <T> > ApplyCompositeRule <_T0>(SequenceMatchRules.IExtractRule <IList <ICoreMap>, T> compositeExtractRule, IList <_T0> merged, IList <T> matchedExpressions, int limit)
            where _T0 : ICoreMap
        {
            // Apply higher order rules
            bool done = false;
            // Limit of number of times rules are applied just in case
            int maxIters = limit;
            int iters    = 0;

            while (!done)
            {
                IList <T> newExprs  = new List <T>();
                bool      extracted = compositeExtractRule.Extract(merged, newExprs);
                if (verbose && extracted)
                {
                    log.Info("applyCompositeRule() extracting with " + compositeExtractRule + " from " + merged + " gives " + newExprs);
                }
                if (extracted)
                {
                    AnnotateExpressions(merged, newExprs);
                    newExprs = MatchedExpression.RemoveNullValues(newExprs);
                    if (!newExprs.IsEmpty())
                    {
                        newExprs = MatchedExpression.RemoveNested(newExprs);
                        newExprs = MatchedExpression.RemoveOverlapping(newExprs);
                        merged   = MatchedExpression.ReplaceMerged(merged, newExprs);
                        // Favor newly matched expressions over older ones
                        Sharpen.Collections.AddAll(newExprs, matchedExpressions);
                        matchedExpressions = MatchedExpression.RemoveNested(newExprs);
                        matchedExpressions = MatchedExpression.RemoveOverlapping(matchedExpressions);
                    }
                    else
                    {
                        extracted = false;
                    }
                }
                done = !extracted;
                iters++;
                if (maxIters > 0 && iters >= maxIters)
                {
                    if (verbose)
                    {
                        log.Warn("Aborting application of composite rules: Maximum iteration " + maxIters + " reached");
                    }
                    break;
                }
            }
            return(new Pair <IList <ICoreMap>, IList <T> >(merged, matchedExpressions));
        }
 // TODO: Remove templating of MatchedExpressions<?>  (keep for now until TimeExpression rules can be decoupled)
 /* Keeps temporary tags created by extractor */
 /* Collapses extraction rules - use with care */
 private static SequenceMatchRules.IExtractRule <I, O> AddRule <I, O>(SequenceMatchRules.IExtractRule <I, O> origRule, SequenceMatchRules.IExtractRule <I, O> rule)
 {
     SequenceMatchRules.ListExtractRule <I, O> r;
     if (origRule is SequenceMatchRules.ListExtractRule)
     {
         r = (SequenceMatchRules.ListExtractRule <I, O>)origRule;
     }
     else
     {
         r = new SequenceMatchRules.ListExtractRule <I, O>();
         if (origRule != null)
         {
             r.AddRules(origRule);
         }
     }
     r.AddRules(rule);
     return(r);
 }
        public virtual IList <T> ExtractExpressions(ICoreMap annotation)
        {
            // Extract potential expressions
            IList <T>   matchedExpressions = new List <T>();
            IList <int> stageIds           = new List <int>(stages.Keys);

            stageIds.Sort();
            foreach (int stageId in stageIds)
            {
                CoreMapExpressionExtractor.Stage <T>          stage            = stages[stageId];
                SequenceMatchRules.IExtractRule <ICoreMap, T> basicExtractRule = stage.basicExtractRule;
                if (stage.clearMatched)
                {
                    matchedExpressions.Clear();
                }
                if (basicExtractRule != null)
                {
                    basicExtractRule.Extract(annotation, matchedExpressions);
                    if (verbose && matchedExpressions != null)
                    {
                        log.Info("extractExpressions() extracting with " + basicExtractRule + " from " + annotation + " gives " + matchedExpressions);
                    }
                    AnnotateExpressions(annotation, matchedExpressions);
                    matchedExpressions = MatchedExpression.RemoveNullValues(matchedExpressions);
                    matchedExpressions = MatchedExpression.RemoveNested(matchedExpressions);
                    matchedExpressions = MatchedExpression.RemoveOverlapping(matchedExpressions);
                }
                IList <ICoreMap> merged = MatchedExpression.ReplaceMergedUsingTokenOffsets(annotation.Get(tokensAnnotationKey), matchedExpressions);
                SequenceMatchRules.IExtractRule <IList <ICoreMap>, T> compositeExtractRule = stage.compositeExtractRule;
                if (compositeExtractRule != null)
                {
                    Pair <IList <ICoreMap>, IList <T> > p = ApplyCompositeRule(compositeExtractRule, merged, matchedExpressions, stage.limitIters);
                    merged             = p.First();
                    matchedExpressions = p.Second();
                }
                matchedExpressions = FilterInvalidExpressions(stage.filterRule, matchedExpressions);
            }
            matchedExpressions.Sort(MatchedExpression.ExprTokenOffsetsNestedFirstComparator);
            if (!keepTags)
            {
                CleanupTags(annotation);
            }
            return(matchedExpressions);
        }
 private void AddBasicRule(SequenceMatchRules.IExtractRule <ICoreMap, T> rule)
 {
     basicExtractRule = AddRule(basicExtractRule, rule);
 }
 private void AddCompositeRule(SequenceMatchRules.IExtractRule <IList <ICoreMap>, T> rule)
 {
     compositeExtractRule = AddRule(compositeExtractRule, rule);
 }