Skip to content

Commit

Permalink
Preparations for integration test of MiniZinc Intelligent Ensembles.
Browse files Browse the repository at this point in the history
Moved DummyEnsembleTest into its own package.
  • Loading branch information
jiracekz committed Jul 23, 2015
1 parent c99a8df commit 9e91bf2
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collection;

import cz.cuni.mff.d3s.deeco.ensembles.EnsembleFactory;
import cz.cuni.mff.d3s.deeco.ensembles.EnsembleFormationException;
import cz.cuni.mff.d3s.deeco.ensembles.EnsembleInstance;
import cz.cuni.mff.d3s.deeco.knowledge.container.KnowledgeContainer;

Expand All @@ -18,14 +19,18 @@ public abstract class MiniZincIntelligentEnsembleFactory implements EnsembleFact
public MiniZincIntelligentEnsembleFactory(MznScriptRunner scriptRunner) {
this.scriptRunner = scriptRunner;
}

public MiniZincIntelligentEnsembleFactory(String scriptPath) {
this(new MznScriptRunner(scriptPath));
}

protected abstract ScriptInputVariableRegistry parseInput(KnowledgeContainer knowledgeContainer);
protected abstract ScriptInputVariableRegistry parseInput(KnowledgeContainer knowledgeContainer) throws EnsembleFormationException;

protected abstract Collection<EnsembleInstance> createInstancesFromOutput(ScriptOutputVariableRegistry scriptOutput);
protected abstract Collection<EnsembleInstance> createInstancesFromOutput(ScriptOutputVariableRegistry scriptOutput) throws EnsembleFormationException;


