Skip to content

Commit

Permalink
[ADD] implementation of ratings manager, process invocations, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ostumpf committed Jan 18, 2015
1 parent 379c050 commit d03e325
Show file tree
Hide file tree
Showing 41 changed files with 957 additions and 350 deletions.
2 changes: 0 additions & 2 deletions jdeeco-core/model/RuntimeModel.ecore
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
defaultValueLiteral="false"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="roles" upperBound="-1"
eType="#//SecurityRole" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="ratingsManager" lowerBound="1"
eType="#//RatingsManager"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="ratingsProcess" eType="#//RatingsProcess"
containment="true" eOpposite="#//RatingsProcess/componentInstance"/>
</eClassifiers>
Expand Down
4 changes: 0 additions & 4 deletions jdeeco-core/model/RuntimeModel.ecorediag
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,6 @@
<element xmi:type="ecore:EAttribute" href="RuntimeModel.ecore#//ComponentInstance/knowledgeManager"/>
<layoutConstraint xmi:type="notation:Location" xmi:id="_dmDUY0MNEeO3_auUY0X6vg"/>
</children>
<children xmi:type="notation:Node" xmi:id="_wHFnwJwoEeSBUrmm7uUAmg" type="2001">
<element xmi:type="ecore:EAttribute" href="RuntimeModel.ecore#//ComponentInstance/ratingsManager"/>
<layoutConstraint xmi:type="notation:Location" xmi:id="_wHFnwZwoEeSBUrmm7uUAmg"/>
</children>
<children xmi:type="notation:Node" xmi:id="_dmD7cEMNEeO3_auUY0X6vg" type="2001">
<element xmi:type="ecore:EAttribute" href="RuntimeModel.ecore#//ComponentInstance/shadowKnowledgeManagerRegistry"/>
<layoutConstraint xmi:type="notation:Location" xmi:id="_dmD7cUMNEeO3_auUY0X6vg"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ public AnnotationProcessor(RuntimeMetadataFactory factory, KnowledgeManagerFacto
* one or more classes extending the <code>AnnotationProcessorExtensionPoint</code> that provide additional processing functionality
* @param knowledgeMangerFactory knowledge manager factory to be used
*/
public AnnotationProcessor(RuntimeMetadataFactory factory, RuntimeMetadata model, KnowledgeManagerFactory knowledgeMangerFactory, AnnotationProcessorExtensionPoint... extensions) {
public AnnotationProcessor(RuntimeMetadataFactory factory, RuntimeMetadata model, KnowledgeManagerFactory knowledgeMangerFactory,
AnnotationProcessorExtensionPoint... extensions) {
this.factory = factory;
this.model = model;
this.extensions = extensions;
this.knowledgeManagerFactory = knowledgeMangerFactory;
this.knowledgeManagerFactory = knowledgeMangerFactory;
}

/**
Expand Down Expand Up @@ -356,7 +357,7 @@ ComponentInstance createComponentInstance(Object obj) throws AnnotationProcessor
km.markAsLocal(initialLocalK.getUpdatedReferences());
km.update(initialLocalK);
componentInstance.setKnowledgeManager(km);
try {
addSecurityTags(clazz, km, initialK);
addVirtualSecurityTags(km, initialLocalK);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cz.cuni.mff.d3s.deeco.integrity;

import java.io.Serializable;

import cz.cuni.mff.d3s.deeco.model.runtime.api.KnowledgePath;

/**
*
* @author Ondřej Štumpf
*
*/
public class RatingsChangeSet implements Serializable {

private static final long serialVersionUID = 2415324270333544655L;

private String authorComponentId, targetComponentId;
private KnowledgePath knowledgePath;
private PathRating rating;

public RatingsChangeSet(String authorComponentId, String targetComponentId, KnowledgePath knowledgePath, PathRating rating) {
this.authorComponentId = authorComponentId;
this.targetComponentId = targetComponentId;
this.knowledgePath = knowledgePath;
this.rating = rating;
}

public String getAuthorComponentId() {
return authorComponentId;
}

public String getTargetComponentId() {
return targetComponentId;
}

public KnowledgePath getKnowledgePath() {
return knowledgePath;
}

public PathRating getPathRating() {
return rating;
}
}
47 changes: 44 additions & 3 deletions jdeeco-core/src/cz/cuni/mff/d3s/deeco/integrity/RatingsHolder.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
package cz.cuni.mff.d3s.deeco.integrity;

import java.util.Map;



/**
*
* @author Ondřej Štumpf
*
*/
public class RatingsHolder extends ReadonlyRatingsHolder {

private PathRating oldRating, newRating;
private final String askingComponentId, targetComponentId;

protected RatingsHolder(String askingComponentId, String targetComponentId, PathRating rating, Map<PathRating, Long> ratings) {
super(ratings);
this.oldRating = rating;
this.newRating = rating;
this.askingComponentId = askingComponentId;
this.targetComponentId = targetComponentId;
}

public void setMyRating(PathRating rating) {
this.oldRating = this.newRating;
this.newRating = rating;

if (oldRating != null) {
Long oldValue = ratings.get(oldRating);
long oldValueL = oldValue == null ? 0 : oldValue.longValue();
ratings.put(oldRating, oldValueL - 1);
}

if (rating != null) {
Long oldValue = ratings.get(rating);
long oldValueL = oldValue == null ? 0 : oldValue.longValue();
ratings.put(rating, oldValueL + 1);
}
}

public PathRating getMyRating() {
return newRating;
}

private PathRating localComponentOpinion;
public boolean isDirty() {
return oldRating != newRating;
}

protected String getAskingComponentId() {
return askingComponentId;
}

public void setOpinion(PathRating rating) {
this.localComponentOpinion = rating;
protected String getTargetComponentId() {
return targetComponentId;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package cz.cuni.mff.d3s.deeco.integrity;

import java.util.List;
import java.util.Map;

import cz.cuni.mff.d3s.deeco.model.runtime.api.KnowledgePath;

/**
*
* @author Ondřej Štumpf
*
*/
public interface RatingsManager {

public ReadonlyRatingsHolder createReadonlyRatingsHolder(String targetComponentId, KnowledgePath absolutePath);
public RatingsHolder createRatingsHolder(String askingComponentId, String targetComponentId, KnowledgePath absolutePath);
public void createRatingsChangeSet(Map<KnowledgePath, RatingsHolder> pathRatings);
public void update(List<RatingsChangeSet> changeSets);
public List<RatingsChangeSet> getRatingsChangeSets();
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,117 @@
package cz.cuni.mff.d3s.deeco.integrity;

import cz.cuni.mff.d3s.deeco.model.runtime.api.ComponentInstance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import cz.cuni.mff.d3s.deeco.model.runtime.api.KnowledgePath;
import cz.cuni.mff.d3s.deeco.task.KnowledgePathHelper;

/**
*
* @author Ondřej Štumpf
*
*/
public class RatingsManagerImpl implements RatingsManager {
private final ComponentInstance component;
private final String id;

public RatingsManagerImpl(String id, ComponentInstance component) {
this.id = id;
this.component = component;

private Map<KnowledgePath, Map<String, Map<String, PathRating>>> ratings; // path->target component->claiming component->rating
private List<RatingsChangeSet> pendingChanges;

public RatingsManagerImpl() {
this.ratings = new HashMap<>();
this.pendingChanges = new ArrayList<>();
}

protected Map<String, PathRating> getRatings(String targetComponentId, KnowledgePath absolutePath) {
if (absolutePath == null || !KnowledgePathHelper.isAbsolutePath(absolutePath)) {
throw new IllegalArgumentException("Knowledge path must be not null and absolute.");
}

if (!ratings.containsKey(absolutePath)) {
ratings.put(absolutePath, new HashMap<>());
}
if (!ratings.get(absolutePath).containsKey(targetComponentId)) {
ratings.get(absolutePath).put(targetComponentId, new HashMap<>());
}

return ratings.get(absolutePath).get(targetComponentId);
}

protected void setRating(String authorComponentId, String targetComponentId, KnowledgePath absolutePath, PathRating rating) {
if (absolutePath == null || !KnowledgePathHelper.isAbsolutePath(absolutePath)) {
throw new IllegalArgumentException("Knowledge path must be not null and absolute.");
}

Map<String, PathRating> targetComponentRatings = getRatings(targetComponentId, absolutePath);
targetComponentRatings.put(authorComponentId, rating);
}

public ReadonlyRatingsHolder createReadonlyRatingsHolder(String targetComponentId, KnowledgePath absolutePath) {
if (absolutePath == null || !KnowledgePathHelper.isAbsolutePath(absolutePath)) {
throw new IllegalArgumentException("Knowledge path must be not null and absolute.");
}

Map<String, PathRating> individualRating = getRatings(targetComponentId, absolutePath);

if (individualRating == null) {
return new ReadonlyRatingsHolder(Collections.emptyMap());
} else {
Map<PathRating, Long> pathRating = aggregateRatings(individualRating);
return new ReadonlyRatingsHolder(pathRating);
}
}

public RatingsHolder createRatingsHolder(String askingComponentId, String targetComponentId, KnowledgePath absolutePath) {
if (absolutePath == null || !KnowledgePathHelper.isAbsolutePath(absolutePath)) {
throw new IllegalArgumentException("Knowledge path must be not null and absolute.");
}

Map<String, PathRating> individualRating = getRatings(targetComponentId, absolutePath);
PathRating oldPathRating = individualRating.get(askingComponentId);
Map<PathRating, Long> pathRating = aggregateRatings(individualRating);
// TODO - write information back

return new RatingsHolder(askingComponentId, targetComponentId, oldPathRating, pathRating);
}

public synchronized void createRatingsChangeSet(Map<KnowledgePath, RatingsHolder> pathRatings) {
if (pathRatings == null) return;

for (Entry<KnowledgePath, RatingsHolder> entry : pathRatings.entrySet()) {
RatingsHolder holder = entry.getValue();
if (holder.isDirty()) {
pendingChanges.add(new RatingsChangeSet(holder.getAskingComponentId(), holder.getTargetComponentId(), entry.getKey(), holder.getMyRating()));
}
}
}

@Override
public synchronized List<RatingsChangeSet> getRatingsChangeSets() {
List<RatingsChangeSet> changes = new ArrayList<>();
changes.addAll(pendingChanges);
return changes;
}

public void clearRatingsChangeSets() {
pendingChanges.clear();
}

@Override
public synchronized void update(List<RatingsChangeSet> changeSets) {
if (changeSets == null) return;

for (RatingsChangeSet changeSet : changeSets) {
setRating(changeSet.getAuthorComponentId(), changeSet.getTargetComponentId(), changeSet.getKnowledgePath(), changeSet.getPathRating());
}
}

private Map<PathRating, Long> aggregateRatings(Map<String, PathRating> individualRating) {
return individualRating.values().stream().collect(Collectors.groupingBy(x -> x, Collectors.counting()));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
*/
public class ReadonlyRatingsHolder {

protected Map<PathRating, Integer> ratings;
protected Map<PathRating, Long> ratings;

public int getOpinionsCount(PathRating rating) {
return ratings.get(rating);
protected ReadonlyRatingsHolder(Map<PathRating, Long> ratings) {
this.ratings = ratings;
}

public long getRatings(PathRating rating) {
Long l = ratings.get(rating);
return l == null ? 0l : l.longValue();
}
}
Loading

0 comments on commit d03e325

Please sign in to comment.