Package org.apache.devicemap.data

Examples of org.apache.devicemap.data.Device


        if (devices == null) {
            throw new RuntimeException("Uninitialized device index");
        }

        Map<String, List<Device>> hits = new HashMap<String, List<Device>>(100);
        Device winner = null;
        String winnerStr = "";

        if (text == null) {
            return getUnknownDevice();
        }

        Util.debugLog("classify: '" + text + "'");

        String[] parts = text.split(" |-|_|/|\\\\|\\[|\\]|\\(|\\)|;");

        //generate ngrams upto size 4
        for (int i = 0; i < parts.length; i++) {
            String pattern = "";
            for (int j = 0; j < 4 && (j + i) < parts.length; j++) {
                if (parts[i + j].isEmpty()) {
                    continue;
                }
               
                pattern += Util.normalize(parts[i + j]);

                List<Device> dlist = patterns.get(pattern);

                if (dlist != null) {
                    hits.put(pattern, dlist);

                    for (Device device : dlist) {
                        Util.debugLog("Hit found: " + pattern + " => " + device.getId() + " " + device.getPatterns());
                    }
                }
            }
        }

        //look for the strongest hit
        for (String hit : hits.keySet()) {
            for (Device device : hits.get(hit)) {
                if (!device.getPatterns().isValid(hits.keySet())) {
                    continue;
                }

                Util.debugLog("Hit candidate: " + hit + " => " + device.getId());

                if (winner != null) {
                    if ("simple".equals(winner.getType()) && !"simple".equals(device.getType())) {
                        winner = device;
                        winnerStr = hit;
                    } else if (hit.length() > winnerStr.length() &&
                            (!"simple".equals(device.getType()) || device.getType().equals(winner.getType()))) {
                        winner = device;
                        winnerStr = hit;
                    }
                } else {
                    winner = device;
                    winnerStr = hit;
                }
            }
        }

        if (winner != null) {
            Util.debugLog("Result: " + winner);
            return winner.getAttributes();
        } else {
            return getUnknownDevice();
        }
    }
View Full Code Here


     * loads device data from an InputStreamReader
     */
    private void loadDeviceData(Reader in) throws IOException {
        XMLParser parser = new XMLParser(in);
        String tag;
        Device device = new Device();
        Map<String, String> attributes = new HashMap<String, String>();

        while (!(tag = parser.getNextTag()).isEmpty()) {
            //new device found
            if (tag.startsWith("<device ")) {
                device.setId(XMLParser.getAttribute(tag, "id"));
                device.setParentId(XMLParser.getAttribute(tag, "parentId"));
            } else if (tag.equals("</device>")) {

                //add the device
                if (device.getId() != null && !device.getId().isEmpty()) {
                    attributes.put("id", device.getId());
                    device.setAttributes(attributes);
                    devices.put(device.getId(), device);
                }

                //reset the device
                device = new Device();
                attributes = new HashMap<String, String>();
            } else if (tag.startsWith("<property ")) {
                //add the property to the device
                String key = XMLParser.getAttribute(tag, "name");
                String value = XMLParser.getAttribute(tag, "value");
View Full Code Here

     */
    private void loadDevicePatterns(Reader in) throws IOException {
        XMLParser parser = new XMLParser(in);
        String tag;
        String builder = "";
        Device device = null;
        String id = "";
        List<String> patterns = new ArrayList<String>();

        while (!(tag = parser.getNextTag()).isEmpty()) {
            //new builder found
            if (tag.startsWith("<builder ")) {
                builder = XMLParser.getAttribute(tag, "class");

                if (builder.lastIndexOf(".") >= 0) {
                    builder = builder.substring(builder.lastIndexOf(".") + 1);
                }
            } else if (tag.startsWith("<device ")) {
                //new device found
                id = XMLParser.getAttribute(tag, "id");
                device = devices.get(id);
            } else if (tag.equals("</device>")) {
                //add the device
                if (device != null) {
                    //TwoStep is an AND pattern, also index the unigram
                    if (builder.equals("TwoStepDeviceBuilder")) {
                        device.getPatterns().setAndPattern(patterns);

                        String unigram = "";

                        for (String pattern : patterns) {
                            if (pattern.contains(unigram)) {
                                unigram = pattern;
                            } else {
                                unigram += pattern;
                            }
                        }

                        device.getPatterns().setPattern(unigram);
                    } else {
                        device.getPatterns().setOrPattern(patterns);
                    }

                    if (builder.equals("SimpleDeviceBuilder")) {
                        device.setType("simple");
                    } else {
                        device.setType("weak");
                    }
                } else {
                    Util.debugLog("ERROR: device not found: '" + id + "'");
                }

View Full Code Here

        if (parentId == null) {
            return;
        }

        Device parent = devices.get(parentId);

        if (parent == null) {
            return;
        }

        mergeParent(parent);

        for (String key : parent.getAttributes().keySet()) {
            String value = parent.getAttributes().get(key);

            if (!device.getAttributes().containsKey(key)) {
                device.getAttributes().put(key, value);
            }
        }
View Full Code Here

TOP

Related Classes of org.apache.devicemap.data.Device

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.