Search in sources :

Example 6 with UMLDiagram

use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram in project ArTEMiS by ls1intum.

the class SyntaxTreeTest method parseSyntaxTreeModelCorrectly.

@Test
void parseSyntaxTreeModelCorrectly() throws IOException {
    UMLDiagram diagram = UMLModelParser.buildModelFromJSON(parseString(SYNTAX_TREE_MODEL_1A).getAsJsonObject(), 1L);
    assertThat(diagram).isInstanceOf(SyntaxTree.class);
    SyntaxTree syntaxTree = (SyntaxTree) diagram;
    assertThat(syntaxTree.getNonTerminals()).hasSize(1);
    assertThat(syntaxTree.getTerminals()).hasSize(4);
    assertThat(syntaxTree.getLinks()).hasSize(4);
    assertThat(syntaxTree.getElementByJSONID("8e21a66e-9fc4-4f82-b2d1-3d26a51b309a")).asInstanceOf(type(SyntaxTreeNonterminal.class)).satisfies(nonterminal -> {
        assertThat(nonterminal.getName()).isEqualTo("stmt");
    });
    assertThat(syntaxTree.getElementByJSONID("3a54da7e-ad8c-410d-83d3-541ec58ea038")).asInstanceOf(type(SyntaxTreeTerminal.class)).satisfies(terminal -> {
        assertThat(terminal.getName()).isEqualTo("=");
    });
    assertThat(syntaxTree.getElementByJSONID("200cbd5f-4ce1-4d96-8bbd-34ef89aae533")).asInstanceOf(type(SyntaxTreeLink.class)).satisfies(link -> {
        assertThat(link.getSource()).isNotNull().isSameAs(syntaxTree.getElementByJSONID("2ea3a753-b2d9-4a57-881e-1f10837ef5a2"));
        assertThat(link.getTarget()).isNotNull().isSameAs(syntaxTree.getElementByJSONID("8e21a66e-9fc4-4f82-b2d1-3d26a51b309a"));
    });
}
Also used : UMLDiagram(de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram) Test(org.junit.jupiter.api.Test) AbstractUMLDiagramTest(de.tum.in.www1.artemis.service.compass.umlmodel.AbstractUMLDiagramTest)

Example 7 with UMLDiagram

use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram in project ArTEMiS by ls1intum.

the class UMLCommunicationDiagramTest method parseCommunicationDiagramModelCorrectly.

@Test
void parseCommunicationDiagramModelCorrectly() throws IOException {
    UMLDiagram diagram = UMLModelParser.buildModelFromJSON(parseString(UMLCommunicationDiagrams.COMMUNICATION_MODEL_2).getAsJsonObject(), 1L);
    assertThat(diagram).isInstanceOf(UMLCommunicationDiagram.class);
    UMLCommunicationDiagram communicationDiagram = (UMLCommunicationDiagram) diagram;
    assertThat(communicationDiagram.getObjectList()).hasSize(5);
    assertThat(communicationDiagram.getCommunicationLinkList()).hasSize(5);
    assertThat(communicationDiagram.getElementByJSONID("619ddf50-f2a6-4004-9fb3-db64ee10cd6e")).isInstanceOf(UMLObject.class);
    assertThat(communicationDiagram.getElementByJSONID("64040203-6e35-4b42-8ab5-71b544a70fa6")).isInstanceOf(UMLCommunicationLink.class);
}
Also used : UMLDiagram(de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram) Test(org.junit.jupiter.api.Test) AbstractUMLDiagramTest(de.tum.in.www1.artemis.service.compass.umlmodel.AbstractUMLDiagramTest)

Example 8 with UMLDiagram

use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram in project Artemis by ls1intum.

the class ModelingPlagiarismDetectionService method checkPlagiarism.

/**
 * Pairwise comparison of text submissions using a TextComparisonStrategy
 *
 * @param modelingSubmissions List of modeling submissions
 * @param minimumSimilarity   the minimum similarity so that the result is considered
 * @param minimumModelSize    the minimum number of model elements to be considered as plagiarism
 * @param minimumScore        the minimum result score (if available) to be considered as plagiarism
 * @param exerciseId          the id of the exercise for which the modeling submissions are compared
 *
 * @return List of submission id pairs and similarity score
 */
public ModelingPlagiarismResult checkPlagiarism(List<ModelingSubmission> modelingSubmissions, double minimumSimilarity, int minimumModelSize, int minimumScore, Long exerciseId) {
    String topic = plagiarismWebsocketService.getModelingExercisePlagiarismCheckTopic(exerciseId);
    ModelingPlagiarismResult result = new ModelingPlagiarismResult();
    Map<UMLDiagram, ModelingSubmission> models = new HashMap<>();
    ObjectMapper objectMapper = new ObjectMapper();
    AtomicInteger processedSubmissionCount = new AtomicInteger(1);
    modelingSubmissions.stream().filter(modelingSubmission -> !modelingSubmission.isEmpty(objectMapper)).filter(modelingSubmission -> minimumScore == 0 || modelingSubmission.getLatestResult() != null && modelingSubmission.getLatestResult().getScore() != null && modelingSubmission.getLatestResult().getScore() >= minimumScore).forEach(modelingSubmission -> {
        String progressMessage = "Getting UML diagram for submission: " + processedSubmissionCount + "/" + modelingSubmissions.size();
        plagiarismWebsocketService.notifyInstructorAboutPlagiarismState(topic, PlagiarismCheckState.RUNNING, List.of(progressMessage));
        try {
            log.debug("Build UML diagram from json");
            UMLDiagram model = UMLModelParser.buildModelFromJSON(parseString(modelingSubmission.getModel()).getAsJsonObject(), modelingSubmission.getId());
            if (model.getAllModelElements().size() >= minimumModelSize) {
                models.put(model, modelingSubmission);
            }
        } catch (IOException e) {
            log.error("Parsing the modeling submission " + modelingSubmission.getId() + " did throw an exception:", e);
        }
        processedSubmissionCount.getAndIncrement();
    });
    log.info("Found {} modeling submissions with at least {} elements to compare", models.size(), minimumModelSize);
    Set<PlagiarismComparison<ModelingSubmissionElement>> comparisons = new HashSet<>();
    List<UMLDiagram> nonEmptyDiagrams = new ArrayList<>(models.keySet());
    long timeBeforeStartInMillis = System.currentTimeMillis();
    // similarity between two different submissions once
    for (int i = 0; i < nonEmptyDiagrams.size(); i++) {
        String progressMessage = "Comparing submissions: " + (i + 1) + "/" + nonEmptyDiagrams.size();
        plagiarismWebsocketService.notifyInstructorAboutPlagiarismState(topic, PlagiarismCheckState.RUNNING, List.of(progressMessage));
        for (int j = i + 1; j < nonEmptyDiagrams.size(); j++) {
            UMLDiagram model1 = nonEmptyDiagrams.get(i);
            UMLDiagram model2 = nonEmptyDiagrams.get(j);
            final double similarity = model1.similarity(model2);
            log.debug("Compare result {} with {}: {}", i, j, similarity);
            if (similarity < minimumSimilarity) {
                // ignore comparison results with too small similarity
                continue;
            }
            ModelingSubmission modelingSubmissionA = models.get(model1);
            ModelingSubmission modelingSubmissionB = models.get(model2);
            log.info("Found similar models {} with {}: {}", i, j, similarity);
            PlagiarismSubmission<ModelingSubmissionElement> submissionA = PlagiarismSubmission.fromModelingSubmission(modelingSubmissionA);
            submissionA.setSize(model1.getAllModelElements().size());
            submissionA.setElements(model1.getAllModelElements().stream().map(ModelingSubmissionElement::fromUMLElement).collect(Collectors.toList()));
            PlagiarismSubmission<ModelingSubmissionElement> submissionB = PlagiarismSubmission.fromModelingSubmission(modelingSubmissionB);
            submissionB.setSize(model2.getAllModelElements().size());
            submissionB.setElements(model2.getAllModelElements().stream().map(ModelingSubmissionElement::fromUMLElement).collect(Collectors.toList()));
            PlagiarismComparison<ModelingSubmissionElement> comparison = new PlagiarismComparison<>();
            comparison.setPlagiarismResult(result);
            comparison.setSimilarity(similarity * 100);
            comparison.setSubmissionA(submissionA);
            comparison.setSubmissionB(submissionB);
            // TODO: Add matches to highlight similar modeling elements
            comparison.setMatches(new HashSet<>());
            comparisons.add(comparison);
        }
    }
    log.info("Found {} similar modeling submission combinations (>{})", comparisons.size(), minimumSimilarity);
    plagiarismWebsocketService.notifyInstructorAboutPlagiarismState(topic, PlagiarismCheckState.COMPLETED, List.of());
    long durationInMillis = System.currentTimeMillis() - timeBeforeStartInMillis;
    int[] similarityDistribution = calculateSimilarityDistribution(comparisons);
    result.setComparisons(comparisons);
    result.setDuration(durationInMillis);
    result.setSimilarityDistribution(similarityDistribution);
    return result;
}
Also used : java.util(java.util) ModelingPlagiarismResult(de.tum.in.www1.artemis.domain.plagiarism.modeling.ModelingPlagiarismResult) Logger(org.slf4j.Logger) JsonParser.parseString(com.google.gson.JsonParser.parseString) UMLDiagram(de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram) Participation(de.tum.in.www1.artemis.domain.participation.Participation) LoggerFactory(org.slf4j.LoggerFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) PlagiarismCheckState(de.tum.in.www1.artemis.domain.PlagiarismCheckState) PlagiarismComparison(de.tum.in.www1.artemis.domain.plagiarism.PlagiarismComparison) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) ModelingSubmissionElement(de.tum.in.www1.artemis.domain.plagiarism.modeling.ModelingSubmissionElement) ModelingSubmission(de.tum.in.www1.artemis.domain.modeling.ModelingSubmission) PlagiarismSubmission(de.tum.in.www1.artemis.domain.plagiarism.PlagiarismSubmission) UMLModelParser(de.tum.in.www1.artemis.service.compass.umlmodel.parsers.UMLModelParser) ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise) PlagiarismComparison(de.tum.in.www1.artemis.domain.plagiarism.PlagiarismComparison) ModelingSubmission(de.tum.in.www1.artemis.domain.modeling.ModelingSubmission) JsonParser.parseString(com.google.gson.JsonParser.parseString) IOException(java.io.IOException) ModelingPlagiarismResult(de.tum.in.www1.artemis.domain.plagiarism.modeling.ModelingPlagiarismResult) UMLDiagram(de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ModelingSubmissionElement(de.tum.in.www1.artemis.domain.plagiarism.modeling.ModelingSubmissionElement) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 9 with UMLDiagram

use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram in project Artemis by ls1intum.

the class UMLModelParserTest method buildModelFromJSON_classDiagram.

@Test
void buildModelFromJSON_classDiagram() throws Exception {
    JsonObject classDiagramJson = loadFileFromResources("test-data/model-submission/example-class-diagram.json");
    List<UMLElement> expectedElements = createExampleClassDiagramElements();
    UMLDiagram umlDiagram = UMLModelParser.buildModelFromJSON(classDiagramJson, 123456789);
    assertThat(umlDiagram.getModelSubmissionId()).isEqualTo(123456789);
    assertThat(umlDiagram.getAllModelElements()).containsExactlyInAnyOrderElementsOf(expectedElements);
}
Also used : UMLDiagram(de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram) UMLElement(de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement) JsonObject(com.google.gson.JsonObject) Test(org.junit.jupiter.api.Test)

Example 10 with UMLDiagram

use of de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram in project Artemis by ls1intum.

the class UMLModelParserTest method buildModelFromJSON_useCaseDiagram_empty.

@Test
void buildModelFromJSON_useCaseDiagram_empty() throws Exception {
    JsonObject useCaseDiagramJson = loadFileFromResources("test-data/model-submission/empty-use-case-diagram.json");
    UMLDiagram umlDiagram = UMLModelParser.buildModelFromJSON(useCaseDiagramJson, 123456789);
    assertThat(umlDiagram.getModelSubmissionId()).isEqualTo(123456789);
    assertThat(umlDiagram.getAllModelElements()).isEmpty();
}
Also used : UMLDiagram(de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram) JsonObject(com.google.gson.JsonObject) Test(org.junit.jupiter.api.Test)

Aggregations

UMLDiagram (de.tum.in.www1.artemis.service.compass.umlmodel.UMLDiagram)38 Test (org.junit.jupiter.api.Test)36 JsonObject (com.google.gson.JsonObject)20 AbstractUMLDiagramTest (de.tum.in.www1.artemis.service.compass.umlmodel.AbstractUMLDiagramTest)16 UMLElement (de.tum.in.www1.artemis.service.compass.umlmodel.UMLElement)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonParser.parseString (com.google.gson.JsonParser.parseString)2 PlagiarismCheckState (de.tum.in.www1.artemis.domain.PlagiarismCheckState)2 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)2 ModelingSubmission (de.tum.in.www1.artemis.domain.modeling.ModelingSubmission)2 Participation (de.tum.in.www1.artemis.domain.participation.Participation)2 PlagiarismComparison (de.tum.in.www1.artemis.domain.plagiarism.PlagiarismComparison)2 PlagiarismSubmission (de.tum.in.www1.artemis.domain.plagiarism.PlagiarismSubmission)2 ModelingPlagiarismResult (de.tum.in.www1.artemis.domain.plagiarism.modeling.ModelingPlagiarismResult)2 ModelingSubmissionElement (de.tum.in.www1.artemis.domain.plagiarism.modeling.ModelingSubmissionElement)2 UMLModelParser (de.tum.in.www1.artemis.service.compass.umlmodel.parsers.UMLModelParser)2 IOException (java.io.IOException)2 java.util (java.util)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors (java.util.stream.Collectors)2