-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] implementation of ratings manager, process invocations, tests
- Loading branch information
Showing
41 changed files
with
957 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
jdeeco-core/src/cz/cuni/mff/d3s/deeco/integrity/RatingsChangeSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
jdeeco-core/src/cz/cuni/mff/d3s/deeco/integrity/RatingsHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 10 additions & 1 deletion
11
jdeeco-core/src/cz/cuni/mff/d3s/deeco/integrity/RatingsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
14 changes: 0 additions & 14 deletions
14
jdeeco-core/src/cz/cuni/mff/d3s/deeco/integrity/RatingsManagerFactory.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
jdeeco-core/src/cz/cuni/mff/d3s/deeco/integrity/RatingsManagerFactoryImpl.java
This file was deleted.
Oops, something went wrong.
113 changes: 106 additions & 7 deletions
113
jdeeco-core/src/cz/cuni/mff/d3s/deeco/integrity/RatingsManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.