- Code polishing.

- Update dependencies.
This commit is contained in:
Lampros Smyrnaios 2023-11-07 15:37:33 +02:00
parent a545e34f04
commit 6d2838f480
10 changed files with 28 additions and 46 deletions

View File

@ -44,13 +44,13 @@
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.13.0</version> <version>2.15.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>32.1.2-jre</version> <version>32.1.3-jre</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>

View File

@ -42,20 +42,16 @@ class ElementSpecCompiler {
specs.add(currentElement.spec); specs.add(currentElement.spec);
if (currentElement.parent == null) { if ( currentElement.parent == null )
currentElement.createRootElemRule(compilationResult); currentElement.createRootElemRule(compilationResult);
} else
else {
currentElement.createSubElemRule(compilationResult); currentElement.createSubElemRule(compilationResult);
}
for (AttributeSpec attrSpec : currentElement.spec.attributeSpecs()) { for ( AttributeSpec attrSpec : currentElement.spec.attributeSpecs() )
currentElement.createAttrRule(compilationResult, attrSpec); currentElement.createAttrRule(compilationResult, attrSpec);
}
for(ElementSpec subElementSpec: currentElement.spec.subElementSpecs()) { for ( ElementSpec subElementSpec: currentElement.spec.subElementSpecs() )
generateRulesForElement(new ElementStruct(currentElement.elementsArray, subElementSpec, currentElement)); generateRulesForElement(new ElementStruct(currentElement.elementsArray, subElementSpec, currentElement));
}
} }
@ -97,21 +93,17 @@ class ElementSpecCompiler {
String valuePrefix, String valuePrefix,
Predicate<String> allowedValuesPredicate, Predicate<String> allowedValuesPredicate,
Cardinality cardinality) { Cardinality cardinality) {
StringBuilder builder = StringBuilder builder =
new StringBuilder(requirementLevel.name()).append(" rule: The ").append(nodeName).append(" ").append(nodeType); new StringBuilder(requirementLevel.name()).append(" rule: The ").append(nodeName).append(" ").append(nodeType);
if (position != ElementPosition.ALL) { if ( position != ElementPosition.ALL )
builder.append(" at ").append(position.name().toLowerCase()).append(" position"); builder.append(" at ").append(position.name().toLowerCase()).append(" position");
}
if (valuePrefix != null) { if ( valuePrefix != null )
builder.append(" has values that start with ").append(valuePrefix).append(","); builder.append(" has values that start with ").append(valuePrefix).append(",");
}
if (!allowsAllValues(allowedValuesPredicate)) { if ( !allowsAllValues(allowedValuesPredicate) )
builder.append(" contains allowed values only,"); builder.append(" contains allowed values only,");
}
builder.append(" has cardinality ").append(cardinality.asText()); builder.append(" has cardinality ").append(cardinality.asText());
@ -123,12 +115,10 @@ class ElementSpecCompiler {
Cardinality cardinality, Cardinality cardinality,
String valuePrefix) { String valuePrefix) {
String id = requirementLevel.name() + " rule: " + nodeName + " has cardinality " + cardinality.asText(); String id = requirementLevel.name() + " rule: " + nodeName + " has cardinality " + cardinality.asText();
if(Helper.isEmpty(valuePrefix)) { if ( Helper.isEmpty(valuePrefix) )
return id; return id;
} else
else {
return id + " for values starting with '" + valuePrefix + "'"; return id + " for values starting with '" + valuePrefix + "'";
}
} }
static final String mergeNodeNames(String... names) { static final String mergeNodeNames(String... names) {
@ -228,6 +218,7 @@ class ElementSpecCompiler {
if (parents == null) { if (parents == null) {
return (Integer count) -> false; // There are no parents, actually return (Integer count) -> false; // There are no parents, actually
} }
int parentsLength = parents.getLength(); int parentsLength = parents.getLength();
if (cardinality == Cardinality.ONE) { if (cardinality == Cardinality.ONE) {
return (Integer count) -> count == parentsLength; // A sub-element can be present in each parent return (Integer count) -> count == parentsLength; // A sub-element can be present in each parent
@ -241,10 +232,8 @@ class ElementSpecCompiler {
else if (cardinality == Cardinality.FOUR_TO_N){ else if (cardinality == Cardinality.FOUR_TO_N){
return (Integer count) -> count >= parentsLength * 4; // Four or more sub-elements can be present in each parent return (Integer count) -> count >= parentsLength * 4; // Four or more sub-elements can be present in each parent
} }
else{ // not reachable else // not reachable
throw new RuleEvaluationException(" Unsupported cardinality " + cardinality, null); throw new RuleEvaluationException(" Unsupported cardinality " + cardinality, null);
}
} }
private static NodeList attributesOf(NodeList elements, String attrName, Predicate<String> allowedValues) { private static NodeList attributesOf(NodeList elements, String attrName, Predicate<String> allowedValues) {
@ -494,10 +483,8 @@ class ElementSpecCompiler {
} catch (Throwable t) { } catch (Throwable t) {
throw new RuleEvaluationException(t.getMessage(), t); throw new RuleEvaluationException(t.getMessage(), t);
} }
} } else
else {
return true; return true;
}
} }
} }
@ -587,10 +574,8 @@ class ElementSpecCompiler {
guidelineEvaluation.setNodesOf(thisId, nodes); guidelineEvaluation.setNodesOf(thisId, nodes);
} }
return result; return result;
} } else
else {
return true; return true;
}
} }
@Override @Override
@ -601,7 +586,6 @@ class ElementSpecCompiler {
private static Predicate<Document> isApplicable(SyntheticRule<Document> rule, private static Predicate<Document> isApplicable(SyntheticRule<Document> rule,
GuidelineEvaluation guidelineEvaluation) { GuidelineEvaluation guidelineEvaluation) {
return (Document doc) -> { return (Document doc) -> {
Rule<Document> applicabilityRule = rule.applicabilityRule(); Rule<Document> applicabilityRule = rule.applicabilityRule();
if (applicabilityRule == null) { if (applicabilityRule == null) {

View File

@ -32,7 +32,7 @@ public final class SyntheticGuideline extends AbstractGuideline<Document> {
logger.debug("Validating document {} in {}", id, Thread.currentThread()); logger.debug("Validating document {} in {}", id, Thread.currentThread());
evaluation.set(new GuidelineEvaluation(id, document, getWeight())); evaluation.set(new GuidelineEvaluation(id, document, getWeight()));
Result result = evaluation.get().evaluate(compilationResult); Result result = evaluation.get().evaluate(compilationResult);
logger.debug("Evaluated: {} in {} with result {}", evaluation.get(), Thread.currentThread(), result); logger.debug("Evaluated: {} in thread_{} with result {}", evaluation.get(), Thread.currentThread().getName(), result);
evaluation.remove(); evaluation.remove();
return result; return result;
} }

View File

@ -308,8 +308,7 @@ public class DataArchiveGuidelinesV2Profile extends AbstractOpenAireProfile {
) )
); );
private static final Map<String, SyntheticGuideline> GUIDELINE_MAP = GUIDELINES. private static final Map<String, SyntheticGuideline> GUIDELINE_MAP = GUIDELINES.stream().
stream().
collect(Collectors.toMap(SyntheticGuideline::getName, (guideline) -> guideline)); collect(Collectors.toMap(SyntheticGuideline::getName, (guideline) -> guideline));
private static final int MAX_SCORE = GUIDELINES.stream().map(SyntheticGuideline::getWeight).reduce(0, Integer::sum); private static final int MAX_SCORE = GUIDELINES.stream().map(SyntheticGuideline::getWeight).reduce(0, Integer::sum);

View File

@ -281,8 +281,7 @@ public final class FAIR_Data_GuidelinesProfile extends AbstractOpenAireProfile {
) )
); );
private static final Map<String, Guideline> GUIDELINE_MAP = GUIDELINES. private static final Map<String, Guideline> GUIDELINE_MAP = GUIDELINES.stream().
stream().
collect(Collectors.toMap(Guideline::getName, (guideline) -> guideline)); collect(Collectors.toMap(Guideline::getName, (guideline) -> guideline));
private static final int MAX_SCORE = GUIDELINES.stream().map(Guideline::getWeight).reduce(0, Integer::sum); private static final int MAX_SCORE = GUIDELINES.stream().map(Guideline::getWeight).reduce(0, Integer::sum);

View File

@ -241,8 +241,7 @@ public final class FAIR_Literature_GuidelinesV4Profile extends AbstractOpenAireP
) )
); );
private static final Map<String, Guideline> GUIDELINE_MAP = GUIDELINES. private static final Map<String, Guideline> GUIDELINE_MAP = GUIDELINES.stream().
stream().
collect(Collectors.toMap(Guideline::getName, (guideline) -> guideline)); collect(Collectors.toMap(Guideline::getName, (guideline) -> guideline));
private static final int MAX_SCORE = GUIDELINES.stream().map(Guideline::getWeight).reduce(0, Integer::sum); private static final int MAX_SCORE = GUIDELINES.stream().map(Guideline::getWeight).reduce(0, Integer::sum);

View File

@ -295,8 +295,7 @@ public final class LiteratureGuidelinesV3Profile extends AbstractOpenAireProfile
) )
); );
private static final Map<String, SyntheticGuideline> GUIDELINE_MAP = GUIDELINES. private static final Map<String, SyntheticGuideline> GUIDELINE_MAP = GUIDELINES.stream().
stream().
collect(Collectors.toMap(SyntheticGuideline::getName, (guideline) -> guideline)); collect(Collectors.toMap(SyntheticGuideline::getName, (guideline) -> guideline));
private static final int MAX_SCORE = GUIDELINES.stream().map(SyntheticGuideline::getWeight).reduce(0, Integer::sum); private static final int MAX_SCORE = GUIDELINES.stream().map(SyntheticGuideline::getWeight).reduce(0, Integer::sum);

