001package org.clafer.ig;
002
003import java.util.ArrayList;
004import java.util.List;
005
006/**
007 * An instance of a model, as claferIG's {@code ClaferModel.hs} has it: its
008 * top-level clafers, each with its children. A state of a trace is one too.
009 *
010 * @param topLevel the top-level clafers
011 */
012public record ClaferModel(List<Clafer> topLevel) {
013
014    public ClaferModel {
015        topLevel = List.copyOf(topLevel);
016    }
017
018    /**
019     * The tuple of name and ordinal is globally unique.
020     */
021    public record Id(String name, int ordinal) implements Comparable<Id> {
022
023        @Override
024        public int compareTo(Id o) {
025            int c = name.compareTo(o.name);
026            return c != 0 ? c : Integer.compare(ordinal, o.ordinal);
027        }
028    }
029
030    /**
031     * The value of a clafer: a clafer it refers to, an integer or a string.
032     */
033    public sealed interface Value {
034    }
035
036    public record AliasValue(Id alias) implements Value {
037    }
038
039    public record IntValue(int value) implements Value {
040    }
041
042    public record StringValue(String value) implements Value {
043    }
044
045    /**
046     * An instance of a clafer: its name, the UID of its clafer, its value
047     * (or null) and its children.
048     */
049    public record Clafer(Id id, String uid, Value value, List<Clafer> children) {
050
051        public Clafer {
052            children = List.copyOf(children);
053        }
054
055        public String name() {
056            return id.name();
057        }
058    }
059
060    /**
061     * All the clafers, each before its children.
062     */
063    public List<Clafer> traverse() {
064        List<Clafer> out = new ArrayList<>();
065        topLevel.forEach(c -> traverse(c, out));
066        return out;
067    }
068
069    private static void traverse(Clafer c, List<Clafer> out) {
070        out.add(c);
071        c.children().forEach(x -> traverse(x, out));
072    }
073
074    /**
075     * The text of the instance: each clafer on a line, indented by its depth.
076     */
077    @Override
078    public String toString() {
079        StringBuilder sb = new StringBuilder();
080        topLevel.forEach(c -> show(c, "", sb));
081        return sb.toString();
082    }
083
084    /**
085     * The text of a clafer and its children, as {@code show} gives it.
086     */
087    public static String show(Clafer c) {
088        StringBuilder sb = new StringBuilder();
089        show(c, "", sb);
090        return sb.toString();
091    }
092
093    private static void show(Clafer c, String indent, StringBuilder sb) {
094        sb.append(indent).append(c.id().name());
095        if (c.value() != null) {
096            sb.append(showValue(c.value()));
097        }
098        sb.append('\n');
099        c.children().forEach(x -> show(x, indent + "  ", sb));
100    }
101
102    static String showValue(Value v) {
103        return switch (v) {
104            case AliasValue a -> " -> " + a.alias().name();
105            case IntValue i -> " -> " + i.value();
106            case StringValue s -> " -> " + s.value();
107        };
108    }
109}