Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
initial contribution of semantic metadata support (#6288)
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer authored and htreu committed Oct 11, 2018
1 parent 2b99b7b commit 842d34a
Show file tree
Hide file tree
Showing 119 changed files with 4,087 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="output" path="target/test-classes"/>
</classpath>
28 changes: 28 additions & 0 deletions bundles/core/org.eclipse.smarthome.core.semantics.test/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.smarthome.core.semantics.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Eclipse SmartHome Core Semantics Tests
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-SymbolicName: org.eclipse.smarthome.core.semantics.test
Bundle-Vendor: Eclipse.org/SmartHome
Bundle-Version: 0.10.0.qualifier
Fragment-Host: org.eclipse.smarthome.core.semantics;bundle-version="0.10.0.qualifier"
Import-Package:
javax.measure.quantity,
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.smarthome.core.items,
org.eclipse.smarthome.core.library,
org.eclipse.smarthome.core.library.items,
org.eclipse.smarthome.core.library.types,
org.eclipse.smarthome.core.types,
org.eclipse.smarthome.test,
org.eclipse.smarthome.test.java,
org.hamcrest;core=split,
org.hamcrest.collection,
org.hamcrest.core,
org.hamcrest.number,
org.junit,
org.junit.rules,
org.junit.runner,
org.junit.runners,
org.mockito,
org.mockito.invocation,
org.mockito.stubbing
Automatic-Module-Name: org.eclipse.smarthome.core.test
19 changes: 19 additions & 0 deletions bundles/core/org.eclipse.smarthome.core.semantics.test/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This content is produced and maintained by the Eclipse SmartHome project.

* Project home: https://eclipse.org/smarthome/

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/eclipse/smarthome

== Copyright Holders

See the NOTICE file distributed with the source code at
https://github.com/eclipse/smarthome/blob/master/NOTICE
for detailed information regarding copyright ownership.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source.. = src/test/java
output.. = target/test-classes/
bin.includes = META-INF/,\
.,\
NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.pde.ui.JunitLaunchConfig">
<booleanAttribute key="append.args" value="true"/>
<booleanAttribute key="askclear" value="false"/>
<booleanAttribute key="automaticAdd" value="false"/>
<booleanAttribute key="automaticValidate" value="true"/>
<stringAttribute key="bootstrap" value=""/>
<stringAttribute key="checked" value="[NONE]"/>
<booleanAttribute key="clearConfig" value="true"/>
<booleanAttribute key="clearws" value="true"/>
<booleanAttribute key="clearwslog" value="false"/>
<stringAttribute key="configLocation" value="${workspace_loc}/.metadata/.plugins/org.eclipse.pde.core/pde-junit"/>
<booleanAttribute key="default" value="false"/>
<booleanAttribute key="default_auto_start" value="true"/>
<booleanAttribute key="includeOptional" value="false"/>
<stringAttribute key="location" value="${workspace_loc}/../junit-workspace"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/org.eclipse.smarthome.core.semantics.test"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="4"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value="=org.eclipse.smarthome.core.semantics.test"/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value=""/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.eclipse.smarthome.core.semantics.test"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.pde.ui.workbenchClasspathProvider"/>
<stringAttribute key="pde.version" value="3.3"/>
<stringAttribute key="product" value="org.eclipse.equinox.p2.director.app.product"/>
<booleanAttribute key="run_in_ui_thread" value="false"/>
<stringAttribute key="selected_target_plugins" value="ch.qos.logback.classic@default:default,ch.qos.logback.core@default:default,ch.qos.logback.slf4j@default:false,com.eclipsesource.jaxrs.jersey-min@default:default,com.google.gson*2.2.4.v201311231704@default:default,com.google.gson*2.7.0.v20170129-0911@default:default,com.google.guava@default:default,com.ibm.icu@default:default,javax.measure.unit-api@default:default,javax.transaction@default:false,javax.xml@default:default,net.bytebuddy.byte-buddy-agent@default:default,net.bytebuddy.byte-buddy@default:default,org.apache.ant@default:default,org.apache.commons.collections@default:default,org.apache.commons.io@default:default,org.apache.commons.lang@default:default,org.apache.felix.scr@1:true,org.codehaus.groovy@default:default,org.eclipse.core.contenttype@default:default,org.eclipse.core.jobs@default:default,org.eclipse.core.runtime@default:true,org.eclipse.emf.common@default:default,org.eclipse.emf.ecore.xmi@default:default,org.eclipse.emf.ecore@default:default,org.eclipse.equinox.app@default:default,org.eclipse.equinox.cm@default:default,org.eclipse.equinox.common@2:true,org.eclipse.equinox.ds@1:true,org.eclipse.equinox.event*1.4.0.v20170105-1446@default:true,org.eclipse.equinox.event*1.4.200.v20180426-0845@default:true,org.eclipse.equinox.preferences@default:default,org.eclipse.equinox.region@default:false,org.eclipse.equinox.registry@default:default,org.eclipse.equinox.transforms.hook@default:false,org.eclipse.equinox.util@default:default,org.eclipse.equinox.weaving.hook@default:false,org.eclipse.osgi.services@default:default,org.eclipse.osgi.util@default:default,org.eclipse.osgi@-1:true,org.hamcrest.core@default:default,org.hamcrest.integration@default:default,org.hamcrest.library@default:default,org.hamcrest.text@default:default,org.hamcrest@default:default,org.junit@default:default,org.mockito.mockito-core@default:default,org.objenesis@default:default,org.slf4j.api@default:default,tec.uom.lib.uom-lib-common@default:default,tec.uom.se@default:default"/>
<stringAttribute key="selected_workspace_plugins" value="org.eclipse.smarthome.config.core@default:default,org.eclipse.smarthome.config.xml@default:default,org.eclipse.smarthome.core.semantics.test@default:default,org.eclipse.smarthome.core.semantics@default:default,org.eclipse.smarthome.core.test@default:false,org.eclipse.smarthome.core.thing@default:default,org.eclipse.smarthome.core@default:true,org.eclipse.smarthome.io.console@default:default,org.eclipse.smarthome.test@default:default"/>
<booleanAttribute key="show_selected_only" value="false"/>
<booleanAttribute key="tracing" value="false"/>
<booleanAttribute key="useCustomFeatures" value="false"/>
<booleanAttribute key="useDefaultConfig" value="true"/>
<booleanAttribute key="useDefaultConfigArea" value="false"/>
<booleanAttribute key="useProduct" value="false"/>
</launchConfiguration>
19 changes: 19 additions & 0 deletions bundles/core/org.eclipse.smarthome.core.semantics.test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.smarthome.bundles</groupId>
<artifactId>core</artifactId>
<version>0.10.0-SNAPSHOT</version>
</parent>
<groupId>org.eclipse.smarthome.core</groupId>
<artifactId>org.eclipse.smarthome.core.semantics.test</artifactId>

<packaging>eclipse-test-plugin</packaging>

<name>Eclipse SmartHome Core Semantics Tests</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.core.semantics;

import static org.junit.Assert.*;

import java.util.Locale;

import org.eclipse.smarthome.core.items.GenericItem;
import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.library.CoreItemFactory;
import org.eclipse.smarthome.core.semantics.model.Location;
import org.eclipse.smarthome.core.semantics.model.equipment.CleaningRobot;
import org.eclipse.smarthome.core.semantics.model.location.Bathroom;
import org.eclipse.smarthome.core.semantics.model.location.Kitchen;
import org.eclipse.smarthome.core.semantics.model.location.Room;
import org.eclipse.smarthome.core.semantics.model.point.Measurement;
import org.eclipse.smarthome.core.semantics.model.property.Temperature;
import org.junit.Before;
import org.junit.Test;

/**
* @author Kai Kreuzer - initial contribution and API
*/
public class SemanticTagsTest {

private GroupItem locationItem;
private GroupItem equipmentItem;
private GenericItem pointItem;

@Before
public void setup() throws Exception {
CoreItemFactory itemFactory = new CoreItemFactory();
locationItem = new GroupItem("TestBathRoom");
locationItem.addTag("Bathroom");

equipmentItem = new GroupItem("Test08");
equipmentItem.addTag("CleaningRobot");

pointItem = itemFactory.createItem(CoreItemFactory.NUMBER, "TestTemperature");
pointItem.addTag("Measurement");
pointItem.addTag("Temperature");
}

@Test
public void testByTagId() {
assertEquals(Location.class, SemanticTags.getById("Location"));
assertEquals(Room.class, SemanticTags.getById("Room"));
assertEquals(Room.class, SemanticTags.getById("Location_Indoor_Room"));
assertEquals(Bathroom.class, SemanticTags.getById("Bathroom"));
assertEquals(Bathroom.class, SemanticTags.getById("Room_Bathroom"));
assertEquals(Bathroom.class, SemanticTags.getById("Indoor_Room_Bathroom"));
assertEquals(Bathroom.class, SemanticTags.getById("Location_Indoor_Room_Bathroom"));
}

@Test
public void testByLabel() {
assertEquals(Kitchen.class, SemanticTags.getByLabel("Kitchen", Locale.ENGLISH));
assertEquals(Kitchen.class, SemanticTags.getByLabel("Küche", Locale.GERMAN));
assertNull(SemanticTags.getByLabel("Badezimmer", Locale.GERMAN));
}

@Test
public void testByLabelOrSynonym() {
assertEquals(Kitchen.class, SemanticTags.getByLabelOrSynonym("Kitchen", Locale.ENGLISH).iterator().next());
assertEquals(Kitchen.class, SemanticTags.getByLabelOrSynonym("Küche", Locale.GERMAN).iterator().next());
assertEquals(Bathroom.class, SemanticTags.getByLabelOrSynonym("Badezimmer", Locale.GERMAN).iterator().next());
}

@Test
public void testGetSemanticType() {
assertEquals(Bathroom.class, SemanticTags.getSemanticType(locationItem));
assertEquals(CleaningRobot.class, SemanticTags.getSemanticType(equipmentItem));
assertEquals(Measurement.class, SemanticTags.getSemanticType(pointItem));
}

@Test
public void testGetProperty() {
assertEquals(Temperature.class, SemanticTags.getProperty(pointItem));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.core.semantics.internal;

import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.util.Locale;
import java.util.Set;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.core.items.GenericItem;
import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.items.MetadataRegistry;
import org.eclipse.smarthome.core.library.CoreItemFactory;
import org.eclipse.smarthome.core.semantics.model.location.Bathroom;
import org.eclipse.smarthome.core.semantics.model.location.LivingRoom;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.osgi.framework.BundleContext;

/**
* @author Kai Kreuzer - initial contribution
*/
public class SemanticsServiceImplTest {

private @Mock BundleContext bundleContext;
private @Mock ItemRegistry itemRegistry;
private @Mock MetadataRegistry metadataRegistry;

private GroupItem locationItem;
private GroupItem equipmentItem;
private GenericItem pointItem;

private SemanticsServiceImpl service;

@Before
public void setup() throws Exception {
initMocks(this);

CoreItemFactory itemFactory = new CoreItemFactory();
locationItem = new GroupItem("TestBathRoom");
locationItem.addTag("Bathroom");
locationItem.setLabel("Joe's Room");

equipmentItem = new GroupItem("Test08");
equipmentItem.addTag("CleaningRobot");

pointItem = itemFactory.createItem(CoreItemFactory.NUMBER, "TestTemperature");
pointItem.addTag("Sensor");
pointItem.addTag("Temperature");
pointItem.setLabel("my Test label");
pointItem.addGroupName(locationItem.getName());
locationItem.addMember(pointItem);

when(metadataRegistry.get(any())).thenReturn(null);
when(itemRegistry.stream()).thenReturn(Stream.of(locationItem, equipmentItem, pointItem))
.thenReturn(Stream.of(locationItem, equipmentItem, pointItem))
.thenReturn(Stream.of(locationItem, equipmentItem, pointItem));

service = new SemanticsServiceImpl();
service.setItemRegistry(itemRegistry);
service.setMetadataRegistry(metadataRegistry);
service.activate(bundleContext);
}

@Test
public void testGetItemsInLocation() throws Exception {
Set<@NonNull Item> items = service.getItemsInLocation(Bathroom.class);
assertTrue(items.contains(pointItem));

items = service.getItemsInLocation("Room", Locale.ENGLISH);
assertTrue(items.contains(pointItem));
}

@Test
public void testGetItemsInLocationByString() throws Exception {
Set<@NonNull Item> items = service.getItemsInLocation("joe's room", Locale.ENGLISH);
assertTrue(items.contains(pointItem));

items = service.getItemsInLocation(LivingRoom.class);
assertTrue(items.isEmpty());
}
}
8 changes: 8 additions & 0 deletions bundles/core/org.eclipse.smarthome.core.semantics/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
33 changes: 33 additions & 0 deletions bundles/core/org.eclipse.smarthome.core.semantics/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.smarthome.core.semantics</name>
<comment>This is the semantics component of Eclipse SmartHome</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
</natures>
</projectDescription>
Loading

0 comments on commit 842d34a

Please sign in to comment.