Skip to content

Commit

Permalink
Avoid Class#forName() registering Kryo classes
Browse files Browse the repository at this point in the history
- calls UtilityFunctionCtors.registerKryo(kryo);
  • Loading branch information
axkr committed Nov 9, 2023
1 parent 5cfb55f commit ff20144
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.matheclipse.core.expression.data.TestReportObjectExpr;
import org.matheclipse.core.expression.data.TestResultObjectExpr;
import org.matheclipse.core.expression.data.TimeObjectExpr;
import org.matheclipse.core.integrate.rubi.UtilityFunctionCtors;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IASTAppendable;
import org.matheclipse.core.interfaces.IBuiltInSymbol;
Expand Down Expand Up @@ -206,7 +207,7 @@ public BuiltInDummy read(Kryo kryo, Input input, Class<? extends BuiltInDummy> t
}
}

private static class IASTSerializer extends Serializer<IAST> {
public static class IASTSerializer extends Serializer<IAST> {

@Override
public void write(Kryo kryo, Output stream, IAST ast) {
Expand Down Expand Up @@ -295,13 +296,6 @@ static public void main(String[] args) throws Exception {
// "kryo.register(B3." + asList.get(i).getSimpleName() + ".class);");
// }


// List<Class<?>> asList = Arrays.asList(UtilityFunctionCtors.class.getDeclaredClasses());
// for (int i = 0; i < asList.size(); i++) {
// System.out.println("kryo.register(Class.forName(\"" + asList.get(i).toString().substring(6)
// + "\"), new IASTSerializer());");
// }

F.initSymbols();
Kryo kryo = initKryo();
// IBuiltInSymbol object = S.Im;
Expand Down Expand Up @@ -346,36 +340,6 @@ public static Kryo initKryo() throws ClassNotFoundException {
kryo.register(org.matheclipse.core.patternmatching.RulesData.class);
kryo.register(org.matheclipse.core.patternmatching.PatternMatcherEquals.class);
kryo.register(org.matheclipse.core.patternmatching.PatternMatcherAndEvaluator.class);
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$EqQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$FalseQ"),
new IASTSerializer());
kryo.register(
Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$FracPart"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$GeQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$GtQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$IGtQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$ILtQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$IntPart"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$LeQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$LtQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$NeQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$PolyQ"),
new IASTSerializer());
kryo.register(Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$Simp"),
new IASTSerializer());
kryo.register(
Class.forName("org.matheclipse.core.integrate.rubi.UtilityFunctionCtors$Unintegrable"),
new IASTSerializer());

// numbers
kryo.register(ApcomplexNum.class);
Expand Down Expand Up @@ -496,6 +460,8 @@ public static Kryo initKryo() throws ClassNotFoundException {
kryo.register(BuiltInSymbol.class, new BuiltInSymbolSerializer());
kryo.register(BuiltInRubi.class, new BuiltInRubiSerializer());
kryo.register(BuiltInDummy.class, new BuiltInDummySerializer());

UtilityFunctionCtors.registerKryo(kryo);
return kryo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import org.matheclipse.core.expression.B1;
import org.matheclipse.core.expression.B2;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.expression.KryoUtil;
import org.matheclipse.core.expression.S;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IASTAppendable;
import org.matheclipse.core.interfaces.IASTMutable;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.ISymbol;
import com.esotericsoftware.kryo.Kryo;

/**
* UtilityFunction constructors from the <a href="http://www.apmaths.uwo.ca/~arich/">Rubi -
Expand Down Expand Up @@ -391,7 +393,7 @@ public static IAST EasyDQ(final IExpr a0, final IExpr a1) {
return F.binaryAST2(F.$rubi("EasyDQ"), a0, a1);
}

private static final class EqQ extends B2 {
public static final class EqQ extends B2 {
public EqQ() {
super();
}
Expand Down Expand Up @@ -2162,15 +2164,16 @@ public static IExpr evalRubiDistPlus(IAST astPlus, EvalEngine engine) {
if (arg1.isAST(Dist) && arg1.size() == 4) {
// dist1 = Dist[u_,v_,x_]
IAST dist1 = engine.evalArgs((IAST) arg1, ISymbol.NOATTRIBUTE, false).orElse((IAST) arg1); // (IAST)
// arg1;
// arg1;
IExpr v = dist1.arg2();
IExpr x = dist1.arg3();
for (int j = i + 1; j < astPlus.size(); j++) {
IExpr arg2 = astPlus.get(j);
if (arg2.isAST(Dist) && arg2.size() == 4 && arg2.getAt(2).equals(v)
&& arg2.getAt(3).equals(x)) {
// dist2=Dist[w_,v_,x_]
IAST dist2 = engine.evalArgs((IAST) arg2, ISymbol.NOATTRIBUTE, false).orElse((IAST) arg2); // (IAST)
IAST dist2 =
engine.evalArgs((IAST) arg2, ISymbol.NOATTRIBUTE, false).orElse((IAST) arg2); // (IAST)
// arg2;
IASTAppendable result = astPlus.removeAtClone(j);
result.remove(i);
Expand Down Expand Up @@ -2201,8 +2204,8 @@ public static IExpr evalRubiDistPlus(IAST astPlus, EvalEngine engine) {
}
} else if (arg1.isTimes2() && arg1.first().isMinusOne() && arg1.second().isAST(Dist)) {
// -1 * Dist[w_,v_,x_]
IAST dist1 =
engine.evalArgs((IAST) arg1.second(), ISymbol.NOATTRIBUTE, false).orElse((IAST) arg1.second()); // (IAST)
IAST dist1 = engine.evalArgs((IAST) arg1.second(), ISymbol.NOATTRIBUTE, false)
.orElse((IAST) arg1.second()); // (IAST)
// arg1.second();
IExpr v = dist1.arg2();
IExpr x = dist1.arg3();
Expand All @@ -2211,7 +2214,8 @@ public static IExpr evalRubiDistPlus(IAST astPlus, EvalEngine engine) {
if (arg2.isAST(Dist) && arg2.size() == 4 && arg2.getAt(2).equals(v)
&& arg2.getAt(3).equals(x)) {
// dist2 = Dist[u_,v_,x_]
IAST dist2 = engine.evalArgs((IAST) arg2, ISymbol.NOATTRIBUTE, false).orElse((IAST) arg2); // (IAST)
IAST dist2 =
engine.evalArgs((IAST) arg2, ISymbol.NOATTRIBUTE, false).orElse((IAST) arg2); // (IAST)
// arg2;
IASTAppendable result = astPlus.removeAtClone(j);
result.remove(i);
Expand Down Expand Up @@ -2621,4 +2625,27 @@ public static void getUtilityFunctionsRuleASTRubi45() {
ast = org.matheclipse.core.integrate.rubi.UtilityFunctions39.RULES;
// org.matheclipse.core.integrate.rubi.UtilityFunctions.init();
}

/**
* Register classes for Kryo serializer.
*
* @param kryo
* @throws ClassNotFoundException
*/
public static void registerKryo(Kryo kryo) throws ClassNotFoundException {
kryo.register(EqQ.class, new KryoUtil.IASTSerializer());
kryo.register(FalseQ.class, new KryoUtil.IASTSerializer());
kryo.register(FracPart.class, new KryoUtil.IASTSerializer());
kryo.register(GeQ.class, new KryoUtil.IASTSerializer());
kryo.register(GtQ.class, new KryoUtil.IASTSerializer());
kryo.register(IGtQ.class, new KryoUtil.IASTSerializer());
kryo.register(ILtQ.class, new KryoUtil.IASTSerializer());
kryo.register(IntPart.class, new KryoUtil.IASTSerializer());
kryo.register(LeQ.class, new KryoUtil.IASTSerializer());
kryo.register(LtQ.class, new KryoUtil.IASTSerializer());
kryo.register(NeQ.class, new KryoUtil.IASTSerializer());
kryo.register(PolyQ.class, new KryoUtil.IASTSerializer());
kryo.register(Simp.class, new KryoUtil.IASTSerializer());
kryo.register(Unintegrable.class, new KryoUtil.IASTSerializer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ private void equalsStringCopy(Object original) {
ois.close();
long end = System.currentTimeMillis();
long temp = start1 - start0;
System.out.println(Long.valueOf(temp).toString());
System.out.println("Starting (write) in milliseconds: " + Long.valueOf(temp).toString());
temp = end - start1;
System.out.println(Long.valueOf(temp).toString());
System.out.println("Reading in milliseconds: " + Long.valueOf(temp).toString());
assertEquals(original.toString(), copy.toString());

} catch (ClassNotFoundException cnfe) {
Expand Down

0 comments on commit ff20144

Please sign in to comment.