List of usage examples for org.jdom2 Element getName
public String getName()
From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java
License:Open Source License
/** * Carga la descripcin de un caso de estudio para sistemas de recomendacin * para grupos./*from www . j a v a2 s.c o m*/ * * @param file Archivo donde se encuentra almacenado el caso de estudio. * @return Caso de estudio recuperado del archivo. * @throws org.jdom2.JDOMException Cuando se intenta cargar un xml que no * tiene la estructura esperada. Chequear si esta desfasada la versin. * @throws IOException Cuando no se puede leer el archivo indicado o existe * algun fallo de conversin de datos al leer el contenido del mismo. */ public static GroupCaseStudyConfiguration loadGroupCaseWithResults(File file) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(file); Element caseStudy = doc.getRootElement(); if (!caseStudy.getName().equals(CASE_ROOT_ELEMENT_NAME)) { throw new IllegalArgumentException("The XML does not contains a Case Study."); } GroupRecommenderSystem<Object, Object> groupRecommenderSystem = GroupRecommenderSystemXML .getGroupRecommenderSystem(caseStudy.getChild(GroupRecommenderSystemXML.ELEMENT_NAME)); GroupFormationTechnique groupFormationTechnique = GroupFormationTechniqueXML .getGroupFormationTechnique(caseStudy.getChild(GroupFormationTechniqueXML.ELEMENT_NAME)); ValidationTechnique validationTechnique = ValidationTechniqueXML .getValidationTechnique(caseStudy.getChild(ValidationTechniqueXML.ELEMENT_NAME)); GroupPredictionProtocol groupPredictionProtocol = GroupPredictionProtocolXML .getGroupPredictionProtocol(caseStudy.getChild(GroupPredictionProtocolXML.ELEMENT_NAME)); RelevanceCriteria relevanceCriteria = RelevanceCriteriaXML .getRelevanceCriteria(caseStudy.getChild(RelevanceCriteriaXML.ELEMENT_NAME)); DatasetLoader<? extends Rating> datasetLoader = DatasetLoaderXML .getDatasetLoader(caseStudy.getChild(DatasetLoaderXML.ELEMENT_NAME)); Map<GroupEvaluationMeasure, GroupEvaluationMeasureResult> groupEvaluationMeasuresResults = getEvaluationMeasures( caseStudy); long seed = Long.parseLong(caseStudy.getAttributeValue(SeedHolder.SEED.getName())); int numExecutions = Integer.parseInt(caseStudy.getAttributeValue(NUM_EXEC_ATTRIBUTE_NAME)); String caseStudyAlias = caseStudy.getAttributeValue(ParameterOwner.ALIAS.getName()); return new GroupCaseStudyConfiguration(groupRecommenderSystem, datasetLoader, groupFormationTechnique, validationTechnique, groupPredictionProtocol, relevanceCriteria, caseStudyAlias, numExecutions, seed, groupEvaluationMeasuresResults); }
From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java
License:Open Source License
public static int extractResultNumExec(File groupCaseStudyXML) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(groupCaseStudyXML); Element caseStudy = doc.getRootElement(); if (!caseStudy.getName().equals(CASE_ROOT_ELEMENT_NAME)) { throw new IllegalArgumentException("The XML does not contains a Case Study."); }// w w w. j av a2 s. c om Integer numExec = new Integer(caseStudy.getAttributeValue(NUM_EXEC_ATTRIBUTE_NAME)); return numExec; }
From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java
License:Open Source License
private static Map<GroupEvaluationMeasure, GroupEvaluationMeasureResult> getEvaluationMeasures( Element caseStudy) {/* w w w .ja va2 s.co m*/ Map<GroupEvaluationMeasure, GroupEvaluationMeasureResult> groupEvaluationMeasures = new TreeMap<>(); Element aggregateValues = caseStudy.getChild(AGGREGATE_VALUES_ELEMENT_NAME); if (aggregateValues == null) { throw new IllegalStateException( "Unable to load a case study description only, the XML must have results details."); } for (Element groupEvaluationMeasureResultElement : aggregateValues.getChildren()) { String name = groupEvaluationMeasureResultElement.getName(); GroupEvaluationMeasure groupEvaluationMeasure = GroupEvaluationMeasuresFactory.getInstance() .getClassByName(name); if (groupEvaluationMeasure == null) { throw new IllegalStateException( "The group evaluation measure '" + name + "' does not exists in delfos' factory"); } else { groupEvaluationMeasures.put(groupEvaluationMeasure, groupEvaluationMeasure .getGroupEvaluationMeasureResultFromXML(groupEvaluationMeasureResultElement)); } } return groupEvaluationMeasures; }
From source file:delfos.group.io.xml.evaluationmeasures.PRSpaceGroupsXML.java
License:Open Source License
public static ConfusionMatricesCurve getConfusionMatricesCurve(Element e) throws UnrecognizedElementException { if (!e.getName().equals(MEASURE_ELEMENT)) { throw new UnrecognizedElementException( "Element name is '" + e.getName() + "', expected value '" + MEASURE_ELEMENT + "'"); }/*from w w w .ja v a 2s .c om*/ Element curveElement = e.getChild(ConfusionMatricesCurveXML.CURVE_ELEMENT); if (e == null) { throw new UnrecognizedElementException( PRSpaceGroupsXML.class.getSimpleName() + ": Curve element not found"); } return ConfusionMatricesCurveXML.getConfusionMatricesCurve(curveElement); }
From source file:delfos.io.xml.casestudy.CaseStudyXML.java
License:Open Source License
/** * Carga un caso de estudio desde un archivo XML * * @param file Archivo XML del que se carga la configuracin del caso de estudio. * @return/*w w w . j a v a2 s. c om*/ * @throws org.jdom2.JDOMException * @throws java.io.IOException */ public static CaseStudyConfiguration loadCase(File file) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(file); Element caseStudy = doc.getRootElement(); if (!caseStudy.getName().equals(CASE_ROOT_ELEMENT_NAME)) { throw new IllegalArgumentException( "The XML does not contains a Case Study (" + file.getAbsolutePath() + ")"); } GenericRecommenderSystem<Object> recommenderSystem = RecommenderSystemXML .getRecommenderSystem(caseStudy.getChild(RecommenderSystemXML.ELEMENT_NAME)); DatasetLoader<? extends Rating> datasetLoader = DatasetLoaderXML .getDatasetLoader(caseStudy.getChild(DatasetLoaderXML.ELEMENT_NAME)); ValidationTechnique validationTechnique = ValidationTechniqueXML .getValidationTechnique(caseStudy.getChild(ValidationTechniqueXML.ELEMENT_NAME)); PredictionProtocol predictionProtocol = PredictionProtocolXML .getPredictionProtocol(caseStudy.getChild(PredictionProtocolXML.ELEMENT_NAME)); RelevanceCriteria relevanceCriteria = RelevanceCriteriaXML .getRelevanceCriteria(caseStudy.getChild(RelevanceCriteriaXML.ELEMENT_NAME)); return new CaseStudyConfiguration(recommenderSystem, datasetLoader, validationTechnique, predictionProtocol, relevanceCriteria); }
From source file:delfos.io.xml.casestudy.CaseStudyXML.java
License:Open Source License
/** * Carga un caso de estudio desde un archivo XML * * @param file Archivo XML del que se carga la configuracin del caso de estudio. * @return/*from w w w.j a v a2 s . c o m*/ * @throws org.jdom2.JDOMException * @throws java.io.IOException */ public static CaseStudyResults loadCaseResults(File file) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(file); Element caseStudy = doc.getRootElement(); if (!caseStudy.getName().equals(CASE_ROOT_ELEMENT_NAME)) { throw new IllegalArgumentException("The XML does not contains a Case Study."); } GenericRecommenderSystem<Object> recommenderSystem = RecommenderSystemXML .getRecommenderSystem(caseStudy.getChild(RecommenderSystemXML.ELEMENT_NAME)); ValidationTechnique validationTechnique = ValidationTechniqueXML .getValidationTechnique(caseStudy.getChild(ValidationTechniqueXML.ELEMENT_NAME)); PredictionProtocol predictionProtocol = PredictionProtocolXML .getPredictionProtocol(caseStudy.getChild(PredictionProtocolXML.ELEMENT_NAME)); DatasetLoader<? extends Rating> datasetLoader = DatasetLoaderXML .getDatasetLoader(caseStudy.getChild(DatasetLoaderXML.ELEMENT_NAME)); EvaluationMeasuresResults aggregatedElement = getAggregateEvaluationMeasures( caseStudy.getChild("Aggregate_values")); return new CaseStudyResults(recommenderSystem, datasetLoader, validationTechnique, predictionProtocol, aggregatedElement.evaluationMeasuresResults, aggregatedElement.buildTime, aggregatedElement.recommendationTime); }
From source file:delfos.io.xml.casestudy.CaseStudyXML.java
License:Open Source License
static EvaluationMeasuresResults getAggregateEvaluationMeasures(Element element) { if (!element.getName().equals("Aggregate_values")) { throw new IllegalArgumentException("The XML element is not an aggregate values element."); }/*from w ww . ja va2s . c o m*/ Map<EvaluationMeasure, Double> ret = new TreeMap<>(); long buildTime = -1; long recommendationTime = -1; for (Element child : element.getChildren()) { String evaluationMeasureName = child.getName(); EvaluationMeasure evaluationMeasure = EvaluationMeasuresFactory.getInstance() .getClassByName(evaluationMeasureName); String valueString = child.getAttributeValue("value"); if (valueString == null) { continue; } double measureValue = new Double(valueString); if (evaluationMeasure == null) { throw new IllegalStateException("Evaluation measure cannot be null."); } ret.put(evaluationMeasure, measureValue); } return new EvaluationMeasuresResults(ret, buildTime, recommendationTime); }
From source file:delfos.io.xml.dataset.DatasetLoaderXML.java
License:Open Source License
public static DatasetLoader<? extends Rating> getDatasetLoader(Element datasetLoaderElement) { if (!datasetLoaderElement.getName().equals(ELEMENT_NAME)) { throw new IllegalArgumentException("The element does not contains a dataset loader. [Element name '" + datasetLoaderElement.getName() + "']"); }/*from w w w. j a v a 2s . co m*/ String name = datasetLoaderElement.getAttributeValue(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_NAME); String parameterOwnerType = datasetLoaderElement .getAttributeValue(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_TYPE); if (parameterOwnerType == null) { datasetLoaderElement.setAttribute(ParameterOwnerXML.PARAMETER_OWNER_ATTRIBUTE_TYPE, ParameterOwnerType.DATASET_LOADER.name()); } if (datasetLoaderElement.getAttribute(CONFIGURED_DATASET_ATTRIBUTE) != null && "true".equals(datasetLoaderElement.getAttributeValue(CONFIGURED_DATASET_ATTRIBUTE))) { //Es un dataset configurado, hay que recuperarlo de la factora de dataset configurados. DatasetLoader<? extends Rating> datasetLoader = ConfiguredDatasetsFactory.getInstance() .getDatasetLoader(name); return datasetLoader; } else { //Un dataset de toda la vida. ParameterOwner parameterOwner = ParameterOwnerXML.getParameterOwner(datasetLoaderElement); if (parameterOwner instanceof DatasetLoader) { DatasetLoader<? extends Rating> datasetLoader = (DatasetLoader) parameterOwner; return datasetLoader; } else { IllegalStateException ex = new IllegalStateException( "The XML does not have the expected structure: The loaded parameter owner is not a dataset loader [" + parameterOwner + "]"); ERROR_CODES.CANNOT_LOAD_CONFIG_FILE.exit(ex); throw ex; } } }
From source file:delfos.io.xml.parameterowner.parameter.ParameterOwnerParameterXML.java
License:Open Source License
public static ParameterOwner getParameterOwnerParameterValue(ParameterOwner parameterOwner, Element parameterElement) { if (parameterElement == null) { throw new IllegalArgumentException("The element cannot be null."); }//w w w . ja v a 2s . c om if (!parameterElement.getName().equals(ParameterXML.PARAMETER_ELEMENT_NAME)) { throw new IllegalArgumentException("The element does not describe a parameter."); } if (!parameterElement.getAttributeValue(ParameterXML.PARAMETER_TYPE) .equals(ParameterOwnerRestriction.class.getSimpleName())) { throw new IllegalArgumentException( "The element does not describe a parameter of type " + ParameterOwnerRestriction.class); } String parameterName = parameterElement.getAttributeValue(ParameterXML.PARAMETER_NAME); Parameter parameterByName = parameterOwner.getParameterByName(parameterName); if (parameterByName == null) { IllegalStateException ex = new IllegalStateException( parameterOwner.getName() + " doesn't have the parameter '" + parameterName + "'\n"); ERROR_CODES.PARAMETER_OWNER_NOT_HAVE_PARAMETER.exit(ex); throw ex; } String parameterValueString = parameterElement.getAttributeValue(ParameterXML.PARAMETER_VALUE); ParameterOwner innerParameterOwner = null; try { innerParameterOwner = (ParameterOwner) parameterByName.parseString(parameterValueString); } catch (ClassCastException ex) { throw new IllegalStateException("El tipo del parametro no es el detectado!!"); } catch (CannotParseParameterValue ex) { ERROR_CODES.PARAMETER_OWNER_ILLEGAL_PARAMETER_VALUE.exit(ex); throw new IllegalStateException(ex); } if (innerParameterOwner == null) { throw new IllegalStateException("Parameter owner '" + parameterValueString + "' not found."); } for (Element innerParameterElement : parameterElement.getChildren(ParameterXML.PARAMETER_ELEMENT_NAME)) { String innerParameterName = innerParameterElement.getAttributeValue(ParameterXML.PARAMETER_NAME); Parameter innerParameter = innerParameterOwner.getParameterByName(innerParameterName); Object innerParameterValue = ParameterXML.getParameterValue(innerParameterOwner, innerParameterElement); innerParameterOwner.setParameterValue(innerParameter, innerParameterValue); } return innerParameterOwner; }
From source file:delfos.io.xml.recommendations.RecommendationsToXML.java
License:Open Source License
/** * Convierte el elemento XML indicado en un objeto que representa las recomendaciones al usuario. * * @param element Elemento XML a convertir. * @return Recomendaciones./* w w w .j a v a2 s . c o m*/ * * @throws IllegalArgumentException Si el elemento no contiene la informacin necesaria para recuperar un objeto * {@link Recommendations}. * * @see RecommendationsToXML#getRecommendationsElement(delfos.RS.Recommendation.Recommendations) * */ public static Recommendations getRecommendations(Element element) { if (!element.getName().equals(RECOMMENDATIONS_ELEMENT_NAME)) { throw new IllegalArgumentException("Element name doesn't match this reader: found '" + element.getName() + "' expected '" + RECOMMENDATIONS_ELEMENT_NAME + "'"); } String idTarget = element.getAttributeValue(ID_TARGET_ATTRIBUTE_NAME); Map<DetailField, Object> details = new TreeMap<>(); for (Attribute attribute : element.getAttributes()) { if (ID_TARGET_ATTRIBUTE_NAME.equals(attribute.getName())) { continue; } DetailField detailField = DetailField.valueOfNoCase(attribute.getName()); String detailFieldValueString = attribute.getValue(); Object detailFieldValue = detailField.parseValue(detailFieldValueString); details.put(detailField, detailFieldValue); } RecommendationComputationDetails recommendationComputationDetails = new RecommendationComputationDetails( details); List<Recommendation> recommendations = new LinkedList<>(); for (Object r : element.getChildren(RECOMMENDATION_ELEMENT_NAME)) { Element recommendationElement = (Element) r; if (!recommendationElement.getName().equals(RECOMMENDATION_ELEMENT_NAME)) { throw new IllegalArgumentException("Element name doesn't match this reader: found '" + recommendationElement.getName() + "' expected '" + RECOMMENDATION_ELEMENT_NAME + "'"); } int idItem = Integer.parseInt(recommendationElement.getAttributeValue(ID_ITEM_ATTRIBUTE_NAME)); double preference = Double .parseDouble(recommendationElement.getAttributeValue(PREFERENCE_ATTRIBUTE_NAME)); recommendations.add(new Recommendation(idItem, preference)); } return RecommendationsFactory.createRecommendations(idTarget, recommendations, recommendationComputationDetails); }