Symfonion.java

1
package com.github.dakusui.symfonion.core;
2
3
import com.github.dakusui.logias.lisp.Context;
4
import com.github.dakusui.symfonion.compat.json.CompatJsonException;
5
import com.github.dakusui.symfonion.compat.json.CompatJsonUtils;
6
import com.github.dakusui.symfonion.compat.json.JsonInvalidPathException;
7
import com.github.dakusui.symfonion.compat.json.JsonPathNotFoundException;
8
import com.github.dakusui.symfonion.song.*;
9
import com.github.dakusui.symfonion.utils.Utils;
10
import com.google.gson.JsonArray;
11
import com.google.gson.JsonElement;
12
import com.google.gson.JsonObject;
13
import com.google.gson.JsonSyntaxException;
14
15
import javax.sound.midi.InvalidMidiDataException;
16
import javax.sound.midi.Sequence;
17
import java.io.File;
18
import java.util.HashMap;
19
import java.util.Map;
20
import java.util.function.Predicate;
21
22
import static com.github.dakusui.symfonion.compat.exceptions.CompatExceptionThrower.*;
23
import static com.github.dakusui.symfonion.compat.exceptions.ExceptionContext.entry;
24
import static com.github.dakusui.symfonion.song.CompatSong.Builder.loadMidiDeviceProfile;
25
26
public class Symfonion {
27
  private JsonObject json;
28
29
  public Symfonion() {
30
  }
31
32
  public Song loadSong(String fileName, Predicate<Measure> barFilter, Predicate<String> partFilter) {
33
    Song ret;
34
    try (var ignored = exceptionContext(entry(ContextKey.SOURCE_FILE, new File(fileName)))) {
35
      try {
36
        this.json = loadSymfonionFile(fileName, new HashMap<>());
37
        ret       = new Song.Builder(json).measureFilter(barFilter)
38
                                          .partFilter(partFilter)
39
                                          .build();
40
      } catch (JsonSyntaxException e) {
41
        throw loadFileException(e.getCause());
42
      } catch (IllegalStateException e) {
43
        throw loadFileException(e);
44
      } catch (JsonPathNotFoundException e) {
45
        throw requiredElementMissingException(e.getProblemCausingNode(), this.json, CompatJsonUtils.formatPath(e.getPath()));
46
      } catch (CompatJsonException e) {
47
        throw new RuntimeException(e.getMessage(), e);
48
      }
49
    }
50 1 1. loadSong : replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::loadSong → NO_COVERAGE
    return ret;
51
  }
52
53
  public CompatSong load(String fileName, Predicate<Bar> barFilter, Predicate<String> partFilter) {
54
    CompatSong ret;
55
    try (var ignored = exceptionContext(entry(ContextKey.SOURCE_FILE, new File(fileName)))) {
56
      try {
57
        this.json = loadSymfonionFile(fileName, new HashMap<>());
58
        ret       = new CompatSong.Builder(json).barFilter(barFilter)
59
                                                .partFilter(partFilter)
60
                                                .build();
61
      } catch (JsonSyntaxException e) {
62
        throw loadFileException(e.getCause());
63
      } catch (IllegalStateException e) {
64
        throw loadFileException(e);
65
      } catch (JsonPathNotFoundException e) {
66
        throw requiredElementMissingException(e.getProblemCausingNode(), this.json, CompatJsonUtils.formatPath(e.getPath()));
67
      } catch (CompatJsonException e) {
68
        throw new RuntimeException(e.getMessage(), e);
69
      }
70
    }
71 1 1. load : replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::load → KILLED
    return ret;
72
  }
73
74
  private static JsonObject loadSymfonionFile(String fileName, Map<String, JsonObject> alreadyReadFiles) {
75 2 1. loadSymfonionFile : replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::loadSymfonionFile → NO_COVERAGE
2. loadSymfonionFile : negated conditional → KILLED
    if (alreadyReadFiles.containsKey(fileName)) return alreadyReadFiles.get(fileName);
76
    JsonObject ret = CompatJsonUtils.toJson(Utils.loadFile(fileName)).getAsJsonObject();
77 1 1. loadSymfonionFile : negated conditional → KILLED
    if (ret.has(Keyword.include.name())) {
78
      File      dir           = new File(fileName).getParentFile();
79
      JsonArray includedFiles = CompatJsonUtils.asJsonArray(ret, Keyword.include.name());
80
      int       i             = 0;
81
      for (JsonElement each : includedFiles) {
82
        String eachFileName = CompatJsonUtils.asString(each);
83 1 1. loadSymfonionFile : negated conditional → NO_COVERAGE
        if (eachFileName == null) {
84
          throw new JsonInvalidPathException(ret, new Object[]{Keyword.include, i});
85
        }
86
        String     eachAbsFileName = new File(dir, eachFileName).getAbsolutePath();
87
        JsonObject included        = CompatJsonUtils.toJson(Utils.loadFile(eachAbsFileName)).getAsJsonObject();
88
        alreadyReadFiles.put(eachAbsFileName, included);
89
        ret = CompatJsonUtils.merge(ret, included);
90 1 1. loadSymfonionFile : Changed increment from 1 to -1 → NO_COVERAGE
        i++;
91
      }
92
    }
93 1 1. loadSymfonionFile : replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::loadSymfonionFile → KILLED
    return ret;
94
  }
95
96
  /**
97
   * Compiles a {@link CompatSong} object into a map of a part name to {@link Sequence} object.
98
   *
99
   * @param song          A song object.
100
   * @param logiasContext
101
   * @return A map from part name to a MIDI sequence object.
102
   */
103
  @Deprecated
104
  public Map<String, Sequence> compile(CompatSong song, Context logiasContext) {
105
    MidiCompiler          compiler = new MidiCompiler(loadMidiDeviceProfile(song.rootJsonObject(), logiasContext));
106
    Map<String, Sequence> ret;
107
    try {
108
      ret = compiler.compile(song);
109
    } catch (InvalidMidiDataException e) {
110
      throw compilationException("Failed to compile a song.", e);
111
    }
112 1 1. compile : replaced return value with Collections.emptyMap for com/github/dakusui/symfonion/core/Symfonion::compile → SURVIVED
    return ret;
113
  }
114
115
  /**
116
   * Compiles a {@link CompatSong} object into a map of a part name to {@link Sequence} object.
117
   *
118
   * @param song          A song object.
119
   * @param logiasContext
120
   * @return A map from part name to a MIDI sequence object.
121
   */
122
  public Map<String, Sequence> compileSong(Song song, Context logiasContext) {
123
    MidiCompiler          compiler = new MidiCompiler(loadMidiDeviceProfile(song.rootJsonObject(), logiasContext));
124
    Map<String, Sequence> ret;
125
    try {
126
      ret = compiler.compile(song);
127
    } catch (InvalidMidiDataException e) {
128
      throw compilationException("Failed to compile a song.", e);
129
    }
130 1 1. compileSong : replaced return value with Collections.emptyMap for com/github/dakusui/symfonion/core/Symfonion::compileSong → NO_COVERAGE
    return ret;
131
  }
132
}

Mutations

50

1.1
Location : loadSong
Killed by : none
replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::loadSong → NO_COVERAGE

71

1.1
Location : load
Killed by : com.github.dakusui.symfonion.tests.InvalidJsonErrorTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.tests.InvalidJsonErrorTest]/[method:missingSection_parts()]
replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::load → KILLED

75

1.1
Location : loadSymfonionFile
Killed by : com.github.dakusui.symfonion.tests.MalformedTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.tests.MalformedTest]/[method:givenMalformed_emptyFile()]
negated conditional → KILLED

2.2
Location : loadSymfonionFile
Killed by : none
replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::loadSymfonionFile → NO_COVERAGE

77

1.1
Location : loadSymfonionFile
Killed by : com.github.dakusui.symfonion.tests.InvalidJsonErrorTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.tests.InvalidJsonErrorTest]/[method:invalid_01()]
negated conditional → KILLED

83

1.1
Location : loadSymfonionFile
Killed by : none
negated conditional → NO_COVERAGE

90

1.1
Location : loadSymfonionFile
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

93

1.1
Location : loadSymfonionFile
Killed by : com.github.dakusui.symfonion.tests.InvalidJsonErrorTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.tests.InvalidJsonErrorTest]/[method:invalid_01()]
replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::loadSymfonionFile → KILLED

112

1.1
Location : compile
Killed by : none
replaced return value with Collections.emptyMap for com/github/dakusui/symfonion/core/Symfonion::compile → SURVIVED
Covering tests

130

1.1
Location : compileSong
Killed by : none
replaced return value with Collections.emptyMap for com/github/dakusui/symfonion/core/Symfonion::compileSong → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.19.1