Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/current reputation #17

Merged
merged 6 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/main/java/reputation/node/models/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import reputation.node.reputation.credibility.NodeCredibility;
import reputation.node.services.NodeTypeService;
import reputation.node.tangle.LedgerConnector;
import reputation.node.tasks.CalculateNodeReputationTask;
import reputation.node.tasks.ChangeDisturbingNodeBehaviorTask;
import reputation.node.tasks.CheckDevicesTask;
import reputation.node.tasks.CheckNodesServicesTask;
Expand All @@ -66,6 +67,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber {
private int checkNodesServicesTaskTime;
private int waitNodesResponsesTaskTime;
private int changeDisturbingNodeBehaviorTaskTime;
private int calculateNodeReputationTaskTime;
private List<Device> devices;
private List<Transaction> nodesWithServices;
private LedgerConnector ledgerConnector;
Expand All @@ -86,10 +88,11 @@ public class Node implements NodeTypeService, ILedgerSubscriber {
private NodeCredibility nodeCredibility;
private CsvWriterService csvWriter;
private String credibilityHeader;
private String[] csvData = new String[10];
private String[] csvData = new String[11];
private long startedExperiment;
private boolean flagStartedExperiment = true;
private boolean changeDisturbingNodeBehaviorFlag = false;
private double currentReputation;
private static final Logger logger = Logger.getLogger(Node.class.getName());

public Node() {}
Expand Down Expand Up @@ -764,6 +767,20 @@ private void createTasks() {
0,
this.checkNodesServicesTaskTime * 1000
);
new Timer()
.scheduleAtFixedRate(
new CalculateNodeReputationTask(
this,
new ReputationUsingKMeans(
this.kMeans,
this.nodeCredibility,
this.getNodeType().getNodeId()
)
),
0,
this.calculateNodeReputationTaskTime * 1000
);

/* Somente se um nó do tipo perturbador. */
if (this.getNodeType().getType().toString().equals("DISTURBING")) {
new Timer()
Expand Down Expand Up @@ -913,6 +930,8 @@ private float calculateCredibility(
this.csvData[7] = String.valueOf(startedExperiment);
/* Salvando o tempo em que calculou a nova credibilidade. */
this.csvData[8] = String.valueOf(System.currentTimeMillis());
/* Salvando a reputação do nó */
this.csvData[10] = String.valueOf(this.reputationValue);

/* Escrevendo na blockchain a credibilidade calculado do nó avaliador */
try {
Expand Down Expand Up @@ -1395,4 +1414,22 @@ public boolean isUseReputation() {
public void setUseReputation(boolean useReputation) {
this.useReputation = useReputation;
}

public double getCurrentReputation() {
return currentReputation;
}

public void setCurrentReputation(double currentReputation) {
this.currentReputation = currentReputation;
}

public int getCalculateNodeReputationTaskTime() {
return calculateNodeReputationTaskTime;
}

public void setCalculateNodeReputationTaskTime(
int calculateNodeReputationTaskTime
) {
this.calculateNodeReputationTaskTime = calculateNodeReputationTaskTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package reputation.node.tasks;

import dlt.client.tangle.hornet.model.transactions.Transaction;
import java.util.List;
import java.util.TimerTask;
import reputation.node.models.Node;
import reputation.node.reputation.IReputation;

/**
* Task para calcular a reputação atual do nó.
*
* @author Allan Capistrano
* @version 1.0.0
*/
public class CalculateNodeReputationTask extends TimerTask {

private final Node node;
private final IReputation reputation;

/**
* Método construtor.
* @param node Node - O nó que verificará a própria reputação.
* @param reputation IReputation - Objeto para calcular a reputação.
*/
public CalculateNodeReputationTask(Node node, IReputation reputation) {
this.node = node;
this.reputation = reputation;
}

@Override
public void run() {
List<Transaction> evaluationTransactions =
this.node.getLedgerConnector()
.getLedgerReader()
.getTransactionsByIndex(this.node.getNodeType().getNodeId(), false);

double reputationValue =
this.reputation.calculate(
evaluationTransactions,
this.node.isUseLatestCredibility(),
this.node.isUseCredibility()
);

this.node.setReputationValue(reputationValue);
}
}
4 changes: 3 additions & 1 deletion src/main/resources/OSGI-INF/blueprint/blueprint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<property name="checkNodesServicesTaskTime" value="${checkNodesServicesTaskTime}" />
<property name="waitNodesResponsesTaskTime" value="${waitNodesResponsesTaskTime}" />
<property name="changeDisturbingNodeBehaviorTaskTime" value="${changeDisturbingNodeBehaviorTaskTime}" />
<property name="calculateNodeReputationTaskTime" value="${calculateNodeReputationTaskTime}" />
<property name="deviceManager" ref="deviceManagerService" />
<property name="ledgerConnector" ref="ledgerConnector" />
<property name="useCredibility" value="${useCredibility}" />
Expand Down Expand Up @@ -72,11 +73,12 @@
<cm:property name="checkNodesServicesTaskTime" value="45" />
<cm:property name="waitNodesResponsesTaskTime" value="30" />
<cm:property name="changeDisturbingNodeBehaviorTaskTime" value="30" />
<cm:property name="calculateNodeReputationTaskTime" value="20" />
<cm:property name="useCredibility" value="true" />
<cm:property name="useLatestCredibility" value="true" />
<cm:property name="useReputation" value="true" />
<cm:property name="debugModeValue" value="true" />
<cm:property name="credibilityHeader" value="Node_ID,Type,C(n),R,Tr(n),Cr_old(n),Cr_new(n),Started_experiment_time,wrote_file_time,Node_provider_ID" />
<cm:property name="credibilityHeader" value="Node_ID,Type,C(n),R,Tr(n),Cr_old(n),Cr_new(n),Started_experiment_time,wrote_file_time,Node_provider_ID,Reputation" />
</cm:default-properties>
</cm:property-placeholder>
</blueprint>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ checkNodesServicesTaskTime=45
waitNodesResponsesTaskTime=30
# Tempo (segundos) para verificar a reputação e alterar o comportamento do nó do tipo Perbubador.
changeDisturbingNodeBehaviorTaskTime=30
# Tempo (segundos) para verificar o valor da reputação.
calculateNodeReputationTaskTime=20
# Determina se deseja usar (true) ou não (false) a credibilidade no sistema.
useCredibility=true
# Determina se é para usar (true) ou não (false) a credibilidade mais recente para o cálculo da reputação.
Expand All @@ -26,6 +28,6 @@ useLatestCredibility=true
useReputation=true
# Cabeçalho do experimento de credibilidades.
# Obs: Separe somente utilizando vírgula
credibilityHeader=Node_ID,Type,C(n),R,Tr(n),Cr_old(n),Cr_new(n),Started_experiment_time,wrote_file_time,Node_provider_ID
credibilityHeader=Node_ID,Type,C(n),R,Tr(n),Cr_old(n),Cr_new(n),Started_experiment_time,wrote_file_time,Node_provider_ID,Reputation

debugModeValue=true