View File

@ -527,8 +527,7 @@ public final class LiteratureGuidelinesV4Profile extends AbstractOpenAireProfile
) )
); );
private static final Map<String, SyntheticGuideline> GUIDELINE_MAP = GUIDELINES. private static final Map<String, SyntheticGuideline> GUIDELINE_MAP = GUIDELINES.stream().
stream().
collect(Collectors.toMap(SyntheticGuideline::getName, (guideline) -> guideline)); collect(Collectors.toMap(SyntheticGuideline::getName, (guideline) -> guideline));
private static final int MAX_SCORE = GUIDELINES.stream().map(SyntheticGuideline::getWeight).reduce(0, Integer::sum); private static final int MAX_SCORE = GUIDELINES.stream().map(SyntheticGuideline::getWeight).reduce(0, Integer::sum);

View File

@ -9,6 +9,8 @@ public class TestUtils {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestUtils.class); private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestUtils.class);
public static final String TEST_FILES_BASE_DIR = "src/test/resources/";
public static DocumentBuilder getDocumentBuilder() public static DocumentBuilder getDocumentBuilder()
{ {

View File

@ -18,12 +18,13 @@ public class Test_v4 {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Test_v4.class); private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Test_v4.class);
private static final String TEST_FILES_V4_DIR = TestUtils.TEST_FILES_BASE_DIR + "openaireguidelinesV4/";
private static final String[] FILES = new String[] { private static final String[] FILES = new String[] {
// "src/test/resources/openaireguidelinesV4/v4_literature_all_invalid_guidelines_record.xml", // TEST_FILES_V4_DIR + "v4_literature_all_invalid_guidelines_record.xml",
"src/test/resources/openaireguidelinesV4/v4_literature_all_guidelines_record.xml", // TEST_FILES_V4_DIR + "v4_literature_all_guidelines_record.xml",
// "src/test/resources/openaireguidelinesV4/oai_mediarep_org_doc_2534.xml", // TEST_FILES_V4_DIR + "oai_mediarep_org_doc_2534.xml",
// "src/test/resources/openaireguidelinesV4/01_gv4.xml" TEST_FILES_V4_DIR + "01_gv4.xml"
}; };
public static void main(String[] args) { public static void main(String[] args) {