Skip to content

Commit

Permalink
[BUG] Make jar indepdendent of sources (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
demid5111 committed Mar 27, 2022
1 parent 6272202 commit d1f640f
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ build/
gradle.properties

*.log
artifacts/
4 changes: 2 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ Example launch:
1. macOS:

```bash
./gradlew run -PappArgs="['-i', 'PATH_TO_PROJECT_ROOT/src/main/resources/description_multilevel.json', '-o', 'run.log']"
./gradlew run -PappArgs="['-i', 'PATH_TO_PROJECT_ROOT/src/main/resources/description_multilevel.json', '-o', 'artifacts']"
```

2. Windows:
```bash
.\gradlew run --args='-i PATH_TO_PROJECT_ROOT\src\main\resources\description_multilevel.json -o run.log'
.\gradlew run --args='-i PATH_TO_PROJECT_ROOT\src\main\resources\description_multilevel.json -o artifacts'
```

### How to use as a jar
Expand All @@ -28,5 +28,5 @@ Example launch:
While *lingvo-dss-sources.jar* contains sources of the library.
4. Run the jar on the given description:
```bash
java -jar .\build\libs\lingvo-dss-all.jar -i .\src\main\resources\description_multilevel.json -o run.log
java -jar .\build\libs\lingvo-dss-all.jar -i .\src\main\resources\description_multilevel.json -o artifacts
```
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ repositories {
mavenCentral()
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

dependencies {
implementation(
[group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.4.1'],
Expand Down
43 changes: 33 additions & 10 deletions src/main/java/dss/lingvo/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
import dss.lingvo.samples.TT2HFLTSCoordinator;
import org.apache.commons.cli.*;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

private Main(){}

public static void main(String[] args) throws IOException {
Options options = new Options();

Option input = new Option("i", "input", true, "input file path");
input.setRequired(true);
Option input2 = new Option("o", "output", true, "output file path");
Option input2 = new Option("o", "output-dir", true,
"output directory containing decision making artifacts");
input2.setRequired(true);

options.addOption(input);
options.addOption(input2);

CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
CommandLine cmd = null;

try {
cmd = parser.parse(options, args);
Expand All @@ -32,16 +34,37 @@ public static void main(String[] args) throws IOException {
formatter.printHelp("utility-name", options);

System.exit(1);
}

String inputFilePathRaw = cmd.getOptionValue("input");
String outputDirectoryPathRaw = cmd.getOptionValue("output-dir");

File inputFile = new File(inputFilePathRaw);
if (!inputFile.exists()) {
System.out.println("Specified file with task description does not exist. Specify a real description");
System.exit(1);
}

File outputDirectory = new File(outputDirectoryPathRaw);

if (!outputDirectory.exists()) {
System.out.println("Specified output directory does not exist. Need to create it first");
Files.createDirectories(outputDirectory.toPath());
}


if (outputDirectory.exists() && !outputDirectory.isDirectory()) {
System.out.println("The path provided as an output directory is not a directory. Specify a real folder.");
System.exit(1);
return;
}

String inputFilePath = cmd.getOptionValue("input");
String outputFilePath = cmd.getOptionValue("output");
Path outputFilePath = Paths.get(outputDirectory.toString(), "out.log");

PrintStream out = new PrintStream(outputFilePath, "UTF-8");
PrintStream out = new PrintStream(outputFilePath.toString(), "UTF-8");
System.setOut(out);

TT2HFLTSCoordinator complextT2HFLTSCoordinator = new TT2HFLTSCoordinator();
complextT2HFLTSCoordinator.go(inputFilePath);
TT2HFLTSCoordinator complexT2HFLTSCoordinator = new TT2HFLTSCoordinator();
complexT2HFLTSCoordinator.go(inputFile, outputDirectory);
}
}
110 changes: 55 additions & 55 deletions src/main/java/dss/lingvo/samples/TT2HFLTSCoordinator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
import dss.lingvo.t2hflts.TT2HFLTSMHTWOWAOperator;
import dss.lingvo.t2hflts.multilevel.TT2HFLTSMHTWOWAMultiLevelOperator;
import dss.lingvo.utils.TTJSONUtils;
import dss.lingvo.utils.TTReportUtils;
import dss.lingvo.utils.TTUtils;
import dss.lingvo.utils.models.input.TTAlternativeModel;
import dss.lingvo.utils.models.input.multilevel.TTJSONMultiLevelInputModel;
import dss.lingvo.utils.models.input.singlelevel.TTJSONInputModel;
import dss.lingvo.utils.models.output.TTJSONOutputModel;
import javafx.util.Pair;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class TT2HFLTSCoordinator {
private TTUtils log = TTUtils.getInstance();
private final TTJSONUtils ttjsonReader = TTJSONUtils.getInstance();

public void go(String inputFilePath) throws IOException {
TTJSONUtils ttjsonReader = TTJSONUtils.getInstance();
private void processSimpleCase(File outputDirectory) throws IOException {
TTJSONInputModel ttjsonModel = ttjsonReader.readJSONDescription("description_from_article.json");

if (ttjsonModel == null) {
Expand All @@ -35,7 +36,6 @@ public void go(String inputFilePath) throws IOException {
// 1. register all scales
TTNormalizedTranslator.registerScalesBatch(ttjsonModel.getScales());


// MHTWOWA example

// 1. Gather feedback and translate directly to the TT2HFLTS (per each expert)
Expand All @@ -57,13 +57,15 @@ public void go(String inputFilePath) throws IOException {

// now need to make the calculation for every alternative
TT2HFLTSMHTWOWAOperator tt2HFLTSMHTWOWAOperator = new TT2HFLTSMHTWOWAOperator();
List<TT2HFLTS> altOverall = tt2HFLTSMHTWOWAOperator.calculate(numAlternatives,
numExperts, p, w, aggEstAll, 7);
List<TT2HFLTS> altOverall = tt2HFLTSMHTWOWAOperator.calculate(numAlternatives, numExperts, p, w, aggEstAll, 7);

TTJSONOutputModel res = TTUtils.prepareAllResultsForJSON(altOverall, ttjsonModel, 7);
// now output results
TTJSONUtils.getInstance().writeResultToJSON("build/resources/result.json", res);
Path outputJSONFilePath = Paths.get(outputDirectory.toString(), "result_simple.json");
TTJSONUtils.getInstance().writeResultToJSON(outputJSONFilePath, res);
}

private void processNumericSample() {
// now how to work with numbers
int targetScaleSize = 7;

Expand All @@ -72,7 +74,9 @@ public void go(String inputFilePath) throws IOException {
float resTranslation = TTNormalizedTranslator.getInstance().getTranslationFromFuzzySet(fSet);
TTTuple resTuple = TTNormalizedTranslator.getInstance().getTTupleForNumericTranslation(resTranslation, 5);
System.out.printf("Translating %f to 2-tuple: %s\n", numeric_assessment, resTuple.toString());
}

private void processweightsGenerationSample() {
float[] expDistr = {0.8f, 0.2f};
float[] resVector = TTUtils.calculateWeightsVector(expDistr, 4);
System.out.println(Arrays.toString(resVector));
Expand All @@ -82,11 +86,10 @@ public void go(String inputFilePath) throws IOException {
sum += v;
}
System.out.printf("Sum of weights should be 1. Actual sum is: %f", sum);
}

//---------------------
// Enable multilevel task solving

float [] ww = {0.33f,0.33f,0.33f};
private void processMHTWASample() {
float[] ww = {0.33f, 0.33f, 0.33f};
List<TTTuple> l = new ArrayList<>();
l.add(new TTTuple("p", 9, 0f, 3));
TT2HFLTS el1 = new TT2HFLTS(l);
Expand All @@ -102,66 +105,63 @@ public void go(String inputFilePath) throws IOException {
ll.add(el3);
TT2HFLTSMHTWAOperator op = new TT2HFLTSMHTWAOperator();
TT2HFLTS rr = op.calculate(ll, ww, 9);
}

private void processMultiLevelAdvancedSample(File inputFile, File outputDirectory) throws IOException {
int targetScaleSize = 7;

TTJSONMultiLevelInputModel model = ttjsonReader.readJSONMultiLevelDescription(inputFilePath, false);
TTJSONMultiLevelInputModel model = ttjsonReader.readJSONMultiLevelDescription(inputFile, false);
List<ArrayList<ArrayList<TT2HFLTS>>> all = TTUtils.getAllEstimationsFromMultiLevelJSONModel(model, 7);

// Step 1. Aggregate by abstraction level
TT2HFLTSMHTWOWAMultiLevelOperator tt2HFLTSMHTWOWAMultiLevelOperator = new TT2HFLTSMHTWOWAMultiLevelOperator();
List<ArrayList<ArrayList<TT2HFLTS>>> allByLevel = tt2HFLTSMHTWOWAMultiLevelOperator
.aggregateByAbstractionLevel(model.getCriteria(),
model.getAbstractionLevels(),
all,
targetScaleSize);

List<ArrayList<ArrayList<TT2HFLTS>>> allByExpert = tt2HFLTSMHTWOWAMultiLevelOperator
.transposeByAbstractionLevel(model.getAbstractionLevels().size(),
model.getAlternatives().size(),
model.getExperts().size(),
allByLevel);
List<ArrayList<ArrayList<TT2HFLTS>>> allByLevel = tt2HFLTSMHTWOWAMultiLevelOperator.aggregateByAbstractionLevel(model.getCriteria(), model.getAbstractionLevels(), all, targetScaleSize);

List<ArrayList<ArrayList<TT2HFLTS>>> allByExpert = tt2HFLTSMHTWOWAMultiLevelOperator.transposeByAbstractionLevel(model.getAbstractionLevels().size(), model.getAlternatives().size(), model.getExperts().size(), allByLevel);

float[] a = new float[model.getExpertWeightsRule().values().size()];
float curMax = 0f;
for (Map.Entry<String, Float> e: model.getExpertWeightsRule().entrySet()){
if (e.getKey().equals("1")){
for (Map.Entry<String, Float> e : model.getExpertWeightsRule().entrySet()) {
if (e.getKey().equals("1")) {
curMax = e.getValue();
break;
}
}
a[0] = curMax;
a[1] = 1-curMax;
List<ArrayList<TT2HFLTS>> altToLevel = tt2HFLTSMHTWOWAMultiLevelOperator
.aggregateByExpert(model.getAbstractionLevels().size(),
model.getAlternatives().size(),
7,
allByExpert,
a);

List<TT2HFLTS> altVec = tt2HFLTSMHTWOWAMultiLevelOperator
.aggregateFinalAltEst(7,
altToLevel);

List<Pair<String, TT2HFLTS>> resZippedVec = IntStream.range(0, altVec.size())
.mapToObj(i -> new Pair<>(model.getAlternatives().get(i).getAlternativeID(), altVec.get(i)))
.collect(Collectors.toList());

Collections.sort(resZippedVec, Collections.reverseOrder(new Comparator<Pair<String, TT2HFLTS>>() {
@Override
public int compare(Pair<String, TT2HFLTS> o1, Pair<String, TT2HFLTS> o2) {
return TTUtils.compareTT2HFLTS(o1.getValue(), o2.getValue());
}
}));
a[1] = 1 - curMax;
List<ArrayList<TT2HFLTS>> altToLevel = tt2HFLTSMHTWOWAMultiLevelOperator.aggregateByExpert(model.getAbstractionLevels().size(), model.getAlternatives().size(), 7, allByExpert, a);

List<TT2HFLTS> altVec = tt2HFLTSMHTWOWAMultiLevelOperator.aggregateFinalAltEst(7, altToLevel);

// all below is just processing

// saving to file
TTJSONOutputModel res = TTUtils.prepareAllResultsForJSON(altVec, model, 7);
Path outputJSONFilePath = Paths.get(outputDirectory.toString(), "result.json");
TTJSONUtils.getInstance().writeResultToJSON(outputJSONFilePath, res);


// printing to console
List<Pair<String, TT2HFLTS>> resZippedVec = IntStream.range(0, altVec.size()).mapToObj(i -> new Pair<>(model.getAlternatives().get(i).getAlternativeID(), altVec.get(i))).collect(Collectors.toList());

resZippedVec.sort(Collections.reverseOrder((o1, o2) -> TTUtils.compareTT2HFLTS(o1.getValue(), o2.getValue())));

System.out.println("\n\n\n[MULTILEVEL] [REPORT] Aggregation results");
for (Pair<String, TT2HFLTS> stringTT2HFLTSPair: resZippedVec){
TTAlternativeModel altInstance = model.getAlternatives()
.stream()
.filter((TTAlternativeModel ttAlternativeModel) -> ttAlternativeModel.getAlternativeID()
.equals(stringTT2HFLTSPair.getKey()))
.findFirst()
.orElse(null);
for (Pair<String, TT2HFLTS> stringTT2HFLTSPair : resZippedVec) {
TTAlternativeModel altInstance = model.getAlternatives().stream().filter((TTAlternativeModel ttAlternativeModel) -> ttAlternativeModel.getAlternativeID().equals(stringTT2HFLTSPair.getKey())).findFirst().orElse(null);
System.out.println(stringTT2HFLTSPair.getKey() + ' ' + altInstance.getAlternativeName());
}
}

public void go(File inputFile, File outputDirectory) throws IOException {
processSimpleCase(outputDirectory);

processNumericSample();

processweightsGenerationSample();

processMHTWASample();

processMultiLevelAdvancedSample(inputFile, outputDirectory);
}
}
14 changes: 11 additions & 3 deletions src/main/java/dss/lingvo/t2/TTNormalizedTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,23 @@ public List<Float> getFuzzySetForNumericEstimation(float estimation, int targetS
float b = range[1];
float d = range[2];
float c = range[3];
Float tmp = null;
float tmp;
if ((a > estimation) || (c < estimation)) {
tmp = 0f;
} else if (estimation <= b) {
tmp = (estimation - a) / (b - a);
if ((b - a) == 0) {
tmp = 0f;
} else {
tmp = (estimation - a) / (b - a);
}
} else if (estimation <= d) {
tmp = 1f;
} else {
tmp = (c - estimation) / (c - d);
if ((c - d) == 0) {
tmp = 0f;
} else {
tmp = (c - estimation) / (c - d);
}
}
resFuzzySet.add(tmp);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/dss/lingvo/utils/TTJSONUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;

public class TTJSONUtils {
private static TTJSONUtils ttjsonReader = new TTJSONUtils();
Expand All @@ -23,22 +24,21 @@ public TTJSONInputModel readJSONDescription(String fileName) throws IOException
return mapper.readValue(modelFileStream, TTJSONInputModel.class);
}

public TTJSONMultiLevelInputModel readJSONMultiLevelDescription(String fileName, boolean isResourceFile) throws IOException {
public TTJSONMultiLevelInputModel readJSONMultiLevelDescription(File file, boolean isResourceFile) throws IOException {
ObjectMapper mapper = new ObjectMapper();

if (isResourceFile) {
InputStream modelFileStream = getClass().getResourceAsStream('/'+fileName);
InputStream modelFileStream = getClass().getResourceAsStream('/'+file.toString());
return mapper.readValue(modelFileStream, TTJSONMultiLevelInputModel.class);
}

File file = new File(fileName);
return mapper.readValue(file, TTJSONMultiLevelInputModel.class);
}

public void writeResultToJSON(String fileName, TTJSONOutputModel jsonString) throws IOException {
public void writeResultToJSON(Path filePath, TTJSONOutputModel jsonString) throws IOException {
ObjectMapper mapper = new ObjectMapper();

// Convert object to JSON string and save into a file directly
mapper.writeValue(new File(fileName), jsonString);
mapper.writeValue(new File(filePath.toString()), jsonString);
}
}
Loading

0 comments on commit d1f640f

Please sign in to comment.