@Override
public Collection<EnsembleInstance> createInstances(KnowledgeContainer container) {
public Collection<EnsembleInstance> createInstances(KnowledgeContainer container) throws EnsembleFormationException {

ScriptInputVariableRegistry inputVars = parseInput(container);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package cz.cuni.mff.d3s.deeco.ensembles.intelligent;

public class OutputVariableParseException extends Exception {
import cz.cuni.mff.d3s.deeco.runtime.DEECoException;

public class OutputVariableParseException extends DEECoException {

/**
*
*/
private static final long serialVersionUID = -3372224239114525697L;

private String variableName;
private Class<?> desiredType;

Expand Down Expand Up @@ -41,11 +48,4 @@ public OutputVariableParseException(String variableName, Class<?> desiredType, S
this.desiredType = desiredType;
}

public OutputVariableParseException(String variableName, Class<?> desiredType, String additionalMessage, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(defaultMessage(variableName, desiredType) + additionalMessage, cause, enableSuppression, writableStackTrace);
this.variableName = variableName;
this.desiredType = desiredType;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cz.cuni.mff.d3s.deeco.ensembles.intelligent;

public class UnsupportedVariableTypeException extends Exception {
import cz.cuni.mff.d3s.deeco.runtime.DEECoException;

public class UnsupportedVariableTypeException extends DEECoException {

/**
*
Expand Down Expand Up @@ -38,10 +40,4 @@ public UnsupportedVariableTypeException(Class<?> usedType, String descriptionMes
this.usedType = usedType;
}

public UnsupportedVariableTypeException(Class<?> usedType, String descriptionMessage, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(descriptionMessage + " " + defaultMessage(usedType), cause, enableSuppression, writableStackTrace);
this.usedType = usedType;
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;
package cz.cuni.mff.d3s.deeco.demo.ensembles;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import static org.junit.Assert.*;
import cz.cuni.mff.d3s.deeco.annotations.processor.AnnotationProcessorException;
import cz.cuni.mff.d3s.deeco.ensembles.EnsembleFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;
package cz.cuni.mff.d3s.deeco.demo.ensembles;

import cz.cuni.mff.d3s.deeco.annotations.Component;

@Component
public class Robot {
public String id;


public Robot(String name) {
this.id = name;
}

public String getName() {
return id;
return id;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;
package cz.cuni.mff.d3s.deeco.demo.ensembles;

import cz.cuni.mff.d3s.deeco.ensembles.EnsembleInstance;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;
package cz.cuni.mff.d3s.deeco.demo.ensembles;

import java.io.PrintStream;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;

import java.util.ArrayList;
import java.util.List;

import cz.cuni.mff.d3s.deeco.ensembles.EnsembleInstance;

public class IntelligentEnsemble implements EnsembleInstance {

public int trainId;

public IntelligentEnsemble(int trainId) {
this.trainId = trainId;
members = new ArrayList<>();
}

public List<Rescuer> members;

@Override
public void performKnowledgeExchange() {
for (Rescuer member : members) {
member.trainId = trainId;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
% Two pendolinos crash, one at kilometer 50, one at kilometer 25.
% Both accidents are equally severe.
%
% We have rescuers located along the track. The task is to send
% half of the rescuer components to one crash and the other half
% to the other accident, while minimizing the summed distance
% of all rescuers to their site.

% the component_count must be divisible by train_count!!!

int: component_count;
int: train_count;

set of int: COMPONENTS = 1..component_count;
set of int: TRAINS = 1..train_count;

array[COMPONENTS] of int: component_pos;
array[TRAINS] of int: train_pos;

array[COMPONENTS] of var TRAINS: component_train;
array[COMPONENTS] of var int: component_distance;
var int: distance_sum_sqr;

% teams have equal size
constraint forall(t in TRAINS)(sum(c in COMPONENTS)(if component_train[c] = t then 1 else 0 endif) = component_count div train_count);

% distance
constraint forall(c in COMPONENTS)(component_distance[c] = abs(component_pos[c] - train_pos[component_train[c]]));
constraint distance_sum_sqr = sum(c in COMPONENTS)(component_distance[c] * component_distance[c]);

solve minimize distance_sum_sqr;
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import cz.cuni.mff.d3s.deeco.ensembles.EnsembleFormationException;
import cz.cuni.mff.d3s.deeco.ensembles.EnsembleInstance;
import cz.cuni.mff.d3s.deeco.ensembles.intelligent.MiniZincIntelligentEnsembleFactory;
import cz.cuni.mff.d3s.deeco.ensembles.intelligent.MznScriptRunner;
import cz.cuni.mff.d3s.deeco.ensembles.intelligent.OutputVariableParseException;
import cz.cuni.mff.d3s.deeco.ensembles.intelligent.ScriptInputVariableRegistry;
import cz.cuni.mff.d3s.deeco.ensembles.intelligent.ScriptOutputVariableRegistry;
import cz.cuni.mff.d3s.deeco.ensembles.intelligent.UnsupportedVariableTypeException;
import cz.cuni.mff.d3s.deeco.knowledge.container.KnowledgeContainer;
import cz.cuni.mff.d3s.deeco.knowledge.container.KnowledgeContainerException;

public class IntelligentEnsembleFactory extends MiniZincIntelligentEnsembleFactory {

private List<Rescuer> rescuers;
private static final Integer[] trainPositions = {50, 25};

public IntelligentEnsembleFactory() {
super("cz/cuni/mff/d3s/deeco/demo/intelligent/IntelligentEnsemble.mzn");
}

@Override
public int getSchedulingOffset() {
return 0;
}

@Override
public int getSchedulingPeriod() {
return 1000;
}

@Override
protected ScriptInputVariableRegistry parseInput(KnowledgeContainer knowledgeContainer) throws EnsembleFormationException {
try {
ScriptInputVariableRegistry result = new ScriptInputVariableRegistry();
rescuers = new ArrayList<>(knowledgeContainer.getTrackedKnowledgeForRole(Rescuer.class));
result.addVariable("component_count", rescuers.size());
result.addVariable("train_count", trainPositions.length);
result.addVariable("component_pos", rescuers.stream().map(r -> r.pos).collect(Collectors.toList()).toArray());
result.addVariable("train_pos", trainPositions);
return result;

} catch (UnsupportedVariableTypeException | KnowledgeContainerException e) {
throw new EnsembleFormationException(e);
}
}

@Override
protected Collection<EnsembleInstance> createInstancesFromOutput(ScriptOutputVariableRegistry scriptOutput) throws EnsembleFormationException {
try {
Integer[] componentPlacement = scriptOutput.getArray1dValue("component_train", Integer.class);

List<EnsembleInstance> result = new ArrayList<>();
for (int i = 1; i <= trainPositions.length; i++) {
IntelligentEnsemble ensemble = new IntelligentEnsemble(i);
result.add(ensemble);
}

for (int j = 0; j < componentPlacement.length; j++) {
((IntelligentEnsemble) result.get(componentPlacement[j])).members.add(rescuers.get(j));
}

return result;

} catch (UnsupportedVariableTypeException | OutputVariableParseException e) {
throw new EnsembleFormationException(e);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.Test;

import cz.cuni.mff.d3s.deeco.annotations.processor.AnnotationProcessorException;
import cz.cuni.mff.d3s.deeco.demo.roles.BoardingEnsemble;
import cz.cuni.mff.d3s.deeco.demo.ensembles.Robot;
import cz.cuni.mff.d3s.deeco.demo.ensembles.SimpleEnsembleFactory;
import cz.cuni.mff.d3s.deeco.runners.DEECoSimulation;
import cz.cuni.mff.d3s.deeco.runtime.DEECoException;
import cz.cuni.mff.d3s.deeco.runtime.DEECoNode;
Expand All @@ -19,7 +17,8 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
new IntelligentEnsemblesTest().testEnsembles(false);
}

@Test
//@Test
// TODO finish the integration test
public void testEnsembles() throws InstantiationException, IllegalAccessException, DEECoException, AnnotationProcessorException {
testEnsembles(true);
}
Expand All @@ -34,13 +33,16 @@ private void testEnsembles(boolean silent) throws InstantiationException, Illega
DEECoNode deeco = realm.createNode(0);
/* deploy components and ensemble factories */

deeco.deployComponent(new Robot("Wall-E"));
deeco.deployComponent(new Robot("Gizmo"));
deeco.deployEnsembleFactory(new SimpleEnsembleFactory());
deeco.deployComponent(new Robot("R2"));
deeco.deployComponent(new RescuerComponent("1", 30));
deeco.deployComponent(new RescuerComponent("2", 0));
deeco.deployComponent(new RescuerComponent("3", 100));
deeco.deployComponent(new RescuerComponent("4", 600));
deeco.deployComponent(new RescuerComponent("5", 70));
deeco.deployComponent(new RescuerComponent("6", 80));
deeco.deployEnsembleFactory(new IntelligentEnsembleFactory());

/* WHEN simulation is performed */
realm.start(999);
realm.start(1001);

if (silent) {
// TODO test assertions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;

import cz.cuni.mff.d3s.deeco.annotations.Role;

@Role
public class Rescuer {

public Integer pos;

public Integer trainId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cz.cuni.mff.d3s.deeco.demo.intelligent;

import cz.cuni.mff.d3s.deeco.annotations.Component;
import cz.cuni.mff.d3s.deeco.annotations.PlaysRole;

@Component
@PlaysRole(Rescuer.class)
public class RescuerComponent {

public String id;

public Integer pos;

public Integer trainId;

public RescuerComponent(String id, Integer pos) {
this.id = id;
this.pos = pos;
}

}

0 comments on commit 9e91bf2

Please sign in to comment.