Example usage for com.mongodb BasicDBList get

List of usage examples for com.mongodb BasicDBList get

Introduction

In this page you can find the example usage for com.mongodb BasicDBList get.

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value at an index.

Usage

From source file:com.querydsl.mongodb.MongodbSerializer.java

License:Apache License

private Object negate(BasicDBObject arg) {
    BasicDBList list = new BasicDBList();
    for (Map.Entry<String, Object> entry : arg.entrySet()) {
        if (entry.getKey().equals("$or")) {
            list.add(asDBObject("$nor", entry.getValue()));

        } else if (entry.getKey().equals("$and")) {
            BasicDBList list2 = new BasicDBList();
            for (Object o : ((BasicDBList) entry.getValue())) {
                list2.add(negate((BasicDBObject) o));
            }//from   w  w  w .  j a v a  2 s  .c  om
            list.add(asDBObject("$or", list2));

        } else if (entry.getValue() instanceof Pattern) {
            list.add(asDBObject(entry.getKey(), asDBObject("$not", entry.getValue())));

        } else if (entry.getValue() instanceof BasicDBObject) {
            list.add(negate(entry.getKey(), (BasicDBObject) entry.getValue()));

        } else {
            list.add(asDBObject(entry.getKey(), asDBObject("$ne", entry.getValue())));
        }
    }
    return list.size() == 1 ? list.get(0) : asDBObject("$or", list);
}

From source file:com.sitewhere.mongodb.device.MongoZone.java

License:Open Source License

/**
 * Copy information from Mongo DBObject to model object.
 * //  www. j a  v a2  s . c o  m
 * @param source
 * @param target
 */
public static void fromDBObject(DBObject source, Zone target) {
    String token = (String) source.get(PROP_TOKEN);
    String siteToken = (String) source.get(PROP_SITE_TOKEN);
    String name = (String) source.get(PROP_NAME);
    String borderColor = (String) source.get(PROP_BORDER_COLOR);
    String fillColor = (String) source.get(PROP_FILL_COLOR);
    Double opacity = (Double) source.get(PROP_OPACITY);

    target.setToken(token);
    target.setSiteToken(siteToken);
    target.setName(name);
    target.setBorderColor(borderColor);
    target.setFillColor(fillColor);
    target.setOpacity(opacity);

    List<Location> locs = new ArrayList<Location>();
    BasicDBList coords = (BasicDBList) source.get(PROP_COORDINATES);
    for (int i = 0; i < coords.size(); i++) {
        DBObject coord = (DBObject) coords.get(i);
        Location loc = new Location();
        loc.setLatitude((Double) coord.get(MongoDeviceLocation.PROP_LATITUDE));
        loc.setLongitude((Double) coord.get(MongoDeviceLocation.PROP_LONGITUDE));
        loc.setElevation((Double) coord.get(MongoDeviceLocation.PROP_ELEVATION));
        locs.add(loc);
    }
    target.setCoordinates(locs);

    MongoSiteWhereEntity.fromDBObject(source, target);
    MongoMetadataProvider.fromDBObject(source, target);
}

From source file:com.streamreduce.core.service.EventServiceImpl.java

License:Apache License

/**
 * Helper method that returns all metadata for a {@link InventoryItem}.
 *
 * @param inventoryItem the cloud inventory item to retrieve metadata for/about
 * @return the metadata//  w  ww  .j a  v a 2 s. c o m
 */
