Skip to content

Commit

Permalink
create Extension SPI, merge ValueFormatterLoader into LPhyCoreLoader #…
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed Dec 6, 2023
1 parent 44c1206 commit 59cd4e0
Show file tree
Hide file tree
Showing 20 changed files with 546 additions and 270 deletions.
141 changes: 79 additions & 62 deletions lphy-base/src/main/java/lphy/base/spi/LPhyBaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import lphy.base.function.tree.PruneTree;
import lphy.core.model.BasicFunction;
import lphy.core.model.GenerativeDistribution;
import lphy.core.spi.LPhyExtension;
import lphy.core.spi.LPhyCoreImpl;

import java.util.Arrays;
import java.util.List;
Expand All @@ -46,67 +46,73 @@
* It requires a public no-args constructor.
* @author Walter Xie
*/
public class LPhyBaseImpl implements LPhyExtension {
public class LPhyBaseImpl extends LPhyCoreImpl { //implements LPhyExtension {

static {
SequenceTypeLoader sequenceTypeLoader = new SequenceTypeLoader();
sequenceTypeLoader.loadAllExtensions();
}

List<Class<? extends GenerativeDistribution>> genDists = Arrays.asList(
// probability distribution
Bernoulli.class, BernoulliMulti.class, Beta.class, Categorical.class, Cauchy.class, Dirichlet.class,
DiscretizedGamma.class, Exp.class, Gamma.class, Geometric.class, InverseGamma.class, LogNormal.class,
NegativeBinomial.class, Normal.class, NormalGamma.class, Poisson.class,
Uniform.class, UniformDiscrete.class, Weibull.class, WeightedDirichlet.class,
// tree distribution
Yule.class, BirthDeathTree.class, FullBirthDeathTree.class, BirthDeathTreeDT.class,
BirthDeathSamplingTree.class, BirthDeathSamplingTreeDT.class, BirthDeathSerialSamplingTree.class,
RhoSampleTree.class, FossilBirthDeathTree.class,
SimBDReverse.class, SimFBDAge.class, SimFossilsPoisson.class,
SerialCoalescent.class, StructuredCoalescent.class, MultispeciesCoalescent.class,
// skyline
SkylineCoalescent.class, ExpMarkovChain.class, RandomComposition.class,
// alignment
Sequence.class, ErrorModel.class, MissingSites.class,
// others
RandomBooleanArray.class, Sample.class,
// phylogenetic distribution
PhyloBrownian.class, PhyloMultivariateBrownian.class, PhyloOU.class,
PhyloCTMC.class, PhyloCTMCSiteModel.class, bSiteRates.class);
@Override
public List<Class<? extends GenerativeDistribution>> declareDistributions() {
return Arrays.asList(
// probability distribution
Bernoulli.class, BernoulliMulti.class, Beta.class, Categorical.class, Cauchy.class, Dirichlet.class,
DiscretizedGamma.class, Exp.class, Gamma.class, Geometric.class, InverseGamma.class, LogNormal.class,
NegativeBinomial.class, Normal.class, NormalGamma.class, Poisson.class,
Uniform.class, UniformDiscrete.class, Weibull.class, WeightedDirichlet.class,
// tree distribution
Yule.class, BirthDeathTree.class, FullBirthDeathTree.class, BirthDeathTreeDT.class,
BirthDeathSamplingTree.class, BirthDeathSamplingTreeDT.class, BirthDeathSerialSamplingTree.class,
RhoSampleTree.class, FossilBirthDeathTree.class,
SimBDReverse.class, SimFBDAge.class, SimFossilsPoisson.class,
SerialCoalescent.class, StructuredCoalescent.class, MultispeciesCoalescent.class,
// skyline
SkylineCoalescent.class, ExpMarkovChain.class, RandomComposition.class,
// alignment
Sequence.class, ErrorModel.class, MissingSites.class,
// others
RandomBooleanArray.class, Sample.class,
// phylogenetic distribution
PhyloBrownian.class, PhyloMultivariateBrownian.class, PhyloOU.class,
PhyloCTMC.class, PhyloCTMCSiteModel.class, bSiteRates.class);
}

List<Class<? extends BasicFunction>> functions = Arrays.asList(ARange.class, ArgI.class,
// Substitution models
JukesCantor.class, K80.class, F81.class, HKY.class, GTR.class, WAG.class,
GeneralTimeReversible.class, LewisMK.class,
NucleotideModel.class,
BModelSetFunction.class,
bSiteModelFunction.class,
@Override
public List<Class<? extends BasicFunction>> declareFunctions() {
return Arrays.asList(ARange.class, ArgI.class,
// Substitution models
JukesCantor.class, K80.class, F81.class, HKY.class, GTR.class, WAG.class,
GeneralTimeReversible.class, LewisMK.class,
NucleotideModel.class,
BModelSetFunction.class,
bSiteModelFunction.class,

// Data types
BinaryDatatypeFunction.class, NucleotidesFunction.class, StandardDatatypeFunction.class,
AminoAcidsFunction.class,
// Taxa
CreateTaxa.class, ExtantTaxa.class, NCharFunction.class, NTaxaFunction.class, TaxaFunction.class,
// Alignment
SelectSitesByMissingFraction.class, RemoveTaxa.class,
VariableSites.class, InvariableSites.class, CopySites.class,
// Tree
LocalBranchRates.class, ExtantTree.class, PruneTree.class, //NodeCount.class, TreeLength.class,
// Matrix
BinaryRateMatrix.class, MigrationMatrix.class, MigrationCount.class,
// IO
Newick.class, ReadNexus.class, ReadFasta.class, ReadDelim.class, ExtractTrait.class, SpeciesTaxa.class,
// Math
SumBoolean.class, SumRows.class, SumCols.class, Sum2dArray.class, Sum.class,// Product.class,
// Set Op
Intersect.class,
// cast
ToDouble.class,
// Utils
Length.class, Unique.class, Sort.class, IfElse.class, //ConcatStr.class,
Get.class, Select.class, Split.class, ParseInt.class, Rep.class, RepArray.class, //Copy.class,
ConcatArray.class, Concat2Str.class);
// Data types
BinaryDatatypeFunction.class, NucleotidesFunction.class, StandardDatatypeFunction.class,
AminoAcidsFunction.class,
// Taxa
CreateTaxa.class, ExtantTaxa.class, NCharFunction.class, NTaxaFunction.class, TaxaFunction.class,
// Alignment
SelectSitesByMissingFraction.class, RemoveTaxa.class,
VariableSites.class, InvariableSites.class, CopySites.class,
// Tree
LocalBranchRates.class, ExtantTree.class, PruneTree.class, //NodeCount.class, TreeLength.class,
// Matrix
BinaryRateMatrix.class, MigrationMatrix.class, MigrationCount.class,
// IO
Newick.class, ReadNexus.class, ReadFasta.class, ReadDelim.class, ExtractTrait.class, SpeciesTaxa.class,
// Math
SumBoolean.class, SumRows.class, SumCols.class, Sum2dArray.class, Sum.class,// Product.class,
// Set Op
Intersect.class,
// cast
ToDouble.class,
// Utils
Length.class, Unique.class, Sort.class, IfElse.class, //ConcatStr.class,
Get.class, Select.class, Split.class, ParseInt.class, Rep.class, RepArray.class, //Copy.class,
ConcatArray.class, Concat2Str.class);
}

/**
* Required by ServiceLoader.
Expand All @@ -115,15 +121,26 @@ public LPhyBaseImpl() {
//TODO do something here, e.g. print package or classes info ?
}

@Override
public List<Class<? extends GenerativeDistribution>> getDistributions() {
return genDists;
}

@Override
public List<Class<? extends BasicFunction>> getFunctions() {
return functions;
}
// @Override
// public void register() {
//
// }
//
// @Override
// public Map<String, Set<Class<?>>> getDistributions() {
// return null;
// }
//
// @Override
// public Map<String, Set<Class<?>>> getFunctions() {
// return null;
// }
//
// @Override
// public TreeSet<Class<?>> getTypes() {
// return null;
// }

public String getExtensionName() {
return "LPhy standard library";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lphy.base.logger.NexusAlignmentFormatter;
import lphy.base.logger.NexusTreeFormatter;
import lphy.core.logger.ValueFormatter;
import lphy.core.spi.LPhyValueFormatter;
import lphy.core.spi.LPhyValueFormatterCoreImpl;

import java.util.Set;

Expand All @@ -13,23 +13,19 @@
* It requires a public no-args constructor.
* @author Walter Xie
*/
public class LPhyBaseValueFormatterImpl implements LPhyValueFormatter {
public class LPhyValueFormatterBaseImpl extends LPhyValueFormatterCoreImpl {//implements LPhyValueFormatter {

/**
* Required by ServiceLoader.
*/
public LPhyBaseValueFormatterImpl() {
}

@Override
public Set<Class<? extends ValueFormatter>> getValueFormatters() {
public Set<Class<? extends ValueFormatter>> declareValueFormatters() {
return Set.of(NexusAlignmentFormatter.class, NexusTreeFormatter.class);
}
// public Map<Class<?>, Set<Class<? extends ValueFormatter>>> getValueFormatterMap() {
// return Map.of( SimpleAlignment.class, Set.of(NexusAlignmentFormatter.class),
// TimeTree.class, Set.of(NexusTreeFormatter.class) );
// }

/**
* Required by ServiceLoader.
*/
public LPhyValueFormatterBaseImpl() {
}

public String getExtensionName() {
return "LPhy base loggers";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public SequenceTypeBaseImpl() {
}

@Override
public Map<String, ? extends SequenceType> getSequenceTypes() {
public Map<String, ? extends SequenceType> declareSequenceTypes() {
Map<String, SequenceType> dataTypeMap = new ConcurrentHashMap<>();
dataTypeMap.put("rna", SequenceType.NUCLEOTIDE);
dataTypeMap.put("dna", SequenceType.NUCLEOTIDE);
Expand All @@ -37,6 +37,11 @@ public SequenceTypeBaseImpl() {
return dataTypeMap;
}

@Override
public void register() {

}

public String getExtensionName() {
return "LPhy base";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public interface SequenceTypeExtension extends Extension {
* The string key is a keyword to represent this SequenceType.
* The keyword can be used to identify and initialise the corresponding sequence type.
*/
Map<String, ? extends SequenceType> getSequenceTypes();
Map<String, ? extends SequenceType> declareSequenceTypes();

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void registerExtensions(String extClsName) {
System.out.println("Registering extension from " + sequenceTypeExtension.getClass().getName());

// sequence types
Map<String, ? extends SequenceType> newDataTypes = sequenceTypeExtension.getSequenceTypes();
Map<String, ? extends SequenceType> newDataTypes = sequenceTypeExtension.declareSequenceTypes();
if (newDataTypes != null)
// TODO validate same sequence type?
newDataTypes.forEach(dataTypeMap::putIfAbsent);
Expand Down
9 changes: 3 additions & 6 deletions lphy-base/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import lphy.base.spi.LPhyBaseImpl;
import lphy.base.spi.LPhyBaseValueFormatterImpl;
import lphy.base.spi.LPhyValueFormatterBaseImpl;
import lphy.base.spi.SequenceTypeBaseImpl;

/**
Expand Down Expand Up @@ -52,12 +52,9 @@
exports lphy.base.logger;

// LPhy extensions
uses lphy.core.spi.LPhyExtension;
uses lphy.core.spi.Extension;
// declare what service interface the provider intends to use
provides lphy.core.spi.LPhyExtension with LPhyBaseImpl;

uses lphy.core.spi.LPhyValueFormatter;
provides lphy.core.spi.LPhyValueFormatter with LPhyBaseValueFormatterImpl;
provides lphy.core.spi.Extension with LPhyBaseImpl, LPhyValueFormatterBaseImpl;

uses lphy.base.spi.SequenceTypeExtension;
// declare what service interface the provider intends to use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void main(String[] args) throws IOException {
// class name with package that implements {@link LPhyExtension}
String clsName = args[2];
// require init ParserLoader
LoaderManager.getLphyCoreLoader().loadExtension(clsName);
LoaderManager.getLphyCoreLoader().loadExtensions(); //TODO broken
}
System.out.println("Creating doc for " + extName + " ...\n");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lphystudio.app.manager;

import lphy.core.spi.LPhyCoreLoader;
import lphy.core.spi.LPhyExtension;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -24,15 +23,15 @@ public class ExtManager {
// pin lphy on the top of the list
public final static String LPHY_ID = "lphy";

final List<LPhyExtension> extensions;
final List<lphy.core.spi.Extension> extensions;
Set<String> jarDirSet = new HashSet<>();

public ExtManager() {
//TODO other loaders ?
LPhyCoreLoader lphyCoreLoader = new LPhyCoreLoader();
lphyCoreLoader.loadAllExtensions();
lphyCoreLoader.loadExtensions();

extensions = lphyCoreLoader.getExtensions();
extensions = lphyCoreLoader.getExtensionMap().values().stream().toList();
System.out.println(extensions);
}

Expand All @@ -44,7 +43,7 @@ public List<Extension> getLoadedLPhyExts() {
List<Extension> extList = new ArrayList<>();

// find all loaded lphy exts
for (LPhyExtension ext : extensions) {
for (lphy.core.spi.Extension ext : extensions) {
Class<?> cls = ext.getClass();
// String pkgNm = "/" + cls.getPackageName().replace(".", "/");
// include jar name
Expand Down
10 changes: 6 additions & 4 deletions lphy-studio/src/main/java/lphystudio/spi/LPhyStudioImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lphy.core.model.BasicFunction;
import lphy.core.model.GenerativeDistribution;
import lphy.core.spi.LPhyExtension;
import lphy.core.spi.LPhyCoreImpl;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -11,7 +11,7 @@
* Empty class to show studio ext in the Extension Manager.
* @author Walter Xie
*/
public class LPhyStudioImpl implements LPhyExtension {
public class LPhyStudioImpl extends LPhyCoreImpl { //} implements LPhyExtension {

/**
* Required by ServiceLoader.
Expand All @@ -20,15 +20,17 @@ public LPhyStudioImpl() {
}

@Override
public List<Class<? extends GenerativeDistribution>> getDistributions() {
public List<Class<? extends GenerativeDistribution>> declareDistributions() {
return new ArrayList<>();
}

@Override
public List<Class<? extends BasicFunction>> getFunctions() {
public List<Class<? extends BasicFunction>> declareFunctions() {
return new ArrayList<>();
}



public String getExtensionName() {
return "LPhy studio";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lphy.core.spi.LPhyValueFormatter;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -12,22 +13,32 @@
* It requires a public no-args constructor.
* @author Walter Xie
*/
public class LPhyStudioValueFormatterImpl implements LPhyValueFormatter {
public class LPhyValueFormatterStudioImpl implements LPhyValueFormatter {
// List<Class<? extends ValueFormatter>> valueFormatters = Arrays.asList(
// AlignmentLog.class, TreeLog.class, VariableLog.class, VariableSummaryLog.class);
/**
* Required by ServiceLoader.
*/
public LPhyStudioValueFormatterImpl() {
public LPhyValueFormatterStudioImpl() {
}
@Override
public Set<Class<? extends ValueFormatter>> getValueFormatters() {
public Set<Class<? extends ValueFormatter>> declareValueFormatters() {
return new HashSet<>();
}

@Override
public Map<Class<?>, Set<Class<? extends ValueFormatter>>> getValueFormatters() {
return null;
}
// public Map<Class<?>, Set<Class<? extends ValueFormatter>>> getValueFormatterMap() {
// return new HashMap<>();
// }

@Override
public void register() {

}

public String getExtensionName() {
return "LPhy studio loggers";
}
Expand Down
Loading

0 comments on commit 59cd4e0

Please sign in to comment.