Skip to content

Commit e1dffa8

Browse files
committed
Refactoring & AI service WIP code
1 parent 12cd2dd commit e1dffa8

File tree

12 files changed

+616
-396
lines changed

12 files changed

+616
-396
lines changed
+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
* limitations under the License.
1919
* ----------------------------------------------------------------------------
2020
*/
21-
package esa.mo.sm.impl.util;
21+
package esa.mo.helpertools.misc;
2222

2323
/**
24+
* The OSValidator allows easy determination of the current OS.
2425
*
2526
* @author Cesar Coelho
2627
*/
+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* limitations under the License.
1919
* ----------------------------------------------------------------------------
2020
*/
21-
package esa.mo.sm.impl.util;
21+
package esa.mo.helpertools.misc;
2222

2323
import java.io.BufferedReader;
2424
import java.io.File;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (c) 2021 Cesar Coelho
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
*/
24+
package esa.mo.platform.impl.provider.adapters;
25+
26+
import esa.mo.helpertools.misc.OSValidator;
27+
import esa.mo.helpertools.misc.ShellCommander;
28+
import esa.mo.platform.impl.provider.gen.ArtificialIntelligenceAdapterInterface;
29+
import java.io.File;
30+
import java.io.IOException;
31+
import java.util.logging.Level;
32+
import java.util.logging.Logger;
33+
34+
/**
35+
* The Artificial Intelligence adapter for the Intel Movidius Neural Compute
36+
* Stick via a python script.
37+
*
38+
* @author Cesar Coelho
39+
*/
40+
public class ArtificialIntelligenceIntelMovidiusAdapter implements ArtificialIntelligenceAdapterInterface {
41+
42+
private static final Logger LOGGER = Logger.getLogger(ArtificialIntelligenceIntelMovidiusAdapter.class.getName());
43+
private final File setupVarsPath;
44+
45+
public ArtificialIntelligenceIntelMovidiusAdapter() throws IOException {
46+
// Check if Python3 is installed!
47+
ShellCommander shellCommander = new ShellCommander();
48+
String cmdPython = "python3 --version";
49+
String out = shellCommander.runCommandAndGetOutputMessage(cmdPython);
50+
String[] splits = out.split("Python ");
51+
52+
if (splits.length == 0) {
53+
throw new IOException("The Python version could not be determined!"
54+
+ " The command returned: " + out);
55+
}
56+
57+
LOGGER.log(Level.INFO, "The Python3 version is: " + splits[1]);
58+
String[] subVersions = splits[1].split("\\.");
59+
60+
if (Integer.valueOf(subVersions[1]) < 6) {
61+
throw new IOException("The installed python3 version is < 3.6.0!"
62+
+ "\n>>>> Please update your Python version!");
63+
}
64+
65+
OSValidator os = new OSValidator();
66+
67+
// Is it Linux or Windows?
68+
if (os.isUnix()) {
69+
// Find the folder where the Intel Movidius software is installed
70+
String[] options = {
71+
"/opt/intel"
72+
};
73+
74+
setupVarsPath = this.crawlOptions(options, "setupvars.sh");
75+
return;
76+
}
77+
78+
if (os.isWindows()) {
79+
// Find the folder where the Intel Movidius software is installed
80+
String[] options = {
81+
"C:\\Program Files (x86)\\Intel",
82+
"C:\\Program Files (x86)\\IntelSWTools",
83+
"C:\\Program Files\\Intel",
84+
"C:\\Program Files\\IntelSWTools"
85+
};
86+
87+
setupVarsPath = this.crawlOptions(options, "setupvars.bat");
88+
89+
// Please install version: 2020.3
90+
return;
91+
}
92+
93+
throw new IOException("The current OS is not supported: " + os.getOS());
94+
}
95+
96+
private File crawlOptions(String[] options, String filename) throws IOException {
97+
for (String option : options) {
98+
File folder = new File(option);
99+
File path = this.findPathToFile(folder, filename);
100+
101+
if (path != null) { // Found!
102+
LOGGER.log(Level.INFO, "The file was found on path:\n"
103+
+ " >> " + path.getAbsolutePath());
104+
return path;
105+
}
106+
}
107+
108+
throw new IOException("The file " + filename + " was not found!");
109+
}
110+
111+
private File findPathToFile(File path, String toBeMatched) {
112+
if (path.isFile()) {
113+
return toBeMatched.equals(path.getName()) ? path : null;
114+
}
115+
116+
// It is a directory... crawl through it
117+
for (File entry : path.listFiles()) {
118+
File file = findPathToFile(entry, toBeMatched);
119+
if (file != null) {
120+
return file;
121+
}
122+
}
123+
124+
return null;
125+
}
126+
127+
@Override
128+
public void setModel(String modelPath, String weightsPath) throws IOException {
129+
130+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
131+
}
132+
133+
@Override
134+
public void executeInference(String inputPath, String outputPath) {
135+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
136+
}
137+
138+
public String generateScriptSH(String pathIntelVar, String pythonFile) {
139+
StringBuilder str = new StringBuilder();
140+
str.append("#!/bin/bash \n\n");
141+
str.append("source ").append(pathIntelVar);
142+
str.append("\n\n");
143+
str.append("python3 ").append(pythonFile);
144+
return str.toString();
145+
}
146+
147+
public String generateScriptBAT(String pathIntelVar, String pythonFile) {
148+
StringBuilder str = new StringBuilder();
149+
str.append(pathIntelVar);
150+
str.append("\n\n");
151+
str.append(pythonFile);
152+
return str.toString();
153+
}
154+
155+
}

