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

Working on Separation of Trees.

- Begin to integrate ML adapters, see issue #13
- Create separate tree for openhab
- Separate concrete ML models into different grammar files
- This partly makes issue #7 obsolete (ML-Model still needs to be renamed)
parent bb95a8e0
Branches
No related tags found
No related merge requests found
Showing
with 235 additions and 88 deletions
...@@ -4,20 +4,20 @@ aspect DecisionTree { ...@@ -4,20 +4,20 @@ aspect DecisionTree {
public class DecisionTreeLeaf implements Leaf { } public class DecisionTreeLeaf implements Leaf { }
//--- classify --- //--- classify ---
syn Leaf DecisionTreeRoot.classify() { syn DecisionTreeLeaf DecisionTreeRoot.classify() {
return getRootRule().classify(); return getRootRule().classify();
} }
syn Leaf DecisionTreeElement.classify(); syn DecisionTreeLeaf DecisionTreeElement.classify();
syn Leaf DecisionTreeRule.classify(); syn DecisionTreeLeaf DecisionTreeRule.classify();
syn Leaf ItemStateCheckRule.classify() { syn DecisionTreeLeaf ItemStateCheckRule.classify() {
boolean chooseLeft = getItemStateCheck().holds(); boolean chooseLeft = getItemStateCheck().holds();
return (chooseLeft ? getLeft() : getRight()).classify(); return (chooseLeft ? getLeft() : getRight()).classify();
} }
syn Leaf DecisionTreeLeaf.classify() = this; syn DecisionTreeLeaf DecisionTreeLeaf.classify() = this;
//--- holds --- //--- holds ---
syn boolean ItemStateCheck.holds() = holdsFor(getItem()); syn boolean ItemStateCheck.holds() = holdsFor(getItem());
......
// ---------------- Decision Tree ------------------------------
DecisionTreeRoot : InternalMachineLearningModel ::= RootRule:DecisionTreeRule ;
abstract DecisionTreeElement ::= Preference:ItemPreference*;
abstract DecisionTreeRule : DecisionTreeElement ::= Left:DecisionTreeElement Right:DecisionTreeElement <Label:String> ;
ItemStateCheckRule : DecisionTreeRule ::= ItemStateCheck ;
abstract ItemStateCheck ::= <Comparator:ComparatorType> ;
rel ItemStateCheck.Item -> Item ;
ItemStateNumberCheck : ItemStateCheck ::= <Value:double> ;
ItemStateStringCheck : ItemStateCheck ::= <Value:String> ;
DecisionTreeLeaf : DecisionTreeElement ::= <ActivityIdentifier:int> <Label:String> ;
...@@ -6,7 +6,7 @@ aspect Logging { ...@@ -6,7 +6,7 @@ aspect Logging {
private org.apache.logging.log4j.Logger DummyMachineLearningModel.logger = org.apache.logging.log4j.LogManager.getLogger(DummyMachineLearningModel.class); private org.apache.logging.log4j.Logger DummyMachineLearningModel.logger = org.apache.logging.log4j.LogManager.getLogger(DummyMachineLearningModel.class);
private org.apache.logging.log4j.Logger Rule.logger = org.apache.logging.log4j.LogManager.getLogger(Rule.class); private org.apache.logging.log4j.Logger Rule.logger = org.apache.logging.log4j.LogManager.getLogger(Rule.class);
private org.apache.logging.log4j.Logger MqttRoot.logger = org.apache.logging.log4j.LogManager.getLogger(MqttRoot.class); private org.apache.logging.log4j.Logger MqttRoot.logger = org.apache.logging.log4j.LogManager.getLogger(MqttRoot.class);
private org.apache.logging.log4j.Logger MachineLearningModel.logger = org.apache.logging.log4j.LogManager.getLogger(MachineLearningModel.class); private org.apache.logging.log4j.Logger InternalMachineLearningModel.logger = org.apache.logging.log4j.LogManager.getLogger(MachineLearningModel.class);
private org.apache.logging.log4j.Logger NeuralNetworkRoot.logger = org.apache.logging.log4j.LogManager.getLogger(NeuralNetworkRoot.class); private org.apache.logging.log4j.Logger NeuralNetworkRoot.logger = org.apache.logging.log4j.LogManager.getLogger(NeuralNetworkRoot.class);
private org.apache.logging.log4j.Logger OutputLayer.logger = org.apache.logging.log4j.LogManager.getLogger(OutputLayer.class); private org.apache.logging.log4j.Logger OutputLayer.logger = org.apache.logging.log4j.LogManager.getLogger(OutputLayer.class);
} }
...@@ -11,7 +11,7 @@ aspect MachineLearning { ...@@ -11,7 +11,7 @@ aspect MachineLearning {
List<ItemPreference> computePreferences(); List<ItemPreference> computePreferences();
} }
syn Leaf MachineLearningModel.classify(); syn Leaf InternalMachineLearningModel.classify();
//--- currentActivityName --- //--- currentActivityName ---
syn String Root.currentActivityName() = JavaUtils.ifPresentOrElseReturn( syn String Root.currentActivityName() = JavaUtils.ifPresentOrElseReturn(
...@@ -21,10 +21,20 @@ aspect MachineLearning { ...@@ -21,10 +21,20 @@ aspect MachineLearning {
); );
//--- currentActivity --- //--- currentActivity ---
syn java.util.Optional<Activity> Root.currentActivity() = resolveActivity(getMachineLearningRoot().hasActivityRecognition() ? getMachineLearningRoot().getActivityRecognition().classify().getActivityIdentifier() : -1); syn java.util.Optional<Activity> Root.currentActivity() {
return resolveActivity(getMachineLearningRoot().hasActivityRecognition() ?
extractActivityIdentifier(getMachineLearningRoot().getActivityRecognition().getDecoder().classify().getPreferences()) :
-1);
}
private int Root.extractActivityIdentifier(List<ItemPreference> preferences) {
if (preferences.isEmpty()) {
return -1;
}
return (int) ((ItemPreferenceDouble) preferences.get(0)).getPreferredValue();
}
//--- currentPreferences --- //--- currentPreferences ---
syn List<ItemPreference> Root.currentPreferences() = getMachineLearningRoot().getPreferenceLearning().classify().computePreferences(); syn List<ItemPreference> Root.currentPreferences() = getMachineLearningRoot().getPreferenceLearning().getDecoder().classify().getPreferences();
//--- canSetActivity --- //--- canSetActivity ---
syn boolean MachineLearningModel.canSetActivity() = false; syn boolean MachineLearningModel.canSetActivity() = false;
...@@ -64,7 +74,7 @@ aspect MachineLearning { ...@@ -64,7 +74,7 @@ aspect MachineLearning {
public void DummyMachineLearningModel.connectItems(List<String> itemNames) { public void DummyMachineLearningModel.connectItems(List<String> itemNames) {
logger.info("Storing items to connect"); logger.info("Storing items to connect");
for (String itemName : itemNames) { for (String itemName : itemNames) {
JavaUtils.ifPresentOrElse(getRoot().resolveItem(itemName), JavaUtils.ifPresentOrElse(getRoot().getOpenHAB2Model().resolveItem(itemName),
this::addItem, this::addItem,
() -> logger.warn("Could not resolve item '{}'", itemName)); () -> logger.warn("Could not resolve item '{}'", itemName));
} }
...@@ -72,10 +82,10 @@ aspect MachineLearning { ...@@ -72,10 +82,10 @@ aspect MachineLearning {
//--- check --- //--- check ---
/** /**
* Checks the NeuralNetwork for all necessary children. * Checks the ML model for all necessary children.
* @return true, if everything is alright. false otherwise * @return true, if everything is alright. false otherwise
*/ */
public boolean MachineLearningModel.check() { public boolean InternalMachineLearningModel.check() {
boolean good = true; boolean good = true;
if (getOutputApplication() == null) { if (getOutputApplication() == null) {
logger.warn("{}: OutputApplication function is null!", mlKind()); logger.warn("{}: OutputApplication function is null!", mlKind());
...@@ -114,4 +124,44 @@ aspect MachineLearning { ...@@ -114,4 +124,44 @@ aspect MachineLearning {
); );
} }
//... ExternalMachineLearningModel ...
private MachineLearningEncoder ExternalMachineLearningModel.encoder;
public void ExternalMachineLearningModel.setEncoder(MachineLearningEncoder encoder) {
this.encoder = encoder;
}
private MachineLearningDecoder ExternalMachineLearningModel.decoder;
public void ExternalMachineLearningModel.setDecoder(MachineLearningDecoder decoder) {
this.decoder = decoder;
}
// eq ExternalMachineLearningModel.classify() = null;
public void ExternalMachineLearningModel.connectItems(List<String> itemNames) { }
//... InternalMachineLearningModel ...
syn InternalMachineLearningHandler InternalMachineLearningModel.handler() {
return new InternalMachineLearningHandler().setModel(this);
}
cache InternalMachineLearningModel.handler();
//--- getEncoder ---
public abstract MachineLearningEncoder MachineLearningModel.getEncoder();
@Override
public MachineLearningEncoder InternalMachineLearningModel.getEncoder() {
return handler();
}
@Override
public MachineLearningEncoder ExternalMachineLearningModel.getEncoder() {
return this.encoder;
}
//--- getDecoder ---
public abstract MachineLearningDecoder MachineLearningModel.getDecoder();
@Override
public MachineLearningDecoder InternalMachineLearningModel.getDecoder() {
return handler();
}
@Override
public MachineLearningDecoder ExternalMachineLearningModel.getDecoder() {
return this.decoder;
}
} }
...@@ -13,44 +13,14 @@ rel RecognitionEvent.Activity -> Activity ; ...@@ -13,44 +13,14 @@ rel RecognitionEvent.Activity -> Activity ;
ManualChangeEvent : ChangeEvent ; ManualChangeEvent : ChangeEvent ;
abstract MachineLearningModel ::= <OutputApplication:DoubleDoubleFunction> ; abstract MachineLearningModel ::= ;
ExternalMachineLearningModel : MachineLearningModel ;
abstract InternalMachineLearningModel : MachineLearningModel ::= <OutputApplication:DoubleDoubleFunction> ;
rel InternalMachineLearningModel.RelevantItem* -> Item ;
rel InternalMachineLearningModel.TargetItem* -> Item ;
abstract ItemPreference ::= ; abstract ItemPreference ::= ;
rel ItemPreference.Item -> Item ; rel ItemPreference.Item -> Item ;
ItemPreferenceColor : ItemPreference ::= <PreferredHSB:TupleHSB> ; ItemPreferenceColor : ItemPreference ::= <PreferredHSB:TupleHSB> ;
ItemPreferenceDouble : ItemPreference ::= <PreferredValue:double> ; ItemPreferenceDouble : ItemPreference ::= <PreferredValue:double> ;
// ---------------- Decision Tree ------------------------------
DecisionTreeRoot : MachineLearningModel ::= RootRule:DecisionTreeRule ;
abstract DecisionTreeElement ::= Preference:ItemPreference*;
abstract DecisionTreeRule : DecisionTreeElement ::= Left:DecisionTreeElement Right:DecisionTreeElement <Label:String> ;
ItemStateCheckRule : DecisionTreeRule ::= ItemStateCheck ;
abstract ItemStateCheck ::= <Comparator:ComparatorType> ;
rel ItemStateCheck.Item -> Item ;
ItemStateNumberCheck : ItemStateCheck ::= <Value:double> ;
ItemStateStringCheck : ItemStateCheck ::= <Value:String> ;
DecisionTreeLeaf : DecisionTreeElement ::= <ActivityIdentifier:int> <Label:String> ;
// ---------------- Neural Network ------------------------------
NeuralNetworkRoot : MachineLearningModel ::= InputNeuron* HiddenNeuron* OutputLayer ;
OutputLayer ::= OutputNeuron* <Combinator:DoubleArrayDoubleFunction> ;
rel OutputLayer.AffectedItem -> Item ;
abstract Neuron ::= Output:NeuronConnection* ;
NeuronConnection ::= <Weight:double> ;
rel NeuronConnection.Neuron <-> Neuron.Input* ;
InputNeuron : Neuron ;
rel InputNeuron.Item -> Item ;
HiddenNeuron : Neuron ::= <ActivationFormula:DoubleArrayDoubleFunction> ;
BiasNeuron : HiddenNeuron ;
OutputNeuron : HiddenNeuron ::= <Label:String> ;
DummyMachineLearningModel : MachineLearningModel ::= Current:DecisionTreeLeaf ;
rel DummyMachineLearningModel.Item* -> Item ;
aspect ModelStatistics { aspect ModelStatistics {
//--- numChannels --- //--- numChannels ---
syn int Root.numChannels() { syn int OpenHAB2Model.numChannels() {
int sum = 0; int sum = 0;
for (Thing thing : getThingList()) { for (Thing thing : getThingList()) {
sum += thing.getNumChannel(); sum += thing.getNumChannel();
...@@ -10,7 +10,7 @@ aspect ModelStatistics { ...@@ -10,7 +10,7 @@ aspect ModelStatistics {
} }
//--- description --- //--- description ---
syn String Root.description() = "[" syn String OpenHAB2Model.description() = "["
+ this.getNumThingType() + " thing type(s), " + this.getNumThingType() + " thing type(s), "
+ this.getNumChannelType() + " channel type(s), " + this.getNumChannelType() + " channel type(s), "
+ this.numChannels() + " channel(s), " + this.numChannels() + " channel(s), "
......
aspect Navigation { aspect Navigation {
syn Comparator<ModelElement> Root.modelElementComparator() { syn Comparator<ModelElement> OpenHAB2Model.modelElementComparator() {
return (e1, e2) -> (e1.getID().compareTo(e2.getID())); return (e1, e2) -> (e1.getID().compareTo(e2.getID()));
} }
//--- items --- //--- items ---
syn java.util.List<Item> Root.items() { syn java.util.List<Item> OpenHAB2Model.items() {
java.util.List<Item> result = new java.util.ArrayList<>(); java.util.List<Item> result = new java.util.ArrayList<>();
addItems(result, getGroupList()); addItems(result, getGroupList());
return result; return result;
} }
private void Root.addItems(java.util.List<Item> result, JastAddList<Group> groups) { private void OpenHAB2Model.addItems(java.util.List<Item> result, JastAddList<Group> groups) {
groups.forEach(group -> group.getItemList().forEach(item -> result.add(item))); groups.forEach(group -> group.getItemList().forEach(item -> result.add(item)));
} }
//--- parameters --- //--- parameters ---
syn java.util.Set<Parameter> Root.parameters() { syn java.util.Set<Parameter> OpenHAB2Model.parameters() {
java.util.Set<Parameter> result = new java.util.TreeSet<>(modelElementComparator()); java.util.Set<Parameter> result = new java.util.TreeSet<>(modelElementComparator());
getThingTypeList().forEach(tt -> tt.getParameterList().forEach(parameter -> result.add(parameter))); getThingTypeList().forEach(tt -> tt.getParameterList().forEach(parameter -> result.add(parameter)));
return result; return result;
} }
//--- channels --- //--- channels ---
syn java.util.Set<Channel> Root.channels() { syn java.util.Set<Channel> OpenHAB2Model.channels() {
java.util.Set<Channel> result = new java.util.TreeSet<>(modelElementComparator()); java.util.Set<Channel> result = new java.util.TreeSet<>(modelElementComparator());
getThingList().forEach(thing -> thing.getChannelList().forEach(channel -> result.add(channel))); getThingList().forEach(thing -> thing.getChannelList().forEach(channel -> result.add(channel)));
return result; return result;
} }
//--- resolveThingType --- //--- resolveThingType ---
syn java.util.Optional<ThingType> Root.resolveThingType(String thingTypeId) { syn java.util.Optional<ThingType> OpenHAB2Model.resolveThingType(String thingTypeId) {
for (ThingType thingType : this.getThingTypeList()) { for (ThingType thingType : this.getThingTypeList()) {
if (thingType.getID().equals(thingTypeId)) { if (thingType.getID().equals(thingTypeId)) {
return java.util.Optional.of(thingType); return java.util.Optional.of(thingType);
...@@ -40,7 +40,7 @@ aspect Navigation { ...@@ -40,7 +40,7 @@ aspect Navigation {
} }
//--- resolveChannel --- //--- resolveChannel ---
syn java.util.Optional<Channel> Root.resolveChannel(String channelId) { syn java.util.Optional<Channel> OpenHAB2Model.resolveChannel(String channelId) {
for (Thing thing : this.getThingList()) { for (Thing thing : this.getThingList()) {
for (Channel channel : thing.getChannelList()) { for (Channel channel : thing.getChannelList()) {
if (channel.getID().equals(channelId)) { if (channel.getID().equals(channelId)) {
...@@ -52,7 +52,7 @@ aspect Navigation { ...@@ -52,7 +52,7 @@ aspect Navigation {
} }
//--- resolveChannelType --- //--- resolveChannelType ---
syn java.util.Optional<ChannelType> Root.resolveChannelType(String channelTypeId) { syn java.util.Optional<ChannelType> OpenHAB2Model.resolveChannelType(String channelTypeId) {
for (ChannelType channelType : this.getChannelTypeList()) { for (ChannelType channelType : this.getChannelTypeList()) {
if (channelType.getID().equals(channelTypeId)) { if (channelType.getID().equals(channelTypeId)) {
return java.util.Optional.of(channelType); return java.util.Optional.of(channelType);
...@@ -62,7 +62,10 @@ aspect Navigation { ...@@ -62,7 +62,10 @@ aspect Navigation {
} }
//--- resolveItem --- //--- resolveItem ---
syn java.util.Optional<Item> Root.resolveItem(String itemId) { syn java.util.Optional<Item> OpenHAB2Model.resolveItem(String itemId) {
if ("activity".equals(itemId)) {
return Optional.of(getActivityItem());
}
for (Item item : items()) { for (Item item : items()) {
if (item.getID().equals(itemId)) { if (item.getID().equals(itemId)) {
return java.util.Optional.of(item); return java.util.Optional.of(item);
...@@ -72,7 +75,7 @@ aspect Navigation { ...@@ -72,7 +75,7 @@ aspect Navigation {
} }
//--- resolveGroup --- //--- resolveGroup ---
syn java.util.Optional<Group> Root.resolveGroup(String groupId) { syn java.util.Optional<Group> OpenHAB2Model.resolveGroup(String groupId) {
for (Group group : this.getGroupList()) { for (Group group : this.getGroupList()) {
if (group.getID().equals(groupId)) { if (group.getID().equals(groupId)) {
return java.util.Optional.of(group); return java.util.Optional.of(group);
...@@ -87,7 +90,7 @@ aspect Navigation { ...@@ -87,7 +90,7 @@ aspect Navigation {
} }
//--- resolveItemCategory --- //--- resolveItemCategory ---
syn java.util.Optional<ItemCategory> Root.resolveItemCategory(String categoryName) { syn java.util.Optional<ItemCategory> OpenHAB2Model.resolveItemCategory(String categoryName) {
for (ItemCategory category : getItemCategoryList()) { for (ItemCategory category : getItemCategoryList()) {
if (category.getName().equals(categoryName)) { if (category.getName().equals(categoryName)) {
return java.util.Optional.of(category); return java.util.Optional.of(category);
...@@ -136,15 +139,11 @@ aspect Navigation { ...@@ -136,15 +139,11 @@ aspect Navigation {
//--- getRoot --- //--- getRoot ---
inh Root ASTNode.getRoot(); inh Root ASTNode.getRoot();
eq Root.getChannelCategory().getRoot() = this; eq Root.getOpenHAB2Model().getRoot() = this;
eq Root.getMqttRoot().getRoot() = this; eq Root.getMqttRoot().getRoot() = this;
eq Root.getInfluxRoot().getRoot() = this; eq Root.getInfluxRoot().getRoot() = this;
eq Root.getMachineLearningRoot().getRoot() = this; eq Root.getMachineLearningRoot().getRoot() = this;
eq Root.getThing().getRoot() = this;
eq Root.getGroup().getRoot() = this;
eq Root.getThingType().getRoot() = this;
eq Root.getChannelType().getRoot() = this;
eq Root.getRule().getRoot() = this; eq Root.getRule().getRoot() = this;
eq Root.getItemCategory().getRoot() = this;
eq Root.getUser().getRoot() = this; eq Root.getUser().getRoot() = this;
eq Root.getLocation().getRoot() = this;
} }
...@@ -100,7 +100,7 @@ aspect NeuralNetwork { ...@@ -100,7 +100,7 @@ aspect NeuralNetwork {
} }
String itemName = itemNames.get(i); String itemName = itemNames.get(i);
InputNeuron neuron = getInputNeuron(i); InputNeuron neuron = getInputNeuron(i);
JavaUtils.ifPresentOrElse(getRoot().resolveItem(itemName), JavaUtils.ifPresentOrElse(getRoot().getOpenHAB2Model().resolveItem(itemName),
neuron::setItem, neuron::setItem,
() -> logger.warn("Could not resolve item '{}'", itemName)); () -> logger.warn("Could not resolve item '{}'", itemName));
} }
......
// ---------------- Neural Network ------------------------------
NeuralNetworkRoot : InternalMachineLearningModel ::= InputNeuron* HiddenNeuron* OutputLayer ;
OutputLayer ::= OutputNeuron* <Combinator:DoubleArrayDoubleFunction> ;
rel OutputLayer.AffectedItem -> Item ;
abstract Neuron ::= Output:NeuronConnection* ;
NeuronConnection ::= <Weight:double> ;
rel NeuronConnection.Neuron <-> Neuron.Input* ;
InputNeuron : Neuron ;
rel InputNeuron.Item -> Item ;
HiddenNeuron : Neuron ::= <ActivationFormula:DoubleArrayDoubleFunction> ;
BiasNeuron : HiddenNeuron ;
OutputNeuron : HiddenNeuron ::= <Label:String> ;
DummyMachineLearningModel : InternalMachineLearningModel ::= Current:DecisionTreeLeaf ;
rel DummyMachineLearningModel.Item* -> Item ;
...@@ -3,8 +3,17 @@ aspect Printing { ...@@ -3,8 +3,17 @@ aspect Printing {
String ASTNode.safeID(ModelElement elem) { return elem == null ? "NULL" : elem.getID(); } String ASTNode.safeID(ModelElement elem) { return elem == null ? "NULL" : elem.getID(); }
//Root ::= Thing* Item* Group* ThingType* ChannelType* MqttRoot ;
syn String Root.prettyPrint() { syn String Root.prettyPrint() {
StringBuilder sb = new StringBuilder();
sb.append(getOpenHAB2Model().prettyPrint());
sb.append(getMqttRoot().prettyPrint());
sb.append(getInfluxRoot().prettyPrint());
sb.append(getMachineLearningRoot().prettyPrint());
return sb.toString();
}
//--- OpenHAB2Model.prettyPrint() ---
syn String OpenHAB2Model.prettyPrint() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Thing t : getThingList()) { for (Thing t : getThingList()) {
sb.append(t.prettyPrint()); sb.append(t.prettyPrint());
...@@ -27,9 +36,6 @@ aspect Printing { ...@@ -27,9 +36,6 @@ aspect Printing {
for (Channel c : channels()) { for (Channel c : channels()) {
sb.append(c.prettyPrint()); sb.append(c.prettyPrint());
} }
sb.append(getMqttRoot().prettyPrint());
sb.append(getInfluxRoot().prettyPrint());
sb.append(getMachineLearningRoot().prettyPrint());
return sb.toString(); return sb.toString();
} }
......
...@@ -27,8 +27,9 @@ aspect Util { ...@@ -27,8 +27,9 @@ aspect Util {
public static Root Root.createEmptyRoot() { public static Root Root.createEmptyRoot() {
Root model = new Root(); Root model = new Root();
model.setOpenHAB2Model(new OpenHAB2Model());
model.setMqttRoot(new MqttRoot()); model.setMqttRoot(new MqttRoot());
model.setInfluxRoot(new InfluxRoot()); model.setInfluxRoot(InfluxRoot.createDefault());
model.setMachineLearningRoot(new MachineLearningRoot()); model.setMachineLearningRoot(new MachineLearningRoot());
return model; return model;
} }
......
...@@ -25,12 +25,12 @@ import java.util.HashMap; ...@@ -25,12 +25,12 @@ import java.util.HashMap;
%goal goal; %goal goal;
Root goal = Root goal =
thing.t goal.r {: insertZero(r.getThingList(), t); return r; :} thing.t goal.r {: insertZero(r.getOpenHAB2Model().getThingList(), t); return r; :}
| item.i goal.r {: return r; :} | item.i goal.r {: return r; :}
| group.g goal.r {: insertZero(r.getGroupList(), g); return r; :} | group.g goal.r {: insertZero(r.getOpenHAB2Model().getGroupList(), g); return r; :}
| thing_type.tt goal.r {: insertZero(r.getThingTypeList(), tt); return r; :} | thing_type.tt goal.r {: insertZero(r.getOpenHAB2Model().getThingTypeList(), tt); return r; :}
| parameter goal.r {: return r; :} | parameter goal.r {: return r; :}
| channel_type.ct goal.r {: insertZero(r.getChannelTypeList(), ct); return r; :} | channel_type.ct goal.r {: insertZero(r.getOpenHAB2Model().getChannelTypeList(), ct); return r; :}
| channel.c goal.r {: return r; :} | channel.c goal.r {: return r; :}
| mqtt_root.mr goal.r {: r.setMqttRoot(mr); return r; :} | mqtt_root.mr goal.r {: r.setMqttRoot(mr); return r; :}
| influx_root.ir goal.r {: r.setInfluxRoot(ir); return r; :} | influx_root.ir goal.r {: r.setInfluxRoot(ir); return r; :}
......
// ---------------- Main ------------------------------ // ---------------- Main ------------------------------
Root ::= Thing* Group* ThingType* ChannelType* ChannelCategory* ItemCategory* User* MqttRoot InfluxRoot Root ::= OpenHAB2Model User* MqttRoot InfluxRoot MachineLearningRoot Rule* Location* ;
MachineLearningRoot Rule* Location* ;
// ---------------- Users ------------------------------ // ---------------- Users ------------------------------
User : LabelledModelElement ; User : LabelledModelElement ;
......
aspect OpenHAB2 {
syn ActivityItem OpenHAB2Model.getActivityItem() {
return new ActivityItem();
}
}
// ---------------- openHAB ------------------------------ // ---------------- openHAB ------------------------------
OpenHAB2Model ::= Thing* Group* ThingType* ChannelType* ChannelCategory* ItemCategory* /ActivityItem:Item/ ;
abstract ModelElement ::= <ID:String> ; abstract ModelElement ::= <ID:String> ;
abstract LabelledModelElement : ModelElement ::= <Label:String> ; abstract LabelledModelElement : ModelElement ::= <Label:String> ;
abstract DescribableModelElement : LabelledModelElement ::= <Description:String> ; abstract DescribableModelElement : LabelledModelElement ::= <Description:String> ;
...@@ -53,4 +55,3 @@ abstract GroupAggregationFunction ; ...@@ -53,4 +55,3 @@ abstract GroupAggregationFunction ;
SimpleGroupAggregationFunction : GroupAggregationFunction ::= <FunctionName:SimpleGroupAggregationFunctionName> ; SimpleGroupAggregationFunction : GroupAggregationFunction ::= <FunctionName:SimpleGroupAggregationFunctionName> ;
ParameterizedGroupAggregationFunction : GroupAggregationFunction ::= <FunctionName:ParameterizedGroupAggregationFunctionName> ParameterizedGroupAggregationFunction : GroupAggregationFunction ::= <FunctionName:ParameterizedGroupAggregationFunctionName>
<Param1:String> <Param2:String> ; <Param1:String> <Param2:String> ;
...@@ -16,7 +16,7 @@ import java.io.*; ...@@ -16,7 +16,7 @@ import java.io.*;
* Main entry point for testing eraser. * Main entry point for testing eraser.
* @author rschoene - Initial contribution * @author rschoene - Initial contribution
*/ */
@SuppressWarnings({"unused", "WeakerAccess", "RedundantThrows"}) @SuppressWarnings({"unused", "RedundantThrows"})
public class Main { public class Main {
public static void main(String[] args) throws IOException, Parser.Exception { public static void main(String[] args) throws IOException, Parser.Exception {
...@@ -70,7 +70,7 @@ public class Main { ...@@ -70,7 +70,7 @@ public class Main {
private static void testPrinterWith(Root model) { private static void testPrinterWith(Root model) {
model.flushTreeCache(); model.flushTreeCache();
System.out.println("Got model: " + model.description()); System.out.println("Got model: " + model.getOpenHAB2Model().description());
System.out.println("PrettyPrinted:"); System.out.println("PrettyPrinted:");
System.out.println(model.prettyPrint()); System.out.println(model.prettyPrint());
} }
...@@ -79,7 +79,7 @@ public class Main { ...@@ -79,7 +79,7 @@ public class Main {
Root model; Root model;
// model = importFromOpenHab(); // model = importFromOpenHab();
model = importFromFile(); model = importFromFile();
System.out.println("Got model: " + model.description()); System.out.println("Got model: " + model.getOpenHAB2Model().description());
// JsonSerializer.write(model, "openhab2-data.json"); // JsonSerializer.write(model, "openhab2-data.json");
testUpdaterWith(model); testUpdaterWith(model);
} }
...@@ -106,7 +106,7 @@ public class Main { ...@@ -106,7 +106,7 @@ public class Main {
public static Root importFromOpenHab() { public static Root importFromOpenHab() {
OpenHab2Importer importer = new OpenHab2Importer(); OpenHab2Importer importer = new OpenHab2Importer();
return importer.importFrom("192.168.1.250", 8080); return importer.importFrom("192.168.1.250", 8080).getRoot();
} }
public static Root importFromFile() { public static Root importFromFile() {
......
...@@ -48,12 +48,17 @@ public class ASTNodeDeserializer extends StdDeserializer<ASTNode> { ...@@ -48,12 +48,17 @@ public class ASTNodeDeserializer extends StdDeserializer<ASTNode> {
r.put(c.getName(), ((node, model) -> f.apply(model, termValue(node, terminalName)))); r.put(c.getName(), ((node, model) -> f.apply(model, termValue(node, terminalName))));
} }
private void addResolverForOpenHAB2Model(Map<String, ResolveAstNodeForOpenHAB2Model> r, Class<?> c, BiFunction<OpenHAB2Model, String, Optional<? extends ASTNode>> f, String terminalName) {
r.put(c.getName(), ((node, model) -> f.apply(model, termValue(node, terminalName))));
}
private Map<String, ResolveAstNode> resolvers = new HashMap<>(); private Map<String, ResolveAstNode> resolvers = new HashMap<>();
private Map<String, ResolveAstNodeForOpenHAB2Model> resolversForOpenHAB2Model = new HashMap<>();
private void initResolvers() { private void initResolvers() {
addResolver(resolvers, ThingType.class, Root::resolveThingType, "ID"); addResolverForOpenHAB2Model(resolversForOpenHAB2Model, ThingType.class, OpenHAB2Model::resolveThingType, "ID");
addResolver(resolvers, ChannelType.class, Root::resolveChannelType, "ID"); addResolverForOpenHAB2Model(resolversForOpenHAB2Model, ChannelType.class, OpenHAB2Model::resolveChannelType, "ID");
addResolver(resolvers, Item.class, Root::resolveItem, "ID"); addResolverForOpenHAB2Model(resolversForOpenHAB2Model, Item.class, OpenHAB2Model::resolveItem, "ID");
addResolver(resolvers, MqttTopic.class, Root::resolveMqttTopic, "IncomingTopic"); addResolver(resolvers, MqttTopic.class, Root::resolveMqttTopic, "IncomingTopic");
} }
...@@ -363,11 +368,15 @@ public class ASTNodeDeserializer extends StdDeserializer<ASTNode> { ...@@ -363,11 +368,15 @@ public class ASTNodeDeserializer extends StdDeserializer<ASTNode> {
} }
} }
interface ResolveAstNode { interface ResolveAstNode {
Optional<? extends ASTNode> resolve(JsonNode node, Root model) throws IOException; Optional<? extends ASTNode> resolve(JsonNode node, Root model) throws IOException;
} }
interface ResolveAstNodeForOpenHAB2Model {
Optional<? extends ASTNode> resolve(JsonNode node, OpenHAB2Model model) throws IOException;
}
class ResolveLater { class ResolveLater {
JsonNode node; JsonNode node;
......
package de.tudresden.inf.st.eraser.jastadd.model;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.time.Instant;
import java.util.List;
/**
* Adapter for internally held machine learning models.
*
* @author rschoene - Initial contribution
*/
public class InternalMachineLearningHandler implements MachineLearningEncoder, MachineLearningDecoder {
private static final Logger logger = LogManager.getLogger(InternalMachineLearningHandler.class);
private InternalMachineLearningModel model;
public InternalMachineLearningHandler setModel(InternalMachineLearningModel model) {
this.model = model;
return this;
}
@Override
public void newData(Root model, List<Item> changedItems) {
logger.debug("Ignored new data of {}", changedItems);
}
@Override
public List<Item> getTargets() {
return model.getTargetItems();
}
@Override
public List<Item> getRelevantItems() {
return model.getRelevantItems();
}
@Override
public void triggerTraining() {
logger.debug("Ignored training trigger.");
}
@Override
public MachineLearningResult classify() {
List<ItemPreference> preferences = model.classify().computePreferences();
return new InternalMachineLearningResult(preferences);
}
@Override
public Instant lastModelUpdate() {
return null;
}
}
package de.tudresden.inf.st.eraser.jastadd.model;
import java.util.List;
/**
* Result of a classification returned by an internally held machine learning model.
*
* @author rschoene - Initial contribution
*/
public class InternalMachineLearningResult implements MachineLearningResult {
private final List<ItemPreference> preferences;
InternalMachineLearningResult(List<ItemPreference> preferences) {
this.preferences = preferences;
}
@Override
public List<ItemPreference> getPreferences() {
return this.preferences;
}
}
package de.tudresden.inf.st.eraser.feedbackloop.api; package de.tudresden.inf.st.eraser.jastadd.model;
import java.time.Instant; import java.time.Instant;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment