Skip to content
Snippets Groups Projects
Commit 54ddb9a5 authored by René Schöne's avatar René Schöne
Browse files

Merge branch '41-cleanup-parser' into 'dev'

Resolve "Cleanup parser"

See merge request !12
parents 81794ffc 4d1f43c6
No related branches found
No related tags found
2 merge requests!19dev to master,!12Resolve "Cleanup parser"
Pipeline #9927 passed
Showing
with 38 additions and 88 deletions
......@@ -8,11 +8,12 @@ import org.junit.jupiter.params.provider.MethodSource;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Testing import from openHAB using {@link OpenHab2Importer}.
......@@ -49,8 +50,13 @@ public class OpenHabImporterTest {
}
private URL makeFileUrl(String fileName, String emptyFileName) throws MalformedURLException {
Path path = Paths.get(directory, fileName);
if (!path.toFile().exists() || !path.toFile().isFile()) {
Path path = null;
try {
path = Paths.get(directory, fileName);
} catch (InvalidPathException e) {
// use empty file
}
if (path == null || !path.toFile().exists() || !path.toFile().isFile()) {
path = Paths.get(directory, "..", emptyFileName);
}
return path.toUri().toURL();
......
......@@ -324,7 +324,7 @@ public class RulesTest {
// add StateSyncGroup as rule to root and trigger rewrite
model.getRoot().addRule(group);
model.getRoot().doFullTraversal();
model.getRoot().doSafeFullTraversal();
colorItem1.setState(TupleHSB.parse("0,0,100"));
......@@ -362,7 +362,7 @@ public class RulesTest {
// add StateSyncGroup as rule to root and trigger rewrite
model.getRoot().addRule(group);
model.getRoot().doFullTraversal();
model.getRoot().doSafeFullTraversal();
Instant i1 = Instant.now();
dateTimeItem1.setState(i1);
......@@ -402,7 +402,7 @@ public class RulesTest {
// add StateSyncGroup as rule to root and trigger rewrite
model.getRoot().addRule(group);
model.getRoot().doFullTraversal();
model.getRoot().doSafeFullTraversal();
numberItem1.setState(123);
......@@ -441,7 +441,7 @@ public class RulesTest {
// add StateSyncGroup as rule to root and trigger rewrite
model.getRoot().addRule(group);
model.getRoot().doFullTraversal();
model.getRoot().doSafeFullTraversal();
stringItem1.setState("123");
......@@ -483,7 +483,7 @@ public class RulesTest {
// add StateSyncGroup as rule to root and trigger rewrite
model.getRoot().addRule(group);
model.getRoot().doFullTraversal();
model.getRoot().doSafeFullTraversal();
switchItem3.setState(false);
......
......@@ -135,12 +135,10 @@ public class TestConfiguration {
File[] files = dir.listFiles();
if (files == null) return;
for (File file: files) {
if (!file.isDirectory()) {
file.delete();
} else {
if (file.isDirectory()) {
cleanDirectory(file);
file.delete();
}
file.delete();
}
}
......
......@@ -136,7 +136,7 @@ public class TestProperties extends Properties {
private static void addPaths(Collection<String> list, String pathList) {
String[] items = pathList.split(",");
for (String item : items) {
item = item.trim().replace('\\', '/');
item = Util.platformIndependentPath(item.trim());
if (!item.isEmpty()) {
list.add(item);
}
......
......@@ -38,14 +38,18 @@ import java.util.*;
*/
public class Util {
public static String platformIndependentPath(String regularPath) {
return regularPath.replace('\\', '/');
}
// public static final String TEST_ROOT = "src/test/resources/tests";
// public static final String TEST_ROOT = "tests";
/**
* Find all test directories
* @param testRoot
* @param tests
* @param excludes
* @param testRoot root directory of tests
* @param tests list of test configurations to add
* @param excludes paths to exclude
*/
private static void addChildTestDirs(File testRoot, List<TestConfiguration> tests,
Collection<String> excludes) {
......@@ -58,7 +62,7 @@ public class Util {
return;
}
for (File child: files) {
addTestDir(testRoot.getPath(), child, tests, excludes);
addTestDir(platformIndependentPath(testRoot.getPath()), child, tests, excludes);
}
}
......@@ -178,7 +182,7 @@ public class Util {
addTestDir(properties.getTestRoot(), new File(properties.getTestRoot()), tests, excludes);
} else {
for (String include: includes) {
addByPattern(new File(properties.getTestRoot()), include.replace('\\', '/'), tests, excludes);
addByPattern(new File(properties.getTestRoot()), platformIndependentPath(include), tests, excludes);
}
}
......
......@@ -114,7 +114,7 @@ public class Application {
//--- PUT /model/items/:identifier ---
Spark.put("", (request, response) -> {
SmartHomeEntityModel SmartHomeEntityModel = root.getSmartHomeEntityModel();
SmartHomeEntityModel smartHomeEntityModel = root.getSmartHomeEntityModel();
Item item;
try {
item = ParserUtils.parseItem(request.body());
......@@ -122,7 +122,7 @@ public class Application {
logger.catching(e);
return makeError(response, 400, "Could not create item. Error message: " + e.getMessage());
}
if (!SmartHomeEntityModel.resolveItem(item.getID()).isPresent()) {
if (!smartHomeEntityModel.resolveItem(item.getID()).isPresent()) {
root.getSmartHomeEntityModel().addNewItem(item);
response.status(201);
return "OK";
......@@ -242,13 +242,13 @@ public class Application {
item.getLabel(),
item.getTopic() != null ? item.getTopic().getTopicString() : null,
item.isSendState(),
wrapMetaData(item.getMetaDataList()));
wrapMetaData(item.getMetaData()));
}
private Map<String, String> wrapMetaData(JastAddList<ItemMetaData> itemMetaDataList) {
private Map<String, String> wrapMetaData(MetaData metaData) {
Map<String, String> result = new HashMap<>();
for (ItemMetaData metaData : itemMetaDataList) {
result.put(metaData.getKey(), metaData.getValue());
for (KeyValuePair keyValuePair : metaData.getKeyValuePairList()) {
result.put(keyValuePair.getKey(), keyValuePair.getValue());
}
return result;
}
......
......@@ -20,13 +20,6 @@ public interface Execute {
*/
void setKnowledgeBase(Root knowledgeBase);
/**
* <b>Deprecated</b>: Use {@link #updateItems(Iterable)} instead.
* @param brightnessAndRgbForItems Map, keys are item names, values are RGB and brightness values
*/
@Deprecated
void updateItems(Map<String, Tuple<Integer, ValuesRGB>> brightnessAndRgbForItems);
/**
* Updates items according to given updates
* @param updates tuples containing item and its new HSB value
......
......@@ -118,9 +118,6 @@ public interface Learner {
* */
EncogModel getTrainedModel(int modelID);
@Deprecated
EncogModel getTrainedModel(URL url, int modelID);
/**
*
* Method for getting normalizer of a model for a specific column/input.
......
package de.tudresden.inf.st.eraser.feedbackloop.execute;
import de.tudresden.inf.st.eraser.commons.color.ColorUtils;
import de.tudresden.inf.st.eraser.commons.color.ColorUtils.ValuesIntegralHSB;
import de.tudresden.inf.st.eraser.commons.color.ColorUtils.ValuesRGB;
import de.tudresden.inf.st.eraser.feedbackloop.api.Execute;
import de.tudresden.inf.st.eraser.jastadd.model.*;
import de.tudresden.inf.st.eraser.util.Tuple;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.*;
import java.util.function.Consumer;
import de.tudresden.inf.st.eraser.jastadd.model.ItemUpdate;
import de.tudresden.inf.st.eraser.jastadd.model.Root;
/**
* Reference implementation for Execute.
......@@ -19,38 +11,11 @@ import java.util.function.Consumer;
*/
public class ExecuteImpl implements Execute {
private Logger logger = LogManager.getLogger(ExecuteImpl.class);
private Root knowledgeBase;
// private Root knowledgeBase;
@Override
public void setKnowledgeBase(Root knowledgeBase) {
this.knowledgeBase = knowledgeBase;
}
@Override
public void updateItems(Map<String, Tuple<Integer, ValuesRGB>> brightnessAndRgbForItems) {
List<ItemUpdate> updates = new ArrayList<>();
for (Map.Entry<String, Tuple<Integer, ValuesRGB>> entry : brightnessAndRgbForItems.entrySet()) {
String itemId = entry.getKey();
resolveOrLogError(itemId, item -> {
if (entry.getValue() == null) {
return;
}
Integer brightness = entry.getValue().x;
ValuesRGB rgb = entry.getValue().y;
ValuesIntegralHSB hsb;
if (rgb != null) {
// also set rgb values
hsb = ColorUtils.convertRGBtoHSB(rgb).toIntegral();
hsb.brightness = brightness;
} else {
hsb = ValuesIntegralHSB.of(0, 100, brightness);
}
hsb.ensureBounds();
updates.add(new ItemUpdateColor(item, TupleHSB.of(hsb.hue, hsb.saturation, hsb.brightness)));
});
}
updateItems(updates);
// this.knowledgeBase = knowledgeBase;
}
@Override
......@@ -59,12 +24,4 @@ public class ExecuteImpl implements Execute {
preference.apply();
}
}
private void resolveOrLogError(String itemId, Consumer<? super Item> consumer) {
Optional<Item> optionalItem = knowledgeBase.getSmartHomeEntityModel().resolveItem(itemId);
if (!optionalItem.isPresent()) {
logger.warn("Could not resolve '{}' as an item.", itemId);
}
optionalItem.ifPresent(consumer);
}
}
......@@ -174,11 +174,6 @@ public class LearnerImpl implements Learner {
return fillModel(modelID);
}
@Override
public EncogModel getTrainedModel(URL url, int modelID) {
return fillModel(modelID);
}
private EncogModel fillModel(int modelID) {
EncogModel encogModel = new EncogModel("NN");
BasicNetwork nn = models.get(modelID).getNetwork();
......
......@@ -59,7 +59,7 @@ public class Main {
private static Root createRootWithItemsFrom(LearnerScenarioDefinition scenarioDefinition) {
Root result = Root.createEmptyRoot();
ParserUtils.createUnknownGroup(
ParserUtils.addToUnknownGroup(
result.getSmartHomeEntityModel(),
scenarioDefinition.relevantItemNames.stream().map(Main::createItem).collect(Collectors.toList()));
return result;
......
......@@ -46,8 +46,8 @@ public class LearnerTestUtils {
itemName -> {
if (itemName.equals("activity")) return;
Item item = createItem(itemName);
ParserUtils.createMqttTopic(item, itemName, result);
group.addItem(item);
item.setTopic(result.getMqttRoot().getOrCreateMqttTopic(itemName));
}
);
// init activities
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment