Symfonion.java

1
package com.github.dakusui.symfonion.core;
2
3
import com.github.dakusui.json.JsonException;
4
import com.github.dakusui.json.JsonInvalidPathException;
5
import com.github.dakusui.json.JsonPathNotFoundException;
6
import com.github.dakusui.json.JsonUtils;
7
import com.github.dakusui.logias.lisp.Context;
8
import com.github.dakusui.symfonion.song.Keyword;
9
import com.github.dakusui.symfonion.song.Song;
10
import com.github.dakusui.symfonion.utils.Utils;
11
import com.google.gson.JsonArray;
12
import com.google.gson.JsonElement;
13
import com.google.gson.JsonObject;
14
import com.google.gson.JsonSyntaxException;
15
16
import javax.sound.midi.InvalidMidiDataException;
17
import javax.sound.midi.Sequence;
18
import java.io.File;
19
import java.util.HashMap;
20
import java.util.Map;
21
22
import static com.github.dakusui.symfonion.exceptions.ExceptionThrower.*;
23
24
public class Symfonion {
25
  Context logiasContext;
26
  private JsonObject json;
27
  
28
  public Symfonion(Context logiasContext) {
29
    this.logiasContext = logiasContext;
30
  }
31
  
32
  public Song load(String fileName)  {
33
    Song ret;
34
    try (var ignored = context($(ContextKey.SOURCE_FILE, new File(fileName)))) {
35
      try {
36
        this.json = loadSymfonionFile(fileName, new HashMap<>());
37
        ret = new Song.Builder(logiasContext, json).build();
38
      } catch (JsonSyntaxException e) {
39
        throw loadFileException(e.getCause());
40
      } catch (IllegalStateException e) {
41
        throw loadFileException(e);
42
      } catch (JsonPathNotFoundException e) {
43
        throw requiredElementMissingException(e.getProblemCausingNode(), this.json, JsonUtils.formatPath(e.getPath()));
44
      } catch (JsonException e) {
45
        throw new RuntimeException(e.getMessage(), e);
46
      }
47
    }
48 1 1. load : replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::load → KILLED
    return ret;
49
  }
50
  
51
  private static JsonObject loadSymfonionFile(String fileName, Map<String, JsonObject> alreadyReadFiles) {
52 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);
53
    JsonObject ret = JsonUtils.toJson(Utils.loadFile(fileName)).getAsJsonObject();
54 1 1. loadSymfonionFile : negated conditional → KILLED
    if (ret.has(Keyword.$include.name())) {
55
      File dir = new File(fileName).getParentFile();
56
      JsonArray includedFiles = JsonUtils.asJsonArray(ret, Keyword.$include.name());
57
      int i = 0;
58
      for (JsonElement each : includedFiles) {
59
        String eachFileName = JsonUtils.asString(each);
60 1 1. loadSymfonionFile : negated conditional → NO_COVERAGE
        if (eachFileName == null) {
61
          throw new JsonInvalidPathException(ret, new Object[]{Keyword.$include, i});
62
        }
63
        String eachAbsFileName = new File(dir, eachFileName).getAbsolutePath();
64
        JsonObject included = JsonUtils.toJson(Utils.loadFile(eachAbsFileName)).getAsJsonObject();
65
        alreadyReadFiles.put(eachAbsFileName, included);
66
        ret = JsonUtils.merge(ret, included);
67 1 1. loadSymfonionFile : Changed increment from 1 to -1 → NO_COVERAGE
        i++;
68
      }
69
    }
70 1 1. loadSymfonionFile : replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::loadSymfonionFile → KILLED
    return ret;
71
  }
72
  
73
  public Map<String, Sequence> compile(Song song)  {
74
    MidiCompiler compiler = new MidiCompiler(song.getLogiasContext());
75
    Map<String, Sequence> ret;
76
    try {
77
      ret = compiler.compile(song);
78
    } catch (InvalidMidiDataException e) {
79
      throw compilationException("Failed to compile a song.", e);
80
    }
81 1 1. compile : replaced return value with Collections.emptyMap for com/github/dakusui/symfonion/core/Symfonion::compile → SURVIVED
    return ret;
82
  }
83
}

Mutations

48

1.1
Location : load
Killed by : com.github.dakusui.symfonion.tests.InvalidJsonErrorTest.missingSection_parts(com.github.dakusui.symfonion.tests.InvalidJsonErrorTest)
replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::load → KILLED

52

1.1
Location : loadSymfonionFile
Killed by : com.github.dakusui.symfonion.tests.MalformedTest.givenMalformed_brokenObject(com.github.dakusui.symfonion.tests.MalformedTest)
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

54

1.1
Location : loadSymfonionFile
Killed by : com.github.dakusui.symfonion.tests.InvalidJsonErrorTest.invalid_01(com.github.dakusui.symfonion.tests.InvalidJsonErrorTest)
negated conditional → KILLED

60

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

67

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

70

1.1
Location : loadSymfonionFile
Killed by : com.github.dakusui.symfonion.tests.InvalidJsonErrorTest.invalid_01(com.github.dakusui.symfonion.tests.InvalidJsonErrorTest)
replaced return value with null for com/github/dakusui/symfonion/core/Symfonion::loadSymfonionFile → KILLED

81

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

Active mutators

Tests examined


Report generated by PIT 1.15.3