Search in sources :

Example 1 with ConstraintSet

use of de.tudarmstadt.ukp.clarin.webanno.model.ConstraintSet in project webanno by webanno.

the class ConstraintsServiceImpl method loadConstraints.

@Override
public ParsedConstraints loadConstraints(Project aProject) throws IOException, ParseException {
    ParsedConstraints merged = null;
    for (ConstraintSet set : listConstraintSets(aProject)) {
        String script = readConstrainSet(set);
        ConstraintsGrammar parser = new ConstraintsGrammar(new StringReader(script));
        Parse p = parser.Parse();
        ParsedConstraints constraints = p.accept(new ParserVisitor());
        if (merged == null) {
            merged = constraints;
        } else {
            // Merge imports
            for (Entry<String, String> e : constraints.getImports().entrySet()) {
                // constraint file(s).
                if (merged.getImports().containsKey(e.getKey()) && !e.getValue().equalsIgnoreCase(merged.getImports().get(e.getKey()))) {
                    // If detected, notify user with proper message and abort merging
                    String errorMessage = "Conflict detected in imports for key \"" + e.getKey() + "\", conflicting values are \"" + e.getValue() + "\" & \"" + merged.getImports().get(e.getKey()) + "\". Please contact Project Admin for correcting this." + "Constraints feature may not work." + "\nAborting Constraint rules merge!";
                    throw new ParseException(errorMessage);
                }
            }
            merged.getImports().putAll(constraints.getImports());
            // Merge scopes
            for (Scope scope : constraints.getScopes()) {
                Scope target = merged.getScopeByName(scope.getScopeName());
                if (target == null) {
                    // Scope does not exist yet
                    merged.getScopes().add(scope);
                } else {
                    // Scope already exists
                    target.getRules().addAll(scope.getRules());
                }
            }
        }
    }
    return merged;
}
Also used : Scope(de.tudarmstadt.ukp.clarin.webanno.constraints.model.Scope) Parse(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse) StringReader(java.io.StringReader) ParserVisitor(de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor) ParsedConstraints(de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints) ParseException(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ParseException) ConstraintSet(de.tudarmstadt.ukp.clarin.webanno.model.ConstraintSet) ConstraintsGrammar(de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar)

Example 2 with ConstraintSet

use of de.tudarmstadt.ukp.clarin.webanno.model.ConstraintSet in project webanno by webanno.

the class ExportUtil method exportProjectConstraints.

/**
 * Copy Project Constraints from file system of this project to export folder
 */
public static void exportProjectConstraints(ConstraintsService constraintsService, Project project, File exportTempDir) throws IOException {
    File constraintsDir = new File(exportTempDir + CONSTRAINTS);
    FileUtils.forceMkdir(constraintsDir);
    String fileName;
    for (ConstraintSet set : constraintsService.listConstraintSets(project)) {
        fileName = set.getName();
        /*
          * Copying with file's original name to save ConstraintSet's name
          */
        FileUtils.copyFile(constraintsService.exportConstraintAsFile(set), new File(constraintsDir, fileName));
    }
}
Also used : ConstraintSet(de.tudarmstadt.ukp.clarin.webanno.model.ConstraintSet) File(java.io.File)

Example 3 with ConstraintSet

use of de.tudarmstadt.ukp.clarin.webanno.model.ConstraintSet in project webanno by webanno.

the class ConstraintsServiceEventAdapter method onProjectImport.

@EventListener
public void onProjectImport(ProjectImportEvent aEvent) throws Exception {
    Project project = aEvent.getProject();
    ZipFile zipFile = aEvent.getZip();
    for (Enumeration zipEnumerate = zipFile.entries(); zipEnumerate.hasMoreElements(); ) {
        ZipEntry entry = (ZipEntry) zipEnumerate.nextElement();
        // Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985)
        String entryName = ZipUtils.normalizeEntryName(entry);
        if (entryName.startsWith(ConstraintsService.CONSTRAINTS + "/")) {
            String fileName = FilenameUtils.getName(entry.getName());
            if (fileName.trim().isEmpty()) {
                continue;
            }
            ConstraintSet constraintSet = new ConstraintSet();
            constraintSet.setProject(project);
            constraintSet.setName(fileName);
            service.createConstraintSet(constraintSet);
            service.writeConstraintSet(constraintSet, zipFile.getInputStream(entry));
            log.info("Imported constraint [" + fileName + "] for project [" + project.getName() + "] with id [" + project.getId() + "]");
        }
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ConstraintSet(de.tudarmstadt.ukp.clarin.webanno.model.ConstraintSet) EventListener(org.springframework.context.event.EventListener)

Aggregations

ConstraintSet (de.tudarmstadt.ukp.clarin.webanno.model.ConstraintSet)3 ConstraintsGrammar (de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ConstraintsGrammar)1 ParseException (de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.ParseException)1 Parse (de.tudarmstadt.ukp.clarin.webanno.constraints.grammar.syntaxtree.Parse)1 ParsedConstraints (de.tudarmstadt.ukp.clarin.webanno.constraints.model.ParsedConstraints)1 Scope (de.tudarmstadt.ukp.clarin.webanno.constraints.model.Scope)1 ParserVisitor (de.tudarmstadt.ukp.clarin.webanno.constraints.visitor.ParserVisitor)1 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)1 File (java.io.File)1 StringReader (java.io.StringReader)1 Enumeration (java.util.Enumeration)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 EventListener (org.springframework.context.event.EventListener)1