Search in sources :

Example 6 with Matcher

use of nl.basjes.parse.useragent.analyze.Matcher in project yauaa by nielsbasjes.

the class AbstractUserAgentAnalyzerDirect method getAllPossibleFieldNames.

public Set<String> getAllPossibleFieldNames() {
    if (allPossibleFieldNamesCache == null) {
        synchronized (this) {
            if (allPossibleFieldNamesCache == null) {
                Set<String> names = new TreeSet<>(CORE_SYSTEM_GENERATED_FIELDS);
                if (wantedFieldNames == null) {
                    for (Matcher matcher : allMatchers) {
                        names.addAll(matcher.getAllPossibleFieldNames());
                    }
                    for (FieldCalculator calculator : fieldCalculators) {
                        names.add(calculator.getCalculatedFieldName());
                    }
                } else {
                    for (Matcher matcher : allMatchers) {
                        // Not all wanted fields are possible !
                        for (String possibleFieldName : matcher.getAllPossibleFieldNames()) {
                            if (wantedFieldNames.contains(possibleFieldName)) {
                                names.add(possibleFieldName);
                            }
                        }
                    }
                    for (FieldCalculator calculator : fieldCalculators) {
                        String possibleFieldName = calculator.getCalculatedFieldName();
                        if (wantedFieldNames.contains(possibleFieldName)) {
                            names.add(possibleFieldName);
                        }
                    }
                    names.remove(SET_ALL_FIELDS);
                }
                allPossibleFieldNamesCache = names;
            }
        }
    }
    return allPossibleFieldNamesCache;
}
Also used : Matcher(nl.basjes.parse.useragent.analyze.Matcher) TreeSet(java.util.TreeSet) FieldCalculator(nl.basjes.parse.useragent.calculate.FieldCalculator)

Example 7 with Matcher

use of nl.basjes.parse.useragent.analyze.Matcher in project yauaa by nielsbasjes.

the class AbstractUserAgentAnalyzerDirect method initializeMatchers.

public synchronized void initializeMatchers() {
    if (matchersHaveBeenInitialized) {
        return;
    }
    LOG.info("Initializing Analyzer data structures");
    if (allMatchers.isEmpty()) {
        throw new InvalidParserConfigurationException("No matchers were loaded at all.");
    }
    long start = System.nanoTime();
    allMatchers.forEach(Matcher::initialize);
    long stop = System.nanoTime();
    matchersHaveBeenInitialized = true;
    LOG.info("Built in {} msec : Hashmap {}, Ranges map:{}", (stop - start) / 1000000, informMatcherActions.size(), informMatcherActionRanges.size());
    for (Matcher matcher : allMatchers) {
        if (matcher.getActionsThatRequireInput() == 0) {
            zeroInputMatchers.add(matcher);
        }
    }
    // Reset all Matchers
    for (Matcher matcher : allMatchers) {
        matcher.reset();
    }
    touchedMatchers = new MatcherList(32);
}
Also used : MatcherList(nl.basjes.parse.useragent.analyze.MatcherList) InvalidParserConfigurationException(nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException) Matcher(nl.basjes.parse.useragent.analyze.Matcher)

Example 8 with Matcher

use of nl.basjes.parse.useragent.analyze.Matcher in project yauaa by nielsbasjes.

the class DebugUserAgent method toMatchTrace.

String toMatchTrace(List<String> highlightNames) {
    StringBuilder sb = new StringBuilder(4096);
    sb.append('\n');
    sb.append("+=========================================+\n");
    sb.append("| Matcher results that have been combined |\n");
    sb.append("+=========================================+\n");
    sb.append('\n');
    appliedMatcherResults.sort((o1, o2) -> {
        Matcher m1 = o1.getValue();
        Matcher m2 = o2.getValue();
        return m1.getMatcherSourceLocation().compareTo(m2.getMatcherSourceLocation());
    });
    for (Pair<UserAgent, Matcher> pair : appliedMatcherResults) {
        sb.append('\n');
        sb.append("+================\n");
        sb.append("+ Applied matcher\n");
        sb.append("+----------------\n");
        UserAgent result = pair.getLeft();
        Matcher matcher = pair.getRight();
        sb.append(matcher.toString());
        sb.append("+----------------\n");
        sb.append("+ Results\n");
        sb.append("+----------------\n");
        for (String fieldName : result.getAvailableFieldNamesSorted()) {
            AgentField field = result.get(fieldName);
            if (field.getConfidence() >= 0) {
                String marker = "";
                if (highlightNames.contains(fieldName)) {
                    marker = " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
                }
                sb.append("| ").append(fieldName).append('(').append(field.getConfidence());
                if (field.isDefaultValue()) {
                    sb.append(" => isDefaultValue");
                }
                sb.append(") = ").append(field.getValue()).append(marker).append('\n');
            }
        }
        sb.append("+================\n");
    }
    return sb.toString();
}
Also used : Matcher(nl.basjes.parse.useragent.analyze.Matcher) AgentField(nl.basjes.parse.useragent.AgentField) UserAgent(nl.basjes.parse.useragent.UserAgent) MutableUserAgent(nl.basjes.parse.useragent.UserAgent.MutableUserAgent)

Aggregations

Matcher (nl.basjes.parse.useragent.analyze.Matcher)8 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AgentField (nl.basjes.parse.useragent.AgentField)2 UserAgent (nl.basjes.parse.useragent.UserAgent)2 MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)2 InvalidParserConfigurationException (nl.basjes.parse.useragent.analyze.InvalidParserConfigurationException)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 Nonnull (javax.annotation.Nonnull)1 ImmutableUserAgent (nl.basjes.parse.useragent.UserAgent.ImmutableUserAgent)1 MatcherList (nl.basjes.parse.useragent.analyze.MatcherList)1 Match (nl.basjes.parse.useragent.analyze.MatchesList.Match)1 UselessMatcherException (nl.basjes.parse.useragent.analyze.UselessMatcherException)1 FieldCalculator (nl.basjes.parse.useragent.calculate.FieldCalculator)1 MatcherConfig (nl.basjes.parse.useragent.config.MatcherConfig)1 TestCase (nl.basjes.parse.useragent.config.TestCase)1 UserAgentTreeFlattener (nl.basjes.parse.useragent.parse.UserAgentTreeFlattener)1