Skip to content

Commit

Permalink
make use of services, enhance PackageHealthChecker CompEvol#834
Browse files Browse the repository at this point in the history
  • Loading branch information
rbouckaert committed Apr 7, 2022
1 parent 264a98b commit 78081fa
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 21 deletions.
15 changes: 8 additions & 7 deletions beast.app/src/beast/app/inputeditor/BEASTObjectInputEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import beast.base.core.BEASTInterface;
import beast.base.core.Input;
import beast.pkgmgmt.PackageManager;

public class BEASTObjectInputEditor extends InputEditor.Base {
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -72,7 +71,7 @@ void simpleInit(Input<?> input, BEASTInterface beastObject) {
addComboBox(this, input, beastObject);

if (m_bAddButtons) {
if (BEASTObjectPanel.countInputs((BEASTInterface) m_input.get(), doc) > 0) {
if (BEASTObjectPanel.countInputs(m_input.get(), doc) > 0) {
m_editBEASTObjectButton = new SmallButton("e", true);
if (input.get() == null) {
m_editBEASTObjectButton.setEnabled(false);
Expand Down Expand Up @@ -193,8 +192,10 @@ protected void addComboBox(JComponent box, Input<?> input, BEASTInterface beastO
String id2;
if (o == null) {
id2 = beastObject0.getID();
} else {
} else if (o instanceof BEASTInterface) {
id2 = ((BEASTInterface) o).getID();
} else {
id2 = input.getName();
}
if (id2.indexOf('.')>=0) {
id2 = id2.substring(0, id2.indexOf('.'));
Expand Down Expand Up @@ -473,9 +474,9 @@ protected void addComboBox(JComponent box, Input<?> input, BEASTInterface beastO
// }
// }

String[] getAvailablePlugins() {
List<String> beastObjectNames = PackageManager.find(m_input.getType(), "beast");
return beastObjectNames.toArray(new String[0]);
} // getAvailablePlugins
// String[] getAvailablePlugins() {
// List<String> beastObjectNames = PackageManager.find(m_input.getType(), "beast");
// return beastObjectNames.toArray(new String[0]);
// } // getAvailablePlugins

} // class PluginInputEditor
13 changes: 8 additions & 5 deletions beast.app/src/beast/app/inputeditor/BEASTObjectPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,16 @@ public boolean getOK() {
/**
* add all inputs of a beastObject to a box *
*/
public static int countInputs(BEASTInterface beastObject, BeautiDoc doc) {
public static int countInputs(Object beastObject, BeautiDoc doc) {
if (beastObject == null) {
return 0;
}
if (!(beastObject instanceof BEASTInterface)) {
return 1;
}
int inputCount = 0;
try {
if (beastObject == null) {
return 0;
}
List<Input<?>> inputs = beastObject.listInputs();
List<Input<?>> inputs = ((BEASTInterface)beastObject).listInputs();
for (Input<?> input : inputs) {
String fullInputName = beastObject.getClass().getName() + "." + input.getName();
if (!doc.beautiConfig.suppressBEASTObjects.contains(fullInputName)) {
Expand Down
12 changes: 5 additions & 7 deletions beast.app/src/beast/app/inputeditor/InputEditorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.Box;
Expand All @@ -20,7 +21,6 @@
import beast.base.core.Input;
import beast.base.core.Log;
import beast.base.core.Input.Validate;
import beast.base.inference.ModelLogger;
import beast.pkgmgmt.BEASTClassLoader;
import beast.pkgmgmt.PackageManager;

Expand Down Expand Up @@ -56,11 +56,8 @@ public void init() {
}

if (beast.pkgmgmt.Utils6.isJUnitTest() || inputEditorMap.size() == 0) {
String[] PACKAGE_DIRS = {"beast.app","beast.base"};
for (String packageName : PACKAGE_DIRS) {
List<String> inputEditors = PackageManager.find("beast.app.inputeditor.InputEditor", packageName);
registerInputEditors(inputEditors.toArray(new String[0]));
}
Set<String> inputEditors = PackageManager.listServices("beast.app.inputeditor.InputEditor");
registerInputEditors(inputEditors.toArray(new String[0]));
}
}

Expand Down Expand Up @@ -260,6 +257,7 @@ public InputEditor createInputEditor(Input<?> input, int listItemNr, BEASTInterf

}
}
// System.err.println("createInputEditor: " + inputEditor.getClass().getName());
inputEditor.setDoc(doc);
inputEditor.init(input, beastObject, listItemNr, expandOption, addButtons);
((JComponent) inputEditor).setBorder(BorderFactory.createEmptyBorder());
Expand Down Expand Up @@ -322,7 +320,7 @@ public List<String> getAvailablePlugins(Input<?> input, BEASTInterface parent, L
}
/* add all beastObject-classes of type assignable to the input */
if (doc.isExpertMode()) {
List<String> classes = PackageManager.find(input.getType(), "beast");
Set<String> classes = PackageManager.listServices(BEASTInterface.class.getName());
for (String className : classes) {
try {
Object o = BEASTClassLoader.forName(className).newInstance();
Expand Down
47 changes: 46 additions & 1 deletion beast.app/src/beast/app/tools/PackageHealthChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
Expand All @@ -27,7 +29,9 @@
import beast.base.core.Description;
import beast.base.core.Input;
import beast.base.core.Log;
import beast.base.inference.Logger;
import beast.base.inference.Runnable;
import beast.base.parser.XMLParser;
import beast.base.util.FileUtils;
import beast.pkgmgmt.BEASTClassLoader;
import beast.pkgmgmt.BEASTVersion;
Expand Down Expand Up @@ -81,6 +85,8 @@ public void run() throws Exception {
checkServices(versionFileName);
checkFolders();
checkSourceCode();
checkXMLExample();
// checkBEAUTITemplates();

// clean up package directory
deleteRecursively(new File(packageDir));
Expand All @@ -91,6 +97,45 @@ public void run() throws Exception {
System.exit(0);
}

private void checkXMLExample() throws IOException {
PackageManager.loadExternalJars();
Logger.FILE_MODE = Logger.LogFileMode.overwrite;

String separator = Utils.isWindows() ? "\\\\" : File.separator;
List<String> failedFiles = new ArrayList<>();
for (String fileName : new File(packageDir + separator + "examples").list()) {
Log.warning("Processing " + fileName);
XMLParser parser = new XMLParser();
try {
parser.parseFile(new File(packageDir + separator + "examples" + separator + fileName));
} catch (Exception e) {
e.printStackTrace()
;
out.println("Example Xml parsing failed for " + fileName
+ ": " + e.getMessage());
failedFiles.add(fileName);
}
}
}

private void checkBEAUTITemplates() {
String separator = Utils.isWindows() ? "\\\\" : File.separator;
List<String> failedFiles = new ArrayList<>();
for (String fileName : new File(packageDir + separator + "templates").list()) {
Log.warning("Processing " + fileName);
XMLParser parser = new XMLParser();
try {
String xml = FileUtils.load(new File(packageDir + separator + "templates" + separator + fileName));
parser.parseBareFragment(xml, false);
} catch (Exception e) {
e.printStackTrace()
;
out.println("BEAUti template parsing failed for " + fileName
+ ": " + e.getMessage());
failedFiles.add(fileName);
}
} }

private void checkServices(String versionFileName) {
Map<String, Set<String>> declaredSerices = collectDecladedServices(versionFileName);
for (String file : new File(packageDir + "/lib").list()) {
Expand Down Expand Up @@ -330,7 +375,7 @@ private String determinPackageName(String versionFileName) {
Node node = content.item(i);
if (node instanceof Element) {
String name = ((Element) node).getNodeName();
if (!(name.equals("packageapp") || name.equals("map") || name.equals("addonapp") || name.equals("depends") || name.equals("servicex"))) {
if (!(name.equals("packageapp") || name.equals("map") || name.equals("addonapp") || name.equals("depends") || name.equals("service"))) {
report("Unrecognised element found in version.xml, which will be ignored:" + name + " (potentially a typo)");
}
}
Expand Down
3 changes: 3 additions & 0 deletions beast.base/src/beast/base/parser/XMLParserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ public static int getLevenshteinDistance(final String s, final String t) {
}

public static String resolveClass(String specClass, String [] nameSpaces) {
if (specClass.equals("beast.base.core.BEASTInterface")) {
return "beast.base.core.BEASTInterface";
}
for (String nameSpace : nameSpaces) {
if (XMLParserUtils.getBeastObjectNames().contains(nameSpace + specClass)) {
String clazzName = nameSpace + specClass;
Expand Down
2 changes: 1 addition & 1 deletion beast.base/src/beast/base/parser/XMLProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class XMLProducer extends XMLParser {
*/
int indent;

final public static String DEFAULT_NAMESPACE = "beast.core:beast.evolution.alignment:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihood";
final public static String DEFAULT_NAMESPACE = "beast.core:beast.evolution.alignment:beast.evolution.tree.coalescent:beast.core.util:beast.evolution.nuc:beast.evolution.operators:beast.evolution.sitemodel:beast.evolution.substitutionmodel:beast.evolution.likelihoodbeast.base.evolution.alignment:beast.pkgmgmt:beast.base.core:beast.base.inference:beast.base.evolution.tree.coalescent:beast.pkgmgmt:beast.base.core:beast.base.inference.util:beast.evolution.nuc:beast.base.evolution.operator:beast.base.inference.operator:beast.base.evolution.sitemodel:beast.base.evolution.substitutionmodel:beast.base.evolution.likelihood";
//final public static String DO_NOT_EDIT_WARNING = "DO NOT EDIT the following machine generated text, they are used in Beauti";

public XMLProducer() {
Expand Down

0 comments on commit 78081fa

Please sign in to comment.