001/* 002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V.. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.isas.mztab2.validation.handlers; 017 018import de.isas.mztab2.cvmapping.CvMappingUtils; 019import de.isas.mztab2.cvmapping.RuleEvaluationResult; 020import de.isas.mztab2.cvmapping.SetOperations; 021import de.isas.mztab2.io.serialization.ParameterConverter; 022import de.isas.mztab2.model.ValidationMessage; 023import de.isas.mztab2.validation.CvTermValidationHandler; 024import info.psidev.cvmapping.CvMappingRule; 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.List; 028import java.util.Set; 029import uk.ac.ebi.pride.jmztab2.utils.errors.CrossCheckErrorType; 030import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabError; 031import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabErrorType; 032 033/** 034 * Implements 'OR' logic, meaning alternative possible terms or term roots for 035 * an object may be present. 036 * @author nilshoffmann 037 */ 038public class OrValidationHandler implements CvTermValidationHandler { 039 040 @Override 041 public List<ValidationMessage> handleParameters(RuleEvaluationResult result, 042 boolean errorOnTermNotInRule) { 043 // or logic means, any of the defined terms may be present, or none 044 final List<ValidationMessage> messages = new ArrayList<>(); 045 // all defined terms or children thereof need to appear 046 Set<String> matchedParameters = SetOperations.intersection(result. 047 getAllowedParameters(). 048 keySet(), result.getFoundParameters(). 049 keySet()); 050 if (matchedParameters.isEmpty()) { 051 for (String s : result.getAllowedParameters(). 052 keySet()) { 053 MZTabErrorType errorType = null; 054 switch (result.getRule(). 055 getRequirementLevel()) { 056 case MAY: 057 errorType = CrossCheckErrorType.CvTermOptional; 058 break; 059 case SHOULD: 060 errorType = CrossCheckErrorType.CvTermRecommended; 061 break; 062 case MUST: 063 errorType = CrossCheckErrorType.CvTermRequired; 064 break; 065 default: 066 throw new IllegalArgumentException( 067 "Unknown requirement level value: " + result. 068 getRule(). 069 getRequirementLevel() + "! Supported are: " + Arrays. 070 toString(CvMappingRule.RequirementLevel. 071 values())); 072 } 073 MZTabError error = new MZTabError(errorType, -1, 074 new ParameterConverter().convert(result. 075 getAllowedParameters(). 076 get( 077 s)), result.getRule(). 078 getCvElementPath(), CvMappingUtils. 079 niceToString( 080 result.getRule())); 081 messages.add(error.toValidationMessage()); 082 } 083 } 084 return messages; 085 } 086}