Skip to content

Commit

Permalink
Fix misc bugs (#1338)
Browse files Browse the repository at this point in the history
* Fix incorrect duplicate deps checking, regen pom

* Add StringUtil docstring and test

* Fix incorrect full type creation

* Add additional fallback lookup for service configs based on generic superclasses

* Fix broken installation

* Fix Sphinx, sort of
  • Loading branch information
AutonomicPerfectionist authored Sep 7, 2023
1 parent 0ecdc68 commit 9ff1d96
Show file tree
Hide file tree
Showing 11 changed files with 600 additions and 558 deletions.
948 changes: 474 additions & 474 deletions pom.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/org/myrobotlab/codec/CodecUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public static String makeFullTypeName(String type) {
return null;
}
if (!type.contains(".")) {
return String.format("org.myrobotlab.service.%s", type);
return ("Service".equals(type)) ? "org.myrobotlab.framework.Service" : String.format("org.myrobotlab.service.%s", type);
}
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ synchronized public void install(String location, String[] serviceTypes) {
}

if (err.size() > 0) {
log.error("had errors - repo will not be updated");
log.error("had errors - repo will not be updated. Errors:\n{}", err);
} else {

// TODO - promote to Repo.setInstalled
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/myrobotlab/framework/repo/MavenWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ public void createPom(String location, String[] serviceTypes) throws IOException

// If we haven't seen this dependency before, add it to our known
// dependencies
if (!allDependencies.containsKey(serviceDependency.getKey()))
allDependencies.put(serviceDependency.getKey(), new ArrayList<>(List.of(serviceDependency)));
if (!allDependencies.containsKey(serviceDependency.getProjectCoordinates()))
allDependencies.put(serviceDependency.getProjectCoordinates(), new ArrayList<>(List.of(serviceDependency)));
else {
// We have seen it, so loop over all dependencies with matching keys
allDependencies.get(serviceDependency.getKey()).forEach(existingDependency -> {
allDependencies.get(serviceDependency.getProjectCoordinates()).forEach(existingDependency -> {

// Check priority, if this dependency is higher priority than
// existing,
Expand All @@ -173,7 +173,7 @@ public void createPom(String location, String[] serviceTypes) throws IOException
serviceDependency.setSkipped(true);
});
// Add the dependency to the known dependencies
allDependencies.get(serviceDependency.getKey()).add(serviceDependency);
allDependencies.get(serviceDependency.getProjectCoordinates()).add(serviceDependency);

}
});
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/org/myrobotlab/framework/repo/ServiceData.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
*/
public class ServiceData implements Serializable {

/**
* the set of all categories
*/
public TreeMap<String, Category> categoryTypes = new TreeMap<>();

/**
* all services meta data is contained here
*/
public TreeMap<String, MetaData> serviceTypes = new TreeMap<>();

static private ServiceData localInstance = null;

static final public String LIBRARIES = "libraries";
Expand All @@ -57,7 +67,7 @@ public class ServiceData implements Serializable {

private static final long serialVersionUID = 1L;

static private String serviceDataCacheFileName = LIBRARIES + File.separator + "serviceData.json";
static private final String serviceDataCacheFileName = LIBRARIES + File.separator + "serviceData.json";

/**
* clears all overrides. All services shall be using the standard hard co
Expand Down Expand Up @@ -92,9 +102,8 @@ static public synchronized ServiceData generate() throws IOException {
List<String> services = FileIO.getServiceList();

log.info("found {} services", services.size());
for (int i = 0; i < services.size(); ++i) {
for (String fullClassName : services) {

String fullClassName = services.get(i);
log.debug("querying {}", fullClassName);
try {

Expand All @@ -112,7 +121,7 @@ static public synchronized ServiceData generate() throws IOException {
sd.add(serviceType);

for (String cat : serviceType.categories) {
Category category = null;
Category category;
if (serviceType.isAvailable()) {
if (sd.categoryTypes.containsKey(cat)) {
category = sd.categoryTypes.get(cat);
Expand All @@ -135,10 +144,10 @@ static public synchronized ServiceData generate() throws IOException {
}

static public List<ServiceDependency> getDependencyKeys(String fullTypeName) {
List<ServiceDependency> keys = new ArrayList<ServiceDependency>();
List<ServiceDependency> keys = new ArrayList<>();
ServiceData sd = getLocalInstance();
if (!sd.serviceTypes.containsKey(fullTypeName)) {
log.error("{} not defined in service types");
log.error("{} not defined in service types", fullTypeName);
return keys;
}

Expand Down Expand Up @@ -278,16 +287,6 @@ static public Map<String, ServiceReservation> getOverrides() {
return planStore;
}

/**
* the set of all categories
*/
TreeMap<String, Category> categoryTypes = new TreeMap<String, Category>();

/**
* all services meta data is contained here
*/
TreeMap<String, MetaData> serviceTypes = new TreeMap<String, MetaData>();

public ServiceData() {
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/myrobotlab/framework/repo/ServiceDependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ public String getKey() {
return String.format("%s/%s/%s/%s", groupId, artifactId, version, ext);
}

/**
* Gives the Maven coordinates for this dependency,
* which are the group ID, artifact ID, and version
* all separated by colons.
* @return The Maven coordinates for this dependency
*/
public String getCoordinates() {
return String.format("%s:%s:%s", groupId, artifactId, version);
}

/**
* Gives the unique Maven coordinates of this dependency's project.
* This does not give the version, and is mainly used to determine
* if two dependencies are for the same project.
*
* @return The group ID and the artifact ID, separated by a colon
*/
public String getProjectCoordinates() {
return String.format("%s:%s", groupId, artifactId);
}

public void add(ServiceExclude serviceExclude) {
excludes.add(serviceExclude);
}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/org/myrobotlab/service/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,7 @@ static private synchronized ServiceInterface createService(String name, String t
return null;
}

String fullTypeName;
if (type.contains(".")) {
fullTypeName = type;
} else {
fullTypeName = String.format("org.myrobotlab.service.%s", type);
}
String fullTypeName = CodecUtils.makeFullTypeName(type);

ServiceInterface si = Runtime.getService(fullName);
if (si != null) {
Expand Down Expand Up @@ -902,7 +897,7 @@ public static Runtime getInstance() {
if (startYml.enable) {
Runtime.load("runtime", "Runtime");
}
((RuntimeConfig) runtime.config).add("runtime");
runtime.config.add("runtime");

runtime.startService();
// platform virtual is higher priority than service virtual
Expand Down
60 changes: 23 additions & 37 deletions src/main/java/org/myrobotlab/service/Sphinx.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.util.HashSet;
import java.util.Map;

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
import org.apache.commons.lang.StringUtils;
import org.myrobotlab.framework.Message;
import org.myrobotlab.framework.Service;
Expand All @@ -57,7 +59,6 @@
import org.slf4j.Logger;

import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;

Expand Down Expand Up @@ -90,29 +91,28 @@ public void run() {
String newPath = FileIO.getCfgDir() + File.separator + myService.getName() + ".xml";
File localGramFile = new File(newPath);

warn("CONFIG NOT IMPLEMENTED, ONLY SUPPORTS BASE EN-US");

info("loading grammar file");
if (localGramFile.exists()) {
info(String.format("grammar config %s", newPath));
cm = new ConfigurationManager(newPath);
} else {
// resource in jar default
info(String.format("grammar /resource/Sphinx/simple.xml"));
cm = new ConfigurationManager(this.getClass().getResource(FileIO.gluePaths(getResourceDir(), "/Sphinx/simple.xml")));
}

info("starting recognizer");
// start the word recognizer
recognizer = (Recognizer) cm.lookup("recognizer");
recognizer.allocate();

info("starting microphone");
microphone = (Microphone) cm.lookup("microphone");
if (!microphone.startRecording()) {
log.error("Cannot start microphone.");
recognizer.deallocate();
}
// recognizer = cm.lookup("recognizer");
Configuration configuration = new Configuration();

configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);

SpeechRecognizerConfig c = (SpeechRecognizerConfig)config;
SpeechRecognizerConfig c = config;


// loop the recognition until the program exits.
Expand All @@ -121,7 +121,7 @@ public void run() {

info("listening: %b", c.listening);
invoke("listeningEvent", true);
Result result = recognizer.recognize();
Result result = recognizer.getResult().getResult();

if (!c.listening) {
// we could have stopped listening
Expand Down Expand Up @@ -220,7 +220,7 @@ public void run() {
public final static Logger log = LoggerFactory.getLogger(Sphinx.class.getCanonicalName());
transient Microphone microphone = null;
transient ConfigurationManager cm = null;
transient Recognizer recognizer = null;
transient LiveSpeechRecognizer recognizer = null;

transient SpeechProcessor speechProcessor = null;

Expand Down Expand Up @@ -434,7 +434,7 @@ public boolean isRecording() {
*
*/
public synchronized boolean onIsSpeaking(Boolean talking) {
SpeechRecognizerConfig c = (SpeechRecognizerConfig)config;
SpeechRecognizerConfig c = config;

if (talking) {
c.listening = false;
Expand Down Expand Up @@ -501,7 +501,7 @@ public void lockOutAllGrammarExcept(String lockPhrase) {
@Override
public synchronized void pauseListening() {
log.info("Pausing Listening");
SpeechRecognizerConfig c = (SpeechRecognizerConfig)config;
SpeechRecognizerConfig c = config;

c.listening = false;
if (microphone != null && recognizer != null) {
Expand Down Expand Up @@ -539,11 +539,7 @@ public void resumeListening() {
SpeechRecognizerConfig c = (SpeechRecognizerConfig)config;

c.listening = true;
if (microphone != null) {
// TODO: no idea if this does anything useful.
microphone.clear();
microphone.startRecording();
}
recognizer.startRecognition(true);
}

// FYI - grammar must be created BEFORE we start to listen
Expand Down Expand Up @@ -587,8 +583,7 @@ private String cleanGrammar(String grammar) {
}

public void startRecordingx() {
microphone.clear();
microphone.startRecording();
recognizer.startRecognition(true);
}

/**
Expand All @@ -598,13 +593,12 @@ public void startRecordingx() {
*/
@Override
public void stopListening() {
if (microphone != null) {
microphone.stopRecording();
microphone.clear();
}
SpeechRecognizerConfig c = (SpeechRecognizerConfig)config;
SpeechRecognizerConfig c = config;

c.listening = false;
if (recognizer != null) {
recognizer.stopRecognition();
}
if (speechProcessor != null) {
speechProcessor.isRunning = false;
}
Expand All @@ -615,14 +609,6 @@ public void stopListening() {
public void stopService() {
super.stopService();
stopListening();
if (recognizer != null) {
recognizer.deallocate();
recognizer = null;
}
if (microphone != null) {
microphone.stopRecording();
microphone = null;
}
}

@Override
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/org/myrobotlab/service/config/ServiceConfig.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package org.myrobotlab.service.config;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.TreeMap;

import org.myrobotlab.codec.CodecUtils;
import org.myrobotlab.framework.Peer;
import org.myrobotlab.framework.Plan;
import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.interfaces.ServiceInterface;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.service.Runtime;
import org.slf4j.Logger;
Expand Down Expand Up @@ -209,6 +215,12 @@ public Peer putPeerType(String peerKey, String fullName, String peerType) {
}

public static Plan getDefault(Plan plan, String name, String inType) {
// if ("Service".equals(inType) || Service.class.getCanonicalName().equals(inType)) {
// ServiceConfig sc = new ServiceConfig();
// sc.type = inType;
// plan.put(name, sc);
// return plan;
// }
try {

// if (type == null) {
Expand All @@ -227,11 +239,28 @@ public static Plan getDefault(Plan plan, String name, String inType) {
// plan.merge();
config.getDefault(plan, name);

} catch (ClassNotFoundException e) {
log.info("could not find {} loading generalized ServiceConfig", inType);
ServiceConfig sc = new ServiceConfig();
sc.type = inType;
plan.put(name, sc);
} catch (ClassNotFoundException cnfe) {
try {
Class<? extends Service> serviceClass = Class.forName(CodecUtils.makeFullTypeName(inType)).asSubclass(Service.class);
Type superClass = serviceClass.getGenericSuperclass();
if (superClass instanceof ParameterizedType) {
ParameterizedType genericSuperClass = (ParameterizedType) superClass;
System.out.println("Got generic superclass: " + genericSuperClass + " for service class " + serviceClass);
Class<? extends ServiceConfig> configClass = ((Class<?>) genericSuperClass.getActualTypeArguments()[0]).asSubclass(ServiceConfig.class);
ServiceConfig newConfig = configClass.getConstructor().newInstance();
newConfig.type = inType;
newConfig.getDefault(plan, name);
} else {
throw new NoSuchElementException("Superclass is not generic");
}

} catch (NoClassDefFoundError | ClassNotFoundException | NoSuchElementException | NoSuchMethodException | InstantiationException |
IllegalAccessException | InvocationTargetException | ClassCastException ignored) {
log.info("could not find config class for {}, loading generalized ServiceConfig", inType);
ServiceConfig sc = new ServiceConfig();
sc.type = inType;
plan.put(name, sc);
}
} catch (Exception e) {
Runtime.getInstance().error(e);
}
Expand Down
Loading

0 comments on commit 9ff1d96

Please sign in to comment.