* [NTG-context] Re: Lua question - table.save, table.load
2025-01-11 16:48 ` [NTG-context] " Hans Hagen
@ 2025-01-11 21:10 ` Jeong Dal via ntg-context
2025-01-12 11:00 ` Jeong Dal via ntg-context
1 sibling, 0 replies; 4+ messages in thread
From: Jeong Dal via ntg-context @ 2025-01-11 21:10 UTC (permalink / raw)
To: mailing list for ConTeXt users; +Cc: Jeong Dal
[-- Attachment #1.1: Type: text/plain, Size: 6568 bytes --]
Dear Hans,
Thanks for the reply.
>> The only way is to change the file name each time which is not good to make a macro.
>> Is there a way to clear the contents in the tempList.lua before I save another table?
>
> so you add to that list?
>
I need more explanation.
In the document.truthtable(), it saves the truth table of the defined logic function to the file “tempList.lua”
table.save("tempList.lua", tfList)
Just after that I run
\processMPbuffer[Kmap_***]
In whch
lua("MP = { } MP.data = table.load('tempList.lua')");
calls the contents of the tempList.
I did this process several times using the different logic function as following.
I want that the file “tempList.lus” contains the current truth table value only not appending it to the last truth table value.
So I’d like to clear the contents of the tempList.lua after loading it in MetaFun or before calling table.save("tempList.lua", tfList)
Of the new truth table tfList.
I made a working example here.
\startMPdefinitions
numeric u,gap;
numeric uk, wd, ht;
% numeric tVal[][],a,b,c;
string val[],codeOne[],codeTwo[];
uk := LineHeight;
wd := 1.5uk;
ht := uk+5pt;
def KmapFrameNew(expr first,second) =
codeOne[0] := "0";
codeOne[1] := "1";
codeTwo[0] := "00";
codeTwo[1] := "01";
codeTwo[2] := "11";
codeTwo[3] := "10";
% label rows
label.top(textext("$" & first & "$"), (-.7wd,0));
label.top(textext("$" & second & "$"), (-.3wd,.5ht));
% drawing lines
draw (-1.2wd,0) -- origin -- ((2*(length second)+1)*wd,0);
draw (0, 1.1ht) -- origin -- (0,-(2*(length first)+.2)*ht);
draw (-wd,ht) --origin;
% label the rows
if (length first) = 1:
for i = 0,1:
label.top(textext(codeOne[i]), ((-.7wd, -(i+1)*ht)));
endfor;
else:
for i = 0 upto 3:
label.top(textext(codeTwo[i]), ((-.7wd, -(i+1)*ht)));
endfor;
fi;
%label the columns
if (length second) = 1:
for i = 0,1:
label.top(textext(codeOne[i]), ((i+1)*wd,.3ht));
endfor;
else:
for i = 0 upto 3:
label.top(textext(codeTwo[i]), ((i+1)*wd,.3ht));
endfor;
fi;
setbounds currentpicture to boundingbox currentpicture enlarged 2mm;
draw boundingbox currentpicture;
enddef;
\stopMPdefinitions
\startluacode
function document.MakeHead(p,a)
if not a then
local t = p
p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|"
a = t
end
context.starttabulate { p }
context.FL()
for i=1,#a do
context.NC() context(a[i])
end
context.NC() context.NR()
context.LL()
end
function document.MakeFooter()
context.HL()
context.stoptabulate()
end
local tf = {false, true }
local tfList = {}
local val = 0
function document.truthTable(a,func)
document.MakeHead(a)
for i,s in ipairs(tf) do
for j,t in ipairs(tf) do
for k,u in ipairs(tf) do
context.NC()
context(s and "1" or "0")
context.NC()
context(t and "1" or "0")
context.NC()
if #a == 3 then
break
end
context(u and "1" or "0")
context.NC()
val = func(s,t,u) and "1" or "0"
context(val)
context.NC()
context.AR()
table.insert(tfList, val)
-- context(#tfList)
end
if #a == 3 then
val = func(s,t) and "1" or "0"
context(val)
context.NC()
context.AR()
table.insert(tfList, val)
end
end
end
document.MakeFooter()
table.save("tempList.lua", tfList)
end
\stopluacode
\startbuffer[tbl:p2q]
\startluacode
function document.logicF(p,q)
return ((not p) or q)
end
document.truthTable({ "x", "y", "x \\to y" },document.logicF)
\stopluacode
\stopbuffer
\startbuffer[Kmap_p2qR]
numeric n,u,k ;
lua("MP = { } MP.data = table.load('tempList.lua')") ;
lua("mp.print('n := ',\#MP.data)") ;
KmapFrameNew("p","q")
for i = 0,1:
for j = 1,2:
label.top(textext(lua("MP.data[" & decimal (2*i+j) & "]")),(j*wd,-(i+1)*ht));
endfor;
endfor;
draw unitsquare xyscaled (1wd,2ht) shifted (1.5wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625blue ;
draw unitsquare xyscaled (1.8wd,ht) shifted (.6wd,-1.1ht) withpen pencircle scaled 2pt withcolor .625red ;
\stopbuffer
\startbuffer[tbl:ABC]
\startluacode
function document.logicF(p,q,r)
return (not p) and (not(p and r) or q)
end
document.truthTable({ "A", "B", "C","(\\overbar{AC}+B)\\overbar{A}"},document.logicF)
\stopluacode
\stopbuffer
\startbuffer[Kmap_ABCR]
lua("MP = { } MP.data = table.load('tempList.lua')") ;
numeric n,k ;
%lua("mp.print('n := ',\#MP.data)") ;
KmapFrameNew("A","BC")
for i = 0,1:
k := 1;
for j = 1,2,4,3:
label.top(textext(lua("MP.data[" & decimal (4*i+j) & "]")),(k*wd,-(i+1)*ht));
k := k + 1;
endfor;
endfor;
draw unitsquare xyscaled (3.6wd,ht) shifted (.7wd,-1.1ht) withpen pencircle scaled 2pt withcolor .625red ;
\stopbuffer
\startbuffer[tbl:xyz]
\startluacode
function document.logicF(p,q,r)
return (not q) and (p or r)
end
document.truthTable({ "x", "y", "z","f(x,y,x)"},document.logicF)
\stopluacode
\stopbuffer
\startbuffer[Kmap_xyzR]
lua("MP = { } MP.data = table.load('tempList.lua')") ;
numeric n,k ;
KmapFrameNew("x","yz")
for i = 0,1:
k := 1;
for j = 1,2,4,3:
label.top(textext(lua("MP.data[" & decimal (4*i+j) & "]")),(k*wd,-(i+1)*ht));
k := k + 1;
endfor;
endfor;
draw unitsquare xyscaled (1wd,2ht) shifted (1.5wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625blue ;
draw unitsquare xyscaled (1.8wd,1ht) shifted (.6wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625red ;
\stopbuffer
\starttext
\startplacetable[]
{\startcombination[distance=1.5cm,2*1]
{\getbuffer[tbl:p2q]}{진리표}
{\processMPbuffer[Kmap_p2qR]}{K-map}
\stopcombination}
\stopplacetable
\startplacetable[]
{\startcombination[distance=1.5cm,2*1]
{\getbuffer[tbl:ABC]}{진리표}
{\processMPbuffer[Kmap_ABCR]}{K-map}
\stopcombination}
\stopplacetable
\startplacetable[location=here,reference=tbl:Fxyz]
{\startcombination[distance=1.5cm,1*2]
{\getbuffer[tbl:xyz]}{}
{\processMPbuffer[Kmap_xyzR]}{}
\stopcombination}
\stopplacetable
\stoptext
[-- Attachment #1.2: Type: text/html, Size: 46908 bytes --]
[-- Attachment #2: Type: text/plain, Size: 511 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 4+ messages in thread