List of usage examples for org.jdom2 Document Document
public Document()
From source file:delfos.configuration.scopes.ConfiguredDatasetsScope.java
License:Open Source License
public synchronized void saveConfiguredDatasets() { Document doc = new Document(); Element root = new Element(CONFIGURED_DATASETS_ROOT_ELEMENT_NAME); for (ConfiguredDataset configuredDataset : ConfiguredDatasetsFactory.getInstance() .getAllConfiguredDatasets()) { Element thisDatasetLoader = new Element(CONFIGURED_DATASET_ELEMENT_NAME); thisDatasetLoader.setAttribute(CONFIGURED_DATASET_ELEMENT_NAME_ATTRIBUTE, configuredDataset.getName()); thisDatasetLoader.setAttribute(CONFIGURED_DATASET_ELEMENT_DESCRIPTION_ATTRIBUTE, configuredDataset.getDescription()); Element datasetLoaderElement = DatasetLoaderXML.getElement(configuredDataset.getDatasetLoader()); thisDatasetLoader.addContent(datasetLoaderElement); root.addContent(thisDatasetLoader); }/*from ww w .j ava 2 s .co m*/ doc.addContent(root); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); File fileOfConfiguredDatasets = ConfigurationManager.getConfigurationFile(this); try (FileWriter fileWriter = new FileWriter(fileOfConfiguredDatasets)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_CONFIGURED_DATASETS_FILE.exit(ex); } }
From source file:delfos.group.GroupRecommendationManager.java
License:Open Source License
private static void writeXML(Collection<Recommendation> groupRecommendations, Map<Integer, Collection<Recommendation>> singleUserRecommendations, File outputFile) { Element root = new Element(CASE_ROOT_ELEMENT_NAME); Set<Integer> itemsIntersection = new TreeSet<>(); //Miro los items recomendados para el grupo for (Recommendation r : groupRecommendations) { itemsIntersection.add(r.getItem().getId()); }/* ww w . j a va2 s. c om*/ //Elimino los que no aparecen recomendades para los miembros for (int idMember : singleUserRecommendations.keySet()) { Set<Integer> thisMemberItems = new TreeSet<>(); singleUserRecommendations.get(idMember).stream().forEach((r) -> { thisMemberItems.add(r.getItem().getId()); }); itemsIntersection.retainAll(thisMemberItems); } itemsIntersection = Collections.unmodifiableSet(itemsIntersection); for (int idMember : singleUserRecommendations.keySet()) { Element thisMemberElement = new Element(MEMBER_ELEMENT_NAME); thisMemberElement.setAttribute(MEMBER_ELEMENT_NAMEID_ATTRIBUTE_NAME, Integer.toString(idMember)); for (Recommendation r : singleUserRecommendations.get(idMember)) { if (itemsIntersection.contains(r.getItem().getId())) { Element recommendation = new Element(RECOMMENDATION_ELEMENT_NAME); recommendation.setAttribute(RECOMMENDATION_ELEMENT_ID_ITEM_ATTRIBUTE_NAME, Integer.toString(r.getItem().getId())); recommendation.setAttribute(RECOMMENDATION_ELEMENT_PREFERENCE_ATTRIBUTE_NAME, Double.toString(r.getPreference().doubleValue())); thisMemberElement.addContent(recommendation); } } root.addContent(thisMemberElement); } Element groupElement = new Element(GROUP_ELEMENT_NAME); StringBuilder str = new StringBuilder(); Integer[] idMembers = singleUserRecommendations.keySet().toArray(new Integer[0]); str.append(idMembers[0]); for (int i = 1; i < idMembers.length; i++) { str.append(",").append(idMembers[i]); } groupElement.setAttribute(GROUP_ELEMENT_MEMBERS_ATTRIBUTE_NAME, str.toString()); for (Recommendation r : groupRecommendations) { if (itemsIntersection.contains(r.getItem().getId())) { Element recommendation = new Element(RECOMMENDATION_ELEMENT_NAME); recommendation.setAttribute(RECOMMENDATION_ELEMENT_ID_ITEM_ATTRIBUTE_NAME, Integer.toString(r.getItem().getId())); recommendation.setAttribute(RECOMMENDATION_ELEMENT_PREFERENCE_ATTRIBUTE_NAME, Double.toString(r.getPreference().doubleValue())); groupElement.addContent(recommendation); } } root.addContent(groupElement); Document doc = new Document(); doc.addContent(root); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); try (FileWriter fileWriter = new FileWriter(outputFile)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }
From source file:delfos.group.grs.consensus.ConsensusOfIndividualRecommendationsToXML.java
License:Open Source License
public static void writeConsensusInputXML(DatasetLoader datasetLoader, GroupRecommendations groupRecommendations, Collection<RecommendationsToUser> membersRecommendations, File outputFile) {/* w w w. j av a 2 s. c o m*/ Element root = new Element(CONSENSUS_ROOT_ELEMENT_NAME); for (RecommendationsToUser recommendationsToMember : membersRecommendations) { User member = recommendationsToMember.getUser(); Element thisMemberElement = new Element(MEMBER_ELEMENT_NAME); thisMemberElement.setAttribute(MEMBER_ELEMENT_NAME_ID_ATTRIBUTE_NAME, Integer.toString(member.getId())); List<Recommendation> sortedRecommendations = recommendationsToMember.getRecommendations().stream() .sorted(Recommendation.BY_PREFERENCE_DESC).collect(Collectors.toList()); int rank = 1; for (Recommendation r : sortedRecommendations) { Element recommendation = new Element(RECOMMENDATION_ELEMENT_NAME); recommendation.setAttribute(RECOMMENDATION_ELEMENT_ID_ITEM_ATTRIBUTE_NAME, Integer.toString(r.getItem().getId())); double preferenceInDomain = datasetLoader.getRatingsDataset().getRatingsDomain() .trimValueToDomain(r.getPreference()).doubleValue(); recommendation.setAttribute(RECOMMENDATION_ELEMENT_PREFERENCE_ATTRIBUTE_NAME, Double.toString(preferenceInDomain)); recommendation.setAttribute(RECOMMENDATION_ELEMENT_RANK_ATTRIBUTE_NAME, Integer.toString(rank)); thisMemberElement.addContent(recommendation); rank++; } root.addContent(thisMemberElement); } Element groupElement = new Element(GROUP_ELEMENT_NAME); String members = groupRecommendations.getGroupOfUsers().toString(); groupElement.setAttribute(GROUP_ELEMENT_MEMBERS_ATTRIBUTE_NAME, members); int rank = 1; List<Recommendation> sortedGroupRecommendations = groupRecommendations.getRecommendations().stream() .sorted(Recommendation.BY_PREFERENCE_DESC).collect(Collectors.toList()); for (Recommendation r : sortedGroupRecommendations) { Element recommendation = new Element(RECOMMENDATION_ELEMENT_NAME); recommendation.setAttribute(RECOMMENDATION_ELEMENT_ID_ITEM_ATTRIBUTE_NAME, Integer.toString(r.getIdItem())); recommendation.setAttribute(RECOMMENDATION_ELEMENT_PREFERENCE_ATTRIBUTE_NAME, Double.toString(r.getPreference().doubleValue())); recommendation.setAttribute(RECOMMENDATION_ELEMENT_RANK_ATTRIBUTE_NAME, Integer.toString(rank)); groupElement.addContent(recommendation); rank++; } root.addContent(groupElement); Document doc = new Document(); doc.addContent(root); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); try (FileWriter fileWriter = new FileWriter(outputFile)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }
From source file:delfos.group.grs.consensus.ConsensusOfIndividualRecommendationsToXML.java
License:Open Source License
public static <RatingType extends Rating> void writeRecommendationMembersRatingsXML( Map<Integer, Map<Integer, RatingType>> membersRatings, Collection<Integer> candidateItems, File groupPredictionRequestsFile) { Element root = new Element(RECOMMENDATION_INPUT_ROOT_ELEMENT_NAME); Element membersRatingsElement = new Element(RECOMMENDATION_INPUT_MEMBERS_RATINGS_ELEMENT_NAME); for (int idMember : membersRatings.keySet()) { Element thisMemberRatingsElement = new Element(RECOMMENDATION_INPUT_MEMBER_RATINGS_ELEMENT_NAME); thisMemberRatingsElement.setAttribute(RECOMMENDATION_INPUT_MEMBER_RATINGS_ID_USER_ATTRIBUTE_NAME, Integer.toString(idMember)); Map<Integer, RatingType> memberRatings = membersRatings.get(idMember); for (RatingType memberRating : memberRatings.values()) { Element ratingElement = new Element(RECOMMENDATION_INPUT_MEMBER_RATINGS_RATING_ELEMENT_NAME); ratingElement.setAttribute(RECOMMENDATION_INPUT_MEMBER_RATINGS_ID_USER_ATTRIBUTE_NAME, Integer.toString(memberRating.getIdUser())); ratingElement.setAttribute(RECOMMENDATION_INPUT_MEMBER_RATINGS_ID_ITEM_ATTRIBUTE_NAME, Integer.toString(memberRating.getIdItem())); ratingElement.setAttribute(RECOMMENDATION_INPUT_MEMBER_RATINGS_RATING_VALUE_ATTRIBUTE_NAME, Double.toString(memberRating.getRatingValue().doubleValue())); thisMemberRatingsElement.addContent(ratingElement); }//from w ww. j a va 2s .c o m membersRatingsElement.addContent(thisMemberRatingsElement); } root.addContent(membersRatingsElement); Element candidateItemsElement = new Element(RECOMMENDATION_INPUT_ID_ITEM_LIST_ELEMENT_NAME); for (int idItemRequested : candidateItems) { Element itemRequestedElement = new Element(RECOMMENDATION_INPUT_ITEM_REQUEST_ELEMENT_NAME); itemRequestedElement.setAttribute(RECOMMENDATION_INPUT_ITEM_REQUEST_ID_ITEM_ATTRIBUTE_NAME, Integer.toString(idItemRequested)); candidateItemsElement.addContent(itemRequestedElement); } root.addContent(candidateItemsElement); Document doc = new Document(); doc.addContent(root); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); try (FileWriter fileWriter = new FileWriter(groupPredictionRequestsFile)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }
From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java
License:Open Source License
public synchronized static void caseStudyToXMLFile_fullResults(GroupCaseStudy caseStudyGroup, File file) { if (!caseStudyGroup.isFinished()) { throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava"); }/* ww w . j a v a 2 s . c o m*/ Document doc = new Document(); Element casoDeUso = new Element("Case"); casoDeUso.setAttribute(SeedHolder.SEED.getName(), Long.toString(caseStudyGroup.getSeedValue())); casoDeUso.setAttribute(NUM_EXEC_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.getNumExecutions())); casoDeUso.setAttribute(ParameterOwner.ALIAS.getName(), caseStudyGroup.getAlias()); casoDeUso.addContent(GroupRecommenderSystemXML.getElement(caseStudyGroup.getGroupRecommenderSystem())); casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudyGroup.getDatasetLoader())); casoDeUso.addContent(GroupFormationTechniqueXML.getElement(caseStudyGroup.getGroupFormationTechnique())); casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudyGroup.getValidationTechnique())); casoDeUso.addContent(GroupPredictionProtocolXML.getElement(caseStudyGroup.getGroupPredictionProtocol())); casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudyGroup.getRelevanceCriteria())); casoDeUso.addContent(getResultsElement(caseStudyGroup)); casoDeUso.addContent(getAggregatedResultsElement(caseStudyGroup)); doc.addContent(casoDeUso); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); FileUtilities.createDirectoriesForFile(file); try (FileWriter fileWriter = new FileWriter(file)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }
From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java
License:Open Source License
/** * Saves the xml with the description of the case study in de file * specified.//w w w. j a va2 s. c om * * @param caseStudyGroup Group case study whose description is saved. * @param file File in which the description is saved. */ public static void caseStudyToXMLFile_onlyDescription(GroupCaseStudy caseStudyGroup, File file) { if (caseStudyGroup.isFinished()) { throw new IllegalArgumentException("Ya se ha ejecutado el caso de estudio!"); } Document doc = new Document(); Element casoDeUso = new Element(CASE_ROOT_ELEMENT_NAME); casoDeUso.setAttribute(SeedHolder.SEED.getName(), Long.toString(caseStudyGroup.getSeedValue())); casoDeUso.setAttribute(NUM_EXEC_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.getNumExecutions())); casoDeUso.setAttribute(ParameterOwner.ALIAS.getName(), caseStudyGroup.getAlias()); casoDeUso.setAttribute(HASH_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashCode())); casoDeUso.setAttribute(HASH_DATA_VALIDATION_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashCode())); casoDeUso.addContent(GroupRecommenderSystemXML.getElement(caseStudyGroup.getGroupRecommenderSystem())); casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudyGroup.getDatasetLoader())); casoDeUso.addContent(GroupFormationTechniqueXML.getElement(caseStudyGroup.getGroupFormationTechnique())); casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudyGroup.getValidationTechnique())); casoDeUso.addContent(GroupPredictionProtocolXML.getElement(caseStudyGroup.getGroupPredictionProtocol())); casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudyGroup.getRelevanceCriteria())); doc.addContent(casoDeUso); FileUtilities.createDirectoriesForFile(file); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); try (FileWriter fileWriter = new FileWriter(file)) { outputter.output(doc, fileWriter); fileWriter.close(); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }
From source file:delfos.group.io.xml.casestudy.GroupCaseStudyXML.java
License:Open Source License
private static void caseStudyToXMLFile_aggregateResults(GroupCaseStudy caseStudyGroup, File file) { if (!caseStudyGroup.isFinished()) { throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava"); }//w w w. j a va 2s. c o m Document doc = new Document(); Element casoDeUso = new Element(CASE_ROOT_ELEMENT_NAME); casoDeUso.setAttribute(SeedHolder.SEED.getName(), Long.toString(caseStudyGroup.getSeedValue())); casoDeUso.setAttribute(NUM_EXEC_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.getNumExecutions())); casoDeUso.setAttribute(ParameterOwner.ALIAS.getName(), caseStudyGroup.getAlias()); casoDeUso.setAttribute(HASH_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashCode())); casoDeUso.setAttribute(HASH_DATA_VALIDATION_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashDataValidation())); casoDeUso.setAttribute(HASH_TECHNIQUE_ATTRIBUTE_NAME, Integer.toString(caseStudyGroup.hashTechnique())); casoDeUso.addContent(GroupRecommenderSystemXML.getElement(caseStudyGroup.getGroupRecommenderSystem())); casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudyGroup.getDatasetLoader())); casoDeUso.addContent(GroupFormationTechniqueXML.getElement(caseStudyGroup.getGroupFormationTechnique())); casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudyGroup.getValidationTechnique())); casoDeUso.addContent(GroupPredictionProtocolXML.getElement(caseStudyGroup.getGroupPredictionProtocol())); casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudyGroup.getRelevanceCriteria())); casoDeUso.addContent(getAggregatedResultsElement(caseStudyGroup)); doc.addContent(casoDeUso); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); FileUtilities.createDirectoriesForFile(file); try (FileWriter fileWriter = new FileWriter(file)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }
From source file:delfos.io.xml.casestudy.CaseStudyXML.java
License:Open Source License
public synchronized static void caseStudyToXMLFile(CaseStudy caseStudy, File file) { if (!caseStudy.isFinished()) { throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava"); }//from ww w . ja va 2 s .c om Document doc = new Document(); Element casoDeUso = new Element(CASE_ROOT_ELEMENT_NAME); casoDeUso.setAttribute(SEED_ATTRIBUTE_NAME, Long.toString(caseStudy.getSeedValue())); casoDeUso.setAttribute(NUM_EXEC_ATTRIBUTE_NAME, Integer.toString(caseStudy.getNumExecutions())); casoDeUso.addContent(RecommenderSystemXML.getElement(caseStudy.getRecommenderSystem())); casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudy.getValidationTechnique())); casoDeUso.addContent(PredictionProtocolXML.getElement(caseStudy.getPredictionProtocol())); casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudy.getRelevanceCriteria())); casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudy.getDatasetLoader())); casoDeUso.addContent(getResultsElement(caseStudy)); casoDeUso.addContent(getAggregatedResultsElement(caseStudy)); doc.addContent(casoDeUso); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); try (FileWriter fileWriter = new FileWriter(file)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }
From source file:delfos.io.xml.casestudy.CaseStudyXML.java
License:Open Source License
public synchronized static void caseStudyToXMLFile(CaseStudy caseStudy, String descriptiveName, File f) { if (!caseStudy.isFinished()) { throw new UnsupportedOperationException("No se ha ejecutado el caso de uso todava"); }//from w w w. j a v a 2 s .co m Document doc = new Document(); Element casoDeUso = new Element("Case"); casoDeUso.addContent(RecommenderSystemXML.getElement(caseStudy.getRecommenderSystem())); casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudy.getValidationTechnique())); casoDeUso.addContent(PredictionProtocolXML.getElement(caseStudy.getPredictionProtocol())); casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudy.getRelevanceCriteria())); casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudy.getDatasetLoader())); casoDeUso.addContent(getResultsElement(caseStudy)); casoDeUso.addContent(getAggregatedResultsElement(caseStudy)); doc.addContent(casoDeUso); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); try (FileWriter fileWriter = new FileWriter(f)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }
From source file:delfos.io.xml.casestudy.CaseStudyXML.java
License:Open Source License
private static void caseStudyToXMLFile_onlyDescription(CaseStudy caseStudy, File file) { if (caseStudy.isFinished()) { throw new IllegalArgumentException("Ya se ha ejecutado el caso de estudio!"); }//w w w . j a va 2 s .co m Document doc = new Document(); Element casoDeUso = new Element(CASE_ROOT_ELEMENT_NAME); casoDeUso.addContent(RecommenderSystemXML.getElement(caseStudy.getRecommenderSystem())); casoDeUso.addContent(ValidationTechniqueXML.getElement(caseStudy.getValidationTechnique())); casoDeUso.addContent(PredictionProtocolXML.getElement(caseStudy.getPredictionProtocol())); casoDeUso.addContent(RelevanceCriteriaXML.getElement(caseStudy.getRelevanceCriteria())); casoDeUso.addContent(DatasetLoaderXML.getElement(caseStudy.getDatasetLoader())); //casoDeUso.addContent(getResultsElement(caseStudy)); //casoDeUso.addContent(getAggregatedResultsElement(caseStudy)); doc.addContent(casoDeUso); XMLOutputter outputter = new XMLOutputter(Constants.getXMLFormat()); try (FileWriter fileWriter = new FileWriter(file)) { outputter.output(doc, fileWriter); } catch (IOException ex) { ERROR_CODES.CANNOT_WRITE_RESULTS_FILE.exit(ex); } }