core/mo-services-impl/nmf-platform-generic-impl/src/main/java/esa/mo/platform/impl/provider/gen/ArtificialIntelligenceAdapterInterface.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public interface ArtificialIntelligenceAdapterInterface {
3636
/**
3737
* Executes the inference on the Artificial Intelligence device.
3838
*
39-
* @param path The file path to a folder with a set of files to be
39+
* @param inputPath The path to a folder with a set of files to be
4040
* processed.
41+
* @param outputPath The path to a folder to store the processed files.
4142
*/
42-
public void executeInference(String path);
43+
public void executeInference(String inputPath, String outputPath);
4344

4445
}

core/mo-services-impl/nmf-platform-generic-impl/src/main/java/esa/mo/platform/impl/provider/gen/ArtificialIntelligenceProviderServiceImpl.java

+47-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.ccsds.moims.mo.platform.artificialintelligence.ArtificialIntelligenceHelper;
3434
import org.ccsds.moims.mo.platform.artificialintelligence.provider.ArtificialIntelligenceInheritanceSkeleton;
3535
import esa.mo.helpertools.connections.ConnectionProvider;
36+
import java.io.IOException;
37+
import java.util.ArrayList;
3638

3739
public class ArtificialIntelligenceProviderServiceImpl extends ArtificialIntelligenceInheritanceSkeleton {
3840

@@ -41,6 +43,8 @@ public class ArtificialIntelligenceProviderServiceImpl extends ArtificialIntelli
4143
private boolean initialiased = false;
4244
private final ConnectionProvider connection = new ConnectionProvider();
4345
private ArtificialIntelligenceAdapterInterface adapter;
46+
private final static Long TIMESTAMP = System.currentTimeMillis();
47+
private final ArrayList<String> modelPaths = new ArrayList();
4448

4549
/**
4650
* creates the MAL objects, the publisher used to create updates and starts
@@ -102,11 +106,51 @@ public void close() {
102106

103107
@Override
104108
public Long setModel(String modelPath, MALInteraction interaction) throws MALInteractionException, MALException {
105-
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
109+
if (modelPath == null) {
110+
throw new MALException("The modelPath is null!");
111+
}
112+
113+
for (String path : modelPaths) {
114+
if (path.equals(modelPath)) {
115+
throw new MALException("The model already exists!");
116+
}
117+
}
118+
119+
modelPaths.add(modelPath);
120+
return TIMESTAMP + modelPaths.indexOf(modelPath);
106121
}
107122

108123
@Override
109-
public String doInference(Long modelId, String inputTilesPath, MALInteraction interaction) throws MALInteractionException, MALException {
110-
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
124+
public String doInference(Long modelId, String inputTilesPath,
125+
MALInteraction interaction) throws MALInteractionException, MALException {
126+
if (modelId == null) {
127+
throw new MALException("The modelId is null!");
128+
}
129+
130+
if (inputTilesPath == null) {
131+
throw new MALException("The inputTilesPath is null!");
132+
}
133+
134+
String modelPath = modelPaths.get((int) (modelId - TIMESTAMP));
135+
136+
if (!modelPath.endsWith(".xml")) {
137+
throw new MALException("The model does not end with the file extension: .xml");
138+
}
139+
140+
String weightsPath = modelPath.substring(0, modelPath.length() - 4) + ".bin";
141+
Logger.getLogger(ArtificialIntelligenceProviderServiceImpl.class.getName()).log(
142+
Level.INFO, "The weights file path is:\n >> " + weightsPath);
143+
144+
try {
145+
adapter.setModel(modelPath, weightsPath);
146+
String outputTilesPath = "";
147+
adapter.executeInference(inputTilesPath, outputTilesPath);
148+
return outputTilesPath;
149+
} catch (IOException ex) {
150+
Logger.getLogger(ArtificialIntelligenceProviderServiceImpl.class.getName()).log(
151+
Level.SEVERE, "The inference could not be performed!", ex);
152+
}
153+
154+
throw new MALException("The inference could not be performed!");
111155
}
112156
}

core/mo-services-impl/nmf-platform-generic-impl/src/main/java/esa/mo/platform/impl/provider/gen/GPSNMEAonlyAdapter.java

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package esa.mo.platform.impl.provider.gen;
2222

2323
import esa.mo.helpertools.misc.Const;
24+
import esa.mo.platform.impl.provider.gen.GPSAdapterInterface;
2425
import esa.mo.platform.impl.util.HelperGPS;
2526
import java.io.IOException;
2627
import java.util.logging.Level;

core/mo-services-impl/nmf-software-management-impl/src/main/java/esa/mo/sm/impl/provider/AppsLauncherManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import esa.mo.helpertools.connections.SingleConnectionDetails;
3232
import esa.mo.helpertools.helpers.HelperMisc;
3333
import esa.mo.helpertools.misc.Const;
34-
import esa.mo.sm.impl.util.OSValidator;
34+
import esa.mo.helpertools.misc.OSValidator;
3535
import java.io.File;
3636
import java.io.FileInputStream;
3737
import java.io.IOException;

core/mo-services-impl/nmf-software-management-impl/src/main/java/esa/mo/sm/impl/provider/CommandExecutorProviderServiceImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import esa.mo.com.impl.util.HelperArchive;
2727
import esa.mo.helpertools.connections.ConfigurationProviderSingleton;
2828
import esa.mo.helpertools.connections.ConnectionProvider;
29-
import esa.mo.sm.impl.util.OSValidator;
29+
import esa.mo.helpertools.misc.OSValidator;
3030
import java.io.File;
3131
import java.io.IOException;
3232
import java.nio.file.Paths;

0 commit comments

Comments
 (0)