functions
This commit is contained in:
@@ -16,6 +16,7 @@ import org.solovyev.android.calculator.model.MathEntityBuilder;
|
||||
import org.solovyev.common.JBuilder;
|
||||
import org.solovyev.common.math.MathRegistry;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -96,7 +97,7 @@ public class CalculatorFunctionsMathRegistry extends AbstractCalculatorMathRegis
|
||||
final AFunction result = new AFunction();
|
||||
result.setName(entity.getName());
|
||||
result.setContent(((CustomFunction) entity).getContent());
|
||||
result.setParameterNames(((CustomFunction) entity).getParameterNames());
|
||||
result.setParameterNames(new ArrayList<String>(((CustomFunction) entity).getParameterNames()));
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
|
@@ -72,12 +72,6 @@ public class ToJsclTextProcessor implements TextProcessor<PreparedExpression, St
|
||||
}
|
||||
}
|
||||
|
||||
if (mathTypeBefore != null &&
|
||||
(mathTypeBefore.getMathType() == MathType.function || mathTypeBefore.getMathType() == MathType.operator) &&
|
||||
CollectionsUtils.find(MathType.openGroupSymbols, startsWithFinder) != null) {
|
||||
throw new CalculatorParseException(i, s, new CalculatorMessage(CalculatorMessages.msg_005, MessageType.error, mathTypeBefore.getMatch()));
|
||||
}
|
||||
|
||||
i = mathTypeResult.processToJscl(result, i);
|
||||
}
|
||||
return result;
|
||||
|
@@ -6,16 +6,18 @@
|
||||
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.math.function.IConstant;
|
||||
import jscl.math.function.IFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.simpleframework.xml.Element;
|
||||
import org.simpleframework.xml.ElementList;
|
||||
import org.simpleframework.xml.Root;
|
||||
import org.simpleframework.xml.Transient;
|
||||
import org.solovyev.android.calculator.MathPersistenceEntity;
|
||||
import org.solovyev.common.math.MathEntity;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -25,7 +27,7 @@ import java.util.List;
|
||||
* Time: 5:25 PM
|
||||
*/
|
||||
|
||||
@Root
|
||||
@Root(name = "function")
|
||||
public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
|
||||
/*
|
||||
@@ -42,20 +44,20 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@Element
|
||||
@Element(name = "body")
|
||||
@NotNull
|
||||
private String content;
|
||||
|
||||
@Element(required = false)
|
||||
@ElementList
|
||||
@NotNull
|
||||
private List<String> parameterNames = Collections.emptyList();
|
||||
private List<String> parameterNames = new ArrayList<String>();
|
||||
|
||||
@Element
|
||||
private boolean system;
|
||||
|
||||
@Element(required = false)
|
||||
@Nullable
|
||||
private String description;
|
||||
@NotNull
|
||||
private String description = "";
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
@@ -86,7 +88,7 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
final IFunction that = ((IFunction) mathEntity);
|
||||
this.name = that.getName();
|
||||
this.content = that.getContent();
|
||||
this.description = this.getContent();
|
||||
this.description = StringUtils.getNotEmpty(this.getDescription(), "");
|
||||
this.system = that.isSystem();
|
||||
if (that.isIdDefined()) {
|
||||
this.id = that.getId();
|
||||
@@ -104,7 +106,7 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* GETTERS/SEETTERS
|
||||
* GETTERS/SETTERS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
@@ -144,8 +146,8 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
@@ -201,29 +203,15 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
this.id = function.getId();
|
||||
}
|
||||
|
||||
public Builder(@NotNull IConstant function) {
|
||||
this.name = function.getName();
|
||||
public Builder(@NotNull String name,
|
||||
@NotNull String value,
|
||||
@NotNull List<String> parameterNames) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.parameterNames = parameterNames;
|
||||
}
|
||||
|
||||
this.value = function.getValue();
|
||||
|
||||
this.system = function.isSystem();
|
||||
this.description = function.getDescription();
|
||||
if (function.isIdDefined()) {
|
||||
this.id = function.getId();
|
||||
}
|
||||
}
|
||||
|
||||
public Builder(@NotNull String name, @NotNull Double value) {
|
||||
this(name, String.valueOf(value));
|
||||
}
|
||||
|
||||
public Builder(@NotNull String name, @Nullable String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@NotNull
|
||||
public Builder setName(@NotNull String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
@@ -262,8 +250,8 @@ public class AFunction implements IFunction, MathPersistenceEntity {
|
||||
result.name = name;
|
||||
result.content = value;
|
||||
result.system = system;
|
||||
result.description = description;
|
||||
result.parameterNames = parameterNames;
|
||||
result.description = StringUtils.getNotEmpty(description, "");
|
||||
result.parameterNames = new ArrayList<String>(parameterNames);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@@ -0,0 +1,155 @@
|
||||
package org.solovyev.android.calculator.model;
|
||||
|
||||
import jscl.util.ExpressionGenerator;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
import org.solovyev.common.equals.CollectionEqualizer;
|
||||
import org.solovyev.common.equals.EqualsUtils;
|
||||
import org.solovyev.common.text.StringUtils;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 11/14/12
|
||||
* Time: 8:06 PM
|
||||
*/
|
||||
public class FunctionsTest {
|
||||
|
||||
private static final String xml = "<functions>\n" +
|
||||
" <functions class=\"java.util.ArrayList\">\n" +
|
||||
" <function>\n" +
|
||||
" <name>test</name>\n" +
|
||||
" <body>x+y</body>\n" +
|
||||
" <parameterNames class=\"java.util.ArrayList\">\n" +
|
||||
" <string>x</string>\n" +
|
||||
" <string>y</string>\n" +
|
||||
" </parameterNames>\n" +
|
||||
" <system>false</system>\n" +
|
||||
" <description>description</description>\n" +
|
||||
" </function>\n" +
|
||||
" <function>\n" +
|
||||
" <name>z_2</name>\n" +
|
||||
" <body>e^(z_1^2+z_2^2)</body>\n" +
|
||||
" <parameterNames class=\"java.util.ArrayList\">\n" +
|
||||
" <string>z_1</string>\n" +
|
||||
" <string>z_2</string>\n" +
|
||||
" </parameterNames>\n" +
|
||||
" <system>true</system>\n" +
|
||||
" <description></description>\n" +
|
||||
" </function>\n" +
|
||||
" <function>\n" +
|
||||
" <name>z_2</name>\n" +
|
||||
" <body>e^(z_1^2+z_2^2)</body>\n" +
|
||||
" <parameterNames class=\"java.util.ArrayList\">\n" +
|
||||
" <string>z_1</string>\n" +
|
||||
" <string>z_2</string>\n" +
|
||||
" </parameterNames>\n" +
|
||||
" <system>true</system>\n" +
|
||||
" <description></description>\n" +
|
||||
" </function>\n" +
|
||||
" </functions>\n" +
|
||||
"</functions>";
|
||||
|
||||
@Test
|
||||
public void testXml() throws Exception {
|
||||
final Functions in = new Functions();
|
||||
|
||||
AFunction first = new AFunction.Builder("test", "x+y", Arrays.asList("x", "y")).setDescription("description").setSystem(false).create();
|
||||
in.getEntities().add(first);
|
||||
|
||||
AFunction second = new AFunction.Builder("z_2", "e^(z_1^2+z_2^2)", Arrays.asList("z_1", "z_2")).setSystem(true).create();
|
||||
in.getEntities().add(second);
|
||||
|
||||
AFunction third = new AFunction.Builder("z_2", "e^(z_1^2+z_2^2)", Arrays.asList("z_1", "z_2")).setSystem(true).setDescription("").create();
|
||||
in.getEntities().add(third);
|
||||
|
||||
final Functions out = testXml(in, xml);
|
||||
|
||||
Assert.assertTrue(!out.getEntities().isEmpty());
|
||||
|
||||
final AFunction firstOut = out.getEntities().get(0);
|
||||
final AFunction secondOut = out.getEntities().get(1);
|
||||
|
||||
assertEquals(first, firstOut);
|
||||
assertEquals(second, secondOut);
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Functions testXml(@NotNull Functions in, @Nullable String expectedXml) throws Exception {
|
||||
final String actualXml = toXml(in);
|
||||
|
||||
if (expectedXml != null) {
|
||||
Assert.assertEquals(expectedXml, actualXml);
|
||||
}
|
||||
|
||||
final Serializer serializer = new Persister();
|
||||
final Functions out = serializer.read(Functions.class, actualXml);
|
||||
final String actualXml2 = toXml(out);
|
||||
Assert.assertEquals(actualXml, actualXml2);
|
||||
return out;
|
||||
}
|
||||
|
||||
private String toXml(Functions in) throws Exception {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final Serializer serializer = new Persister();
|
||||
serializer.write(in, sw);
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomXml() throws Exception {
|
||||
final Functions in = new Functions();
|
||||
|
||||
final Random random = new Random(new Date().getTime());
|
||||
|
||||
ExpressionGenerator generator = new ExpressionGenerator(10);
|
||||
for ( int i = 0; i < 1000; i++ ) {
|
||||
final String content = generator.generate();
|
||||
|
||||
final String paramsString = StringUtils.generateRandomString(random.nextInt(10));
|
||||
final List<String> parameterNames = new ArrayList<String>();
|
||||
for (int j = 0; j < paramsString.length(); j++) {
|
||||
parameterNames.add(String.valueOf(paramsString.charAt(j)));
|
||||
}
|
||||
|
||||
final AFunction.Builder builder = new AFunction.Builder("test_" + i, content, parameterNames);
|
||||
|
||||
if ( random.nextBoolean() ) {
|
||||
builder.setDescription(StringUtils.generateRandomString(random.nextInt(100)));
|
||||
}
|
||||
|
||||
builder.setSystem(random.nextBoolean());
|
||||
|
||||
in.getEntities().add(builder.create());
|
||||
}
|
||||
|
||||
testXml(in, null);
|
||||
}
|
||||
|
||||
private void assertEquals(@NotNull final AFunction expected,
|
||||
@NotNull AFunction actual) {
|
||||
//Assert.assertEquals(expected.getId(), actual.getId());
|
||||
Assert.assertEquals(expected.getContent(), actual.getContent());
|
||||
Assert.assertEquals(expected.getDescription(), actual.getDescription());
|
||||
Assert.assertEquals(expected.getName(), actual.getName());
|
||||
Assert.assertThat(actual.getParameterNames(), new BaseMatcher<List<String>>() {
|
||||
@Override
|
||||
public boolean matches(Object item) {
|
||||
return EqualsUtils.areEqual(expected.getParameterNames(), (List<String>)item, new CollectionEqualizer<String>(null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user