private Map<String, Object> getMetadataFromInventoryItem(InventoryItem inventoryItem) {
    // NOTE: We're not using CloudService methods here for performance reasons
    Map<String, Object> civMetadata = new HashMap<>();

    // Right now, we are only creating extended metadata for AWS EC2 instance items
    if (inventoryItem.getConnection().getProviderId().equals(ProviderIdConstants.AWS_PROVIDER_ID)
            && inventoryItem.getType().equals(Constants.COMPUTE_INSTANCE_TYPE)) {
        DBObject cMetadata = genericCollectionDAO.getById(DAODatasourceType.BUSINESS,
                Constants.INVENTORY_ITEM_METADATA_COLLECTION_NAME, inventoryItem.getMetadataId());

        if (cMetadata == null) {
            // Fill in the metadata based on the last event for this target
            Event previousEvent = getLastEventForTarget(inventoryItem.getId());

            if (previousEvent != null) {
                Map<String, Object> peMetadata = previousEvent.getMetadata();

                if (peMetadata != null) {
                    civMetadata.put("targetIP", peMetadata.get("targetIP"));
                    civMetadata.put("targetOS", peMetadata.get("targetOS"));
                    civMetadata.put("targetISO3166Code", peMetadata.get("targetISO3166Code"));
                    civMetadata.put("targetRegion", peMetadata.get("targetRegion"));
                    civMetadata.put("targetZone", peMetadata.get("targetZone"));
                }
            }
        } else {
            // Fill in the metadata from the available node metadata

            // Get the IP address
            if (cMetadata.containsField("publicAddresses")) {
                BasicDBList publicAddresses = (BasicDBList) cMetadata.get("publicAddresses");

                // TODO: How do we want to handle multiple IP addresses?
                if (publicAddresses.size() > 0) {
                    civMetadata.put("targetIP", publicAddresses.get(0));
                }
            }

            // Get location information (ISO 3166 code, region and availability zone)
            if (cMetadata.containsField("location") && cMetadata.get("location") != null) {
                BasicDBObject location = (BasicDBObject) cMetadata.get("location");
                boolean regionProcessed = false;
                boolean zoneProcessed = false;

                while (location != null) {
                    if (regionProcessed && zoneProcessed) {
                        break;
                    }

                    String locationScope = location.containsField("scope") ? location.getString("scope") : null;

                    if (locationScope != null) {
                        LocationScope scope = LocationScope.valueOf(locationScope);

                        switch (scope) {
                        case REGION:
                            civMetadata.put("targetRegion", location.get("id"));
                            regionProcessed = true;
                            break;
                        case ZONE:
                            BasicDBList iso3166Codes = (BasicDBList) location.get("iso3166Codes");

                            civMetadata.put("targetISO3166Code", iso3166Codes.get(0));
                            civMetadata.put("targetZone", location.get("id"));
                            zoneProcessed = true;
                            break;
                        }
                    }

                    location = location.containsField("parent") && location.get("parent") != null
                            ? (BasicDBObject) location.get("parent")
                            : null;
                }
            }

            // Get OS name
            if (cMetadata.containsField("operatingSystem")) {
                BasicDBObject operatingSystem = (BasicDBObject) cMetadata.get("operatingSystem");

                if (operatingSystem != null) {
                    if (operatingSystem.containsField("family")) {
                        civMetadata.put("targetOS", operatingSystem.get("family"));
                    }
                }
            }
        }
    }

    return civMetadata;
}

From source file:cz.vse.fis.keg.entityclassifier.core.ontologymapper.YagoOntologyManager.java

License:Open Source License

public HashSet getYagoHypernyms(String entityTitle, String entityURI, String lang, String origin) {

    HashSet hypernymsList = new HashSet();
    try {/*ww  w  .j a  va 2s  . c  o  m*/
        DBCursor cursor = db.getCollection("entities_yago").find(new BasicDBObject().append("uri", entityURI));
        Model mainModel = ModelFactory.createDefaultModel();

        while (cursor.hasNext()) {

            DBObject resObj = cursor.next();

            BasicDBList types = (BasicDBList) resObj.get("types");

            if (types != null) {

                for (int i = 0; i < types.size(); i++) {

                    DBObject type = (DBObject) types.get(i); // yago type (pointer in yago taxonomy)

                    Hypernym hyp1 = new Hypernym();
                    hyp1.setEntityURL(entityURI);
                    hyp1.setEntity(entityTitle);
                    hyp1.setTypeURL(type.get("uri").toString());
                    hyp1.setType(type.get("label").toString());
                    hyp1.setOrigin(origin);
                    hyp1.setAccuracy("-1.0");
                    hypernymsList.add(hyp1);

                    mainModel.union(getHierarchyModel(type.get("uri").toString()));

                }
            }
        }

        StmtIterator iter = mainModel.listStatements(new SimpleSelector((Resource) null, null, (RDFNode) null));

        while (iter.hasNext()) {

            Statement stm = iter.next();

            Hypernym hyp = new Hypernym();
            hyp.setEntityURL(entityURI);
            hyp.setEntity(entityTitle);
            hyp.setTypeURL(stm.getObject().toString());
            hyp.setOrigin(origin);
            hyp.setAccuracy("-1.0");

            String typeLabel = getYagoTypeLabel(stm.getObject().toString());

            if (typeLabel != null) {
                hyp.setType(typeLabel);
                hypernymsList.add(hyp);
            }
        }
    } catch (Exception ex) {
        Logger.getLogger(YagoOntologyManager.class.getName()).log(Level.SEVERE,
                "Problem with the mongodb client.", ex);
    }
    return hypernymsList;
}

From source file:cz.vse.fis.keg.entityclassifier.core.ontologymapper.YagoOntologyManager.java

License:Open Source License

public String getYagoTypeLabel(String uri) {

    DBCursor cursor = db.getCollection("entities_yago").find(new BasicDBObject().append("uri", uri));

    if (cursor.size() > 0) {
        DBObject tmp = cursor.next();//w  w  w . j  a  v a  2 s .c o m
        BasicDBList labels = (BasicDBList) tmp.get("labels");

        if (labels != null) {
            DBObject tmp2 = (DBObject) labels.get(0);
            return tmp2.get("label").toString();
        }
    }

    return null;

}

From source file:cz.vse.fis.keg.entityclassifier.core.ontologymapper.YagoOntologyManager.java

License:Open Source License

public Model processEntity(String entityTitle) {

    DBCursor cursor = db.getCollection("entities_yago").find(new BasicDBObject().append("uri",
            "http://yago-knowledge.org/resource/" + entityTitle.replaceAll(" ", "_")));

    while (cursor.hasNext()) {

        DBObject resObj = cursor.next();

        BasicDBList types = (BasicDBList) resObj.get("types");

        if (types != null) {
            for (int i = 0; i < types.size(); i++) {
                DBObject type = (DBObject) types.get(i);
                Hypernym h = new Hypernym();
                h.setEntity(entityTitle);
                h.setEntityURL(resObj.get("uri").toString());
                h.setType(type.get("label").toString());
                h.setTypeURL(type.get("uri").toString());
                h.setOrigin("thd-derived");

                OntoRecord initRecord = new OntoRecord();
                initRecord.setUri(type.get("uri").toString());

                while (initRecord != null) {

                    initRecord = getSuperclass(initRecord.getUri());

                    if (initRecord != null) {

                        Hypernym hypernymDerived = new Hypernym();
                        hypernymDerived.setEntity(entityTitle);
                        hypernymDerived.setEntityURL(resObj.get("uri").toString());
                        hypernymDerived.setType(initRecord.getLabel());
                        hypernymDerived.setTypeURL(initRecord.getUri());
                        hypernymDerived.setOrigin("thd-derived");
                    }/*from w w  w .  j ava2 s .  c o m*/
                }
            }
        }
    }
    return null;
}

From source file:cz.vse.fis.keg.entityclassifier.core.THDInstance.java

License:Open Source License

public ArrayList<Hypernym> getDBpediaHypernyms(String entityTitle, String lang) throws UnknownHostException {

    ArrayList<Hypernym> hypernymsList = new ArrayList<Hypernym>();
    queryObj = new BasicDBObject();
    queryObj.append("label", entityTitle);
    queryObj.append("types",
            new BasicDBObject().append("$elemMatch", new BasicDBObject().append("origin", "dbpedia")));
    DBObject resObj;/*from   w  ww .  j a  va2s.  c o  m*/
    BasicDBList dbTypesList;
    switch (lang) {
    case "en":

        resObj = MongoDBClient.getDBInstance().getCollection("en_entities_dbpedia").findOne(queryObj);

        if (resObj != null) {

            dbTypesList = (BasicDBList) resObj.get("types");

            for (int i = 0; i < dbTypesList.size(); i++) {
                DBObject obj = (DBObject) dbTypesList.get(i);

                Hypernym hypernym = new Hypernym();
                hypernym.setEntity(entityTitle);
                hypernym.setEntityURL(resObj.get("uri").toString());
                hypernym.setType(obj.get("label").toString());
                hypernym.setTypeURL(obj.get("uri").toString());
                hypernym.setOrigin("dbpedia");
                if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                    hypernymsList.add(hypernym);
                }
            }
        }
        return hypernymsList;

    case "de":
        resObj = MongoDBClient.getDBInstance().getCollection("de_entities_dbpedia").findOne(queryObj);

        if (resObj != null) {
            dbTypesList = (BasicDBList) resObj.get("types");

            for (int i = 0; i < dbTypesList.size(); i++) {

                DBObject obj = (DBObject) dbTypesList.get(i);

                Hypernym hypernym = new Hypernym();
                hypernym.setEntity(entityTitle);
                hypernym.setEntityURL(resObj.get("uri").toString());
                hypernym.setType(obj.get("label").toString());
                hypernym.setTypeURL(obj.get("uri").toString());
                hypernym.setOrigin("dbpedia");
                if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                    hypernymsList.add(hypernym);
                }
            }
        }

        return hypernymsList;

    case "nl":
        resObj = MongoDBClient.getDBInstance().getCollection("nl_entities_dbpedia").findOne(queryObj);

        if (resObj != null) {
            dbTypesList = (BasicDBList) resObj.get("types");

            for (int i = 0; i < dbTypesList.size(); i++) {

                DBObject obj = (DBObject) dbTypesList.get(i);

                Hypernym hypernym = new Hypernym();
                hypernym.setEntity(entityTitle);
                hypernym.setEntityURL(resObj.get("uri").toString());
                hypernym.setType(obj.get("label").toString());
                hypernym.setTypeURL(obj.get("uri").toString());
                hypernym.setOrigin("dbpedia");

                if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                    hypernymsList.add(hypernym);
                }
            }
        }

        return hypernymsList;

    default:
        return hypernymsList;
    }
}

From source file:cz.vse.fis.keg.entityclassifier.core.THDWorker.java

License:Open Source License

public HashSet getTHDHypernymsLHDv2(String entityTitle, String lang) throws UnknownHostException {
    HashSet hypernymsList = new HashSet();
    try {//  ww  w  . ja v  a2s .c  o m
        BasicDBObject queryObj = new BasicDBObject();
        queryObj.append("types",
                new BasicDBObject().append("$elemMatch", new BasicDBObject().append("origin", "thd")));

        switch (lang) {
        case "en":
            String encodedURIEn = "http://dbpedia.org/resource/"
                    + URLEncoder.encode(URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8"), "UTF-8");
            String notEncodedURIEn = "http://dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", encodedURIEn);

            DBObject resObj = MongoDBClient.getDBInstance().getCollection("en_entities_thd_lhd10")
                    .findOne(queryObj);

            if (resObj != null) {

                BasicDBList e = (BasicDBList) resObj.get("types");

                for (int i = 0; i < e.size(); i++) {

                    DBObject type = (DBObject) e.get(i);

                    String mappingStr = type.get("mapping").toString();

                    // hypernym is mapped to dbpedia ontology
                    // creating hierarchy from the dbpedia ontology
                    if (mappingStr.equals("dbOnto")) {

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(notEncodedURIEn);
                        hypernym.setType(type.get("label").toString());
                        hypernym.setTypeURL(type.get("uri").toString());
                        hypernym.setOrigin(type.get("origin").toString());
                        hypernym.setAccuracy(type.get("accuracy").toString());
                        hypernym.setBounds(type.get("bounds").toString());
                        hypernymsList.add(hypernym);

                        OntoRecord initRecord = new OntoRecord();
                        initRecord.setUri(type.get("uri").toString());

                        while (initRecord != null) {

                            initRecord = DBpediaOntologyManager.getInstance().getSubclass(initRecord.getUri(),
                                    lang);

                            if (initRecord == null) {
                                return hypernymsList;
                            } else {
                                Hypernym hypernymDerived = new Hypernym();
                                hypernymDerived.setEntity(entityTitle);
                                hypernymDerived.setEntityURL(notEncodedURIEn);
                                hypernymDerived.setType(initRecord.getLabel());
                                hypernymDerived.setTypeURL(initRecord.getUri());
                                hypernymDerived.setOrigin("thd-derived");
                                hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                hypernymDerived.setBounds(type.get("bounds").toString());
                                hypernymsList.add(hypernymDerived);
                            }
                        }
                    }
                    // the type is DBpedia instance, doesn't matter, add it it to the types list
                    else {
                        Hypernym hypernymInst = new Hypernym();
                        hypernymInst.setEntity(entityTitle);
                        hypernymInst.setEntityURL(notEncodedURIEn);
                        hypernymInst.setType(type.get("label").toString());
                        hypernymInst.setTypeURL(type.get("uri").toString());
                        hypernymInst.setOrigin(type.get("origin").toString());
                        hypernymInst.setAccuracy(type.get("accuracy").toString());
                        hypernymInst.setBounds(type.get("bounds").toString());
                        hypernymsList.add(hypernymInst);

                        // try to map the DBpedia instance type to a DBpedia Ontology type
                        String mappedType = TypeMapper.getInstance().getTypeMapping(lang,
                                type.get("uri").toString());

                        if (mappedType != null) {

                            Hypernym hypernym2 = new Hypernym();
                            hypernym2.setEntity(entityTitle);
                            hypernym2.setEntityURL(notEncodedURIEn);
                            hypernym2.setType(mappedType.split("/")[mappedType.split("/").length - 1]);
                            hypernym2.setTypeURL(mappedType);
                            hypernym2.setOrigin(type.get("origin").toString());
                            hypernym2.setAccuracy(type.get("accuracy").toString());
                            hypernym2.setBounds(type.get("bounds").toString());
                            hypernymsList.add(hypernym2);

                            OntoRecord initRecord = new OntoRecord();
                            initRecord.setUri(mappedType);

                            while (initRecord != null) {

                                initRecord = DBpediaOntologyManager.getInstance()
                                        .getSubclass(initRecord.getUri(), lang);

                                if (initRecord == null) {
                                    return hypernymsList;
                                } else {
                                    Hypernym hypernymDerived = new Hypernym();
                                    hypernymDerived.setEntity(entityTitle);
                                    hypernymDerived.setEntityURL(notEncodedURIEn);
                                    hypernymDerived.setType(initRecord.getLabel());
                                    hypernymDerived.setTypeURL(initRecord.getUri());
                                    hypernymDerived.setOrigin("thd-derived");
                                    hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                    hypernymDerived.setBounds(type.get("bounds").toString());
                                    hypernymsList.add(hypernymDerived);
                                }
                            }
                        }
                    }
                }
            }
            return hypernymsList;

        case "de":

            String encodedURIDe = "http://de.dbpedia.org/resource/"
                    + URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8");
            String notEncodedURIDe = "http://de.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", encodedURIDe);

            resObj = MongoDBClient.getDBInstance().getCollection("de_entities_thd_lhd10").findOne(queryObj);

            if (resObj != null) {

                BasicDBList typesList = (BasicDBList) resObj.get("types");

                for (int i = 0; i < typesList.size(); i++) {

                    DBObject type = (DBObject) typesList.get(i);

                    String mappingStr = type.get("mapping").toString();
                    // hypernym is mapped to dbpedia ontology
                    // creating hierarchy from the dbpedia ontology
                    if (mappingStr.equals("dbOnto")) {

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(notEncodedURIDe);
                        hypernym.setType(type.get("label").toString());
                        hypernym.setTypeURL(type.get("uri").toString());
                        hypernym.setAccuracy(type.get("accuracy").toString());
                        hypernym.setBounds(type.get("bounds").toString());
                        hypernym.setOrigin(type.get("origin").toString());
                        hypernymsList.add(hypernym);

                        OntoRecord initRecord = new OntoRecord();
                        initRecord.setUri(type.get("uri").toString());

                        while (initRecord != null) {

                            initRecord = DBpediaOntologyManager.getInstance().getSubclass(initRecord.getUri(),
                                    lang);

                            if (initRecord != null) {

                                Hypernym hypernymDerived = new Hypernym();
                                hypernymDerived.setEntity(entityTitle);
                                hypernymDerived.setEntityURL(notEncodedURIDe);
                                hypernymDerived.setType(initRecord.getLabel());
                                hypernymDerived.setTypeURL(initRecord.getUri());
                                hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                hypernymDerived.setOrigin("thd-derived");
                                hypernymsList.add(hypernymDerived);
                            }
                        }
                    }
                    // type is DBpedia instance, doesn't matter, add it to the types list
                    else {
                        Hypernym hypernymInst = new Hypernym();
                        hypernymInst.setEntity(entityTitle);
                        hypernymInst.setEntityURL(notEncodedURIDe);
                        hypernymInst.setType(type.get("label").toString());
                        hypernymInst.setTypeURL(type.get("uri").toString());
                        hypernymInst.setOrigin(type.get("origin").toString());
                        hypernymInst.setAccuracy(type.get("accuracy").toString());
                        hypernymInst.setBounds(type.get("bounds").toString());
                        hypernymsList.add(hypernymInst);

                        // try to map the DBpedia instance type to a DBpedia Ontology type
                        String mappedType = TypeMapper.getInstance().getTypeMapping("de",
                                type.get("uri").toString());

                        if (mappedType != null) {
                            Hypernym hypernym2 = new Hypernym();
                            hypernym2.setEntity(entityTitle);
                            hypernym2.setEntityURL(notEncodedURIDe);
                            hypernym2.setType(mappedType.split("/")[mappedType.split("/").length - 1]);
                            hypernym2.setTypeURL(mappedType);
                            hypernym2.setOrigin(type.get("origin").toString());
                            hypernym2.setAccuracy(type.get("accuracy").toString());
                            hypernym2.setBounds(type.get("bounds").toString());
                            hypernymsList.add(hypernym2);

                            OntoRecord initRecord = new OntoRecord();
                            initRecord.setUri(mappedType);

                            while (initRecord != null) {

                                initRecord = DBpediaOntologyManager.getInstance()
                                        .getSubclass(initRecord.getUri(), lang);

                                if (initRecord == null) {
                                    return hypernymsList;
                                } else {
                                    Hypernym hypernymDerived = new Hypernym();
                                    hypernymDerived.setEntity(entityTitle);
                                    hypernymDerived.setEntityURL(notEncodedURIDe);
                                    hypernymDerived.setType(initRecord.getLabel());
                                    hypernymDerived.setTypeURL(initRecord.getUri());
                                    hypernymDerived.setOrigin("thd-derived");
                                    hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                    hypernymDerived.setBounds(type.get("bounds").toString());
                                    hypernymsList.add(hypernymDerived);
                                }
                            }
                        }
                    }
                }
            }
            return hypernymsList;

        case "nl":

            String encodedURINl = "http://nl.dbpedia.org/resource/"
                    + URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8");
            String notEncodedURINl = "http://nl.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", encodedURINl);

            resObj = MongoDBClient.getDBInstance().getCollection("nl_entities_thd_lhd10").findOne(queryObj);

            if (resObj != null) {

                BasicDBList typesList = (BasicDBList) resObj.get("types");

                for (int i = 0; i < typesList.size(); i++) {

                    DBObject type = (DBObject) typesList.get(i);

                    String typeURI = type.get("mapping").toString();
                    // hypernym is mapped to dbpedia ontology
                    // creating hierarchy from the dbpedia ontology
                    if (typeURI.equals("dbOnto")) {

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(notEncodedURINl);
                        hypernym.setType(type.get("label").toString());
                        hypernym.setTypeURL(type.get("uri").toString());
                        hypernym.setAccuracy(type.get("accuracy").toString());
                        hypernym.setBounds(type.get("bounds").toString());
                        hypernym.setOrigin(type.get("origin").toString());
                        hypernymsList.add(hypernym);

                        OntoRecord initRecord = new OntoRecord();
                        initRecord.setUri(type.get("uri").toString());

                        while (initRecord != null) {

                            initRecord = DBpediaOntologyManager.getInstance().getSubclass(initRecord.getUri(),
                                    lang);

                            if (initRecord != null) {

                                Hypernym hypernymDerived = new Hypernym();
                                hypernymDerived.setEntity(entityTitle);
                                hypernymDerived.setEntityURL(notEncodedURINl);
                                hypernymDerived.setType(initRecord.getLabel());
                                hypernymDerived.setTypeURL(initRecord.getUri());
                                hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                hypernymDerived.setOrigin("thd-derived");
                                hypernymsList.add(hypernymDerived);
                            }
                        }
                    }
                    // type is DBpedia instance, doesn't matter, add it to the types list
                    else {
                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(notEncodedURINl);
                        hypernym.setType(type.get("label").toString());
                        hypernym.setTypeURL(type.get("uri").toString());
                        hypernym.setOrigin(type.get("origin").toString());
                        hypernym.setAccuracy(type.get("accuracy").toString());
                        hypernym.setBounds(type.get("bounds").toString());
                        hypernymsList.add(hypernym);

                        // try to map the DBpedia instance type to a DBpedia Ontology type
                        String mappedType = TypeMapper.getInstance().getTypeMapping("nl",
                                type.get("uri").toString());

                        if (mappedType != null) {

                            Hypernym hypernym2 = new Hypernym();
                            hypernym2.setEntity(entityTitle);
                            hypernym2.setEntityURL(notEncodedURINl);
                            hypernym2.setType(mappedType.split("/")[mappedType.split("/").length - 1]);
                            hypernym2.setTypeURL(mappedType);
                            hypernym2.setOrigin(type.get("origin").toString());
                            hypernym2.setAccuracy(type.get("accuracy").toString());
                            hypernym2.setBounds(type.get("bounds").toString());
                            hypernymsList.add(hypernym2);

                            OntoRecord initRecord = new OntoRecord();
                            initRecord.setUri(mappedType);

                            while (initRecord != null) {

                                initRecord = DBpediaOntologyManager.getInstance()
                                        .getSubclass(initRecord.getUri(), lang);

                                if (initRecord == null) {
                                    return hypernymsList;
                                } else {
                                    Hypernym hypernymDerived = new Hypernym();
                                    hypernymDerived.setEntity(entityTitle);
                                    hypernymDerived.setEntityURL(notEncodedURINl);
                                    hypernymDerived.setType(initRecord.getLabel());
                                    hypernymDerived.setTypeURL(initRecord.getUri());
                                    hypernymDerived.setOrigin("thd-derived");
                                    hypernymDerived.setAccuracy(type.get("accuracy").toString());
                                    hypernymDerived.setBounds(type.get("bounds").toString());
                                    hypernymsList.add(hypernymDerived);
                                }
                            }
                        }
                    }
                }
            }
            return hypernymsList;
        }
        return hypernymsList;
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(THDWorker.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hypernymsList;
}

