aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Dudas2013-03-21 11:16:49 (EDT)
committerZoltan Ujhelyi2013-03-24 15:31:18 (EDT)
commitc812933994df2f35e6e157ea45cc74b4071a74cb (patch)
treee5f488e987a6435b5be63a59ec409ea83ff2e707
parentb176dc865517f70aa19e804688de1103b63beb8b (diff)
downloadorg.eclipse.incquery-c812933994df2f35e6e157ea45cc74b4071a74cb.zip
org.eclipse.incquery-c812933994df2f35e6e157ea45cc74b4071a74cb.tar.gz
org.eclipse.incquery-c812933994df2f35e6e157ea45cc74b4071a74cb.tar.bz2
[398720] closureOf function and related tests
-rw-r--r--plugins/org.eclipse.incquery.runtime.base/src/org/eclipse/incquery/runtime/base/api/FunctionalDependencyHelper.java50
-rw-r--r--tests/org.eclipse.incquery.runtime.tests/src/org/eclipse/incquery/runtime/tests/FunctionalDependencyHelperTest.java71
2 files changed, 121 insertions, 0 deletions
diff --git a/plugins/org.eclipse.incquery.runtime.base/src/org/eclipse/incquery/runtime/base/api/FunctionalDependencyHelper.java b/plugins/org.eclipse.incquery.runtime.base/src/org/eclipse/incquery/runtime/base/api/FunctionalDependencyHelper.java
new file mode 100644
index 0000000..1929f62
--- /dev/null
+++ b/plugins/org.eclipse.incquery.runtime.base/src/org/eclipse/incquery/runtime/base/api/FunctionalDependencyHelper.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2010-2013, Adam Dudas, Istvan Rath and Daniel Varro
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Adam Dudas - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.incquery.runtime.base.api;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+/**
+ * Helper utility class for functional dependency analysis.
+ *
+ * Throughout this class attribute sets are represented as generic sets and functional dependencies as maps from
+ * attribute set (generic sets) to attribute set (generic sets)
+ *
+ * @author Adam Dudas
+ *
+ */
+public class FunctionalDependencyHelper {
+ /**
+ * Get the closure of the specified attribute set relative to the specified functional dependencies.
+ *
+ * @param attributes
+ * The attributes to get the closure of.
+ * @param dependencies
+ * The functional dependencies of which the closure operation is relative to.
+ * @return The closure of the specified attribute set relative to the specified functional dependencies.
+ */
+ public static <A> Set<A> closureOf(Set<A> attributes, Map<Set<A>, Set<A>> dependencies) {
+ Set<A> closureSet = new HashSet<A>();
+
+ for (Set<A> closureSet1 = new HashSet<A>(attributes); closureSet.addAll(closureSet1);) {
+ closureSet1 = new HashSet<A>(closureSet);
+ for (Entry<Set<A>, Set<A>> dependency : dependencies.entrySet()) {
+ if (closureSet.containsAll(dependency.getKey()))
+ closureSet1.addAll(dependency.getValue());
+ }
+ }
+
+ return closureSet;
+ }
+}
diff --git a/tests/org.eclipse.incquery.runtime.tests/src/org/eclipse/incquery/runtime/tests/FunctionalDependencyHelperTest.java b/tests/org.eclipse.incquery.runtime.tests/src/org/eclipse/incquery/runtime/tests/FunctionalDependencyHelperTest.java
new file mode 100644
index 0000000..4c1c889
--- /dev/null
+++ b/tests/org.eclipse.incquery.runtime.tests/src/org/eclipse/incquery/runtime/tests/FunctionalDependencyHelperTest.java
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * Copyright (c) 2010-2013, Adam Dudas, Istvan Rath and Daniel Varro
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Adam Dudas - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.incquery.runtime.tests;
+
+import static org.eclipse.incquery.runtime.base.api.FunctionalDependencyHelper.closureOf;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.incquery.runtime.base.api.FunctionalDependencyHelper;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * Tests for {@link FunctionalDependencyHelper}.
+ *
+ * @author Adam Dudas
+ *
+ */
+public class FunctionalDependencyHelperTest {
+
+ private static final Set<Object> emptySet = ImmutableSet.<Object> of();
+ private static final Map<Set<Object>, Set<Object>> emptyMap = ImmutableMap.<Set<Object>, Set<Object>> of();
+ private static final Object a = new Object();
+ private static final Object b = new Object();
+ private static final Object c = new Object();
+ private static final Object d = new Object();
+ private static final Object e = new Object();
+ private static final Map<Set<Object>, Set<Object>> testDependencies = ImmutableMap.<Set<Object>, Set<Object>> of(
+ ImmutableSet.of(a, b), ImmutableSet.of(c), // AB -> C
+ ImmutableSet.of(a), ImmutableSet.of(d), // A -> D
+ ImmutableSet.of(d), ImmutableSet.of(e), // D -> E
+ ImmutableSet.of(a, c), ImmutableSet.of(b)); // AC -> B
+
+ @Test
+ public void testClosureOfEmptyAttributeSetEmptyDependencySet() {
+ assertEquals(emptySet, closureOf(emptySet, emptyMap));
+ }
+
+ @Test
+ public void testClosureOfEmptyAttributeSet() {
+ assertEquals(emptySet, closureOf(emptySet, testDependencies));
+ }
+
+ @Test
+ public void testClosureOfEmptyDependencySet() {
+ Set<Object> X = ImmutableSet.of(a, b, c, d);
+ assertEquals(X, closureOf(X, emptyMap));
+ }
+
+ @Test
+ public void testClosureOf() {
+ assertEquals(ImmutableSet.of(a, d, e), closureOf(ImmutableSet.of(a), testDependencies));
+ assertEquals(ImmutableSet.of(a, b, c, d, e), closureOf(ImmutableSet.of(a, b), testDependencies));
+ assertEquals(ImmutableSet.of(a, b, c, d, e), closureOf(ImmutableSet.of(a, c), testDependencies));
+ assertEquals(ImmutableSet.of(b), closureOf(ImmutableSet.of(b), testDependencies));
+ assertEquals(ImmutableSet.of(d, e), closureOf(ImmutableSet.of(d), testDependencies));
+ }
+
+}