public class OntoSpreadStateXMLSerializer {
public static OntoSpreadXMLState asXML(OntoSpreadState ontoSpreadState){
ObjectFactory factory = new ObjectFactory();
OntoSpreadXMLState ontoSpreadXMLState = factory.createOntoSpreadXMLState();
ontoSpreadXMLState.setScoredConcepts(factory.createScoredConceptTOListType());
List<ScoredConceptTOType> scoredConceptTOs = ontoSpreadXMLState.getScoredConcepts().getScoredConceptTOs();
for (ScoredConceptTO scoredConceptTO : ontoSpreadState.getInitialConcepts()) {
ScoredConceptTOType scoredXML = new ScoredConceptTOType();
scoredXML.setConceptUri(scoredConceptTO.getConceptUri());
scoredXML.setScore(scoredConceptTO.getScore());
scoredConceptTOs.add(scoredXML);
}
ontoSpreadXMLState.setSpreadTime(ontoSpreadState.getSpreadTime());
ontoSpreadXMLState.setSpreadedConcepts(factory.createSpreadConceptListType());
List<String> spreadedConcepts = ontoSpreadXMLState.getSpreadedConcepts().getSpreadedConcepts();
for (String spreadedUri : ontoSpreadState.getSpreadedConcepts()) {
spreadedConcepts.add(spreadedUri);
}
ontoSpreadXMLState.setSpreadPathTable(factory.createEntryPathTableListType());
Map<String, PathTO[]> spreadPathTable = ontoSpreadState.getSpreadPathTable();
for (String key : spreadPathTable.keySet()) {
PathTO[] pathTOs = spreadPathTable.get(key);
EntryPathTableType entry = new EntryPathTableType();
entry.setUri(key);
for (PathTO pathTO : pathTOs) {
PathTOType pathToAdd = new PathTOType();
pathToAdd.setUri(pathTO.getConceptUri());
pathToAdd.setRelations(factory.createListUrisType());
if(pathTO.getRelationsUri()!=null){
pathToAdd.getRelations().getRelations().addAll(Arrays.asList(pathTO.getRelationsUri()));
}
entry.setPathTOs(factory.createPathTOTypeList());
entry.getPathTOs().getPathTOs().add(pathToAdd );
}
ontoSpreadXMLState.getSpreadPathTable().getSpreadTableTOs().add(entry );
}
ontoSpreadXMLState.setFinalSpreadedConcepts(factory.createSpreadedConceptTOListType());
for (SpreadedConceptTO spreadedConcept : ontoSpreadState.getFinalSpreadedConcepts()) {
SpreadedConceptTOType spreadedXML = new SpreadedConceptTOType();
spreadedXML.setConceptUri(spreadedConcept.getConcept().getUri());
PathTO[] pathTOs = spreadedConcept.getSpreadPath();
for (PathTO pathTO : pathTOs) {
PathTOType pathToAdd = new PathTOType();
pathToAdd.setUri(pathTO.getConceptUri());
pathToAdd.setRelations(factory.createListUrisType());
if(pathTO.getRelationsUri() != null){
pathToAdd.getRelations().getRelations().addAll(Arrays.asList(pathTO.getRelationsUri()));
}
spreadedXML.setPathTOs(factory.createPathTOTypeList());
spreadedXML.getPathTOs().getPathTOs().add(pathToAdd);
}
spreadedXML.setScore(spreadedConcept.getScore());
ontoSpreadXMLState.getFinalSpreadedConcepts().getSpreadedConceptTOs().add(spreadedXML);
}
ontoSpreadXMLState.setUriDepthPair(factory.createUriDepthListType());
List<UriDepthPairType> uriDepthPairs = ontoSpreadXMLState.getUriDepthPair().getUriDepthPairs();
for (UriDepthPair uriPair : ontoSpreadState.getSortedList()) {
UriDepthPairType uriXMLPair = new UriDepthPairType();
uriXMLPair.setConceptUri(uriPair.getUri());
uriXMLPair.setDepth(uriPair.getDepth());
uriDepthPairs.add(uriXMLPair );
}
ontoSpreadXMLState.setConcepts(factory.createEntryConceptsListType());
List<EntryConceptsType> entryConceptTOs = ontoSpreadXMLState.getConcepts().getEntryConceptTOs();
for (String key : ontoSpreadState.getConcepts().keySet()) {
Double value = ontoSpreadState.getConcepts().get(key);
EntryConceptsType entryXML = new EntryConceptsType();
entryXML.setUri(key);
entryXML.setValue(value);
entryConceptTOs.add(entryXML );
}
HashMap<PathTO,Integer> prizeTable = ontoSpreadState.getConceptsToPrize().getPrizeTable();
ontoSpreadXMLState.setPrizeTable(factory.createEntryPrizeTableListType());
for (PathTO pathTO : prizeTable.keySet()) {
EntryPrizeTableTOType entry = new EntryPrizeTableTOType();
PathTOType pathToAdd = new PathTOType();
pathToAdd.setUri(pathTO.getConceptUri());
pathToAdd.setRelations(factory.createListUrisType());
if(pathTO.getRelationsUri() != null){
pathToAdd.getRelations().getRelations().addAll(Arrays.asList((pathTO.getRelationsUri())));
}
entry.setHits(prizeTable.get(pathTO));
entry.setPathTO(pathToAdd);
ontoSpreadXMLState.getPrizeTable().getEntryPrizeTableTOs().add(entry);
}
ConceptTO conceptToSpread = ontoSpreadState.getConceptToSpread();
ontoSpreadXMLState.setConceptTOSpread(conceptToSpread.getUri());
ontoSpreadXMLState.setHasIteration(ontoSpreadState.isHasIteration());
ontoSpreadXMLState.setCurrentScore(ontoSpreadState.getCurrentScore());
return ontoSpreadXMLState;
}