/plugins/net.bioclipse.qsar.test/src/net/bioclipse/qsar/test/TestRead.java

https://github.com/miquelrojascherto/bioclipse.qsar · Java · 168 lines · 100 code · 37 blank · 31 comment · 13 complexity · d8b8454e1ce1c01c166c67d2c86fa254 MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2009 Ola Spjuth.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Ola Spjuth - initial API and implementation
  10. ******************************************************************************/
  11. package net.bioclipse.qsar.test;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.Collections;
  15. import net.bioclipse.qsar.DescriptorType;
  16. import net.bioclipse.qsar.DescriptorlistType;
  17. import net.bioclipse.qsar.DocumentRoot;
  18. import net.bioclipse.qsar.QsarPackage;
  19. import net.bioclipse.qsar.QsarType;
  20. import net.bioclipse.qsar.ResourceType;
  21. import net.bioclipse.qsar.StructurelistType;
  22. import net.bioclipse.qsar.util.QsarResourceFactoryImpl;
  23. import net.sf.bibtexml.BibTeXMLEntriesClass;
  24. import net.sf.bibtexml.BibtexmlPackage;
  25. import net.sf.bibtexml.util.BibtexmlResourceFactoryImpl;
  26. import org.eclipse.emf.common.util.Diagnostic;
  27. import org.eclipse.emf.common.util.URI;
  28. import org.eclipse.emf.ecore.EObject;
  29. import org.eclipse.emf.ecore.resource.Resource;
  30. import org.eclipse.emf.ecore.resource.ResourceSet;
  31. import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
  32. import org.eclipse.emf.ecore.util.Diagnostician;
  33. import org.eclipse.emf.ecore.util.EcoreUtil;
  34. import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
  35. public class TestRead {
  36. public static void main(String[] args) {
  37. System.out.println("start");
  38. // Register the package -- only needed for stand-alone!
  39. QsarPackage qsarPackage=QsarPackage.eINSTANCE;
  40. // Register the package -- only needed for stand-alone!
  41. BibtexmlPackage bibPackage=BibtexmlPackage.eINSTANCE;
  42. // Create a resource set to hold the resources.
  43. //
  44. ResourceSet resourceSet = new ResourceSetImpl();
  45. // Register the appropriate resource factory to handle all file extensions.
  46. //
  47. resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
  48. (Resource.Factory.Registry.DEFAULT_EXTENSION,
  49. new QsarResourceFactoryImpl());
  50. // Register the package to ensure it is available during loading.
  51. //
  52. resourceSet.getPackageRegistry().put
  53. (QsarPackage.eNS_URI,
  54. QsarPackage.eINSTANCE);
  55. // Register the package to ensure it is available during loading.
  56. //
  57. resourceSet.getPackageRegistry().put
  58. (BibtexmlPackage.eNS_URI,
  59. BibtexmlPackage.eINSTANCE);
  60. EcoreUtil.resolveAll( resourceSet );
  61. // Get the URI of the model file.
  62. URI fileURI = URI.createFileURI(new File("myQSAR2.xml").getAbsolutePath());
  63. // Demand load the resource for this file.
  64. Resource resource=null;
  65. try{
  66. resource = resourceSet.getResource(fileURI, true);
  67. }catch (Exception e){
  68. e.printStackTrace();
  69. }
  70. DocumentRoot root=(DocumentRoot) resource.getContents().get(0);
  71. QsarType qsar=root.getQsar();
  72. System.out.println(fileURI);
  73. System.out.println("read all success");
  74. // Print the contents of the resource to System.out.
  75. try
  76. {
  77. resource.save(System.out, Collections.EMPTY_MAP);
  78. }
  79. catch (IOException e) {}
  80. //Molecules
  81. //=========
  82. if (qsar.getStructurelist()==null){
  83. System.out.println("No moleculesList.");
  84. return;
  85. }
  86. StructurelistType structlist = qsar.getStructurelist();
  87. if (structlist.getResources()==null || structlist.getResources().size()<=0){
  88. System.out.println("No structures in StructureList.");
  89. return;
  90. }
  91. for (ResourceType mol : structlist.getResources()){
  92. System.out.println(" ++ Mol: " + mol.getId() + " [" + mol.getFile()+"]");
  93. }
  94. //Descriptors
  95. //===========
  96. if (qsar.getDescriptorlist()==null){
  97. System.out.println("No descriptorList.");
  98. return;
  99. }
  100. DescriptorlistType desclist = qsar.getDescriptorlist();
  101. if (desclist.getDescriptors()==null || desclist.getDescriptors().size()<=0){
  102. System.out.println("No descriptors in descriptorList.");
  103. return;
  104. }
  105. for (DescriptorType desc : desclist.getDescriptors()){
  106. String impl = desc.getProvider();
  107. System.out.println(" ** desc: " + desc.getId() + " [" + impl + "]");
  108. }
  109. //Metadata
  110. //===========
  111. System.out.println(" ## Metadata. DS name: " + qsar.getMetadata().getDatasetname() + " - Authors: " + qsar.getMetadata().getAuthors());
  112. for (BibTeXMLEntriesClass ref : qsar.getMetadata().getReference()){
  113. System.out.println(" == ref: " + ref.getArticle().getAuthor() + " - " + ref.getArticle().getTitle());
  114. }
  115. // Validate the contents of the loaded resource.
  116. //
  117. for (EObject eObject : resource.getContents()) {
  118. Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
  119. if (diagnostic.getSeverity() != Diagnostic.OK) {
  120. printDiagnostic(diagnostic, "");
  121. }
  122. }
  123. }
  124. protected static void printDiagnostic(Diagnostic diagnostic, String indent) {
  125. System.out.print(indent);
  126. System.out.println(diagnostic.getMessage());
  127. for (Diagnostic child : diagnostic.getChildren()) {
  128. printDiagnostic(child, indent + " ");
  129. }
  130. }
  131. }