From source file:cz.vse.fis.keg.entityclassifier.core.THDWorker.java

License:Open Source License

public HashSet getDBpediaHypernyms(String entityTitle, String lang) throws UnknownHostException {

    HashSet hypernymsList = new HashSet();
    try {//  w  ww  .  j  a v a  2s.c om
        BasicDBObject queryObj = new BasicDBObject();
        queryObj.append("types",
                new BasicDBObject().append("$elemMatch", new BasicDBObject().append("origin", "dbpedia")));

        BasicDBList dbTypesList;
        switch (lang) {
        case "en":
            String uriEn = "http://dbpedia.org/resource/"
                    + URLEncoder.encode(entityTitle.replace(" ", "_"), "UTF-8");
            queryObj.append("uri", uriEn);
            DBCursor cursorEN = MongoDBClient.getDBInstance().getCollection("en_entities_dbpedia")
                    .find(queryObj);

            while (cursorEN.hasNext()) {
                DBObject resObj = cursorEN.next();
                if (resObj != null) {

                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {
                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");
                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorEN.close();

            return hypernymsList;

        case "de":

            String uriDe = "http://de.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", uriDe);
            DBCursor cursorDE = MongoDBClient.getDBInstance().getCollection("de_entities_dbpedia")
                    .find(queryObj);

            while (cursorDE.hasNext()) {
                DBObject resObj = cursorDE.next();
                if (resObj != null) {
                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {

                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");
                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorDE.close();

            return hypernymsList;

        case "nl":

            String uriNl = "http://nl.dbpedia.org/resource/" + entityTitle.replace(" ", "_");
            queryObj.append("uri", uriNl);
            DBCursor cursorNL = MongoDBClient.getDBInstance().getCollection("nl_entities_dbpedia")
                    .find(queryObj);

            while (cursorNL.hasNext()) {
                DBObject resObj = cursorNL.next();
                if (resObj != null) {
                    dbTypesList = (BasicDBList) resObj.get("types");

                    for (int i = 0; i < dbTypesList.size(); i++) {

                        DBObject obj = (DBObject) dbTypesList.get(i);

                        Hypernym hypernym = new Hypernym();
                        hypernym.setEntity(entityTitle);
                        hypernym.setEntityURL(resObj.get("uri").toString());
                        hypernym.setType(obj.get("label").toString());
                        hypernym.setTypeURL(obj.get("uri").toString());
                        hypernym.setOrigin("dbpedia");
                        hypernym.setAccuracy("-1.0");

                        if (obj.get("uri").toString().contains("http://dbpedia.org/ontology/")) {
                            hypernymsList.add(hypernym);
                        }
                    }
                }
            }
            cursorNL.close();

            return hypernymsList;

        default:
            return hypernymsList;
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(THDWorker.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hypernymsList;
}

From source file:dao.CocinaDAO.java

public List<Mensaje> getMensajes() {
    int estado;/*from w w w .ja  v  a2s.  co m*/
    List<Mensaje> mensajes = null;
    Mensaje mensaje = null;
    ConexionMLab con = new ConexionMLab();
    MongoClient mongo = con.getConexion();
    try {
        DB db = mongo.getDB("pizzaplaneta");
        DBCollection coleccion = db.getCollection("pedido");
        DBCursor cursor = coleccion.find();
        mensajes = new ArrayList<>();
        while (cursor.hasNext()) {
            DBObject dbo = cursor.next();
            BasicDBList dbo1 = (BasicDBList) dbo.get("estados");
            DBObject est = (DBObject) (dbo1.get(dbo1.size() - 1));
            estado = (Integer) est.get("id");
            if (estado == 1 || estado == 0) {
                mensaje = new Mensaje((String) est.get("fechaHora"), estado, (Integer) dbo.get("_id"));
                mensajes.add(mensaje);
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        mongo.close();
    }
    return mensajes;

}