Back to index

wims  3.65+svn20090927
Public Member Functions | Static Public Member Functions | Package Attributes
WIMSchem.Templates Class Reference

List of all members.

Public Member Functions

 Templates (Class cls)
int NumTemplates ()
Molecule GetTemplate (int N)
void AddTemplate (Molecule Mol)

Static Public Member Functions

static byte[] loadURL (URL url) throws IOException
static String loadFile (String fname) throws IOException
static String load (String fileOrURL) throws IOException

Package Attributes

ArrayList< Moleculetempl = new ArrayList<Molecule>()

Detailed Description

Definition at line 19 of file Templates.java.


Constructor & Destructor Documentation

WIMSchem.Templates.Templates ( Class  cls) [inline]

Definition at line 51 of file Templates.java.

                               {

       ArrayList<String> list=new ArrayList<String>();

       // jm.evers : if an param template is defined, load them in the ArrayList.
       boolean inapplet=false;
       if(MainApplet.templateURL != null){
           for(int p=0; p<MainApplet.templateURL.length ;p++ ){
              try {
                  Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader((load(MainApplet.templateURL[p])))));
                  templ.add(mol);
                  inapplet=true;
                  System.out.println("loading template"+p);
              } catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct MDLMol or Native files??");}
           }
           // if this was not successfull: maybe the templates are javascript strings?
           if(!inapplet){
              for(int p=0; p<MainApplet.templateURL.length ;p++ ){
                  try {
                  System.out.println("MainApplet.templateURL[p]="+MainApplet.templateURL[p]);
                     Molecule mol=MoleculeStream.ReadUnknown(new BufferedReader(new StringReader(MainApplet.templateURL[p].toString())));
                     templ.add(mol);
                     inapplet=true;
                     System.out.println("loading template"+p);
                  }   catch (IOException e) {System.out.println("FAILED loading template"+p+"\n Are you using correct javascript strings??");}
              }
           }
       }
       
       if(!inapplet){
       // read the list of molecules from the directory file, then create each one of them
           try {
              InputStream istr=cls.getResourceAsStream("/templ/list");
              BufferedReader in=new BufferedReader(new InputStreamReader(istr));
              String line;
              while ((line=in.readLine())!=null) {list.add(line);}
              istr.close();
           }
           catch (IOException e) {
              System.out.println("Failed to obtain list of templates:\n"+e.toString());
              return;
           }
           
           try {
              for (int n=0;n<list.size();n++){
                  InputStream istr=cls.getResourceAsStream("/templ/"+list.get(n));
                  Molecule mol=MoleculeStream.ReadNative(istr);
                  templ.add(mol);
                  istr.close();
              }
           }
           catch (IOException e){
              System.out.println("Failed to obtain particular template:\n"+e.toString());
              return;
           }
       }
       // sort the molecules by an index of "complexity" (smaller molecules first, carbon-only favoured)
       
       int[] complex=new int[templ.size()];
       for (int n=0;n<templ.size();n++)
       {
           Molecule mol=templ.get(n);
           complex[n]=mol.NumAtoms()*100;
           boolean nonCH=false;
           for (int i=1;i<=mol.NumAtoms();i++) 
              if (mol.AtomElement(i).compareTo("C")!=0 && mol.AtomElement(i).compareTo("H")!=0) nonCH=true;
           if (!nonCH) complex[n]-=1000;
           for (int i=1;i<=mol.NumBonds();i++) complex[n]=complex[n]+mol.BondOrder(i);
       }
       
       int p=0;
       while (p<templ.size()-1)
       {
           if (complex[p]>complex[p+1])
           {
              int i=complex[p]; complex[p]=complex[p+1]; complex[p+1]=i;
              Molecule mol=templ.get(p); templ.set(p,templ.get(p+1)); templ.set(p+1,mol);
              if (p>0) p--;
           }
           else p++;
       }
    }

Here is the call graph for this function:


Member Function Documentation

void WIMSchem.Templates.AddTemplate ( Molecule  Mol) [inline]

Definition at line 135 of file Templates.java.

{templ.add(Mol);}

Here is the caller graph for this function:

Definition at line 134 of file Templates.java.

{return templ.get(N);}

Here is the caller graph for this function:

static String WIMSchem.Templates.load ( String  fileOrURL) throws IOException [inline, static]

Definition at line 43 of file Templates.java.

                                                                   {
       try {
           URL url = new URL(fileOrURL);
           return new String(loadURL(url));
       } catch (Exception e) { return loadFile(fileOrURL);}
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static String WIMSchem.Templates.loadFile ( String  fname) throws IOException [inline, static]

Definition at line 38 of file Templates.java.

                                                                   {
        byte[] bytes = loadURL(new URL("file:" + fname));
       return new String(bytes);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static byte [] WIMSchem.Templates.loadURL ( URL  url) throws IOException [inline, static]

Definition at line 24 of file Templates.java.

                                                              {
        int bufSize = 1024 * 2;
       byte [] buf = new byte[bufSize];
       ByteArrayOutputStream bout = new ByteArrayOutputStream();
       BufferedInputStream   in   = new BufferedInputStream(url.openStream());
       int n;
       while ((n = in.read(buf)) > 0){
           bout.write(buf, 0, n);
       }
       try 
       { in.close(); } catch (Exception ignored) { }
       return bout.toByteArray();
    }

Here is the caller graph for this function:

Definition at line 133 of file Templates.java.

{return templ.size();}

Here is the caller graph for this function:


Member Data Documentation

ArrayList<Molecule> WIMSchem.Templates.templ = new ArrayList<Molecule>() [package]

Definition at line 21 of file Templates.java.


The documentation for this class was generated from the following file: