Skip to content

Commit

Permalink
Added list of roles into Knowledge Manager. The value is filled in when
Browse files Browse the repository at this point in the history
a component instance is created in the AnnotationProcessor, but it is
not copied when the knowledge is cloned (TBD in cooperation with Vláďa).
  • Loading branch information
jiracekz committed Mar 3, 2015
1 parent 6105e36 commit 4dc4213
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ ComponentInstance createComponentInstance(Object obj) throws AnnotationProcessor
if (id == null) {
id = new StringBuilder().append(clazz.getSimpleName()).append(UUID.randomUUID().toString()).toString();
}
KnowledgeManager km = knowledgeManagerFactory.create(id, componentInstance);

Class<?>[] roles = RoleAnnotationsHelper.getPlaysRoleAnnotations(clazz);
KnowledgeManager km = knowledgeManagerFactory.create(id, componentInstance, roles);
km.update(initialK);
km.markAsLocal(initialLocalK.getUpdatedReferences());
km.update(initialLocalK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@
*/
public class RoleAnnotationsHelper {

public static Class<?>[] getCoordinatorRoleAnnotations(Class<?> clazz) throws AnnotationCheckerException {
public static Class<?>[] getCoordinatorRoleAnnotations(Class<?> clazz) {
Class<?>[] result = translateCoordinatorRoles(clazz.getAnnotationsByType(CoordinatorRole.class));
checkRolesAreValid(result);
return result;
}

public static Class<?>[] getMemberRoleAnnotations(Class<?> clazz) throws AnnotationCheckerException {
public static Class<?>[] getMemberRoleAnnotations(Class<?> clazz) {
Class<?>[] result = translateMemberRoles(clazz.getAnnotationsByType(MemberRole.class));
checkRolesAreValid(result);
return result;
}

public static Class<?>[] getPlaysRoleAnnotations(Class<?> clazz) throws AnnotationCheckerException {
public static Class<?>[] getPlaysRoleAnnotations(Class<?> clazz) {
Class<?>[] result = translatePlaysRole(clazz.getAnnotationsByType(PlaysRole.class));
checkRolesAreValid(result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public void validateEnsemble(Class<?> ensembleClass, EnsembleDefinition ensemble
}

Class<?>[] coordinatorRoles = RoleAnnotationsHelper.getCoordinatorRoleAnnotations(ensembleClass);
RoleAnnotationsHelper.checkRolesAreValid(coordinatorRoles);
Class<?>[] memberRoles = RoleAnnotationsHelper.getMemberRoleAnnotations(ensembleClass);
RoleAnnotationsHelper.checkRolesAreValid(memberRoles);
if (coordinatorRoles.length > 1 || memberRoles.length > 1) {
throw new AnnotationCheckerException("Only one CoordinatorRole and one MemberRole annotation is allowed per ensemble.");
}
Expand Down Expand Up @@ -122,6 +124,7 @@ void checkRolesImplementation(Object obj) throws AnnotationCheckerException {

// check if the class contains all fields from the role classes
Class<?>[] roleAnnotations = RoleAnnotationsHelper.getPlaysRoleAnnotations(componentClass);
RoleAnnotationsHelper.checkRolesAreValid(roleAnnotations);
for (Class<?> roleClass : roleAnnotations) {
checkRoleFieldsImplementation(knowledgeFields, roleClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*/
@SuppressWarnings("unchecked")
public class BaseKnowledgeManager implements KnowledgeManager {

private final Map<KnowledgePath, Object> knowledge;
private final Map<KnowledgePath, String> knowledgeAuthors;
private final Map<PathNodeField, List<SecurityTag>> securityTags;
Expand All @@ -51,10 +51,12 @@ public class BaseKnowledgeManager implements KnowledgeManager {

private final ComponentInstance component;
private final String id;
private final Class<?>[] roleClasses;

public BaseKnowledgeManager(String id, ComponentInstance component) {
public BaseKnowledgeManager(String id, ComponentInstance component, Class<?>[] roleClasses) {
this.id = id;
this.component = component;
this.roleClasses = roleClasses != null ? roleClasses : new Class<?>[0];
this.knowledge = new HashMap<>();
this.knowledgeChangeListeners = new HashMap<>();
this.localKnowledgePaths = new LinkedList<>();
Expand Down Expand Up @@ -810,4 +812,9 @@ public void lockKnowledgePath(KnowledgePath knowledgePath) {
this.lockedKnowledgePaths.add(knowledgePath);
}

@Override
public Class<?>[] getRoles() {
return roleClasses;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class CloningKnowledgeManager extends BaseKnowledgeManager {

private final Cloner c;

public CloningKnowledgeManager(String id, ComponentInstance component) {
super(id, component);
public CloningKnowledgeManager(String id, ComponentInstance component, Class<?>[] roleClasses) {
super(id, component, roleClasses);
c = new Cloner();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
public class CloningKnowledgeManagerFactory implements KnowledgeManagerFactory {

@Override
public KnowledgeManager create(String id, ComponentInstance component) {
return new CloningKnowledgeManager(id, component);
public KnowledgeManager create(String id, ComponentInstance component, Class<?>[] roleClasses) {
return new CloningKnowledgeManager(id, component, roleClasses);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public KnowledgeManagerContainer(KnowledgeManagerFactory knowledgeManagerFactory
* @return {@link KnowledgeManager} the newly created object containing
* values for the specified knowledge paths.
*/
public KnowledgeManager createLocal(String id, ComponentInstance component) {
public KnowledgeManager createLocal(String id, ComponentInstance component, Class<?>[] roleClasses) {
if (locals.containsKey(id))
return locals.get(id);

KnowledgeManager result = new CloningKnowledgeManager(id, component);
KnowledgeManager result = new CloningKnowledgeManager(id, component, roleClasses);
locals.put(id, result);

for (LocalListener listener : localListeners) {
Expand Down Expand Up @@ -201,7 +201,7 @@ public Collection<KnowledgeManager> createReplica(String id) {
} else {
replicas.put(id, new HashMap<>());
for (ComponentInstance component : runtimeModel.getComponentInstances()) {
KnowledgeManager result = new CloningKnowledgeManager(id, component);
KnowledgeManager result = new CloningKnowledgeManager(id, component, component.getKnowledgeManager().getRoles());
replicas.get(id).put(component, result);
for (ReplicaListener listener : replicaListeners) {
listener.replicaRegistered(result, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface KnowledgeManagerFactory {

public KnowledgeManager create(String id, ComponentInstance component);
public KnowledgeManager create(String id, ComponentInstance component, Class<?>[] roleClasses);

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,10 @@ public ValueSet get(Collection<KnowledgePath> knowledgeReferenceList)
*/
boolean isLocked(KnowledgePath knowledgePath);

/**
* Returns all the roles of the component that created the knowledge.
* @return All the roles of the component that created the knowledge.
*/
Class<?>[] getRoles();

}
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ void replaceKnowledgeManager(ComponentInstance ci) {
}

// create a new KM with the same id and knowledge values
KnowledgeManager km = kmContainer.createLocal(ci.getKnowledgeManager().getId(), ci);
KnowledgeManager km = kmContainer.createLocal(ci.getKnowledgeManager().getId(), ci, ci.getKnowledgeManager().getRoles());
km.markAsLocal(ci.getKnowledgeManager().getLocalPaths());

if (initialKnowledge != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class BaseKnowledgeManagerTest {

@Before
public void setUp() throws Exception {
tested = new BaseKnowledgeManager("TEST", null);
tested = new BaseKnowledgeManager("TEST", null, null);
tested.update(createKnowledge());
initMocks(this);
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public void testInnerKnowledgeGet() throws Exception {

@Test(expected = KnowledgeNotFoundException.class)
public void testNullBaseKnowledgeAccess() throws Exception {
tested = new BaseKnowledgeManager("TEST", null);
tested = new BaseKnowledgeManager("TEST", null, null);
// WHEN a field is accessed from the knowledge manager initialized with
// null base knowledge
KnowledgePath kp = RuntimeModelHelper.createKnowledgePath("number");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.internal.verification.Times;

import cz.cuni.mff.d3s.deeco.model.runtime.api.ComponentInstance;
import cz.cuni.mff.d3s.deeco.model.runtime.api.RuntimeMetadata;
import cz.cuni.mff.d3s.deeco.model.runtime.custom.RuntimeMetadataFactoryExt;
Expand Down Expand Up @@ -42,12 +44,17 @@ public void setUp() {
// create runtime with a single component
this.runtimeMetaData = RuntimeMetadataFactoryExt.eINSTANCE.createRuntimeMetadata();

KnowledgeManager mockKnowledgeManager = Mockito.mock(KnowledgeManager.class);
Mockito.when(mockKnowledgeManager.getRoles()).thenReturn(null);

ComponentInstance component1 = RuntimeMetadataFactoryExt.eINSTANCE.createComponentInstance();
component1.setName("C1");
component1.setKnowledgeManager(mockKnowledgeManager);
this.runtimeMetaData.getComponentInstances().add(component1);

ComponentInstance component2 = RuntimeMetadataFactoryExt.eINSTANCE.createComponentInstance();
component2.setName("C2");
component2.setKnowledgeManager(mockKnowledgeManager);
this.runtimeMetaData.getComponentInstances().add(component2);

this.tested = new KnowledgeManagerContainer(new CloningKnowledgeManagerFactory(), runtimeMetaData);
Expand All @@ -60,7 +67,7 @@ public void notifyLocalCreatedTest() {
tested.registerLocalListener(localListener);
tested.registerReplicaListener(replicaListener);
// and WHEN new local knowledge manager has been created
KnowledgeManager local = tested.createLocal("L", null);
KnowledgeManager local = tested.createLocal("L", null, null);
// THEN the listener is notified about this fact
verify(localListener).localCreated(local, tested);
// and none of the replica listeners is notified
Expand Down Expand Up @@ -90,7 +97,7 @@ public void notifyLocalRemovedTest() {
tested.registerLocalListener(localListener);
tested.registerReplicaListener(replicaListener);
// and WHEN a local knowledge manager has been removed
KnowledgeManager local = tested.createLocal("L", null);
KnowledgeManager local = tested.createLocal("L", null, null);
tested.removeLocal(local);
// THEN the listener is notified about this fact
verify(localListener).localRemoved(local, tested);
Expand All @@ -104,9 +111,9 @@ public void getLocalsTest() {
// container instance
Collection<KnowledgeManager> locals = new LinkedList<>();
KnowledgeManager l1, l2, l3;
locals.add(l1 = tested.createLocal("L1", null));
locals.add(l2 = tested.createLocal("L2", null));
locals.add(l3 = tested.createLocal("L3", null));
locals.add(l1 = tested.createLocal("L1", null, null));
locals.add(l2 = tested.createLocal("L2", null, null));
locals.add(l3 = tested.createLocal("L3", null, null));
// WHEN the container is accessed for all local knowledge managers
Collection<KnowledgeManager> containerLocals = tested.getLocals();
// THEN the container returns all local knowledge managers created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CloningKnowledgeManagerTest {

@Before
public void setUp() throws Exception {
tested = new CloningKnowledgeManager("TEST", null);
tested = new CloningKnowledgeManager("TEST", null, null);
tested.update(BaseKnowledgeManagerTest.createKnowledge());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public void testKnowledgeDataSerialization() throws IOException, ClassNotFoundEx
}

// create a new KM with the same id and knowledge values
KnowledgeManager km = container.createLocal(component.getKnowledgeManager().getId(), component);
KnowledgeManager km = container.createLocal(component.getKnowledgeManager().getId(), component,
component.getKnowledgeManager().getRoles());
km.update(cs);

List<? extends KnowledgeData> kd = kdManager.prepareLocalKnowledgeData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public void setUp() throws Exception {
model.getComponentInstances().add(component);
model.getEnsembleDefinitions().add(edefinition);

kmReplacement = new BaseKnowledgeManager("component", component);
when(kmContainer.createLocal(anyString(), anyObject())).thenReturn(kmReplacement);
kmReplacement = new BaseKnowledgeManager("component", component, null);
when(kmContainer.createLocal(anyString(), anyObject(), any())).thenReturn(kmReplacement);

spy = mock(RuntimeFrameworkImpl.class);

Expand Down Expand Up @@ -874,7 +874,7 @@ public void replaceKnowledgeManagerReplacesTheKM() {
tested.replaceKnowledgeManager(component);

// THEN the KM of the instance is replaced by a new one created by kmContainer for the instance
verify(kmContainer).createLocal(km.getId(), component);
verify(kmContainer).createLocal(km.getId(), component, km.getRoles());
assertEquals(kmReplacement, component.getKnowledgeManager());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void setUp() throws InvalidKeyException, CertificateEncodingException, Ke
metaData = new KnowledgeMetaData("xy", 13, "A", 123456, 45);
ComponentInstance component = mock(ComponentInstance.class);

localKnowledgeManager = new BaseKnowledgeManager("sender_id", component);
localKnowledgeManager = new BaseKnowledgeManager("sender_id", component, null);

ChangeSet changeSet = new ChangeSet();
changeSet.setValue(RuntimeModelHelper.createKnowledgePath("secured"), "secured_value");
Expand All @@ -90,7 +90,7 @@ public void setUp() throws InvalidKeyException, CertificateEncodingException, Ke
tmpComponent.getSecurityRoles().add(role);
when(component.getSecurityRoles()).thenReturn(tmpComponent.getSecurityRoles());

replicaKnowledgeManager = new BaseKnowledgeManager("receiver_id", component);
replicaKnowledgeManager = new BaseKnowledgeManager("receiver_id", component, null);
securityHelper = new SecurityHelper();

KeyPair role1Pair = securityHelper.generateKeyPair();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public void setUp() throws KnowledgeNotFoundException, KnowledgeUpdateException
localComponent = factory.createComponentInstance();
shadowComponent = factory.createComponentInstance();

localKnowledgeManager = new CloningKnowledgeManager("123", localComponent);
shadowKnowledgeManager = new CloningKnowledgeManager("124", shadowComponent);
localKnowledgeManager = new CloningKnowledgeManager("123", localComponent, null);
shadowKnowledgeManager = new CloningKnowledgeManager("124", shadowComponent, null);

localComponent.setKnowledgeManager(localKnowledgeManager);
shadowComponent.setKnowledgeManager(shadowKnowledgeManager);
Expand Down Expand Up @@ -144,7 +144,7 @@ public void checkSecurity_replicaTest() throws TaskInvocationException, Knowledg
ensembleDefinition.getKnowledgeExchange().getParameters().add(param3);
when(ensembleController.getEnsembleDefinition()).thenReturn(ensembleDefinition);

KnowledgeManager shadowKnowledgeManager = new CloningKnowledgeManager("remote", localComponent);
KnowledgeManager shadowKnowledgeManager = new CloningKnowledgeManager("remote", localComponent, null);

// given necessary data are created
ChangeSet changeSet = new ChangeSet();
Expand Down Expand Up @@ -282,7 +282,7 @@ public void checkSecurity_ContextTest() throws TaskInvocationException, Knowledg
localKnowledgeManager.setSecurityTags(RuntimeModelHelper.createKnowledgePath("mapKey"), Arrays.asList(roleTag));
localKnowledgeManager.update(createKnowledge(), "remote_component_ID");

KnowledgeManager remoteKnowledgeManager = new BaseKnowledgeManager("remote_component_ID", localKnowledgeManager.getComponent());
KnowledgeManager remoteKnowledgeManager = new BaseKnowledgeManager("remote_component_ID", localKnowledgeManager.getComponent(), localKnowledgeManager.getRoles());
ChangeSet changeSet = new ChangeSet();
changeSet.setValue(RuntimeModelHelper.createKnowledgePath("field_shadow"), 654);
remoteKnowledgeManager.update(changeSet);
Expand Down
Loading

0 comments on commit 4dc4213

Please sign in to comment.