Skip to content
Snippets Groups Projects
Commit a2663330 authored by BIGARET Sebastien's avatar BIGARET Sebastien
Browse files

Execution sets now hold extra information for programs.

This extra information is stored in the static inner class ProgramInfo
--they are here for future use by execution planners mainly.
parent 74479ce3
No related branches found
No related tags found
No related merge requests found
......@@ -3,8 +3,9 @@
*/
package eu.telecom_bretagne.praxis.core.execution;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import eu.telecom_bretagne.praxis.core.workflow.Program;
......@@ -14,10 +15,18 @@ import eu.telecom_bretagne.praxis.server.execution.platform.PlatformDescription;
* A set of activities ready to be executed on a given platform.
* @author Sébastien Bigaret
*/
public class ExecutionSet extends HashSet<Program>
public class ExecutionSet extends AbstractSet<Program> implements java.io.Serializable
{
private static final long serialVersionUID = -9199892622056742816L;
public static class ProgramInfo
{
int sequence;
}
private HashMap<Program, ProgramInfo> map = new HashMap<Program, ProgramInfo>();
private PlatformDescription platform;
/**
* A generic container that can be used by e.g. planning algorithms
*/
......@@ -35,14 +44,21 @@ public class ExecutionSet extends HashSet<Program>
@Override
public boolean add(Program p)
{
boolean b = super.add(p);
if (b)
if (map.containsKey(p))
return false;
ProgramInfo info = null;
final ExecutionSet originalSet = p.getExecutionSet();
if (originalSet != null)
{
if (p.getExecutionSet() != null)
info = originalSet.getInfoForProgram(p);
p.getExecutionSet().remove(p);
p.setExecutionSet(this);
}
return b;
else
info = new ProgramInfo();
p.setExecutionSet(this);
map.put(p, info);
return true;
}
/**
......@@ -70,7 +86,7 @@ public class ExecutionSet extends HashSet<Program>
{
if (!(program instanceof Program))
return false;
boolean b = super.remove(program);
boolean b = map.remove(program) != null;
if (b)
{
((Program) program).setExecutionSet(null);
......@@ -82,9 +98,9 @@ public class ExecutionSet extends HashSet<Program>
public boolean removeAll(Collection< ? > c)
{
/*
* Extracted from java.util.AbstractCollection
* Extracted from java.util.AbstractSet
*
* The problem in AbstractCollection.removeAll(): if c.size() >= this.size(), remove() is not called and the
* The problem in AbstractSet.removeAll(): if c.size() >= this.size(), remove() is not called and the
* removed activities' executionSets are not nullified.
*/
boolean modified = false;
......@@ -98,7 +114,24 @@ public class ExecutionSet extends HashSet<Program>
{
for (Program p: this)
p.setExecutionSet(null);
super.clear();
map.clear();
}
@Override
public int size()
{
return map.size();
}
/**
* Returns the ProgramInfo object attached to a program. Modifying the returned object actually modifies the
* information attached to the supplied program within this execution set.
* @param program
* @return the ProgramInfo object attached to the {@code program}, or {@code null} if the set has no such program.
*/
public ProgramInfo getInfoForProgram(Program program)
{
return map.get(program);
}
public PlatformDescription getPlatform()
......@@ -138,7 +171,7 @@ public class ExecutionSet extends HashSet<Program>
{
return new Iterator<Program>()
{
Iterator <Program> provider = ExecutionSet.super.iterator();
private final Iterator <Program> provider = ExecutionSet.this.map.keySet().iterator();
@Override
public boolean hasNext() { return provider.hasNext(); }
......
......@@ -15,6 +15,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import eu.telecom_bretagne.praxis.core.execution.ExecutionSet.ProgramInfo;
import eu.telecom_bretagne.praxis.core.workflow.Program;
/**
......@@ -43,6 +44,7 @@ public class ExecutionSetTest
assertEquals(1, s.size());
assertTrue(s.contains(p1));
assertFalse(s.add(p1));
assertTrue(s.getInfoForProgram(p1)!=null);
}
@Test
......@@ -59,6 +61,7 @@ public class ExecutionSetTest
assertTrue(s1.remove(a1));
assertTrue(s1.isEmpty());
assertNull(a1.getExecutionSet());
assertNull(s1.getInfoForProgram(a1));
}
@Test
......@@ -67,11 +70,15 @@ public class ExecutionSetTest
ExecutionSet s1 = new ExecutionSet(), s2 = new ExecutionSet();
Program a1 = new Program();
s1.add(a1);
ProgramInfo info_s1 = s1.getInfoForProgram(a1);
s2.add(a1);
ProgramInfo info_s2 = s2.getInfoForProgram(a1);
assertTrue(s1.isEmpty());
assertEquals(1, s2.size());
assertTrue(s2.contains(a1));
assertEquals(s2, a1.getExecutionSet());
assertEquals(info_s1, info_s2);
assertNull(s1.getInfoForProgram(a1));
}
@Test
......@@ -90,6 +97,13 @@ public class ExecutionSetTest
assertTrue(s1.contains(a1));
assertTrue(s1.contains(a2));
ProgramInfo info_a1 = s1.getInfoForProgram(a1);
ProgramInfo info_a2 = s1.getInfoForProgram(a2);
ProgramInfo info_a3 = s1.getInfoForProgram(a3);
assertTrue(info_a1!=null);
assertTrue(info_a2!=null);
assertTrue(info_a3!=null);
// reassignAll
s2.add(b1);
s2.addAll(Arrays.asList(new Program[]{a1, a3}));
......@@ -105,6 +119,12 @@ public class ExecutionSetTest
assertTrue(s2.contains(a1));
assertTrue(s2.contains(a3));
assertFalse(s2.contains(a2));
assertNull("No more info for a1 in s1", s1.getInfoForProgram(a1));
assertNull("No more info for a3 in s1", s1.getInfoForProgram(a3));
assertEquals("info for a2 should be untouched", info_a2, s1.getInfoForProgram(a2));
assertEquals("Info should have been transmitted", info_a1, s2.getInfoForProgram(a1));
assertEquals("Info should have been transmitted", info_a3, s2.getInfoForProgram(a3));
}
@Test
......@@ -124,6 +144,8 @@ public class ExecutionSetTest
assertEquals(s1, a2.getExecutionSet());
assertNull(a1.getExecutionSet());
assertNull(a3.getExecutionSet());
assertNull("s1 has no more info for a1", s1.getInfoForProgram(a1));
assertNull("s1 has no more info for a3", s1.getInfoForProgram(a3));
assertFalse(s1.removeAll(Arrays.asList(new Program[]{ a1, a3, a1 })));
......@@ -132,6 +154,7 @@ public class ExecutionSetTest
assertTrue(s1.isEmpty());
assertNull(a2.getExecutionSet());
assertNull("s1 has no more info for a2", s1.getInfoForProgram(a2));
}
@Test
......@@ -144,6 +167,7 @@ public class ExecutionSetTest
s.clear();
assertTrue(s.isEmpty());
assertNull(a1.getExecutionSet());
assertNull("set has no more info for a1", s.getInfoForProgram(a1));
}
/* @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment