Search in sources :

Example 1 with UMLElement

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);
    }
}
Also used : UMLElement(de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement)

Example 2 with UMLElement

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!");
    }
}
Also used : UMLElement(de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement) UMLComponentRelationship(de.tum.in.www1.artemis.service.compass.umlmodel.component.UMLComponentRelationship) IOException(java.io.IOException)

Example 3 with UMLElement

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);
}
Also used : UMLElement(de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement) JsonObject(com.google.gson.JsonObject) UMLComponentInterface(de.tum.in.www1.artemis.service.compass.umlmodel.component.UMLComponentInterface) UMLComponentDiagram(de.tum.in.www1.artemis.service.compass.umlmodel.component.UMLComponentDiagram) JsonElement(com.google.gson.JsonElement) UMLComponentRelationship(de.tum.in.www1.artemis.service.compass.umlmodel.component.UMLComponentRelationship) UMLComponent(de.tum.in.www1.artemis.service.compass.umlmodel.component.UMLComponent)

Example 4 with UMLElement

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;
}
Also used : java.util(java.util) Feedback(de.tum.in.www1.artemis.domain.Feedback) Logger(org.slf4j.Logger) FeedbackSelector(de.tum.in.www1.artemis.service.compass.controller.FeedbackSelector) de.tum.in.www1.artemis.repository(de.tum.in.www1.artemis.repository) TimeLogUtil(de.tum.in.www1.artemis.service.util.TimeLogUtil) LoggerFactory(org.slf4j.LoggerFactory) AssessmentType(de.tum.in.www1.artemis.domain.enumeration.AssessmentType) Collectors(java.util.stream.Collectors) UMLElement(de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement) Result(de.tum.in.www1.artemis.domain.Result) ModelElement(de.tum.in.www1.artemis.domain.modeling.ModelElement) ModelClusterFactory(de.tum.in.www1.artemis.service.compass.controller.ModelClusterFactory) Service(org.springframework.stereotype.Service) ModelCluster(de.tum.in.www1.artemis.domain.modeling.ModelCluster) ModelingSubmission(de.tum.in.www1.artemis.domain.modeling.ModelingSubmission) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise) UMLElement(de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement) ModelCluster(de.tum.in.www1.artemis.domain.modeling.ModelCluster) Result(de.tum.in.www1.artemis.domain.Result) ModelClusterFactory(de.tum.in.www1.artemis.service.compass.controller.ModelClusterFactory) ModelElement(de.tum.in.www1.artemis.domain.modeling.ModelElement) Feedback(de.tum.in.www1.artemis.domain.Feedback)

Example 5 with UMLElement

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!");
    }
}
Also used : UMLElement(de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement) IOException(java.io.IOException)

Aggregations

UMLElement (de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement)58 Test (org.junit.jupiter.api.Test)40 AbstractUMLDiagramTest (de.tum.in.www1.artemis.service.compass.umlmodel.AbstractUMLDiagramTest)28 JsonObject (com.google.gson.JsonObject)10 JsonElement (com.google.gson.JsonElement)6 UMLComponentRelationship (de.tum.in.www1.artemis.service.compass.umlmodel.component.UMLComponentRelationship)6 UMLDiagram (de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram)4 IOException (java.io.IOException)4 Feedback (de.tum.in.www1.artemis.domain.Feedback)2 Result (de.tum.in.www1.artemis.domain.Result)2 AssessmentType (de.tum.in.www1.artemis.domain.enumeration.AssessmentType)2 ModelCluster (de.tum.in.www1.artemis.domain.modeling.ModelCluster)2 ModelElement (de.tum.in.www1.artemis.domain.modeling.ModelElement)2 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)2 ModelingSubmission (de.tum.in.www1.artemis.domain.modeling.ModelingSubmission)2 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)2 de.tum.in.www1.artemis.repository (de.tum.in.www1.artemis.repository)2 FeedbackSelector (de.tum.in.www1.artemis.service.compass.controller.FeedbackSelector)2 ModelClusterFactory (de.tum.in.www1.artemis.service.compass.controller.ModelClusterFactory)2 UMLActivity (de.tum.in.www1.artemis.service.compass.umlmodel.activity.UMLActivity)2