Skip to content

Commit

Permalink
config map to list, registered functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alx652 committed Mar 14, 2024
1 parent 4a86126 commit 72ae6f9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

public class RegisteredFunctionProperties {

private List<Map<String, Object>> registeredfunctions;
private Map<String, Map<String, Object>> registeredfunctions;


public List<Map<String, Object>> getRegisteredfunctions() {
public Map<String, Map<String, Object>> getRegisteredfunctions() {
return registeredfunctions;
}

public void setRegisteredfunctions(List<Map<String, Object>> registeredfunctions) {
public void setRegisteredfunctions(Map<String, Map<String, Object>> registeredfunctions) {
this.registeredfunctions = registeredfunctions;
}

Expand Down
14 changes: 14 additions & 0 deletions gsrs-core/src/main/java/gsrs/util/RegisteredFunctionConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gsrs.util;

import lombok.Data;

import java.util.Map;

@Data
public class RegisteredFunctionConfig {
private Class registeredFunctionClass;
private String key;
private Double order;
private boolean disabled = false;
private Map<String, Object> parameters;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package ix.core.util.pojopointer;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import gov.nih.ncats.common.util.CachedSupplier;
import gsrs.RegisteredFunctionProperties;
import gsrs.springUtils.AutowireHelper;

import gsrs.util.RegisteredFunctionConfig;
import ix.core.util.pojopointer.extensions.RegisteredFunction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsFirst;

@Component
public class LambdaParseRegistry implements ApplicationListener<ContextRefreshedEvent> {
Expand Down Expand Up @@ -69,12 +75,14 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
map.put("skip", LongBasedLambdaArgumentParser.of("skip", (p) -> new SkipPath(p)));

if(registeredFunctionProperties !=null) {
for (Map<String, Object> m : registeredFunctionProperties.getRegisteredfunctions()) {
try {
String className = (String) m.get("class");
Class<?> c = ClassUtils.forName(className, null);
RegisteredFunction rf = AutowireHelper.getInstance().autowireAndProxy( (RegisteredFunction) c.getDeclaredConstructor().newInstance());

List<? extends RegisteredFunctionConfig> configs = loadRegisteredFunctionsFromConfiguration();

for (RegisteredFunctionConfig config : configs) {
try {
RegisteredFunction rf = AutowireHelper.getInstance().autowireAndProxy(
(RegisteredFunction) config.getRegisteredFunctionClass().getDeclaredConstructor().newInstance()
);
LambdaArgumentParser p = rf.getFunctionURIParser();
System.out.println("Found special Function:" + p.getKey());
map.put(p.getKey(), p);
Expand All @@ -85,7 +93,6 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
}
}


// try {
// functionFactory
// .getRegisteredFunctions()
Expand All @@ -104,6 +111,33 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
instance = this;
}

private List<? extends RegisteredFunctionConfig> loadRegisteredFunctionsFromConfiguration() {
String reportTag = "RegisteredFunctionConfig";
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, Map<String, Object>> map = registeredFunctionProperties.getRegisteredfunctions();
if (map == null || map.isEmpty()) {
return Collections.emptyList();
}
for (String k: map.keySet()) {
map.get(k).put("key", k);
}
List<Object> list = map.values().stream().collect(Collectors.toList());
List<? extends RegisteredFunctionConfig> configs = mapper.convertValue(list, new TypeReference<List<? extends RegisteredFunctionConfig>>() { });
System.out.println( reportTag + "found before filtering: " + configs.size());
configs = configs.stream().filter(c->!c.isDisabled()).sorted(Comparator.comparing(c->c.getOrder(),nullsFirst(naturalOrder()))).collect(Collectors.toList());
System.out.println(reportTag + " active after filtering: " + configs.size());
System.out.println(String.format("%s|%s|%s|%s", reportTag, "class", "key", "order", "isDisabled"));
for (RegisteredFunctionConfig config : configs) {
System.out.println(String.format("%s|%s|%s|%s", reportTag, config.getRegisteredFunctionClass(), config.getKey(), config.getOrder(), config.isDisabled()));
}
return configs;
} catch (Throwable t) {
throw t;
}
}


public Optional<Function<String,? extends PojoPointer>> getPojoPointerParser(final String key) throws NoSuchElementException {
Function<String,? extends PojoPointer> parser= subURIparsers.get().get(key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ public class GsrsExportConfiguration {


CachedSupplier initializer = CachedSupplier.ofInitializer( ()->{

String reportTag = "ExporterFactoryConfig";
log.trace("inside initializer");

// The configuration (Hocon maps) need to be read first so that overrides occur in the right order
// Then, we put them in these MapLists
Map<String, List<Class>> factoriesMapList = new HashMap<String, List<Class>>();
Map<String, List<ExporterFactoryConfig>> exporterFactoriesMapList = new HashMap<String, List<ExporterFactoryConfig>>();

Expand All @@ -78,13 +76,13 @@ public class GsrsExportConfiguration {
Map<String, ExporterFactoryConfig> map = entry1.getValue();
String mapKey = entry1.getKey();
List<ExporterFactoryConfig> configs = map.values().stream().collect(Collectors.toList());
System.out.println("Exporter factory configurations for [" + mapKey + "] found before filtering: " + configs.size());
System.out.println(reportTag + " for [" + mapKey + "] found before filtering: " + configs.size());
configs = configs.stream().filter(p -> !p.isDisabled()).sorted(Comparator.comparing(i -> i.getOrder(), nullsFirst(naturalOrder()))).collect(Collectors.toList());
System.out.println("Exporter factory configurations for [" + mapKey + "] found after filtering: " + configs.size());
System.out.println(reportTag + " for [" + mapKey + "] found after filtering: " + configs.size());
exporterFactoriesMapList.put(mapKey, configs);
System.out.println(String.format("%s|%s|%s|%s|%s", "ScheduledTaskConfig", "context", "class", "key", "order", "isDisabled"));
System.out.println(String.format("%s|%s|%s|%s", reportTag, "context", "class", "key", "order", "isDisabled"));
for (ExporterFactoryConfig config : configs) {
System.out.println(String.format("%s|%s|%s|%s|%s", "context", "ExporterFactoryConfig", config.getExporterFactoryClass(), config.getKey(), config.getOrder(), config.isDisabled()));
System.out.println(String.format("%s|%s|%s|%s", "reportTag", mapKey, config.getExporterFactoryClass(), config.getKey(), config.getOrder(), config.isDisabled()));
}
}
}
Expand Down
33 changes: 16 additions & 17 deletions gsrs-spring-boot-autoconfigure/src/main/resources/gsrs-core.conf
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,26 @@ gsrs.cv.jsonFile="/extended-cv.json"
#spring.h2.console.enabled=true
#spring.h2.console.path=/h2-console


ix.api.registeredfunctions=[
ix.api.registeredfunctions.StringLengthRegisteredFunction =
{
"registeredFunctionClass": "ix.core.util.pojopointer.extensions.StringLengthRegisteredFunction",
"order": 1000
}
ix.api.registeredfunctions.StringSplitRegisteredFunction =
{
"class":"ix.core.util.pojopointer.extensions.StringLengthRegisteredFunction"
},
# {
# "class":"ix.core.util.pojopointer.extensions.InChIRegisteredFunction"
# },
# {
# "class":"ix.core.util.pojopointer.extensions.InChIFullRegisteredFunction"
# },
{
"class":"ix.core.util.pojopointer.extensions.StringSplitRegisteredFunction"
},
"registeredFunctionClass":"ix.core.util.pojopointer.extensions.StringSplitRegisteredFunction",
"order": 1200
}
ix.api.registeredfunctions.SelectRegisteredFunction =
{
"class":"ix.core.util.pojopointer.extensions.SelectRegisteredFunction"
},
"registeredFunctionClass":"ix.core.util.pojopointer.extensions.SelectRegisteredFunction",
"order": 1300
}
ix.api.registeredfunctions.StringJoinRegisteredFunction =
{
"class":"ix.core.util.pojopointer.extensions.StringJoinRegisteredFunction"
"registeredFunctionClass":"ix.core.util.pojopointer.extensions.StringJoinRegisteredFunction",
"order": 1400
}
]

ix.json.typeIdResolvers = [ "ix"]

Expand Down

0 comments on commit 72ae6f9

Please sign in to comment.