Skip to content

Commit

Permalink
extract vectorMatch #294 #364
Browse files Browse the repository at this point in the history
  • Loading branch information
walterxie committed May 31, 2023
1 parent c4572bb commit 1e923d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
38 changes: 3 additions & 35 deletions lphy/src/main/java/lphy/core/parser/ParserLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import lphy.core.spi.LPhyLoader;
import lphy.core.util.LoggerUtils;
import lphy.core.vectorization.IID;
import lphy.core.vectorization.VectorizedDistribution;
import lphy.core.vectorization.VectorizedFunction;
import lphy.core.vectorization.VectorMatch;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -184,9 +183,9 @@ private static Generator constructGenerator(String name, Constructor constructor
// return (Generator) constructor.newInstance(initargs);
// } else
return iid;
} else if (vectorMatch(arguments, initargs) > 0) {
} else if (VectorMatch.vectorMatch(arguments, initargs) > 0) {
// do vector match
return vectorGenerator(constructor, arguments, initargs);
return VectorMatch.vectorGenerator(constructor, arguments, initargs);
} else {
throw new RuntimeException("ERROR! No match in '" + name + "' constructor arguments, including vector match! ");
}
Expand All @@ -198,37 +197,6 @@ private static Generator constructGenerator(String name, Constructor constructor
return null;
}

public static int vectorMatch(List<Argument> arguments, Object[] initargs) {
int vectorMatches = 0;
for (int i = 0; i < arguments.size(); i++) {
Argument argument = arguments.get(i);
Value argValue = (Value) initargs[i];

if (argValue == null) {
if (!argument.optional) return 0;
} else {
if (argument.type.isAssignableFrom(argValue.value().getClass())) {
// direct type match
} else if (argValue.value().getClass().isArray() && argument.type.isAssignableFrom(argValue.value().getClass().getComponentType())) {
// vector match
vectorMatches += 1;
}
}
}
return vectorMatches;
}

public static Generator vectorGenerator(Constructor constructor, List<Argument> arguments, Object[] vectorArgs) throws IllegalAccessException, InvocationTargetException, InstantiationException {

if (GenerativeDistribution.class.isAssignableFrom(constructor.getDeclaringClass())) {
return new VectorizedDistribution(constructor, arguments, vectorArgs);
} else if (DeterministicFunction.class.isAssignableFrom(constructor.getDeclaringClass())) {
return new VectorizedFunction(constructor, arguments, vectorArgs);
} else
throw new IllegalArgumentException("Unexpected Generator class! Expecting a GenerativeDistribution or a DeterministicFunction");

}

private static Set<Class<?>> getGenerativeDistributionClasses(String name) {
return genDistDictionary.get(name);
}
Expand Down
45 changes: 45 additions & 0 deletions lphy/src/main/java/lphy/core/vectorization/VectorMatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lphy.core.vectorization;

import lphy.core.graphicalmodel.components.*;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
* Vectorization for arrays
*/
public class VectorMatch {

public static int vectorMatch(List<Argument> arguments, Object[] initargs) {
int vectorMatches = 0;
for (int i = 0; i < arguments.size(); i++) {
Argument argument = arguments.get(i);
Value argValue = (Value) initargs[i];

if (argValue == null) {
if (!argument.optional) return 0;
} else {
if (argument.type.isAssignableFrom(argValue.value().getClass())) {
// direct type match
} else if (argValue.value().getClass().isArray() &&
argument.type.isAssignableFrom(argValue.value().getClass().getComponentType())) {
// vector match
vectorMatches += 1;
}
}
}
return vectorMatches;
}

public static Generator vectorGenerator(Constructor constructor, List<Argument> arguments, Object[] vectorArgs) throws IllegalAccessException, InvocationTargetException, InstantiationException {

if (GenerativeDistribution.class.isAssignableFrom(constructor.getDeclaringClass())) {
return new VectorizedDistribution(constructor, arguments, vectorArgs);
} else if (DeterministicFunction.class.isAssignableFrom(constructor.getDeclaringClass())) {
return new VectorizedFunction(constructor, arguments, vectorArgs);
} else
throw new IllegalArgumentException("Unexpected Generator class! Expecting a GenerativeDistribution or a DeterministicFunction");

}
}

0 comments on commit 1e923d9

Please sign in to comment.