aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/hu.bme.mit.inf.dslreasoner.domains.cps/xtend-gen/hu/bme/mit/inf/dslreasoner/domains/cps/cplex/CbcCpsMain.java
blob: a6ff0d0e85cd89a92a10c7e6deae2925eb56f93e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package hu.bme.mit.inf.dslreasoner.domains.cps.cplex;

import hu.bme.mit.inf.dslreasoner.domains.cps.CpsPackage;
import hu.bme.mit.inf.dslreasoner.domains.cps.CyberPhysicalSystem;
import hu.bme.mit.inf.dslreasoner.domains.cps.Request;
import hu.bme.mit.inf.dslreasoner.domains.cps.Requirement;
import hu.bme.mit.inf.dslreasoner.domains.cps.cplex.CpsToLpTranslator;
import hu.bme.mit.inf.dslreasoner.domains.cps.generator.CpsGenerator;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;

@SuppressWarnings("all")
public class CbcCpsMain {
  private static final String PROBLEM_FILE = "problem.lp";
  
  private static final String SOLUTION_FILE = "solution.txt";
  
  private static final Pattern VALUE_REGEX = Pattern.compile("Optimal - objective value\\s*([0-9]+(\\.[0-9]+)?)");
  
  private CbcCpsMain() {
    new IllegalStateException("This is a static utility class and should not be instantiated directly.");
  }
  
  public static void main(final String[] args) {
    try {
      Map<String, Object> _extensionToFactoryMap = Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap();
      XMIResourceFactoryImpl _xMIResourceFactoryImpl = new XMIResourceFactoryImpl();
      _extensionToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION, _xMIResourceFactoryImpl);
      EPackage.Registry.INSTANCE.put(CpsPackage.eNS_URI, CpsPackage.eINSTANCE);
      final CpsGenerator generator = new CpsGenerator(1, 4, 1);
      final CyberPhysicalSystem problem = generator.generateCpsProblem();
      final CpsToLpTranslator toLp = new CpsToLpTranslator(problem, 10, true);
      final CharSequence lp = toLp.getLpProblem();
      FileWriter _fileWriter = new FileWriter(CbcCpsMain.PROBLEM_FILE);
      final BufferedWriter writer = new BufferedWriter(_fileWriter);
      try {
        writer.append(lp);
      } finally {
        writer.close();
      }
      final Process process = new ProcessBuilder().inheritIO().command("cbc", CbcCpsMain.PROBLEM_FILE, "solve", "solu", CbcCpsMain.SOLUTION_FILE).start();
      boolean _waitFor = process.waitFor(120, TimeUnit.SECONDS);
      boolean _not = (!_waitFor);
      if (_not) {
        System.err.println("Timeout reached");
        process.destroyForcibly();
        System.exit((-1));
      }
      int _exitValue = process.exitValue();
      boolean _notEquals = (_exitValue != 0);
      if (_notEquals) {
        int _exitValue_1 = process.exitValue();
        String _plus = ("Unexpected exit value " + Integer.valueOf(_exitValue_1));
        System.err.println(_plus);
        System.exit((-1));
      }
      FileReader _fileReader = new FileReader(CbcCpsMain.SOLUTION_FILE);
      final BufferedReader reader = new BufferedReader(_fileReader);
      double value = Double.NaN;
      try {
        String line = null;
        while (((line = reader.readLine()) != null)) {
          {
            InputOutput.<String>println(line);
            final Matcher matcher = CbcCpsMain.VALUE_REGEX.matcher(line);
            boolean _matches = matcher.matches();
            if (_matches) {
              value = Double.parseDouble(matcher.group(1));
            }
          }
        }
      } finally {
        reader.close();
      }
      final Function1<Request, List<Integer>> _function = (Request it) -> {
        final Function1<Requirement, Integer> _function_1 = (Requirement it_1) -> {
          return Integer.valueOf(it_1.getCount());
        };
        return ListExtensions.<Requirement, Integer>map(it.getRequirements(), _function_1);
      };
      final Function2<Integer, Integer, Integer> _function_1 = (Integer p1, Integer p2) -> {
        return Integer.valueOf(((p1).intValue() + (p2).intValue()));
      };
      Integer _reduce = IterableExtensions.<Integer>reduce(IterableExtensions.<Request, Integer>flatMap(problem.getRequests(), _function), _function_1);
      final int applicationCost = ((_reduce).intValue() * 5);
      final double cost = (applicationCost + value);
      InputOutput.println();
      InputOutput.<String>println(("Cost: " + Double.valueOf(cost)));
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
}