#
# plot_poset(P,<options>) plot the poset P using maple 2D graphics,
#  or output a description of P in the DOT language <www.graphviz.org>.
# The default layout algorithm uses 'filter' to arrange the vertices
#  into levels, and then applies 'sort' to each level of vertices.
#  The initial layout is then adjusted by `posets/layout` so that no
#  vertex intersects the interior of an edge.
#
# The <options> may include any of the following in any order...
#  proportional   use horizontal spacing between vertices that is inversely
#                 proportional to the number of vertices in each level.
#  levels=<list>  use the <list> to arrange the vertices into levels.
#                 If the levels are sets, replace them with sorted lists.
#  stretch=<num>  stretch the vertical axis by the given factor.
#  ecolor=<color> use the specified color for edges (default=red).
#  ecolor=<table> use the <table> to choose the color of each edge.
#                 Edges of P not in the table are given the default color.
#  vcolor=<color> use the specified color for vertices or their labels
#                 (default=white in V.2 or V.3; black otherwise).
#  labels         label the vertices, rather than plotting them as points.
#  labels=<table> use the entries of <table> to produce a label for each
#                 vertex (labels must be of type 'string').
#  dot            print a description of P in the DOT language, ignoring
#                 the 'proportional' and 'stretch' options.
#  dot=<file>     do the same, but write the output to <file>.
# For 'dot' plots, any option not in the above list is ignored.
# For maple plots, all other options are passed to plots[display].
#  Useful examples include title, font, symbol,...
#
# The main procedure is responsible only for parsing and sanitizing
#  the input, and then passing the problem to the appropriate plotter.
#
`posets/plot_poset`:=proc(P) local X,F,C,lbl,eclr,vclr,i,x,v,opts,str;
  if nargs>1 and type(args[2],'integer') then X:={$1..args[2]}; i:=3
    elif nargs>1 and type(args[2],'set') then X:=args[2]; i:=3
    else X:=map(op,P); i:=2 fi;
  eclr:=eval(`posets/default`['ecolor'],1);
  vclr:=eval(`posets/default`['vcolor'],1);
  F:=false; str:=2.0; lbl:=false; C:=table();
  opts:=['scaling'='constrained','axes'='none'];
  for v in [args[i..nargs]] do
    if op(1,v)='levels' then F:=op(2,v);
      if X<>{op(map(op,F))} then
        ERROR(`level partition does not match vertex set`) fi
    elif op(1,v)='proportional' then str:=-str
    elif op(1,v)='stretch' then str:=str*op(2,v)
    elif op(1,v)='vcolor' then vclr:=op(2,v)
    elif op(1,v)='ecolor' then
      if type(op(2,v),'table') then C:=op(2,v) else eclr:=op(2,v) fi
    elif op(1,v)='labels' then
      if nops(v)>1 then lbl:=op(op(2,v));
        for x in X do if not assigned(lbl[x]) then
          ERROR(`missing vertex label(s)`) fi od
        else lbl:=table([seq(x=convert(x,'string'),x=X)]) fi
    elif op(1,v)='dot' then opts:=[op(v)]
    elif opts[1]<>'dot' then opts:=[op(opts),v] fi
  od;
  C:=`posets/plot_poset/ct`(P,C,eclr);
  if opts[1]='dot' then `posets/dotplot`(opts,C,X,F,vclr,lbl) else
    if F=false then F:=posets['filter'](P,X) fi;
    F:=map(y->if type(y,'set') then sort([op(y)]) else y fi,F);
    `posets/plot_poset/2d`(P,X,F,str,vclr,C,lbl,opts)
  fi
end:
#
# Extract and sanitize the color/edge table.
#
`posets/plot_poset/ct`:=proc(P,C,eclr) local new,cols,Q,i;
  cols:=map(op,[indices(C)]);
  new:=traperror([seq(C[i] intersect P,i=cols),{}]);
  if new=lasterror then ERROR(`invalid color table`) fi;
  cols:=[op(cols),eclr]; Q:=P minus map(op,{op(new)});
  if not member(eclr,cols,'i') then ERROR(`this cannot happen`) fi;
  new:=subsop(i={op(Q),op(new[i])},new);
  new:=zip((u,v)->if nops(v)>0 then [u,v] fi,cols,new);
  sort(new,(u,v)->evalb(nops(u[2])>=nops(v[2])))
end:
#
# Take the parsed input and make a Maple 2D plot
#
`posets/plot_poset/2d`:=proc(P,X,F,str,vclr,C,lbl) local r,pt,p,i,j;
  if str<0 then r:=-str/nops(F);
    pt:=[seq([seq((2*j-1)/i,j=1..i)],i=map(nops,F))]
  else r:=str;
    pt:=[seq([seq(2*j-i,j=1..i)],i=map(nops,F))] fi;
  pt:=`posets/layout`(P,F,evalb(str<0),pt);
  pt:=table([seq(i[1]=[1.0*i[2],r*i[3]],i=pt)]);
  if type(lbl,'table') then
    p:=plots['textplot']([seq([op(pt[i]),lbl[i]],i=X)],'color'=vclr)
  else
    p:=plot([seq(pt[i],i=X)],'color'=vclr,'style'='point') fi;
  p:=p,seq(plot({seq([pt[i[1]],pt[i[2]]],i=j[2])},'color'=j[1]),j=C);
  plots['display']({p},op(args[8]))
end:
#
save `posets/plot_poset`, `posets/plot_poset/2d`, `posets/plot_poset/ct`,
  cat(HomeLib,`/posets/plot_poset.m`);
read cat(HomeLib,`/help/posets/plot_poset`);
quit;

