Skip to content

Commit 3e6a943

Browse files
committed
POC #972 FullDefiniton collect all unprotected symbols for Save
- added an empty line after each row in `Definition, FullDefinition`
1 parent d2693c2 commit 3e6a943

File tree

6 files changed

+131
-13
lines changed

6 files changed

+131
-13
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/FileFunctions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
12021202
IExpr arg2 = ast.arg2();
12031203
StringBuilder buf = new StringBuilder();
12041204
if (arg2.isSymbol()) {
1205-
buf.append(((ISymbol) arg2).definition());
1205+
buf.append(((ISymbol) arg2).fullDefinitionToString());
12061206
}
12071207
try (FileWriter writer = new FileWriter(fileName.toString())) {
12081208
writer.write(buf.toString());

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/BuiltInDummy.java

+36
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,23 @@ public IAST definition() {
274274
return result;
275275
}
276276

277+
/** {@inheritDoc} */
278+
@Override
279+
public IAST fullDefinition() {
280+
IASTAppendable result = F.ListAlloc();
281+
if (hasAssignedSymbolValue()) {
282+
if (isEvalFlagOn(SETDELAYED_FLAG_ASSIGNED_VALUE)) {
283+
result.append(F.SetDelayed(this, assignedValue()));
284+
} else {
285+
result.append(F.Set(this, assignedValue()));
286+
}
287+
}
288+
if (fRulesData != null) {
289+
result.appendAll(fRulesData.definition());
290+
}
291+
return result;
292+
}
293+
277294
/** {@inheritDoc} */
278295
@Override
279296
public String definitionToString() {
@@ -302,6 +319,25 @@ public String definitionToString() {
302319
return buf.toString();
303320
}
304321

322+
/** {@inheritDoc} */
323+
@Override
324+
public String fullDefinitionToString() {
325+
IAST fullDefinition = fullDefinition();
326+
OutputFormFactory off = OutputFormFactory.get(EvalEngine.get().isRelaxedSyntax());
327+
off.setIgnoreNewLine(true);
328+
StringWriter buf = new StringWriter();
329+
for (int i = 1; i < fullDefinition.size(); i++) {
330+
if (!off.convert(buf, fullDefinition.getRule(i))) {
331+
return "ERROR-IN-OUTPUTFORM";
332+
}
333+
if (i < fullDefinition.size() - 1) {
334+
buf.append("\n\n");
335+
off.setColumnCounter(0);
336+
}
337+
}
338+
return buf.toString();
339+
}
340+
305341
/** {@inheritDoc} */
306342
@Override
307343
public boolean equals(final Object obj) {

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/Symbol.java

+73-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import java.io.Serializable;
55
import java.io.StringWriter;
66
import java.util.Collection;
7+
import java.util.HashSet;
8+
import java.util.Iterator;
79
import java.util.List;
810
import java.util.Locale;
911
import java.util.Map;
12+
import java.util.Set;
1013
import java.util.function.Function;
14+
import java.util.function.Predicate;
1115
import org.matheclipse.core.basic.Config;
1216
import org.matheclipse.core.builtin.AttributeFunctions;
1317
import org.matheclipse.core.convert.AST2Expr;
@@ -268,6 +272,53 @@ public IAST definition() {
268272
return result;
269273
}
270274

275+
/** {@inheritDoc} */
276+
@Override
277+
public IAST fullDefinition() {
278+
Set<ISymbol> symbolSet = new HashSet<ISymbol>();
279+
IAST rules = definition();
280+
for (int i = 1; i < rules.size(); i++) {
281+
IExpr rule = rules.get(i);
282+
collectSymbolsRecursive(rule, symbolSet, x -> x.isSymbol() && !(x.isProtected()));
283+
}
284+
if (symbolSet.size() > 0) {
285+
IASTAppendable fullDefinition = F.ListAlloc(rules.size() + symbolSet.size());
286+
Iterator<ISymbol> iterator = symbolSet.iterator();
287+
while (iterator.hasNext()) {
288+
ISymbol symbol = iterator.next();
289+
IAST subRules = symbol.definition();
290+
fullDefinition.appendArgs(subRules);
291+
}
292+
return fullDefinition;
293+
}
294+
return F.NIL;
295+
}
296+
297+
private static void collectSymbolsRecursive(IExpr expr, Set<ISymbol> symbolSet,
298+
Predicate<ISymbol> predicate) {
299+
if (expr.isAST()) {
300+
IAST list = (IAST) expr;
301+
IExpr head = expr.head();
302+
if (head.isSymbol()) {
303+
if (predicate.test((ISymbol) head)) {
304+
symbolSet.add((ISymbol) head);
305+
}
306+
} else {
307+
collectSymbolsRecursive(head, symbolSet, x -> x.isSymbol());
308+
}
309+
for (int i = 1; i < list.size(); i++) {
310+
IExpr arg = list.getRule(i);
311+
collectSymbolsRecursive(arg, symbolSet, predicate);
312+
}
313+
} else {
314+
if (expr.isSymbol()) {
315+
if (predicate.test((ISymbol) expr)) {
316+
symbolSet.add((ISymbol) expr);
317+
}
318+
}
319+
}
320+
}
321+
271322
/** {@inheritDoc} */
272323
@Override
273324
public String definitionToString() {
@@ -278,18 +329,37 @@ public String definitionToString() {
278329
buf.append(this.toString());
279330
buf.append(")=");
280331
buf.append(attributesList.toString());
281-
buf.append("\n");
332+
buf.append("\n\n");
282333
}
283334

284335
OutputFormFactory off = OutputFormFactory.get(EvalEngine.get().isRelaxedSyntax());
285336
off.setIgnoreNewLine(true);
286337
IAST list = definition();
287338
for (int i = 1; i < list.size(); i++) {
288-
if (!off.convert(buf, list.get(i))) {
339+
if (!off.convert(buf, list.getRule(i))) {
289340
return "ERROR-IN-OUTPUTFORM";
290341
}
291342
if (i < list.size() - 1) {
292-
buf.append("\n");
343+
buf.append("\n\n");
344+
off.setColumnCounter(0);
345+
}
346+
}
347+
return buf.toString();
348+
}
349+
350+
/** {@inheritDoc} */
351+
@Override
352+
public String fullDefinitionToString() {
353+
IAST fullDefinition = fullDefinition();
354+
OutputFormFactory off = OutputFormFactory.get(EvalEngine.get().isRelaxedSyntax());
355+
off.setIgnoreNewLine(true);
356+
StringWriter buf = new StringWriter();
357+
for (int i = 1; i < fullDefinition.size(); i++) {
358+
if (!off.convert(buf, fullDefinition.getRule(i))) {
359+
return "ERROR-IN-OUTPUTFORM";
360+
}
361+
if (i < fullDefinition.size() - 1) {
362+
buf.append("\n\n");
293363
off.setColumnCounter(0);
294364
}
295365
}

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/interfaces/ISymbol.java

+3
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ default void assignValue(IExpr value) {
212212
*/
213213
public IAST definition();
214214

215+
public IAST fullDefinition();
216+
215217
/**
216218
* Return the rules associated to this symbol in <code>String</code> representation
217219
*
@@ -220,6 +222,7 @@ default void assignValue(IExpr value) {
220222
*/
221223
public String definitionToString() throws IOException;
222224

225+
public String fullDefinitionToString();
223226
/**
224227
* Evaluate the given expression for the &quot;down value&quot; rules associated with this symbol
225228
*

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/FileFunctionsTest.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ public void testSave() {
1010
Config.FILESYSTEM_ENABLED = true;
1111
check("temp = FileNameJoin({$TemporaryDirectory, \"saved.txt\"});Print(temp);", //
1212
"");
13-
check("a = 1000", //
14-
"1000");
15-
check("Save(temp, a)", //
13+
check("g(x_) := x^3;"//
14+
+ "SetAttributes(f, Listable);"//
15+
+ "f(x_) := g(x^2);", //
1616
"");
17-
check("Clear(a)", //
17+
check("Save(temp, f)", //
1818
"");
19+
check("Clear(f,g)", //
20+
"");
21+
check("{f(2),g(7)}", //
22+
"{f(2),g(7)}");
1923
check("Get(temp)", //
20-
"{1000}");
21-
check("a", //
22-
"1000");
24+
"");
25+
check("{f(2),g(7)}", //
26+
"{64,343}");
2327
}
2428

2529

symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/LowercaseTestCase.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -3354,7 +3354,10 @@ public void testCondition() {
33543354
"1");
33553355
check("Definition(q)", //
33563356
"q(0,0)=1\n" //
3357-
+ "q(i_,j_)/;i<0||j<0=0\n" + "q(i_,j_):=q(i,j)=q(-1+i,j)+q(i,-1+j)");
3357+
+ "\n" //
3358+
+ "q(i_,j_)/;i<0||j<0=0\n" //
3359+
+ "\n" //
3360+
+ "q(i_,j_):=q(i,j)=q(-1+i,j)+q(i,-1+j)");
33583361
check("q(5,5)", //
33593362
"252");
33603363

@@ -4960,7 +4963,9 @@ public void testDefinition() {
49604963
check("f(x_):={x}", //
49614964
"");
49624965
check("Definition(f)", //
4963-
"Attributes(f)={Listable}\n" + "f(x_):={x}");
4966+
"Attributes(f)={Listable}\n"//
4967+
+ "\n"//
4968+
+ "f(x_):={x}");
49644969
}
49654970

49664971
@Test

0 commit comments

Comments
 (0)