Skip to content

Commit

Permalink
Component isolation strategy implemented correctly based on specified
Browse files Browse the repository at this point in the history
interfaces.
  • Loading branch information
iridin committed Apr 19, 2017
1 parent 69424d1 commit fdf83d2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
*******************************************************************************/
package cz.cuni.mff.d3s.jdeeco.adaptation.componentIsolation;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import cz.cuni.mff.d3s.deeco.knowledge.KnowledgeManager;
import cz.cuni.mff.d3s.deeco.knowledge.ValueSet;
import cz.cuni.mff.d3s.deeco.knowledge.container.KnowledgeContainer;
import cz.cuni.mff.d3s.deeco.knowledge.container.KnowledgeContainerException;
import cz.cuni.mff.d3s.deeco.knowledge.container.TrackingKnowledgeContainer;
import cz.cuni.mff.d3s.deeco.logging.Log;
import cz.cuni.mff.d3s.deeco.model.runtime.api.ComponentInstance;
import cz.cuni.mff.d3s.deeco.model.runtime.api.KnowledgePath;
import cz.cuni.mff.d3s.metaadaptation.componentisolation.Port;

/**
Expand All @@ -34,34 +39,30 @@
public class ComponentImpl implements cz.cuni.mff.d3s.metaadaptation.componentisolation.Component {

private final ComponentInstance component;
private boolean verbose;

public ComponentImpl(ComponentInstance component){
if(component == null){
throw new IllegalArgumentException(String.format("The %s argument is null.", "component"));
}

this.component = component;
verbose = false;
}

/* (non-Javadoc)
* @see cz.cuni.mff.d3s.metaadaptation.componentisolation.Component#addPort(cz.cuni.mff.d3s.metaadaptation.componentisolation.Port)
*/
@Override
public void addPort(Port port) {
// TODO: implement
throw new UnsupportedOperationException();

public void setVerbosity(boolean verbosity){
this.verbose = verbosity;
}

/* (non-Javadoc)
* @see cz.cuni.mff.d3s.metaadaptation.componentisolation.Component#removePort(cz.cuni.mff.d3s.metaadaptation.componentisolation.Port)
*/
@Override
public void removePort(Port port) {
// TODO: implement properly
KnowledgeManager kManager = component.getKnowledgeManager();
kManager.updateRoles(null);
Log.i("Removing the role of the " + kManager.getId());
kManager.removeRole(((PortImpl) port).getRole());
Log.i(String.format("Removing the role \"%s\" of the \"%s\" component.",
port.toString(), kManager.getId()));
}

/* (non-Javadoc)
Expand All @@ -80,40 +81,53 @@ public Set<Port> getPorts() {
}

/* (non-Javadoc)
* @see cz.cuni.mff.d3s.metaadaptation.componentisolation.Component#monitorHealth(cz.cuni.mff.d3s.metaadaptation.componentisolation.Port)
* @see cz.cuni.mff.d3s.metaadaptation.componentisolation.Component#getFaultyKnowledge()
*/
@Override
public void monitorHealth(Port port) {
// TODO: check whether this is needed

public Set<String> getFaultyKnowledge() {
try {
KnowledgeContainer kc = TrackingKnowledgeContainer.createFromKnowledgeManagers(
component.getKnowledgeManager(),
component.getShadowKnowledgeManagerRegistry().getShadowKnowledgeManagers());
FaultyKnowledgeReportingRole knowledge;
knowledge = kc.getUntrackedLocalKnowledgeForRole(component, FaultyKnowledgeReportingRole.class);
return knowledge.faultyKnowledge;
} catch (KnowledgeContainerException e) {
// If the component doesn't implement FaultyKnowledgeReportingRole don't check it
if(verbose){
Log.w(e.getMessage());
}
return Collections.emptySet();
}
}

/* (non-Javadoc)
* @see cz.cuni.mff.d3s.metaadaptation.componentisolation.Component#getHealth(cz.cuni.mff.d3s.metaadaptation.componentisolation.Port)
* @see cz.cuni.mff.d3s.metaadaptation.componentisolation.Component#getKnowledge()
*/
@SuppressWarnings("unchecked")
@Override
public boolean getHealth(Port port) {
public Map<String, Object> getKnowledge() {
Map<String, Object> knowledge = new HashMap<>();

try {
KnowledgeContainer kc = TrackingKnowledgeContainer.createFromKnowledgeManagers(
component.getKnowledgeManager(),
component.getShadowKnowledgeManagerRegistry().getShadowKnowledgeManagers());
Collection<Object> knowledge = kc.getUntrackedKnowledgeForRole(((PortImpl) port).getRole());

if (knowledge.size() > 0) {
for (Object o : knowledge) {
Field id = o.getClass().getField("id");
if (component.getKnowledgeManager().getId().equals(id.get(o))) {
Field isWorking = o.getClass().getField("isWorking");
return (boolean) isWorking.get(o);
}

}
Collection<KnowledgePath> kps = component.getKnowledgeManager().getAllPaths();
ValueSet vSet = component.getKnowledgeManager().get(kps);
for (KnowledgePath kp : vSet.getKnowledgePaths()) {
String k = kp.getNodes().get(kp.getNodes().size() - 1).toString();
knowledge.put(k, vSet.getValue(kp));
}
} catch (Exception e) {
System.err.println(e.getMessage());
}

return true;
return knowledge;
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return component.getKnowledgeManager().getId();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public void onStartup() throws PluginStartupFailedException {
Set<Component> c = new HashSet<>();
for(DEECoNode node : nodes){
for(ComponentInstance ci : node.getRuntimeMetadata().getComponentInstances()){
c.add(new ComponentImpl(ci));
ComponentImpl component = new ComponentImpl(ci);
component.setVerbosity(verbose);
c.add(component);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,14 @@

import java.util.Set;

import cz.cuni.mff.d3s.metaadaptation.componentisolation.Port;
import cz.cuni.mff.d3s.deeco.annotations.Role;

/**
* @author Dominik Skoda <skoda@d3s.mff.cuni.cz>
*
*/
public class ConnectorImpl implements cz.cuni.mff.d3s.metaadaptation.componentisolation.Connector {

/* (non-Javadoc)
* @see cz.cuni.mff.d3s.metaadaptation.componentisolation.Connector#getPorts()
*/
@Override
public Set<Port> getPorts() {
// TODO Auto-generated method stub
return null;
}
@Role
public class FaultyKnowledgeReportingRole {

public Set<String> faultyKnowledge;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,49 @@
*******************************************************************************/
package cz.cuni.mff.d3s.jdeeco.adaptation.componentIsolation;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

/**
* @author Dominik Skoda <skoda@d3s.mff.cuni.cz>
*
*/
public class PortImpl implements cz.cuni.mff.d3s.metaadaptation.componentisolation.Port {

@SuppressWarnings("rawtypes")
private final Class role;
private final Class<?> role;


@SuppressWarnings("rawtypes")
public PortImpl(Class role){
public PortImpl(Class<?> role){
if(role == null){
throw new IllegalArgumentException(String.format("The %s argument is null.", "role"));
}
this.role = role;
}

@SuppressWarnings("rawtypes")
public Class getRole(){
public Class<?> getRole(){
return role;
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return role.getName();
}

/* (non-Javadoc)
* @see cz.cuni.mff.d3s.metaadaptation.componentisolation.Port#getExposedKnowledge()
*/
@Override
public Set<String> getExposedKnowledge() {
Set<String> exposedKnowledge = new HashSet<>();

for(Field f : role.getFields()){
exposedKnowledge.add(f.getName());
}

return exposedKnowledge;
}
}

0 comments on commit fdf83d2

Please sign in to comment.