Skip to content

Commit

Permalink
https://github.com/metadatacenter/cedar-project/issues/1198
Browse files Browse the repository at this point in the history
  • Loading branch information
Attila L. Egyedi committed Nov 8, 2023
1 parent edb7301 commit f167b32
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -121,11 +122,9 @@ public Response generateRules(@PathParam(PP_ID) String templateId) throws CedarE
c.must(c.user()).be(LoggedIn);
c.must(c.user()).have(CedarPermission.RULES_INDEX_REINDEX);

List<String> templateIds = new ArrayList<>(Arrays.asList(templateId));
List<String> templateIds = new ArrayList<>(Collections.singletonList(templateId));
// Run the rules generation process in a new thread
Executors.newSingleThreadExecutor().submit(() -> {
valueRecommenderService.generateRules(templateIds);
});
Executors.newSingleThreadExecutor().submit(() -> valueRecommenderService.generateRules(templateIds));
return Response.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ private static ProcessingReport validate(JsonNode schema, JsonNode instance) thr

public static String extractProcessingReportMessages(ProcessingReport report) {
if (!report.isSuccess()) {
String msg = "";
StringBuilder msg = new StringBuilder();
for (final ProcessingMessage reportLine : report) {
msg += reportLine.getMessage() + "\n";
msg.append(reportLine.getMessage()).append("\n");
}
return msg;
return msg.toString();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ logger.hibernate.level = warn

logger.netty.name = io.netty.util.internal
logger.netty.level = warn

logger.elasticsearch.name = org.elasticsearch.transport
logger.elasticsearch.level = warn
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.metadatacenter.config.CedarConfig;
import org.metadatacenter.exception.CedarProcessingException;
import org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.AssociationRulesService;
import org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.AssociationRulesUtils;
import org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.RulesGenerationStatusManager;
import org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.elasticsearch.EsRule;
import org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.elasticsearch.EsRuleItem;
Expand All @@ -19,7 +18,6 @@
import org.metadatacenter.intelligentauthoring.valuerecommender.util.CedarFieldUtils;
import org.metadatacenter.intelligentauthoring.valuerecommender.util.CedarTextUtils;
import org.metadatacenter.intelligentauthoring.valuerecommender.util.CedarUtils;
import org.metadatacenter.search.IndexedDocumentType;
import org.metadatacenter.server.search.elasticsearch.service.RulesIndexingService;
import org.metadatacenter.intelligentauthoring.valuerecommender.io.*;
import org.metadatacenter.server.valuerecommender.model.RulesGenerationStatus;
Expand Down Expand Up @@ -244,7 +242,7 @@ private List<RecommendedValue> generateRecommendations(List<Field> populatedFiel
double newSupport = labelSupportMap.get(valueLabel) + rule.getSupport();
labelSupportMap.put(valueLabel, newSupport);
}
totalSupport += rule.getSupport();
totalSupport += (int) rule.getSupport();
}

// Calculate the recommendation score for each value based on the support
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.metadatacenter.intelligentauthoring.valuerecommender.associationrules;

import org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.elasticsearch.EsRule;
import org.metadatacenter.intelligentauthoring.valuerecommender.domainobjects.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weka.associations.Apriori;
import weka.associations.AssociationRule;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,22 +323,22 @@ private static ArffInstance generateArffInstance(Object templateInstanceDocument

// Build the JsonPath expression
for (TemplateNode field : fields) {
String jsonPath = "$";
StringBuilder jsonPath = new StringBuilder("$");
// Analyze the field path to check if it contains arrays. For every array we need to specify an index (e.g., a[2])
String path = "";
for (String key : field.getPath()) {
jsonPath = jsonPath + ("['" + key + "']");
jsonPath.append("['").append(key).append("']");
// If it is an array, concat index
path = generatePathDotNotation(path, key);
if (arraysPaths != null && arraysPaths.contains(path)) {
int index = arraysPaths.indexOf(path);
jsonPath = jsonPath + ("[" + arraysIndexes.get(index) + "]");
jsonPath.append("[").append(arraysIndexes.get(index)).append("]");
}
}
// Query the instance to extract the value
//System.out.println("Path: " + jsonPath);
try {
Map attValueMap = JsonPath.read(templateInstanceDocument, jsonPath);
Map attValueMap = JsonPath.read(templateInstanceDocument, jsonPath.toString());
Optional<ArffAttributeValue> attValue = CedarUtils.getValueOfField(attValueMap);
if (attValue.isPresent()) {
attValues.add(attValue.get().getArffValueString()); // Escape single quote
Expand Down Expand Up @@ -593,17 +593,13 @@ public static List<EsRule> toEsRules(Apriori aprioriResults, String templateId)

// Build premise
List<EsRuleItem> esPremise = new ArrayList<>();
Iterator<Item> itPremise = rule.getPremise().iterator();
while (itPremise.hasNext()) {
Item item = itPremise.next();
for (Item item : rule.getPremise()) {
esPremise.add(buildEsRuleItem(item));
}

// Build consequence
List<EsRuleItem> esConsequence = new ArrayList<>();
Iterator<Item> itConsequence = rule.getConsequence().iterator();
while (itConsequence.hasNext()) {
Item item = itConsequence.next();
for (Item item : rule.getConsequence()) {
esConsequence.add(buildEsRuleItem(item));
}

Expand Down Expand Up @@ -789,16 +785,16 @@ public static String getEsItemFieldNormalizedValue(Item item) {
}

public static String ruleItemsToString(List<EsRuleItem> ruleItems, boolean showNullTypes) {
String result = "";
StringBuilder result = new StringBuilder();
boolean firstPremiseItem = true;
for (EsRuleItem ruleItem : ruleItems) {
if (!firstPremiseItem) {
result += " AND ";
result.append(" AND ");
}
result += ruleItem.toPrettyString(false);
result.append(ruleItem.toPrettyString(false));
firstPremiseItem = false;
}
return result;
return result.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.metadatacenter.intelligentauthoring.valuerecommender.associationrules;

import org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.elasticsearch.EsRule;
import org.metadatacenter.intelligentauthoring.valuerecommender.domainobjects.Field;
import weka.associations.AssociationRule;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.elasticsearch;

import java.util.Optional;

/**
* This class represents an attribute value.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.elasticsearch;

import com.sun.nio.sctp.AssociationChangeNotification;
import org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.AssociationRulesUtils;
import weka.associations.AssociationRule;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.metadatacenter.intelligentauthoring.valuerecommender.associationrules.elasticsearch;

import org.metadatacenter.intelligentauthoring.valuerecommender.util.FieldValueResultUtils;

import java.util.List;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,5 @@
package org.metadatacenter.intelligentauthoring.valuerecommender.domainobjects;

public class RecommendationDetails {
public record RecommendationDetails(String rule, Double contextMatchingScore, Double ruleConfidence, Double ruleSupport, RecommendedValue.RecommendationType recommendationType) {

private final String rule;
private final Double contextMatchingScore;
private final Double ruleConfidence;
private final Double ruleSupport;
private final RecommendedValue.RecommendationType recommendationType;

public RecommendationDetails(String rule, Double contextMatchingScore, Double ruleConfidence, Double ruleSupport,
RecommendedValue.RecommendationType recommendationType) {

this.rule = rule;
this.contextMatchingScore = contextMatchingScore;
this.ruleConfidence = ruleConfidence;
this.ruleSupport = ruleSupport;
this.recommendationType = recommendationType;
}

public String getRule() {
return rule;
}

public Double getContextMatchingScore() {
return contextMatchingScore;
}

public Double getRuleConfidence() {
return ruleConfidence;
}

public Double getRuleSupport() {
return ruleSupport;
}

public RecommendedValue.RecommendationType getRecommendationType() {
return recommendationType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int compareTo(RecommendedValue value) {
if (Math.abs(value1 - value2) >= 0.001) {
return Double.compare(value1, value2);
} else { // If the recommendation scores are close enough, return the value with higher support
return Double.compare(value.getDetails().getRuleSupport(), getDetails().getRuleSupport());
return Double.compare(value.getDetails().ruleSupport(), getDetails().ruleSupport());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public static void indexAllFilesInFolder(Client client, String indexName, String
File[] listOfFiles = folder.listFiles();


for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
for (File file : listOfFiles) {
if (file.isFile()) {
String fileContent = readFile(file.getPath(), StandardCharsets.UTF_8);
indexJson(client, indexName, typeName, fileContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static List<String> getMappings(String uri, boolean includeCurrentUri) {
}

public static boolean isSameConcept(String uri1, String uri2) {
if (uri1.toLowerCase().equals(uri2.toLowerCase())) {
if (uri1.equalsIgnoreCase(uri2)) {
return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.metadatacenter.intelligentauthoring.valuerecommender.util;

import java.util.List;

/**
* Constants of general utility.
* All member of this class are immutable.
Expand Down

0 comments on commit f167b32

Please sign in to comment.