Example usage for java.util HashMap isEmpty

List of usage examples for java.util HashMap isEmpty

Introduction

In this page you can find the example usage for java.util HashMap isEmpty.

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:cm.aptoide.pt.MainActivity.java

public void getAllRepoStatus() {
    final HashMap<String, Long> serversToParse = new HashMap<String, Long>();
    new Thread(new Runnable() {

        @Override//from  www  . ja v  a 2  s .  co m
        public void run() {
            String repos = "";
            String hashes = "";
            Cursor cursor = db.getStores(false);
            int i = 0;
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                String repo;
                if (i > 0) {
                    repos = repos + ",";
                    hashes = hashes + ",";
                }
                repo = cursor.getString(1);
                repo = RepoUtils.split(repo);
                repos = repos + repo;
                hashes = hashes + cursor.getString(2);
                i++;
                serversToParse.put(repo, cursor.getLong(0));

            }
            cursor.close();

            if (!serversToParse.isEmpty()) {

                String url = "http://webservices.aptoide.com/webservices/listRepositoryChange/" + repos + "/"
                        + hashes + "/json";
                System.out.println(url);
                try {
                    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                    connection.connect();
                    int rc = connection.getResponseCode();
                    if (rc == 200) {
                        NetworkUtils utils = new NetworkUtils();
                        JSONObject json = utils.getJsonObject(url, mContext);

                        JSONArray array = json.getJSONArray("listing");

                        for (int o = 0; o != array.length(); o++) {
                            boolean parse = Boolean
                                    .parseBoolean(array.getJSONObject(o).getString("hasupdates"));
                            long id = serversToParse.get(array.getJSONObject(o).getString("repo"));
                            Server server = db.getServer(id, false);

                            if (parse) {
                                service.parseServer(db, server);
                            } else {
                                service.parseTop(db, server);
                                service.parseLatest(db, server);
                                service.addStoreInfo(db, server);
                            }

                        }

                    }
                    connection.disconnect();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    if (!ApplicationAptoide.MULTIPLESTORES) {
                        getApplicationContext().sendBroadcast(new Intent("PARSE_FAILED"));
                    }
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

}

From source file:DAO.DataAccessObject.java

/**
 * Methode zur Erzeugung eines Auftragskopfes und seinen Positionen
 * @param Typ Typ des Auftrags (mglich sind: Barauftrag, Sofortauftrag, 
 *                              Terminauftrag, Bestellauftrag)
 * @param Artikel HashMap die ein Artikel(ArtikelID) und die bestellte Menge 
 *                enthlt//w w w .ja  va 2  s .c o  m
 * @param Auftragstext Zustzlicher Kommentar fr einen Auftrag
 * @param GeschaeftspartnerID Eindeutige Nummer eines Geschftspartners
 * @param ZahlungskonditionID Eindeute Nummer einer Zahlungskondition
 * @param Status Status des Auftrags
 * @param Abschlussdatum Auftragsabschlussdatum
 * @param Lieferdatum Datum der Lieferung
 * @throws ApplicationException Wenn die Stammdaten nicht gefunden werden
 *                              knnen oder der Auftrag nicht erstellt
 *                              werden knnte
 */
public void erstelleAuftragskopf(String Typ, HashMap<Long, Integer> Artikel, String Auftragstext,
        long GeschaeftspartnerID, long ZahlungskonditionID, String Status, Date Abschlussdatum,
        Date Lieferdatum) throws ApplicationException {

    if (Artikel.isEmpty()) {
        throw new ApplicationException("Fehler", "Es wurden keine Auftragspositionen angegeben.");
    }

    //Variable fr den berechneten Auftragswert
    double Auftragswert = 0;

    //Hole den Geschftspartner anhand der ID aus der Datenbank
    Geschaeftspartner gp = em.find(Geschaeftspartner.class, GeschaeftspartnerID);

    //Prfe ob der Geschftspartner mit dieser ID existiert
    if (gp == null) {
        throw new ApplicationException("Fehler",
                "Der angegebene Geschftspartner konnte nicht " + "gefunden werden.");
    }

    //Hole Zahlungskondition anhand der ID aus der Datenbank
    Zahlungskondition paymentCondition = em.find(Zahlungskondition.class, ZahlungskonditionID);

    //Prfe ob die Zahlungskondition mit dieser ID existiert
    if (!Typ.equals("Barauftrag") && paymentCondition == null) {
        throw new ApplicationException("Fehler", "Zahlungskondition konnte nicht gefunden werden.");
    }

    //Suche den Status anhand des Namnes in der Datenbank
    Status state = this.gibStatusPerName(Status);

    //Prfen ob ein Status mit diesem Namen existiert
    if (state == null) {
        throw new ApplicationException("Fehler", "Status konnte nicht gefunden werden");
    }

    if ((Typ.equals("Barauftrag") || Typ.equals("Sofortauftrag") || Typ.equals("Terminauftrag"))
            && gp.getTyp().equals("Lieferant")) {
        throw new ApplicationException(FEHLER_TITEL,
                "Fr Lieferanten kann diese Art von Auftrag nicht " + "angelegt werden.");
    }

    if (Typ.equals("Bestellauftrag") && gp.getTyp().equals("Kunde")) {
        throw new ApplicationException(FEHLER_TITEL, "Fr Kunden kann keine Bestellauftrag angelegt werden.");
    }

    //Hole das aktuelle Systemdatum
    Calendar cal = Calendar.getInstance();
    Date Erfassungsdatum = cal.getTime();

    Auftragskopf ak = null;
    ArrayList<Auftragsposition> positionen = new ArrayList<>();

    //Anhand des bergebenen Typs wird ein entsprechendes Objekt erzeugt
    switch (Typ) {
    case "Barauftrag":
        ak = new Barauftragskopf(Auftragstext, Auftragswert, gp, state, Abschlussdatum, Erfassungsdatum,
                Lieferdatum);
        break;
    case "Sofortauftrag":
        ak = new Sofortauftragskopf(Auftragstext, Auftragswert, gp, state, paymentCondition, Abschlussdatum,
                Erfassungsdatum, Lieferdatum);
        break;
    case "Terminauftrag":
        ak = new Terminauftragskopf(Auftragstext, Auftragswert, gp, state, paymentCondition, Abschlussdatum,
                Erfassungsdatum, Lieferdatum);
        break;
    case "Bestellauftrag":
        ak = new Bestellauftragskopf(Auftragstext, Auftragswert, gp, state, paymentCondition, Abschlussdatum,
                Erfassungsdatum, Lieferdatum);
        //Wenn keine gltige Auftragsart bergeben wurde
        break;
    default:
        throw new ApplicationException("Fehler", "Ungltige Auftragsart!");
    }

    //Prfe ob der Auftragskopf nicht erstellt worden ist
    if (ak == null) {
        throw new ApplicationException("Fehler", "Fehler bei der Erzeugung des Auftragskopfes");
    }

    try {

        //Hole alle ID aus der Artikelliste
        Set<Long> artikelIDS = Artikel.keySet();

        //Fr jede ID...
        for (long ID : artikelIDS) {

            //...wird der entsprechende Artikel in der Datenbank gesucht
            Artikel artikel = em.find(Artikel.class, ID);

            //Es wird geprft ob der Artikel existiert
            if (artikel == null) {
                throw new ApplicationException("Fehler", "Der angegebene Artikel konne nicht gefunden werden");
            }

            ak.addPosition(artikel, Artikel.get(ID));

        }

        //Transaktion starten
        em.getTransaction().begin();

        //Persistieren den Auftrag mit der Positionsliste
        em.persist(ak);

        //Transaktion beenden
        em.getTransaction().commit();

    } catch (RollbackException re) {

        //Der Commit ist fehlgeschlagen
        //Dadurch wird implizit ein Rollback ausgefhrt
        throw new ApplicationException(FEHLER_TITEL,
                "Commit ist fehlgeschlagen. Transkation wurde " + "rckgngig gemacht.");

    } catch (PersistenceException pe) {

        //Es ist ein Fehler beim Persistieren der Daten aufgetreten
        //Hier muss ein Rollback manuell durchgefhrt werdeb
        em.getTransaction().rollback();

        throw new ApplicationException(FEHLER_TITEL,
                "Fehler beim Persistieren der Daten. Transkation wurde " + "rckgngig gemacht.");

    } catch (Throwable th) {

        //Ein unerwarteter Fehler ist aufgetreten
        //Wenn eine Transaktion aktiv ist, muss diese rckgngig gemacht
        //werden
        if (em != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }

        throw new ApplicationException(FEHLER_TITEL, th.getMessage());

    }
}

From source file:ffx.potential.parsers.BiojavaFilter.java

/**
 * Assign force field atoms types to common chemistries using "biotype"
 * records./*from ww  w . j  av  a2s .  com*/
 */
public void assignAtomTypes() {
    /**
     * Create a new List to store bonds determined based on PDB atom names.
     */
    bondList = new ArrayList<>();

    /**
     * To Do: Look for cyclic peptides and disulfides.
     */
    Polymer[] polymers = activeMolecularAssembly.getChains();

    /**
     * Loop over chains.
     */
    if (polymers != null) {
        logger.info(format("\n Assigning atom types for %d chains.", polymers.length));
        for (Polymer polymer : polymers) {
            List<Residue> residues = polymer.getResidues();
            int numberOfResidues = residues.size();
            /**
             * Check if all residues are known amino acids.
             */
            boolean isProtein = true;
            if (!residues.isEmpty()) {
                //renameNTerminusHydrogens(residues.get(0)); Not safe to use until it distinguishes between true N-termini and N-terminal residues in general.
            }
            for (int residueNumber = 0; residueNumber < numberOfResidues; residueNumber++) {
                Residue residue = residues.get(residueNumber);
                String name = residue.getName().toUpperCase();
                boolean aa = false;
                for (AminoAcid3 amino : aminoAcidList) {
                    if (amino.toString().equalsIgnoreCase(name)) {
                        aa = true;
                        renameNonstandardHydrogens(residue);
                        break;
                    }
                }
                // Check for a patch.
                if (!aa) {
                    HashMap<String, AtomType> types = forceField.getAtomTypes(name);
                    if (types.isEmpty()) {
                        isProtein = false;
                        break;
                    } else {
                        logger.info(" Patch found for non-standard amino acid " + name);
                    }
                }
            }

            /**
             * If all the residues in this chain have known amino acids
             * names, then attempt to assign atom types.
             */
            if (isProtein) {
                try {
                    logger.info(format(" Amino acid chain %s", polymer.getName()));
                    double dist = properties.getDouble("chainbreak", 3.0);
                    // Detect main chain breaks!
                    List<List<Residue>> subChains = findChainBreaks(residues, dist);
                    for (List<Residue> subChain : subChains) {
                        assignAminoAcidAtomTypes(subChain);
                    }
                } catch (MissingHeavyAtomException missingHeavyAtomException) {
                    logger.severe(missingHeavyAtomException.toString());
                } catch (MissingAtomTypeException missingAtomTypeException) {
                    logger.severe(missingAtomTypeException.toString());
                }
                continue;
            }

            /**
             * Check if all residues have known nucleic acids names.
             */
            boolean isNucleicAcid = true;
            for (int residueNumber = 0; residueNumber < numberOfResidues; residueNumber++) {
                Residue residue = residues.get(residueNumber);
                String name = residue.getName().toUpperCase();
                /**
                 * Convert 1 and 2-character nucleic acid names to
                 * 3-character names.
                 */
                if (name.length() == 1) {
                    if (name.equals("A")) {
                        name = NucleicAcid3.ADE.toString();
                    } else if (name.equals("C")) {
                        name = NucleicAcid3.CYT.toString();
                    } else if (name.equals("G")) {
                        name = NucleicAcid3.GUA.toString();
                    } else if (name.equals("T")) {
                        name = NucleicAcid3.THY.toString();
                    } else if (name.equals("U")) {
                        name = NucleicAcid3.URI.toString();
                    }
                } else if (name.length() == 2) {
                    if (name.equals("YG")) {
                        name = NucleicAcid3.YYG.toString();
                    }
                }
                residue.setName(name);
                NucleicAcid3 nucleicAcid = null;
                for (NucleicAcid3 nucleic : nucleicAcidList) {
                    String nuc3 = nucleic.toString();
                    nuc3 = nuc3.substring(nuc3.length() - 3);
                    if (nuc3.equalsIgnoreCase(name)) {
                        nucleicAcid = nucleic;
                        break;
                    }
                }
                if (nucleicAcid == null) {
                    logger.info(format("Nucleic acid was not recognized %s.", name));
                    isNucleicAcid = false;
                    break;
                }
            }

            /**
             * If all the residues in this chain have known nucleic acids
             * names, then attempt to assign atom types.
             */
            if (isNucleicAcid) {
                try {
                    logger.info(format(" Nucleic acid chain %s", polymer.getName()));
                    assignNucleicAcidAtomTypes(residues);
                } catch (MissingHeavyAtomException missingHeavyAtomException) {
                    logger.severe(missingHeavyAtomException.toString());
                } catch (MissingAtomTypeException missingAtomTypeException) {
                    logger.severe(missingAtomTypeException.toString());
                }
            }
        }
    }

    // Assign ion atom types.
    ArrayList<MSNode> ions = activeMolecularAssembly.getIons();
    if (ions != null && ions.size() > 0) {
        logger.info(format(" Assigning atom types for %d ions.", ions.size()));
        for (MSNode m : ions) {
            Molecule ion = (Molecule) m;
            String name = ion.getResidueName().toUpperCase();
            HetAtoms hetatm = HetAtoms.valueOf(name);
            Atom atom = ion.getAtomList().get(0);
            if (ion.getAtomList().size() != 1) {
                logger.severe(format(" Check residue %s of chain %s.", ion.toString(), ion.getChainID()));
            }
            try {
                switch (hetatm) {
                case NA:
                    atom.setAtomType(findAtomType(2003));
                    break;
                case K:
                    atom.setAtomType(findAtomType(2004));
                    break;
                case MG:
                case MG2:
                    atom.setAtomType(findAtomType(2005));
                    break;
                case CA:
                case CA2:
                    atom.setAtomType(findAtomType(2006));
                    break;
                case CL:
                    atom.setAtomType(findAtomType(2007));
                    break;
                case ZN:
                case ZN2:
                    atom.setAtomType(findAtomType(2008));
                    break;
                case BR:
                    atom.setAtomType(findAtomType(2009));
                    break;
                default:
                    logger.severe(format(" Check residue %s of chain %s.", ion.toString(), ion.getChainID()));
                }
            } catch (Exception e) {
                String message = "Error assigning atom types.";
                logger.log(Level.SEVERE, message, e);
            }
        }
    }
    // Assign water atom types.
    ArrayList<MSNode> water = activeMolecularAssembly.getWaters();
    if (water != null && water.size() > 0) {
        logger.info(format(" Assigning atom types for %d waters.", water.size()));
        for (MSNode m : water) {
            Molecule wat = (Molecule) m;
            try {
                Atom O = buildHeavy(wat, "O", null, 2001);
                Atom H1 = buildHydrogen(wat, "H1", O, 0.96e0, null, 109.5e0, null, 120.0e0, 0, 2002);
                H1.setHetero(true);
                Atom H2 = buildHydrogen(wat, "H2", O, 0.96e0, H1, 109.5e0, null, 120.0e0, 0, 2002);
                H2.setHetero(true);
            } catch (Exception e) {
                String message = "Error assigning atom types to a water.";
                logger.log(Level.SEVERE, message, e);
            }
        }
    }

    // Assign small molecule atom types.
    ArrayList<Molecule> molecules = activeMolecularAssembly.getMolecules();
    for (MSNode m : molecules) {
        Molecule molecule = (Molecule) m;
        String moleculeName = molecule.getResidueName();
        logger.info(" Attempting to patch " + moleculeName);
        ArrayList<Atom> moleculeAtoms = molecule.getAtomList();
        boolean patched = true;
        HashMap<String, AtomType> types = forceField.getAtomTypes(moleculeName);
        /**
         * Assign atom types for all known atoms.
         */
        for (Atom atom : moleculeAtoms) {
            String atomName = atom.getName().toUpperCase();
            AtomType atomType = types.get(atomName);
            if (atomType == null) {
                logger.info(" No atom type was found for " + atomName + " of " + moleculeName + ".");
                patched = false;
                break;
            } else {
                atom.setAtomType(atomType);
                types.remove(atomName);
            }
        }
        /**
         * Create missing hydrogen atoms. Check for missing heavy atoms.
         */
        if (patched && !types.isEmpty()) {
            for (AtomType type : types.values()) {
                if (type.atomicNumber != 1) {
                    logger.info(" Missing heavy atom " + type.name);
                    patched = false;
                    break;
                }
            }
        }
        // Create bonds between known atoms.
        if (patched) {
            for (Atom atom : moleculeAtoms) {
                String atomName = atom.getName();
                String bonds[] = forceField.getBonds(moleculeName, atomName);
                if (bonds != null) {
                    for (String name : bonds) {
                        Atom atom2 = molecule.getAtom(name);
                        if (atom2 != null && !atom.isBonded(atom2)) {
                            buildBond(atom, atom2);
                        }
                    }
                }
            }
        }
        // Create missing hydrogen atoms.
        if (patched && !types.isEmpty()) {
            // Create a hashmap of the molecule's atoms
            HashMap<String, Atom> atomMap = new HashMap<String, Atom>();
            for (Atom atom : moleculeAtoms) {
                atomMap.put(atom.getName().toUpperCase(), atom);
            }
            for (String atomName : types.keySet()) {
                AtomType type = types.get(atomName);
                String bonds[] = forceField.getBonds(moleculeName, atomName.toUpperCase());
                if (bonds == null || bonds.length != 1) {
                    patched = false;
                    logger.info(" Check biotype for hydrogen " + type.name + ".");
                    break;
                }
                // Get the heavy atom the hydrogen is bonded to.
                Atom ia = atomMap.get(bonds[0].toUpperCase());
                Atom hydrogen = new Atom(0, atomName, ia.getAltLoc(), new double[3], ia.getResidueName(),
                        ia.getResidueNumber(), ia.getChainID(), ia.getOccupancy(), ia.getTempFactor(),
                        ia.getSegID());
                logger.fine(" Created hydrogen " + atomName + ".");
                hydrogen.setAtomType(type);
                hydrogen.setHetero(true);
                molecule.addMSNode(hydrogen);
                int valence = ia.getAtomType().valence;
                List<Bond> aBonds = ia.getBonds();
                int numBonds = aBonds.size();
                /**
                 * Try to find the following configuration: ib-ia-ic
                 */
                Atom ib = null;
                Atom ic = null;
                Atom id = null;
                if (numBonds > 0) {
                    Bond bond = aBonds.get(0);
                    ib = bond.get1_2(ia);
                }
                if (numBonds > 1) {
                    Bond bond = aBonds.get(1);
                    ic = bond.get1_2(ia);
                }
                if (numBonds > 2) {
                    Bond bond = aBonds.get(2);
                    id = bond.get1_2(ia);
                }

                /**
                 * Building the hydrogens depends on hybridization and the
                 * locations of other bonded atoms.
                 */
                logger.fine(" Bonding " + atomName + " to " + ia.getName() + " (" + numBonds + " of " + valence
                        + ").");
                switch (valence) {
                case 4:
                    switch (numBonds) {
                    case 3:
                        // Find the average coordinates of atoms ib, ic and id.
                        double b[] = ib.getXYZ(null);
                        double c[] = ib.getXYZ(null);
                        double d[] = ib.getXYZ(null);
                        double a[] = new double[3];
                        a[0] = (b[0] + c[0] + d[0]) / 3.0;
                        a[1] = (b[1] + c[1] + d[1]) / 3.0;
                        a[2] = (b[2] + c[2] + d[2]) / 3.0;
                        // Place the hydrogen at chiral position #1.
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, 1);
                        double e1[] = new double[3];
                        hydrogen.getXYZ(e1);
                        double ret[] = new double[3];
                        diff(a, e1, ret);
                        double l1 = r(ret);
                        // Place the hydrogen at chiral position #2.
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, -1);
                        double e2[] = new double[3];
                        hydrogen.getXYZ(e2);
                        diff(a, e2, ret);
                        double l2 = r(ret);
                        // Revert to #1 if it is farther from the average.
                        if (l1 > l2) {
                            hydrogen.setXYZ(e1);
                        }
                        break;
                    case 2:
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, 0);
                        break;
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 3:
                    switch (numBonds) {
                    case 2:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, ic, 0.0, 0);
                        break;
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 2:
                    switch (numBonds) {
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 1:
                    switch (numBonds) {
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                default:
                    logger.info(" Check biotype for hydrogen " + atomName + ".");
                    patched = false;
                }
                if (!patched) {
                    break;
                } else {
                    buildBond(ia, hydrogen);
                }
            }
        }
        if (!patched) {
            logger.log(Level.WARNING, format(" Deleting unrecognized molecule %s.", m.toString()));
            activeMolecularAssembly.deleteMolecule((Molecule) m);
        } else {
            logger.info(" Patch for " + moleculeName + " succeeded.");
        }
    }
}

From source file:ffx.potential.parsers.PDBFilter.java

/**
 * Assign force field atoms types to common chemistries using "biotype"
 * records./*from w w w.ja v a  2s.  com*/
 */
private void assignAtomTypes() {
    /**
     * Create a list to store bonds defined by PDB atom names.
     */
    bondList = new ArrayList<>();

    /**
     * To Do: Look for cyclic peptides and disulfides.
     */
    Polymer[] polymers = activeMolecularAssembly.getChains();

    /**
     * Loop over chains.
     */
    if (polymers != null) {
        logger.info(format("\n Assigning atom types for %d chains.", polymers.length));
        for (Polymer polymer : polymers) {
            List<Residue> residues = polymer.getResidues();
            int numberOfResidues = residues.size();
            /**
             * Check if all residues are known amino acids.
             */
            boolean isProtein = true;
            for (int residueNumber = 0; residueNumber < numberOfResidues; residueNumber++) {
                Residue residue = residues.get(residueNumber);
                String name = residue.getName().toUpperCase();
                boolean aa = false;
                for (AminoAcid3 amino : aminoAcidList) {
                    if (amino.toString().equalsIgnoreCase(name)) {
                        aa = true;
                        checkHydrogenAtomNames(residue);
                        break;
                    }
                }
                // Check for a patch.
                if (!aa) {
                    logger.info(" Checking for non-standard amino acid patch " + name);
                    HashMap<String, AtomType> types = forceField.getAtomTypes(name);
                    if (types.isEmpty()) {
                        isProtein = false;
                        break;
                    } else {
                        logger.info(" Patch found for non-standard amino acid " + name);
                    }
                }
            }

            /**
             * If all the residues in this chain have known amino acids
             * names, then attempt to assign atom types.
             */
            if (isProtein) {
                try {
                    logger.info(format(" Amino acid chain %s", polymer.getName()));
                    double dist = properties.getDouble("chainbreak", 3.0);
                    // Detect main chain breaks!
                    List<List<Residue>> subChains = findChainBreaks(residues, dist);
                    for (List<Residue> subChain : subChains) {
                        assignAminoAcidAtomTypes(subChain);
                    }
                } catch (MissingHeavyAtomException missingHeavyAtomException) {
                    logger.severe(missingHeavyAtomException.toString());
                } catch (MissingAtomTypeException missingAtomTypeException) {
                    logger.severe(missingAtomTypeException.toString());
                }
                continue;
            }

            /**
             * Check if all residues have known nucleic acids names.
             */
            boolean isNucleicAcid = true;
            for (int residueNumber = 0; residueNumber < numberOfResidues; residueNumber++) {
                Residue residue = residues.get(residueNumber);
                String name = residue.getName().toUpperCase();
                /**
                 * Convert 1 and 2-character nucleic acid names to
                 * 3-character names.
                 */
                if (name.length() == 1) {
                    if (name.equals("A")) {
                        name = NucleicAcid3.ADE.toString();
                    } else if (name.equals("C")) {
                        name = NucleicAcid3.CYT.toString();
                    } else if (name.equals("G")) {
                        name = NucleicAcid3.GUA.toString();
                    } else if (name.equals("T")) {
                        name = NucleicAcid3.THY.toString();
                    } else if (name.equals("U")) {
                        name = NucleicAcid3.URI.toString();
                    }
                } else if (name.length() == 2) {
                    if (name.equals("YG")) {
                        name = NucleicAcid3.YYG.toString();
                    }
                }
                residue.setName(name);
                NucleicAcid3 nucleicAcid = null;
                for (NucleicAcid3 nucleic : nucleicAcidList) {
                    String nuc3 = nucleic.toString();
                    nuc3 = nuc3.substring(nuc3.length() - 3);
                    if (nuc3.equalsIgnoreCase(name)) {
                        nucleicAcid = nucleic;
                        break;
                    }
                }
                if (nucleicAcid == null) {
                    logger.info(format("Nucleic acid was not recognized %s.", name));
                    isNucleicAcid = false;
                    break;
                }
            }

            /**
             * If all the residues in this chain have known nucleic acids
             * names, then attempt to assign atom types.
             */
            if (isNucleicAcid) {
                try {
                    logger.info(format(" Nucleic acid chain %s", polymer.getName()));
                    assignNucleicAcidAtomTypes(residues, forceField, bondList);
                } catch (MissingHeavyAtomException | MissingAtomTypeException e) {
                    logger.severe(e.toString());
                }
            }
        }
    }

    // Assign ion atom types.
    ArrayList<MSNode> ions = activeMolecularAssembly.getIons();
    if (ions != null && ions.size() > 0) {
        logger.info(format(" Assigning atom types for %d ions.", ions.size()));
        for (MSNode m : ions) {
            Molecule ion = (Molecule) m;
            String name = ion.getResidueName().toUpperCase();
            HetAtoms hetatm = HetAtoms.valueOf(name);
            Atom atom = ion.getAtomList().get(0);
            if (ion.getAtomList().size() != 1) {
                logger.severe(format(" Check residue %s of chain %s.", ion.toString(), ion.getChainID()));
            }
            try {
                switch (hetatm) {
                case NA:
                    atom.setAtomType(findAtomType(2003));
                    break;
                case K:
                    atom.setAtomType(findAtomType(2004));
                    break;
                case MG:
                case MG2:
                    atom.setAtomType(findAtomType(2005));
                    break;
                case CA:
                case CA2:
                    atom.setAtomType(findAtomType(2006));
                    break;
                case CL:
                    atom.setAtomType(findAtomType(2007));
                    break;
                case ZN:
                case ZN2:
                    atom.setAtomType(findAtomType(2008));
                    break;
                case BR:
                    atom.setAtomType(findAtomType(2009));
                    break;
                default:
                    logger.severe(format(" Check residue %s of chain %s.", ion.toString(), ion.getChainID()));
                }
            } catch (Exception e) {
                String message = "Error assigning atom types.";
                logger.log(Level.SEVERE, message, e);
            }
        }
    }
    // Assign water atom types.
    ArrayList<MSNode> water = activeMolecularAssembly.getWaters();
    if (water != null && water.size() > 0) {
        logger.info(format(" Assigning atom types for %d waters.", water.size()));
        for (MSNode m : water) {
            Molecule wat = (Molecule) m;
            try {
                Atom O = buildHeavy(wat, "O", null, 2001);
                Atom H1 = buildHydrogen(wat, "H1", O, 0.96e0, null, 109.5e0, null, 120.0e0, 0, 2002);
                H1.setHetero(true);
                Atom H2 = buildHydrogen(wat, "H2", O, 0.96e0, H1, 109.5e0, null, 120.0e0, 0, 2002);
                H2.setHetero(true);
            } catch (Exception e) {
                String message = "Error assigning atom types to a water.";
                logger.log(Level.SEVERE, message, e);
            }
        }
    }

    // Assign small molecule atom types.
    ArrayList<Molecule> molecules = activeMolecularAssembly.getMolecules();
    for (MSNode m : molecules) {
        Molecule molecule = (Molecule) m;
        String moleculeName = molecule.getResidueName();
        logger.info(" Attempting to patch " + moleculeName);
        ArrayList<Atom> moleculeAtoms = molecule.getAtomList();
        boolean patched = true;
        HashMap<String, AtomType> types = forceField.getAtomTypes(moleculeName);
        /**
         * Assign atom types for all known atoms.
         */
        for (Atom atom : moleculeAtoms) {
            String atomName = atom.getName().toUpperCase();
            AtomType atomType = types.get(atomName);
            if (atomType == null) {
                logger.info(" No atom type was found for " + atomName + " of " + moleculeName + ".");
                patched = false;
                break;
            } else {
                logger.fine(" " + atom.toString() + " -> " + atomType.toString());
                atom.setAtomType(atomType);
                types.remove(atomName);
            }
        }
        /**
         * Create missing hydrogen atoms. Check for missing heavy atoms.
         */
        if (patched && !types.isEmpty()) {
            for (AtomType type : types.values()) {
                if (type.atomicNumber != 1) {
                    logger.info(" Missing heavy atom " + type.name);
                    patched = false;
                    break;
                }
            }
        }
        // Create bonds between known atoms.
        if (patched) {
            for (Atom atom : moleculeAtoms) {
                String atomName = atom.getName();
                String bonds[] = forceField.getBonds(moleculeName, atomName);
                if (bonds != null) {
                    for (String name : bonds) {
                        Atom atom2 = molecule.getAtom(name);
                        if (atom2 != null && !atom.isBonded(atom2)) {
                            buildBond(atom, atom2);
                        }
                    }
                }
            }
        }
        // Create missing hydrogen atoms.
        if (patched && !types.isEmpty()) {
            // Create a hashmap of the molecule's atoms
            HashMap<String, Atom> atomMap = new HashMap<String, Atom>();
            for (Atom atom : moleculeAtoms) {
                atomMap.put(atom.getName().toUpperCase(), atom);
            }
            for (String atomName : types.keySet()) {
                AtomType type = types.get(atomName);
                String bonds[] = forceField.getBonds(moleculeName, atomName.toUpperCase());
                if (bonds == null || bonds.length != 1) {
                    patched = false;
                    logger.info(" Check biotype for hydrogen " + type.name + ".");
                    break;
                }
                // Get the heavy atom the hydrogen is bonded to.
                Atom ia = atomMap.get(bonds[0].toUpperCase());
                Atom hydrogen = new Atom(0, atomName, ia.getAltLoc(), new double[3], ia.getResidueName(),
                        ia.getResidueNumber(), ia.getChainID(), ia.getOccupancy(), ia.getTempFactor(),
                        ia.getSegID());
                logger.fine(" Created hydrogen " + atomName + ".");
                hydrogen.setAtomType(type);
                hydrogen.setHetero(true);
                molecule.addMSNode(hydrogen);
                int valence = ia.getAtomType().valence;
                List<Bond> aBonds = ia.getBonds();
                int numBonds = aBonds.size();
                /**
                 * Try to find the following configuration: ib-ia-ic
                 */
                Atom ib = null;
                Atom ic = null;
                Atom id = null;
                if (numBonds > 0) {
                    Bond bond = aBonds.get(0);
                    ib = bond.get1_2(ia);
                }
                if (numBonds > 1) {
                    Bond bond = aBonds.get(1);
                    ic = bond.get1_2(ia);
                }
                if (numBonds > 2) {
                    Bond bond = aBonds.get(2);
                    id = bond.get1_2(ia);
                }

                /**
                 * Building the hydrogens depends on hybridization and the
                 * locations of other bonded atoms.
                 */
                logger.fine(" Bonding " + atomName + " to " + ia.getName() + " (" + numBonds + " of " + valence
                        + ").");
                switch (valence) {
                case 4:
                    switch (numBonds) {
                    case 3:
                        // Find the average coordinates of atoms ib, ic and id.
                        double b[] = ib.getXYZ(null);
                        double c[] = ib.getXYZ(null);
                        double d[] = ib.getXYZ(null);
                        double a[] = new double[3];
                        a[0] = (b[0] + c[0] + d[0]) / 3.0;
                        a[1] = (b[1] + c[1] + d[1]) / 3.0;
                        a[2] = (b[2] + c[2] + d[2]) / 3.0;
                        // Place the hydrogen at chiral position #1.
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, 1);
                        double e1[] = new double[3];
                        hydrogen.getXYZ(e1);
                        double ret[] = new double[3];
                        diff(a, e1, ret);
                        double l1 = r(ret);
                        // Place the hydrogen at chiral position #2.
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, -1);
                        double e2[] = new double[3];
                        hydrogen.getXYZ(e2);
                        diff(a, e2, ret);
                        double l2 = r(ret);
                        // Revert to #1 if it is farther from the average.
                        if (l1 > l2) {
                            hydrogen.setXYZ(e1);
                        }
                        break;
                    case 2:
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, ic, 109.5, 0);
                        break;
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 109.5, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 3:
                    switch (numBonds) {
                    case 2:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, ic, 0.0, 0);
                        break;
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 2:
                    switch (numBonds) {
                    case 1:
                        intxyz(hydrogen, ia, 1.0, ib, 120.0, null, 0.0, 0);
                        break;
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                case 1:
                    switch (numBonds) {
                    case 0:
                        intxyz(hydrogen, ia, 1.0, null, 0.0, null, 0.0, 0);
                        break;
                    default:
                        logger.info(" Check biotype for hydrogen " + atomName + ".");
                        patched = false;
                    }
                    break;
                default:
                    logger.info(" Check biotype for hydrogen " + atomName + ".");
                    patched = false;
                }
                if (!patched) {
                    break;
                } else {
                    buildBond(ia, hydrogen);
                }
            }
        }
        if (!patched) {
            logger.log(Level.WARNING, format(" Deleting unrecognized molecule %s.", m.toString()));
            activeMolecularAssembly.deleteMolecule((Molecule) m);
        } else {
            logger.info(" Patch for " + moleculeName + " succeeded.");
        }
    }
}

From source file:StorageEngineClient.CombineColumnStorageFileInputFormat.java

private void getMoreSplits(JobConf job, Path[] paths1, long maxSize, long minSizeNode, long minSizeRack,
        List<CombineFileSplit> splits) throws IOException {
    if (paths1.length == 0) {
        return;/*ww w.  jav a 2  s . c om*/
    }

    HashMap<String, Path> strpath = new HashMap<String, Path>();
    HashMap<String, Long> pathmap = new HashMap<String, Long>();
    for (Path p : paths1) {
        if (p.toString().contains("_idx")) {
            String pstr = p.toString().substring(0, p.toString().lastIndexOf("_idx"));
            if (!pathmap.containsKey(pstr)) {
                pathmap.put(pstr, 0l);
                strpath.put(pstr, p);
            }
            long len = p.getFileSystem(job).getFileStatus(p).getLen();
            pathmap.put(pstr, pathmap.get(pstr) + len);
        }
    }

    Path[] paths = new Path[pathmap.size()];

    int ii = 0;
    for (String str : pathmap.keySet()) {
        paths[ii++] = strpath.get(str);
    }

    OneFileInfo[] files;

    HashMap<String, List<OneBlockInfo>> rackToBlocks = new HashMap<String, List<OneBlockInfo>>();

    HashMap<OneBlockInfo, String[]> blockToNodes = new HashMap<OneBlockInfo, String[]>();

    HashMap<String, List<OneBlockInfo>> nodeToBlocks = new HashMap<String, List<OneBlockInfo>>();

    files = new OneFileInfo[paths.length];

    long totLength = 0;
    ii = 0;
    for (String str : pathmap.keySet()) {
        files[ii] = new OneFileInfo(strpath.get(str), job, rackToBlocks, blockToNodes, nodeToBlocks);
        totLength += pathmap.get(str);
    }
    ArrayList<OneBlockInfo> validBlocks = new ArrayList<OneBlockInfo>();
    ArrayList<String> nodes = new ArrayList<String>();
    long curSplitSize = 0;

    for (Iterator<Map.Entry<String, List<OneBlockInfo>>> iter = nodeToBlocks.entrySet().iterator(); iter
            .hasNext();) {

        Map.Entry<String, List<OneBlockInfo>> one = iter.next();
        nodes.add(one.getKey());
        List<OneBlockInfo> blocksInNode = one.getValue();

        for (OneBlockInfo oneblock : blocksInNode) {
            if (blockToNodes.containsKey(oneblock)) {
                validBlocks.add(oneblock);
                blockToNodes.remove(oneblock);
                curSplitSize += oneblock.length;

                if (maxSize != 0 && curSplitSize >= maxSize) {
                    addCreatedSplit(job, splits, nodes, validBlocks);
                    curSplitSize = 0;
                    validBlocks.clear();
                }
            }
        }
        if (minSizeNode != 0 && curSplitSize >= minSizeNode) {
            addCreatedSplit(job, splits, nodes, validBlocks);
        } else {
            for (OneBlockInfo oneblock : validBlocks) {
                blockToNodes.put(oneblock, oneblock.hosts);
            }
        }
        validBlocks.clear();
        nodes.clear();
        curSplitSize = 0;
    }

    ArrayList<OneBlockInfo> overflowBlocks = new ArrayList<OneBlockInfo>();
    ArrayList<String> racks = new ArrayList<String>();

    while (blockToNodes.size() > 0) {

        for (Iterator<Map.Entry<String, List<OneBlockInfo>>> iter = rackToBlocks.entrySet().iterator(); iter
                .hasNext();) {

            Map.Entry<String, List<OneBlockInfo>> one = iter.next();
            List<OneBlockInfo> blocks = one.getValue();

            boolean createdSplit = false;
            for (OneBlockInfo oneblock : blocks) {
                if (blockToNodes.containsKey(oneblock)) {
                    validBlocks.add(oneblock);
                    blockToNodes.remove(oneblock);
                    curSplitSize += oneblock.length;
                    for (int i = 0; i < oneblock.hosts.length; i++) {
                        racks.add(oneblock.hosts[i]);
                    }

                    if (maxSize != 0 && curSplitSize >= maxSize) {
                        addCreatedSplit(job, splits, racks, validBlocks);
                        createdSplit = true;
                        break;
                    }
                }
            }

            if (createdSplit) {
                curSplitSize = 0;
                validBlocks.clear();
                racks.clear();
                continue;
            }

            if (!validBlocks.isEmpty()) {
                if (minSizeRack != 0 && curSplitSize >= minSizeRack) {
                    addCreatedSplit(job, splits, racks, validBlocks);
                } else {
                    overflowBlocks.addAll(validBlocks);
                }
            }
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    assert blockToNodes.isEmpty();
    assert curSplitSize == 0;
    assert validBlocks.isEmpty();
    assert racks.isEmpty();

    for (OneBlockInfo oneblock : overflowBlocks) {
        validBlocks.add(oneblock);
        curSplitSize += oneblock.length;

        for (int i = 0; i < oneblock.racks.length; i++) {
            racks.add(oneblock.hosts[i]);
        }

        if (maxSize != 0 && curSplitSize >= maxSize) {
            addCreatedSplit(job, splits, racks, validBlocks);
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    if (!validBlocks.isEmpty()) {
        addCreatedSplit(job, splits, racks, validBlocks);
    }
}

From source file:com.hiperf.common.ui.server.storage.impl.PersistenceHelper.java

@Override
public Map<String, String> getAll(String rootClassName, String filter, String attPrefix, String childClassName,
        String childAttribute) throws PersistenceException {
    EntityManager em = null;//from  w  w w.  ja  v a  2s. c o m

    if (attPrefix != null && !attPrefix.isEmpty()) {
        String[] s = attPrefix.split("\\.");
        if (s == null || s.length == 0)
            s = new String[] { attPrefix };
        StringBuilder join = new StringBuilder();
        StringBuilder select = new StringBuilder();
        String lastClass = rootClassName;
        String lastJoinPrefix = "o.";
        int i = 0;
        for (int j = 0; j < s.length; j++) {
            String ss = s[j];
            PropertyDescriptor[] pds = propertyDescriptorsByClassName.get(lastClass);
            for (PropertyDescriptor pd : pds) {
                if (pd.getName().equals(ss)) {
                    Class<?> pt = pd.getPropertyType();
                    if (Collection.class.isAssignableFrom(pt) || INakedObject.class.isAssignableFrom(pt)) {

                        join.append(" inner join ").append(lastJoinPrefix);
                        if (!lastJoinPrefix.endsWith(".")) {
                            join.append(".");
                        }
                        join.append(ss).append(" y").append(i).append(" ");
                        lastJoinPrefix = "y" + i + ".";
                        i++;
                        if (Collection.class.isAssignableFrom(pt)) {
                            Class<?> clazz;
                            try {
                                clazz = Class.forName(lastClass);
                                ParameterizedType genericType = (ParameterizedType) clazz.getDeclaredField(ss)
                                        .getGenericType();
                                if (genericType != null) {
                                    for (Type t : genericType.getActualTypeArguments()) {

                                        if (t instanceof Class
                                                && INakedObject.class.isAssignableFrom((Class) t)) {
                                            lastClass = ((Class) t).getName();
                                            break;
                                        }
                                    }
                                }
                            } catch (Exception e) {
                                logger.log(Level.SEVERE, "Error", e);
                            }
                        } else {
                            lastClass = pt.getName();
                        }

                    } else if (childClassName.equals(pt.getName())) {
                        lastJoinPrefix += ss + ".";
                        if (i == s.length - 1) {
                            select.append("select ").append(lastJoinPrefix).append(childAttribute)
                                    .append(" from ").append(rootClassName).append(" o ");
                        }
                        lastClass = pt.getName();
                    } else {
                        lastJoinPrefix += ss + ".";
                        lastClass = pt.getName();
                    }
                    break;
                }
            }
        }
        HashMap<String, String> joinMap = null;
        if (join.length() > 0) {
            if (filter != null && !filter.isEmpty()) {
                int idx = filter.indexOf("inner join ");
                if (idx >= 0) {
                    joinMap = new HashMap<>();
                    String[] ff = filter.substring(idx + 11, filter.indexOf("where")).trim()
                            .split("inner join ");
                    for (String fj : ff) {
                        fj = fj.trim();
                        String[] fjj = fj.split(" ");
                        if (fjj.length == 2) {
                            joinMap.put(fjj[0].trim(), fjj[1].trim());
                        }
                    }
                }
            }
        }
        if (joinMap != null && !joinMap.isEmpty() && join.length() > 0) {
            String jj = join.toString();
            for (Entry<String, String> e : joinMap.entrySet()) {
                String tmp = "inner join " + e.getKey();
                int k = jj.indexOf(tmp);
                if (k >= 0) {
                    String ss = join.substring(k + tmp.length()).trim();
                    int m = ss.indexOf(" ");
                    if (m > 0) {
                        ss = ss.substring(0, m);
                    }
                    jj = jj.replace(ss, e.getValue());
                    /*lastJoinPrefix = lastJoinPrefix.replaceAll(ss, e.getValue());
                    join.replace(k, k + tmp.length() + 1 + ss.length(), "");*/
                }
            }
        }
        if (select.length() == 0) {
            select.append("select ").append(lastJoinPrefix).append(childAttribute).append(" from ")
                    .append(rootClassName).append(" o ");
        }
        if (join.length() > 0) {
            select.append(join);
        }
        if (filter != null && !filter.isEmpty()) {
            if (!filter.toLowerCase().contains("where"))
                select.append(" where ");
            select.append(filter);
        }
        select.append(" order by ").append(lastJoinPrefix).append(childAttribute).append(" asc");
        String jpql = select.toString();
        List<Date> dtParams = new ArrayList<Date>();
        try {
            em = getEntityManager();
            jpql = replaceDateParameters(jpql, dtParams);
            Query q = em.createQuery(jpql);
            List<Object> list = getResults(dtParams, q);
            if (list != null && !list.isEmpty()) {
                Map<String, String> map = new LinkedHashMap<String, String>();
                for (Object o : list) {
                    StorageHelper.fillGetAllMap(map, o);
                }
                return map;
            } else
                return null;
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Exception in getAll " + e.getMessage(), e);
            throw new PersistenceException("Exception in getAll " + e.getMessage(), e);
        } finally {
            closeEntityManager(em);
        }
    } else {
        try {
            em = getEntityManager();
            String jql = "select distinct o.";
            jql += childAttribute + " from " + rootClassName + " o ";
            if (filter != null && !filter.isEmpty()) {
                jql += filter;
            }
            jql += " order by o." + childAttribute + " asc";
            List<Date> dtParams = new ArrayList<Date>();
            jql = replaceDateParameters(jql, dtParams);
            Query q = em.createQuery(jql);
            List<Object> list = getResults(dtParams, q);

            if (list != null && !list.isEmpty()) {
                Map<String, String> map = new LinkedHashMap<String, String>();
                for (Object o : list) {
                    StorageHelper.fillGetAllMap(map, o);
                }
                return map;
            } else
                return null;
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Exception in getAll " + e.getMessage(), e);
            throw new PersistenceException("Exception in getAll " + e.getMessage(), e);
        } finally {
            closeEntityManager(em);
        }
    }
}

From source file:StorageEngineClient.CombineFileInputFormat_bak.java

private void getMoreSplits(JobConf job, Path[] paths1, long maxSize, long minSizeNode, long minSizeRack,
        List<CombineFileSplit> splits) throws IOException {
    if (paths1.length == 0) {
        return;//w  w w  . j av a 2 s.com
    }

    Path[] paths = paths1;
    ArrayList<Path> splitable = new ArrayList<Path>();
    ArrayList<Path> unsplitable = new ArrayList<Path>();
    for (int i = 0; i < paths1.length; i++) {
        if (isSplitable(paths1[i].getFileSystem(job), paths1[i])) {
            splitable.add(paths1[i]);
        } else {
            unsplitable.add(paths1[i]);
        }
    }
    if (unsplitable.size() != 0) {
        paths = new Path[splitable.size()];
        splitable.toArray(paths);
    }

    OneFileInfo[] files;

    HashMap<String, List<OneBlockInfo>> rackToBlocks = new HashMap<String, List<OneBlockInfo>>();

    HashMap<OneBlockInfo, String[]> blockToNodes = new HashMap<OneBlockInfo, String[]>();

    HashMap<String, List<OneBlockInfo>> nodeToBlocks = new HashMap<String, List<OneBlockInfo>>();

    files = new OneFileInfo[paths.length];

    long totLength = 0;
    for (int i = 0; i < paths.length; i++) {
        files[i] = new OneFileInfo(paths[i], job, rackToBlocks, blockToNodes, nodeToBlocks);
        totLength += files[i].getLength();
    }
    ArrayList<OneBlockInfo> validBlocks = new ArrayList<OneBlockInfo>();
    ArrayList<String> nodes = new ArrayList<String>();
    long curSplitSize = 0;

    for (Iterator<Map.Entry<String, List<OneBlockInfo>>> iter = nodeToBlocks.entrySet().iterator(); iter
            .hasNext();) {

        Map.Entry<String, List<OneBlockInfo>> one = iter.next();
        nodes.add(one.getKey());
        List<OneBlockInfo> blocksInNode = one.getValue();

        for (OneBlockInfo oneblock : blocksInNode) {
            if (blockToNodes.containsKey(oneblock)) {
                validBlocks.add(oneblock);
                blockToNodes.remove(oneblock);
                curSplitSize += oneblock.length;

                if (maxSize != 0 && curSplitSize >= maxSize) {
                    addCreatedSplit(job, splits, nodes, validBlocks);
                    curSplitSize = 0;
                    validBlocks.clear();
                }
            }
        }
        if (minSizeNode != 0 && curSplitSize >= minSizeNode) {
            addCreatedSplit(job, splits, nodes, validBlocks);
        } else {
            for (OneBlockInfo oneblock : validBlocks) {
                blockToNodes.put(oneblock, oneblock.hosts);
            }
        }
        validBlocks.clear();
        nodes.clear();
        curSplitSize = 0;
    }

    ArrayList<OneBlockInfo> overflowBlocks = new ArrayList<OneBlockInfo>();
    ArrayList<String> racks = new ArrayList<String>();

    while (blockToNodes.size() > 0) {

        for (Iterator<Map.Entry<String, List<OneBlockInfo>>> iter = rackToBlocks.entrySet().iterator(); iter
                .hasNext();) {

            Map.Entry<String, List<OneBlockInfo>> one = iter.next();
            List<OneBlockInfo> blocks = one.getValue();

            boolean createdSplit = false;
            for (OneBlockInfo oneblock : blocks) {
                if (blockToNodes.containsKey(oneblock)) {
                    validBlocks.add(oneblock);
                    blockToNodes.remove(oneblock);
                    curSplitSize += oneblock.length;
                    for (int i = 0; i < oneblock.hosts.length; i++) {
                        racks.add(oneblock.hosts[i]);
                    }

                    if (maxSize != 0 && curSplitSize >= maxSize) {
                        addCreatedSplit(job, splits, racks, validBlocks);
                        createdSplit = true;
                        break;
                    }
                }
            }

            if (createdSplit) {
                curSplitSize = 0;
                validBlocks.clear();
                racks.clear();
                continue;
            }

            if (!validBlocks.isEmpty()) {
                if (minSizeRack != 0 && curSplitSize >= minSizeRack) {
                    addCreatedSplit(job, splits, racks, validBlocks);
                } else {
                    overflowBlocks.addAll(validBlocks);
                }
            }
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    assert blockToNodes.isEmpty();
    assert curSplitSize == 0;
    assert validBlocks.isEmpty();
    assert racks.isEmpty();

    for (OneBlockInfo oneblock : overflowBlocks) {
        validBlocks.add(oneblock);
        curSplitSize += oneblock.length;

        for (int i = 0; i < oneblock.racks.length; i++) {
            racks.add(oneblock.hosts[i]);
        }

        if (maxSize != 0 && curSplitSize >= maxSize) {
            addCreatedSplit(job, splits, racks, validBlocks);
            curSplitSize = 0;
            validBlocks.clear();
            racks.clear();
        }
    }

    if (!validBlocks.isEmpty()) {
        addCreatedSplit(job, splits, racks, validBlocks);
    }

    if (unsplitable.size() != 0) {
        curSplitSize = 0;
        nodes.clear();
        validBlocks.clear();
        for (Path path : unsplitable) {
            FileSystem fs = path.getFileSystem(job);
            FileStatus stat = fs.getFileStatus(path);
            LOG.info(path.toString());
            long len = fs.getFileStatus(path).getLen();
            curSplitSize += len;
            BlockLocation[] locations = path.getFileSystem(job).getFileBlockLocations(stat, 0, len);
            OneBlockInfo oneblock = new OneBlockInfo(path, 0, len, locations[0].getHosts(),
                    locations[0].getTopologyPaths());
            validBlocks.add(oneblock);
            for (int i = 0; i < oneblock.hosts.length; i++) {
                nodes.add(oneblock.hosts[i]);
            }

            if (maxSize != 0 && curSplitSize >= maxSize) {
                addCreatedSplit(job, splits, nodes, validBlocks);
                curSplitSize = 0;
                validBlocks.clear();
                nodes.clear();
            }
        }
        if (!validBlocks.isEmpty()) {
            addCreatedSplit(job, splits, nodes, validBlocks);
        }
    }
}

From source file:imitationNLG.SFX.java

public void runTestWithJAROW(boolean useDAggerArg, boolean useDAggerWord) {
    File dataFile = new File("sfx_data/sfx" + dataset + "/train+valid+test.json");

    String wenFile = "results/wenResults/sfxhotel.log";
    if (dataset.equals("restaurant")) {
        wenFile = "results/wenResults/sfxrest.log";
    }/*ww w.ja  v a2  s.  c  om*/

    boolean useValidation = true;
    boolean useRandomAlignments = false;
    createLists(dataFile);

    //RANDOM DATA SPLIT
    /*int to = (int) Math.round(datasetInstances.size() * 0.1);
     if (to > datasetInstances.size()) {
     to = datasetInstances.size();
     }
     ArrayList<DatasetInstance> trainingData = new ArrayList<>();
     ArrayList<DatasetInstance> validationData = new ArrayList<>();
     ArrayList<DatasetInstance> testingData = new ArrayList<>();
            
     for (String predicate : predicates) {
     trainingData.addAll(datasetInstances.get(predicate).subList(0, ((int) Math.round(datasetInstances.get(predicate).size() * 0.3))));
     validationData.addAll(datasetInstances.get(predicate).subList(((int) Math.round(datasetInstances.get(predicate).size() * 0.3)), ((int) Math.round(datasetInstances.get(predicate).size() * 0.4))));
     testingData.addAll(datasetInstances.get(predicate).subList(((int) Math.round(datasetInstances.get(predicate).size() * 0.4)), datasetInstances.get(predicate).size()));
     }*/
    //WEN DATA SPLIT
    //PRINT RESULTS
    ArrayList<String> mrs = new ArrayList<String>();
    try (BufferedReader br = new BufferedReader(new FileReader(wenFile))) {
        String s;
        while ((s = br.readLine()) != null) {
            if (!s.trim().isEmpty()) {
                mrs.add(s.trim());
            }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Bagel.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Bagel.class.getName()).log(Level.SEVERE, null, ex);
    }
    ArrayList<String> testWenMRs = new ArrayList<>();
    wenDaToGen = new HashMap<>();
    try (BufferedReader br = new BufferedReader(new FileReader(wenFile))) {
        String s;
        boolean inGen = false;
        boolean inRef = false;
        String da = "";
        while ((s = br.readLine()) != null) {
            if (s.startsWith("DA")) {
                inGen = false;
                inRef = false;
                da = s.substring(s.indexOf(":") + 1).replaceAll(",", ";").replaceAll("no or yes", "yes or no")
                        .replaceAll("ave ; presidio", "ave and presidio")
                        .replaceAll("point ; ste", "point and ste").trim();
                testWenMRs.add(da);
            } else if (s.startsWith("Gen")) {
                inGen = true;
            } else if (s.startsWith("Ref")) {
                inRef = true;
            } else if (inGen) {
                inGen = false;

                if (!wenDaToGen.containsKey(da)) {
                    wenDaToGen.put(da.toLowerCase(), s.trim());
                }
                da = "";
            } else if (inRef) {
                if (s.trim().isEmpty()) {
                    inRef = false;
                    da = "";
                }
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Bagel.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Bagel.class.getName()).log(Level.SEVERE, null, ex);
    }

    ArrayList<DatasetInstance> trainingData = new ArrayList<>();
    ArrayList<DatasetInstance> validationData = new ArrayList<>();
    ArrayList<DatasetInstance> testingData = new ArrayList<>();

    System.out.println(testWenMRs.size());
    ArrayList<DatasetInstance> restData = new ArrayList<>();
    for (String predicate : predicates) {
        for (DatasetInstance in : datasetInstances.get(predicate)) {
            if (testWenMRs.contains(in.getMeaningRepresentation().getMRstr())) {
                testingData.add(in);
                testWenMRs.remove(in.getMeaningRepresentation().getMRstr());
            } else {
                restData.add(in);
            }
        }
    }
    Collections.shuffle(restData);
    for (int i = 0; i < restData.size(); i++) {
        if (i < testingData.size()) {
            validationData.add(restData.get(i));
        } else {
            trainingData.add(restData.get(i));
        }
    }
    System.out.println(trainingData.size());
    System.out.println(validationData.size());
    System.out.println(testingData.size());
    System.out.println(testWenMRs);

    r = new Random(seed);

    HashMap<String, JAROW> classifiersAttrs = new HashMap<>();
    HashMap<String, HashMap<String, JAROW>> classifiersWords = new HashMap<>();

    HashMap<Integer, HashSet<String>> nGrams;
    if (!useRandomAlignments) {
        nGrams = createNaiveAlignments(trainingData);
    } else {
        nGrams = createRandomAlignments(trainingData);
    }

    HashMap<String, HashSet<String>> availableAttributeActions = new HashMap<>();
    HashMap<String, HashMap<String, HashSet<Action>>> availableWordActions = new HashMap<>();
    for (DatasetInstance DI : trainingData) {
        String predicate = DI.getMeaningRepresentation().getPredicate();
        if (!availableAttributeActions.containsKey(predicate)) {
            availableAttributeActions.put(predicate, new HashSet<String>());
        }
        if (!availableWordActions.containsKey(predicate)) {
            availableWordActions.put(predicate, new HashMap<String, HashSet<Action>>());
        }

        ArrayList<Action> realization = DI.getTrainRealization();
        for (Action a : realization) {
            if (!a.getAttribute().equals(SFX.TOKEN_END)) {
                String attr = "";
                if (a.getAttribute().contains("=")) {
                    attr = a.getAttribute().substring(0, a.getAttribute().indexOf('='));
                } else {
                    attr = a.getAttribute();
                }
                availableAttributeActions.get(predicate).add(attr);
                if (!availableWordActions.get(predicate).containsKey(attr)) {
                    availableWordActions.get(predicate).put(attr, new HashSet<Action>());
                    availableWordActions.get(predicate).get(attr).add(new Action(SFX.TOKEN_END, attr));
                }
                if (!a.getWord().equals(SFX.TOKEN_START) && !a.getWord().equals(SFX.TOKEN_END)
                        && !a.getWord().matches("([,.?!;:'])")) {
                    if (a.getWord().startsWith(SFX.TOKEN_X)) {
                        if (a.getWord().substring(3, a.getWord().lastIndexOf('_')).toLowerCase().trim()
                                .equals(attr)) {
                            availableWordActions.get(predicate).get(attr).add(new Action(a.getWord(), attr));
                        }
                    } else {
                        availableWordActions.get(predicate).get(attr).add(new Action(a.getWord(), attr));
                    }
                }
                if (attr.equals("[]")) {
                    System.out.println("RR " + realization);
                    System.out.println("RR " + a);
                }
            }
        }
    }
    //ONLY WHEN RANDOM ALIGNMENTS
    if (useRandomAlignments) {
        valueAlignments = new HashMap<>();
    }

    Object[] results = createTrainingDatasets(trainingData, availableAttributeActions, availableWordActions,
            nGrams);
    HashMap<String, ArrayList<Instance>> predicateAttrTrainingData = (HashMap<String, ArrayList<Instance>>) results[0];
    HashMap<String, HashMap<String, ArrayList<Instance>>> predicateWordTrainingData = (HashMap<String, HashMap<String, ArrayList<Instance>>>) results[1];

    boolean setToGo = true;
    if (predicateWordTrainingData.isEmpty() || predicateAttrTrainingData.isEmpty()) {
        setToGo = false;
    }
    if (setToGo) {
        JDAggerForSFX JDWords = new JDAggerForSFX(this);
        Object[] LOLSResults = null;
        if (useValidation) {
            LOLSResults = JDWords.runLOLS(availableAttributeActions, trainingData, predicateAttrTrainingData,
                    predicateWordTrainingData, availableWordActions, valueAlignments, wordRefRolloutChance,
                    validationData, nGrams);
        } else {
            LOLSResults = JDWords.runLOLS(availableAttributeActions, trainingData, predicateAttrTrainingData,
                    predicateWordTrainingData, availableWordActions, valueAlignments, wordRefRolloutChance,
                    testingData, nGrams);
        }

        classifiersAttrs = (HashMap<String, JAROW>) LOLSResults[0];
        classifiersWords = (HashMap<String, HashMap<String, JAROW>>) LOLSResults[1];

        if (useValidation) {
            evaluateGeneration(classifiersAttrs, classifiersWords, trainingData, validationData,
                    availableAttributeActions, availableWordActions, nGrams, true, 100000);
        } else {
            evaluateGeneration(classifiersAttrs, classifiersWords, trainingData, testingData,
                    availableAttributeActions, availableWordActions, nGrams, true, 100000);
        }
    }
    double finalAvgArgDistance = 0.0;
    for (Double avg : crossAvgArgDistances) {
        finalAvgArgDistance += avg;
    }
    finalAvgArgDistance /= crossAvgArgDistances.size();

    System.out.println("==========================");
    System.out.println("==========================");
    System.out.println("Final avg arg distance: \t" + finalAvgArgDistance);

    double finalNISTDistance = 0.0;
    for (Double avg : crossNIST) {
        finalNISTDistance += avg;
    }
    finalNISTDistance /= crossNIST.size();

    System.out.println("Final NIST: \t" + finalNISTDistance);

    double finalBLEUDistance = 0.0;
    for (Double avg : crossBLEU) {
        finalBLEUDistance += avg;
    }
    finalBLEUDistance /= crossBLEU.size();

    System.out.println("Final BLEU: \t" + finalBLEUDistance);

    double finalBLEUSmoothDistance = 0.0;
    for (Double avg : crossBLEUSmooth) {
        finalBLEUSmoothDistance += avg;
    }
    finalBLEUSmoothDistance /= crossBLEUSmooth.size();

    System.out.println("Final BLEUSmooth: \t" + finalBLEUSmoothDistance);
    System.out.println("==========================");
    System.out.println("==========================");
}

From source file:imitationNLG.SFX.java

public String chooseNextValue(String attribute, HashSet<String> attrValuesToBeMentioned,
        ArrayList<DatasetInstance> trainingData) {
    HashMap<String, Integer> relevantValues = new HashMap<>();
    for (String attrValue : attrValuesToBeMentioned) {
        String attr = attrValue.substring(0, attrValue.indexOf('='));
        String value = attrValue.substring(attrValue.indexOf('=') + 1);
        if (attr.equals(attribute)) {
            relevantValues.put(value, 0);
        }//from  ww w  .  j  a  v a 2s . com
    }
    if (!relevantValues.isEmpty()) {
        if (relevantValues.keySet().size() == 1) {
            for (String value : relevantValues.keySet()) {
                return value;
            }
        } else {
            String bestValue = "";
            int minIndex = Integer.MAX_VALUE;
            for (String value : relevantValues.keySet()) {
                if (value.startsWith("x")) {
                    int vI = Integer.parseInt(value.substring(1));
                    if (vI < minIndex) {
                        minIndex = vI;
                        bestValue = value;
                    }
                }
            }
            if (!bestValue.isEmpty()) {
                return bestValue;
            }
            for (DatasetInstance di : trainingData) {
                for (ArrayList<Action> ac : di.getEvalMentionedValueSequences().keySet()) {
                    ArrayList<String> mentionedValueSeq = di.getEvalMentionedValueSequences().get(ac);
                    boolean doesSeqContainValues = true;
                    minIndex = Integer.MAX_VALUE;
                    for (String value : relevantValues.keySet()) {
                        int index = mentionedValueSeq.indexOf(attribute + "=" + value);
                        if (index != -1 && index < minIndex) {
                            minIndex = index;
                            bestValue = value;
                        } else if (index == -1) {
                            doesSeqContainValues = false;
                        }
                    }
                    if (doesSeqContainValues) {
                        relevantValues.put(bestValue, relevantValues.get(bestValue) + 1);
                    }
                }
            }
            int max = -1;
            for (String value : relevantValues.keySet()) {
                if (relevantValues.get(value) > max) {
                    max = relevantValues.get(value);
                    bestValue = value;
                }
            }
            return bestValue;
        }
    }
    return "";
}

From source file:com.sonicle.webtop.core.app.ServiceManager.java

private boolean upgradeServiceDb(ServiceDescriptor desc, ArrayList<SqlUpgradeScript> scripts, String upgradeTag,
        boolean autoUpdate) {
    ConnectionManager conMgr = wta.getConnectionManager();
    UpgradeStatementDAO upgdao = UpgradeStatementDAO.getInstance();
    boolean requireAdmin = false;
    Connection con = null;//from w  w w . j  a v  a 2  s . c  o m

    try {
        if (!scripts.isEmpty()) {
            String serviceId = desc.getManifest().getId();

            // Transforms each statement into a specific object
            List<OUpgradeStatement> stmts = new ArrayList<>();
            short sequence = 0;
            for (SqlUpgradeScript script : scripts) {
                // Extracts and inserts statements
                ArrayList<BaseScriptLine> scriptStatements = script.getStatements();
                String targetDataSource = null;
                logger.trace("Script {}: found {} statement/s", script.getFileName(), scriptStatements.size());
                for (BaseScriptLine statement : scriptStatements) {
                    sequence++;
                    String stmtType, stmtDs;
                    if (statement instanceof AnnotationLine) {
                        stmtDs = null;
                        stmtType = OUpgradeStatement.STATEMENT_TYPE_ANNOTATION;
                        if (statement instanceof DataSourceAnnotationLine) {
                            targetDataSource = ((DataSourceAnnotationLine) statement).getTargetDataSource();
                        }
                    } else if (statement instanceof SqlLine) {
                        stmtDs = targetDataSource;
                        stmtType = OUpgradeStatement.STATEMENT_TYPE_SQL;
                    } else {
                        stmtDs = null;
                        stmtType = OUpgradeStatement.STATEMENT_TYPE_COMMENT;
                    }
                    stmts.add(new OUpgradeStatement(upgradeTag, serviceId, sequence, script.getFileName(),
                            stmtDs, stmtType, statement.getText()));
                }
            }

            // Inserts extracted statements into a dedicated table
            con = conMgr.getConnection();
            Integer maxId = upgdao.maxId(con);
            int count = upgdao.batchInsert(con, stmts);
            if (count != sequence)
                throw new WTException("Statements insertion not fully completed [total: {0}, inserted: {1}]",
                        sequence, count);

            // Reads all inserted statements (NB: id needs to be refreshed because it comes from a db sequence)
            stmts = upgdao.selectFromIdByTagService(con, maxId == null ? -1 : maxId, upgradeTag, serviceId);
            if (stmts.isEmpty()) {
                logger.debug("DB upgrade is not necessary [{}]", serviceId);
            } else {
                if (!autoUpdate) { // Manual
                    requireAdmin = true;

                } else { // Auto: Runs statements...
                    HashMap<String, Connection> conCache = new HashMap<>();
                    try {
                        logger.debug("Executing upgrade statements [{}, {}]", serviceId, stmts.size());
                        boolean ignoreErrors = false;
                        for (OUpgradeStatement stmt : stmts) {

                            if (stmt.getStatementType().equals(OUpgradeStatement.STATEMENT_TYPE_ANNOTATION)) {
                                if (RequireAdminAnnotationLine.matches(stmt.getStatementBody())) {
                                    logger.trace("[{}, {}]: {}", stmt.getServiceId(), stmt.getSequenceNo(),
                                            stmt.getStatementBody());
                                    requireAdmin = true;
                                    break; // Stops iteration!
                                } else if (IgnoreErrorsAnnotationLine.matches(stmt.getStatementBody())) {
                                    ignoreErrors = true; // Sets value! (for next sql statement)
                                }

                            } else if (stmt.getStatementType().equals(OUpgradeStatement.STATEMENT_TYPE_SQL)) {
                                final String sds = stmt.getStatementDataSource();
                                if (StringUtils.isBlank(sds))
                                    throw new WTException("Statement does not provide a DataSource [{0}]",
                                            stmt.getUpgradeStatementId());
                                if (!conCache.containsKey(sds)) {
                                    conCache.put(sds, getUpgradeStatementConnection(conMgr, stmt));
                                }
                                boolean ret = executeUpgradeStatement(conCache.get(sds), stmt, ignoreErrors);
                                try {
                                    upgdao.update(con, stmt);
                                } catch (DAOException ex) {
                                    throw new WTException("Unable to update statement status!", ex);
                                }
                                if (!ret) { // In case of errors...
                                    requireAdmin = true;
                                    break; // Stops iteration!
                                }
                                ignoreErrors = false; // Resets value!

                            } else {
                                logger.trace("[{}, {}]: !!! {}", stmt.getServiceId(), stmt.getSequenceNo(),
                                        StringUtils.replace(stmt.getStatementBody(), "\n", " "));
                            }
                        }
                    } finally {
                        if (!conCache.isEmpty()) {
                            logger.trace("Closing connections [{}]", conCache.size());
                            Iterator<Entry<String, Connection>> it = conCache.entrySet().iterator();
                            while (it.hasNext()) {
                                final Entry<String, Connection> entry = it.next();
                                DbUtils.closeQuietly(entry.getValue());
                                it.remove();
                            }
                        }
                    }
                }
            }
        }
    } catch (Throwable t) {
        requireAdmin = true;
        logger.error("Error handling upgrade script", t);
    } finally {
        DbUtils.closeQuietly(con);
        return requireAdmin;
    }
}