use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement in project ArTEMiS by ls1intum.
the class ComponentDiagramParser method resolveParentComponent.
/**
* Finds the owner element of relationship and sets parent of relationship to that element
*
* @param allUmlElementsMap map of uml elements and ids to find owner element
* @param ownerRelationships map of uml elements and ids of their owners
* @return the UMLComponent object parsed from the JSON object
*/
protected static void resolveParentComponent(Map<String, UMLElement> allUmlElementsMap, Map<UMLElement, String> ownerRelationships) {
for (var ownerEntry : ownerRelationships.entrySet()) {
String ownerId = ownerEntry.getValue();
UMLElement umlElement = ownerEntry.getKey();
UMLElement parentComponent = allUmlElementsMap.get(ownerId);
umlElement.setParentElement(parentComponent);
}
}
use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement in project ArTEMiS by ls1intum.
the class ComponentDiagramParser method parseComponentRelationship.
/**
* Parses the given JSON representation of a UML component interface to a UMLComponentInterface Java object.
*
* @param relationshipJson the JSON object containing the component interface
* @param allUmlElementsMap the JSON object containing the component interface
* @return the UMLComponentInterface object parsed from the JSON object
*/
protected static Optional<UMLComponentRelationship> parseComponentRelationship(JsonObject relationshipJson, Map<String, UMLElement> allUmlElementsMap) throws IOException {
String relationshipType = relationshipJson.get(RELATIONSHIP_TYPE).getAsString();
relationshipType = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, relationshipType);
if (!EnumUtils.isValidEnum(UMLComponentRelationship.UMLComponentRelationshipType.class, relationshipType)) {
return Optional.empty();
}
UMLElement source = UMLModelParser.findElement(relationshipJson, allUmlElementsMap, RELATIONSHIP_SOURCE);
UMLElement target = UMLModelParser.findElement(relationshipJson, allUmlElementsMap, RELATIONSHIP_TARGET);
if (source != null && target != null) {
UMLComponentRelationship newComponentRelationship = new UMLComponentRelationship(source, target, UMLComponentRelationship.UMLComponentRelationshipType.valueOf(relationshipType), relationshipJson.get(ELEMENT_ID).getAsString());
return Optional.of(newComponentRelationship);
} else {
throw new IOException("Relationship source or target not part of model!");
}
}
use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement in project ArTEMiS by ls1intum.
the class ComponentDiagramParser method buildComponentDiagramFromJSON.
/**
* Create a UML component diagram from the model and relationship elements given as JSON arrays. It parses the JSON objects to corresponding Java objects and creates a
* component diagram containing these UML model elements.
*
* @param modelElements the model elements as JSON array
* @param relationships the relationship elements as JSON array
* @param modelSubmissionId the ID of the corresponding modeling submission
* @return a UML component diagram containing the parsed model elements and relationships
* @throws IOException when no corresponding model elements could be found for the source and target IDs in the relationship JSON objects
*/
protected static UMLComponentDiagram buildComponentDiagramFromJSON(JsonArray modelElements, JsonArray relationships, long modelSubmissionId) throws IOException {
Map<String, UMLComponent> umlComponentMap = new HashMap<>();
Map<String, UMLComponentInterface> umlComponentInterfaceMap = new HashMap<>();
Map<String, UMLElement> allUmlElementsMap = new HashMap<>();
List<UMLComponentRelationship> umlComponentRelationshipList = new ArrayList<>();
// owners might not yet be available, therefore we need to store them in a map first before we can resolve them
Map<UMLElement, String> ownerRelationships = new HashMap<>();
// loop over all JSON elements and create the UML objects
for (JsonElement jsonElement : modelElements) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
UMLElement umlElement = null;
String elementType = jsonObject.get(ELEMENT_TYPE).getAsString();
if (UMLComponent.UML_COMPONENT_TYPE.equals(elementType)) {
UMLComponent umlComponent = parseComponent(jsonObject);
umlComponentMap.put(umlComponent.getJSONElementID(), umlComponent);
umlElement = umlComponent;
} else if (UMLComponentInterface.UML_COMPONENT_INTERFACE_TYPE.equals(elementType)) {
UMLComponentInterface umlComponentInterface = parseComponentInterface(jsonObject);
umlComponentInterfaceMap.put(umlComponentInterface.getJSONElementID(), umlComponentInterface);
umlElement = umlComponentInterface;
}
if (umlElement != null) {
allUmlElementsMap.put(umlElement.getJSONElementID(), umlElement);
findOwner(ownerRelationships, jsonObject, umlElement);
}
}
// now we can resolve the owners: for this diagram type, only uml components can be the actual owner
resolveParentComponent(allUmlElementsMap, ownerRelationships);
// loop over all JSON control flow elements and create UML communication links
for (JsonElement rel : relationships) {
Optional<UMLComponentRelationship> componentRelationship = parseComponentRelationship(rel.getAsJsonObject(), allUmlElementsMap);
componentRelationship.ifPresent(umlComponentRelationshipList::add);
}
return new UMLComponentDiagram(modelSubmissionId, new ArrayList<>(umlComponentMap.values()), new ArrayList<>(umlComponentInterfaceMap.values()), umlComponentRelationshipList);
}
use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement in project ArTEMiS by ls1intum.
the class CompassService method getSuggestionResult.
/**
* Selects the feedback suggestion for each element in submission and creates a result from them
* Returns null if no feedback can be selected or the submission has already a manual feedback
*
* @param modelingSubmission the submission to select feedbacks for
* @param modelingExercise the modeling exercise to which the submission belongs
* @return the semi-automatic result that has the feedbacks inside
*/
public Result getSuggestionResult(ModelingSubmission modelingSubmission, ModelingExercise modelingExercise) {
Result result = getAutomaticResultForSubmission(modelingSubmission);
if (result != null) {
List<Feedback> feedbacksForSuggestion = new ArrayList<>();
ModelClusterFactory clusterBuilder = new ModelClusterFactory();
List<UMLElement> elements = clusterBuilder.getModelElements(modelingSubmission);
List<ModelElement> modelElements = modelElementRepository.findByModelElementIdIn(elements.stream().map(UMLElement::getJSONElementID).collect(Collectors.toList()));
List<Long> clusterIds = modelElements.stream().map(ModelElement::getCluster).map(ModelCluster::getId).collect(Collectors.toList());
List<ModelCluster> modelClusters = modelClusterRepository.findAllByIdInWithEagerElements(clusterIds);
List<String> references = modelClusters.stream().flatMap(modelCluster -> modelCluster.getModelElements().stream()).map(modelElement1 -> modelElement1.getModelElementType() + ":" + modelElement1.getModelElementId()).collect(Collectors.toList());
List<Feedback> feedbacks = feedbackRepository.findByReferenceInAndResult_Submission_Participation_Exercise(references, modelingExercise);
for (ModelElement modelElement : modelElements) {
if (modelElement != null) {
ModelCluster cluster = modelClusters.get(modelClusters.indexOf(modelElement.getCluster()));
Set<ModelElement> similarElements = cluster.getModelElements();
List<String> similarReferences = similarElements.stream().map(element -> element.getModelElementType() + ":" + element.getModelElementId()).toList();
List<Feedback> similarFeedbacks = feedbacks.stream().filter(feedback -> similarReferences.contains(feedback.getReference())).collect(Collectors.toList());
Feedback suggestedFeedback = FeedbackSelector.selectFeedback(modelElement, similarFeedbacks, result);
if (suggestedFeedback != null) {
feedbacksForSuggestion.add(suggestedFeedback);
}
}
}
if (feedbacksForSuggestion.isEmpty()) {
return null;
}
// Note, that a result is always initialized with an empty list -> no NPE here
result.getFeedbacks().clear();
result.getFeedbacks().addAll(feedbacksForSuggestion);
result.setHasFeedback(false);
result.setAssessmentType(AssessmentType.SEMI_AUTOMATIC);
}
return result;
}
use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement in project ArTEMiS by ls1intum.
the class UseCaseDiagramParser method parseUseCaseAssociation.
/**
* Parses the given JSON representation of a UML relationship to a UMLUseCaseAssociation Java object.
*
* @param relationshipJson the JSON object containing the relationship
* @param allUmlElementsMap a map containing all objects of the corresponding use case diagram, necessary for assigning source and target element of the relationships
* @return the UMLUseCaseAssociation object parsed from the JSON object
* @throws IOException when no object could be found in the allUmlElementsMap for the source and target ID in the JSON object
*/
private static Optional<UMLUseCaseAssociation> parseUseCaseAssociation(JsonObject relationshipJson, Map<String, UMLElement> allUmlElementsMap) throws IOException {
String relationshipType = relationshipJson.get(RELATIONSHIP_TYPE).getAsString();
relationshipType = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, relationshipType);
if (!EnumUtils.isValidEnum(UMLUseCaseAssociation.UMLUseCaseAssociationType.class, relationshipType)) {
return Optional.empty();
}
var associationType = UMLUseCaseAssociation.UMLUseCaseAssociationType.valueOf(relationshipType);
String name = null;
if (associationType == UMLUseCaseAssociation.UMLUseCaseAssociationType.USE_CASE_ASSOCIATION) {
name = relationshipJson.get(ELEMENT_NAME).getAsString();
}
UMLElement source = UMLModelParser.findElement(relationshipJson, allUmlElementsMap, RELATIONSHIP_SOURCE);
UMLElement target = UMLModelParser.findElement(relationshipJson, allUmlElementsMap, RELATIONSHIP_TARGET);
if (source != null && target != null) {
UMLUseCaseAssociation newComponentRelationship = new UMLUseCaseAssociation(name, source, target, associationType, relationshipJson.get(ELEMENT_ID).getAsString());
return Optional.of(newComponentRelationship);
} else {
throw new IOException("Relationship source or target not part of model!");
}
}
Aggregations