Search in sources :

Example 1 with Revision

use of edu.stanford.bmir.protege.web.server.revision.Revision in project webprotege by protegeproject.

the class HighLevelEventGenerator method translateOntologyChanges.

@Override
public void translateOntologyChanges(Revision revision, ChangeApplicationResult<?> changes, final List<ProjectEvent<?>> projectEventList) {
    // TODO: NEED TIDYING AND SPLITTING UP
    final Set<OWLEntity> changedEntities = new HashSet<>();
    for (OWLOntologyChange change : changes.getChangeList()) {
        change.accept(new OWLOntologyChangeVisitor() {

            @Override
            public void visit(@Nonnull AddAxiom change) {
                final OWLAxiom axiom = change.getAxiom();
                handleAxiomChange(axiom);
            }

            @Override
            public void visit(@Nonnull RemoveAxiom change) {
                handleAxiomChange(change.getAxiom());
            }

            @Override
            public void visit(@Nonnull SetOntologyID change) {
                projectEventList.add(new OntologyFrameChangedEvent(change.getOntology().getOntologyID(), projectId));
            }

            @Override
            public void visit(@Nonnull AddImport change) {
                projectEventList.add(new OntologyFrameChangedEvent(change.getOntology().getOntologyID(), projectId));
            }

            @Override
            public void visit(@Nonnull RemoveImport change) {
                projectEventList.add(new OntologyFrameChangedEvent(change.getOntology().getOntologyID(), projectId));
            }

            @Override
            public void visit(@Nonnull AddOntologyAnnotation change) {
                projectEventList.add(new OntologyFrameChangedEvent(change.getOntology().getOntologyID(), projectId));
            }

            @Override
            public void visit(@Nonnull RemoveOntologyAnnotation change) {
                projectEventList.add(new OntologyFrameChangedEvent(change.getOntology().getOntologyID(), projectId));
            }

            private void handleAxiomChange(OWLAxiom axiom) {
                AxiomSubjectProvider p = new AxiomSubjectProvider();
                OWLObject subject = p.getSubject(axiom);
                Set<OWLEntity> entities;
                if (subject instanceof IRI) {
                    entities = hasGetEntitiesWithIRI.getEntitiesWithIRI((IRI) subject);
                } else if (subject instanceof OWLEntity) {
                    entities = Collections.singleton((OWLEntity) subject);
                } else {
                    entities = Collections.emptySet();
                }
                for (OWLEntity e : entities) {
                    if (!changedEntities.contains(e)) {
                        changedEntities.add(e);
                        ProjectEvent<?> event = e.accept(new OWLEntityVisitorEx<ProjectEvent<?>>() {

                            @Nonnull
                            @Override
                            public ProjectEvent<?> visit(@Nonnull OWLClass cls) {
                                return new ClassFrameChangedEvent(cls, projectId, revision.getUserId());
                            }

                            @Nonnull
                            @Override
                            public ProjectEvent<?> visit(@Nonnull OWLObjectProperty property) {
                                return new ObjectPropertyFrameChangedEvent(property, projectId, revision.getUserId());
                            }

                            @Nonnull
                            @Override
                            public ProjectEvent<?> visit(@Nonnull OWLDataProperty property) {
                                return new DataPropertyFrameChangedEvent(property, projectId, revision.getUserId());
                            }

                            @Nonnull
                            @Override
                            public ProjectEvent<?> visit(@Nonnull OWLNamedIndividual individual) {
                                return new NamedIndividualFrameChangedEvent(individual, projectId, revision.getUserId());
                            }

                            @Nonnull
                            @Override
                            public ProjectEvent<?> visit(@Nonnull OWLDatatype datatype) {
                                return new DatatypeFrameChangedEvent(datatype, projectId, revision.getUserId());
                            }

                            @Nonnull
                            @Override
                            public ProjectEvent<?> visit(@Nonnull OWLAnnotationProperty property) {
                                return new AnnotationPropertyFrameChangedEvent(property, projectId, revision.getUserId());
                            }
                        });
                        projectEventList.add(event);
                    }
                }
            }
        });
    }
    Set<OWLEntityData> changedEntitiesData = new HashSet<>();
    Object subject = changes.getSubject();
    if (subject instanceof OWLEntity) {
        OWLEntity entity = (OWLEntity) subject;
        if (rootOntology.containsEntityInSignature(entity)) {
            String browserText = browserTextProvider.getOWLEntityBrowserText(entity).orElse("");
            changedEntitiesData.add(DataFactory.getOWLEntityData(entity, browserText));
        }
    } else if (subject instanceof OWLEntityData) {
        OWLEntityData entityData = (OWLEntityData) subject;
        if (rootOntology.containsEntityInSignature(entityData.getEntity())) {
            changedEntitiesData.add(entityData);
        }
    } else if (subject instanceof Collection) {
        Collection<?> collection = (Collection<?>) subject;
        collection.stream().filter(element -> element instanceof OWLEntity).map(element -> (OWLEntity) element).filter(rootOntology::containsEntityInSignature).forEach(entity -> {
            String browserText = browserTextProvider.getOWLEntityBrowserText(entity).orElse("");
            changedEntitiesData.add(DataFactory.getOWLEntityData(entity, browserText));
        });
    }
    Optional<RevisionSummary> revisionSummary = hasGetRevisionSummary.getRevisionSummary(revision.getRevisionNumber());
    if (revisionSummary.isPresent()) {
        ProjectEvent<?> event = new ProjectChangedEvent(projectId, revisionSummary.get(), changedEntitiesData);
        projectEventList.add(event);
    }
}
Also used : BrowserTextProvider(edu.stanford.bmir.protege.web.shared.BrowserTextProvider) Revision(edu.stanford.bmir.protege.web.server.revision.Revision) java.util(java.util) org.semanticweb.owlapi.model(org.semanticweb.owlapi.model) HasGetRevisionSummary(edu.stanford.bmir.protege.web.server.change.HasGetRevisionSummary) AxiomSubjectProvider(org.semanticweb.owlapi.util.AxiomSubjectProvider) OWLEntityData(edu.stanford.bmir.protege.web.shared.entity.OWLEntityData) DataFactory(edu.stanford.bmir.protege.web.shared.DataFactory) HasGetEntitiesWithIRI(edu.stanford.bmir.protege.web.shared.HasGetEntitiesWithIRI) Inject(javax.inject.Inject) edu.stanford.bmir.protege.web.shared.event(edu.stanford.bmir.protege.web.shared.event) ProjectId(edu.stanford.bmir.protege.web.shared.project.ProjectId) Nonnull(javax.annotation.Nonnull) ChangeApplicationResult(edu.stanford.bmir.protege.web.server.change.ChangeApplicationResult) RevisionSummary(edu.stanford.bmir.protege.web.shared.revision.RevisionSummary) HasGetEntitiesWithIRI(edu.stanford.bmir.protege.web.shared.HasGetEntitiesWithIRI) Nonnull(javax.annotation.Nonnull) OWLEntityData(edu.stanford.bmir.protege.web.shared.entity.OWLEntityData) AxiomSubjectProvider(org.semanticweb.owlapi.util.AxiomSubjectProvider) HasGetRevisionSummary(edu.stanford.bmir.protege.web.server.change.HasGetRevisionSummary) RevisionSummary(edu.stanford.bmir.protege.web.shared.revision.RevisionSummary)

Example 2 with Revision

use of edu.stanford.bmir.protege.web.server.revision.Revision in project webprotege by protegeproject.

the class ProjectImporter method generateInitialChanges.

private void generateInitialChanges(UserId owner, OWLOntologyManager rootOntologyManager) {
    ImmutableList<OWLOntologyChangeRecord> changeRecords = getInitialChangeRecords(rootOntologyManager);
    revisionStore.addRevision(new Revision(owner, RevisionNumber.getRevisionNumber(1), changeRecords, System.currentTimeMillis(), "Initial import"));
}
Also used : OWLOntologyChangeRecord(org.semanticweb.owlapi.change.OWLOntologyChangeRecord) Revision(edu.stanford.bmir.protege.web.server.revision.Revision)

Example 3 with Revision

use of edu.stanford.bmir.protege.web.server.revision.Revision in project webprotege by protegeproject.

the class ChangeManager method logAndBroadcastAppliedChanges.

private <R> Revision logAndBroadcastAppliedChanges(UserId userId, ChangeListGenerator<R> changeList, ChangeApplicationResult<R> finalResult) {
    // Generate a description for the changes that were actually applied
    String changeDescription = changeList.getMessage(finalResult);
    // Log the changes
    List<OWLOntologyChangeRecord> changeRecords = finalResult.getChangeList().stream().map(OWLOntologyChange::getChangeRecord).collect(toList());
    Revision revision = changeManager.addRevision(userId, changeRecords, changeDescription);
    // TODO: THis list of "listeners" should be injected
    List<OWLOntologyChange> changes = finalResult.getChangeList();
    documentStore.saveOntologyChanges(changes);
    classHierarchyProvider.handleChanges(changes);
    objectPropertyHierarchyProvider.handleChanges(changes);
    dataPropertyHierarchyProvider.handleChanges(changes);
    annotationPropertyHierarchyProvider.handleChanges(changes);
    return revision;
}
Also used : OWLOntologyChangeRecord(org.semanticweb.owlapi.change.OWLOntologyChangeRecord) Revision(edu.stanford.bmir.protege.web.server.revision.Revision)

Example 4 with Revision

use of edu.stanford.bmir.protege.web.server.revision.Revision in project webprotege by protegeproject.

the class WatchedChangesManager method getProjectChangesForWatches.

public ImmutableList<ProjectChange> getProjectChangesForWatches(Set<Watch> watches) {
    Set<OWLEntity> superEntities = new HashSet<>();
    Set<OWLEntity> directWatches = new HashSet<>();
    for (Watch watch : watches) {
        if (watch.getType() == BRANCH) {
            OWLEntity entity = watch.getEntity();
            superEntities.add(entity);
            directWatches.add(entity);
        } else {
            directWatches.add(watch.getEntity());
        }
    }
    if (superEntities.isEmpty() && directWatches.isEmpty()) {
        return ImmutableList.of();
    }
    ImmutableList.Builder<ProjectChange> result = ImmutableList.builder();
    List<Revision> revisionsCopy = changeManager.getRevisions();
    for (Revision revision : revisionsCopy) {
        for (OWLEntity watchedEntity : getWatchedEntities(superEntities, directWatches, revision)) {
            ImmutableList<ProjectChange> changes = projectChangesManager.getProjectChangesForSubjectInRevision(watchedEntity, revision);
            result.addAll(changes);
        }
    }
    return result.build();
}
Also used : Revision(edu.stanford.bmir.protege.web.server.revision.Revision) ImmutableList(com.google.common.collect.ImmutableList) Watch(edu.stanford.bmir.protege.web.shared.watches.Watch) ProjectChange(edu.stanford.bmir.protege.web.shared.change.ProjectChange) HashSet(java.util.HashSet)

Example 5 with Revision

use of edu.stanford.bmir.protege.web.server.revision.Revision in project webprotege by protegeproject.

the class ChangeManager method applyChanges.

/**
 * Applies ontology changes to the ontologies contained within a project.
 *
 * @param userId                     The userId of the user applying the changes.  Not {@code null}.
 * @param changeListGenerator        A generator which creates a list of changes (based on the state of the project at
 *                                   the time of change application).  The idea behind passing in a change generator is that the list of changes to
 *                                   be applied can be created based on the state of the project immediately before they are applied.  This is
 *                                   necessary where the changes depend on the structure/state of the ontology.  This method guarantees that no third
 *                                   party
 *                                   ontology changes will take place between the {@link ChangeListGenerator#generateChanges(ChangeGenerationContext)}
 *                                   method being called and the changes being applied.
 * @return A {@link ChangeApplicationResult} that describes the changes which took place an any renaminings.
 * @throws NullPointerException      if any parameters are {@code null}.
 * @throws PermissionDeniedException if the user identified by {@code userId} does not have permssion to write to
 *                                   ontologies in this project.
 */
@Override
public <R> ChangeApplicationResult<R> applyChanges(final UserId userId, final ChangeListGenerator<R> changeListGenerator) throws PermissionDeniedException {
    // noinspection ResultOfMethodCallIgnored
    checkNotNull(userId);
    // noinspection ResultOfMethodCallIgnored
    checkNotNull(changeListGenerator);
    // Final check of whether the user can actually edit the project
    Subject subject = forUser(userId);
    ProjectResource projectResource = new ProjectResource(projectId);
    if (!accessManager.hasPermission(subject, projectResource, EDIT_ONTOLOGY.getActionId())) {
        throw new PermissionDeniedException("You do not have permission to edit this project", userInSessionFactory.getUserInSession(userId));
    }
    final List<OWLOntologyChange> appliedChanges;
    final ChangeApplicationResult<R> finalResult;
    // with true entity IRIs
    try {
        // Compute the changes that need to take place.  We don't allow any other writes here because the
        // generation of the changes may depend upon the state of the project
        changeProcesssingLock.lock();
        final ChangeGenerationContext context = new ChangeGenerationContext(userId);
        OntologyChangeList<R> gen = changeListGenerator.generateChanges(context);
        // We have our changes
        List<OWLOntologyChange> changes = gen.getChanges();
        // We coin fresh entities for places where tmp: is the scheme - the name for the entity comes from
        // the fragment
        final Map<IRI, IRI> iriRenameMap = new HashMap<>();
        final ChangeSetEntityCrudSession session = getEntityCrudKitHandler().createChangeSetSession();
        Set<OWLOntologyChange> changesToRename = new HashSet<>();
        List<OWLOntologyChange> freshEntityChanges = new ArrayList<>();
        for (OWLOntologyChange change : changes) {
            for (OWLEntity entity : change.getSignature()) {
                if (DataFactory.isFreshEntity(entity)) {
                    if (entity.isOWLClass()) {
                        if (!accessManager.hasPermission(subject, projectResource, CREATE_CLASS.getActionId())) {
                            throw new PermissionDeniedException("You do not have permission to create new classes", userInSessionFactory.getUserInSession(userId));
                        }
                    } else if (entity.isOWLObjectProperty() || entity.isOWLDataProperty() || entity.isOWLAnnotationProperty()) {
                        if (!accessManager.hasPermission(subject, projectResource, CREATE_PROPERTY.getActionId())) {
                            throw new PermissionDeniedException("You do not have permission to create new properties", userInSessionFactory.getUserInSession(userId));
                        }
                    } else if (entity.isOWLNamedIndividual()) {
                        if (!accessManager.hasPermission(subject, projectResource, CREATE_INDIVIDUAL.getActionId())) {
                            throw new PermissionDeniedException("You do not have permission to create new individuals", userInSessionFactory.getUserInSession(userId));
                        }
                    } else if (entity.isOWLDatatype()) {
                        if (!accessManager.hasPermission(subject, projectResource, CREATE_DATATYPE.getActionId())) {
                            throw new PermissionDeniedException("You do not have permission to create new datatypes", userInSessionFactory.getUserInSession(userId));
                        }
                    }
                    changesToRename.add(change);
                    IRI currentIRI = entity.getIRI();
                    if (!iriRenameMap.containsKey(currentIRI)) {
                        String shortName = DataFactory.getFreshEntityShortName(entity);
                        OWLEntityCreator<? extends OWLEntity> creator = getEntityCreator(session, userId, shortName, (EntityType<? extends OWLEntity>) entity.getEntityType());
                        freshEntityChanges.addAll(creator.getChanges());
                        IRI replacementIRI = creator.getEntity().getIRI();
                        iriRenameMap.put(currentIRI, replacementIRI);
                    }
                }
            }
        }
        List<OWLOntologyChange> allChangesIncludingRenames = new ArrayList<>();
        final OWLObjectDuplicator duplicator = new OWLObjectDuplicator(dataFactory, iriRenameMap);
        for (OWLOntologyChange change : changes) {
            if (changesToRename.contains(change)) {
                OWLOntologyChange replacementChange = getRenamedChange(change, duplicator);
                allChangesIncludingRenames.add(replacementChange);
            } else {
                allChangesIncludingRenames.add(change);
            }
        }
        allChangesIncludingRenames.addAll(freshEntityChanges);
        List<OWLOntologyChange> minimisedChanges = getMinimisedChanges(allChangesIncludingRenames);
        final EventTranslatorManager eventTranslatorManager = eventTranslatorManagerProvider.get();
        eventTranslatorManager.prepareForOntologyChanges(minimisedChanges);
        // Now we do the actual changing, so we lock the project here.  No writes or reads can take place whilst
        // we apply the changes
        final Optional<Revision> revision;
        try {
            projectChangeWriteLock.lock();
            ProjectOWLOntologyManager manager = ((ProjectOWLOntologyManager) rootOntology.getOWLOntologyManager());
            List<OWLOntologyChange> effectiveChanges = getEffectiveChanges(minimisedChanges);
            manager.getDelegate().applyChanges(effectiveChanges);
            appliedChanges = effectiveChanges;
            final RenameMap renameMap = new RenameMap(iriRenameMap);
            R renamedResult = getRenamedResult(changeListGenerator, gen.getResult(), renameMap);
            finalResult = new ChangeApplicationResult<>(renamedResult, appliedChanges, renameMap);
            if (!appliedChanges.isEmpty()) {
                Revision rev = logAndBroadcastAppliedChanges(userId, changeListGenerator, finalResult);
                revision = Optional.of(rev);
                projectDetailsRepository.setModified(projectId, rev.getTimestamp(), userId);
            } else {
                revision = Optional.empty();
            }
        } finally {
            // Release for reads
            projectChangeWriteLock.unlock();
        }
        if (revision.isPresent() && !(changeListGenerator instanceof SilentChangeListGenerator)) {
            List<ProjectEvent<?>> highLevelEvents = new ArrayList<>();
            Revision rev = revision.get();
            eventTranslatorManager.translateOntologyChanges(rev, finalResult, highLevelEvents);
            if (changeListGenerator instanceof HasHighLevelEvents) {
                highLevelEvents.addAll(((HasHighLevelEvents) changeListGenerator).getHighLevelEvents());
            }
            projectEventManager.postEvents(highLevelEvents);
            projectChangedWebhookInvoker.invoke(userId, rev.getRevisionNumber(), rev.getTimestamp());
        }
    } finally {
        changeProcesssingLock.unlock();
    }
    return finalResult;
}
Also used : OWLObjectDuplicator(org.semanticweb.owlapi.util.OWLObjectDuplicator) EventTranslatorManager(edu.stanford.bmir.protege.web.server.events.EventTranslatorManager) Subject(edu.stanford.bmir.protege.web.server.access.Subject) Revision(edu.stanford.bmir.protege.web.server.revision.Revision) ProjectResource(edu.stanford.bmir.protege.web.server.access.ProjectResource) PermissionDeniedException(edu.stanford.bmir.protege.web.shared.permissions.PermissionDeniedException) RenameMap(edu.stanford.bmir.protege.web.server.owlapi.RenameMap) ProjectEvent(edu.stanford.bmir.protege.web.shared.event.ProjectEvent)

Aggregations

Revision (edu.stanford.bmir.protege.web.server.revision.Revision)7 OWLOntologyChangeRecord (org.semanticweb.owlapi.change.OWLOntologyChangeRecord)4 ImmutableList (com.google.common.collect.ImmutableList)1 ProjectResource (edu.stanford.bmir.protege.web.server.access.ProjectResource)1 Subject (edu.stanford.bmir.protege.web.server.access.Subject)1 ChangeApplicationResult (edu.stanford.bmir.protege.web.server.change.ChangeApplicationResult)1 HasGetRevisionSummary (edu.stanford.bmir.protege.web.server.change.HasGetRevisionSummary)1 EventTranslatorManager (edu.stanford.bmir.protege.web.server.events.EventTranslatorManager)1 RenameMap (edu.stanford.bmir.protege.web.server.owlapi.RenameMap)1 BrowserTextProvider (edu.stanford.bmir.protege.web.shared.BrowserTextProvider)1 DataFactory (edu.stanford.bmir.protege.web.shared.DataFactory)1 HasGetEntitiesWithIRI (edu.stanford.bmir.protege.web.shared.HasGetEntitiesWithIRI)1 ProjectChange (edu.stanford.bmir.protege.web.shared.change.ProjectChange)1 OWLEntityData (edu.stanford.bmir.protege.web.shared.entity.OWLEntityData)1 edu.stanford.bmir.protege.web.shared.event (edu.stanford.bmir.protege.web.shared.event)1 ProjectEvent (edu.stanford.bmir.protege.web.shared.event.ProjectEvent)1 PermissionDeniedException (edu.stanford.bmir.protege.web.shared.permissions.PermissionDeniedException)1 ProjectId (edu.stanford.bmir.protege.web.shared.project.ProjectId)1 RevisionSummary (edu.stanford.bmir.protege.web.shared.revision.RevisionSummary)1 UserId (edu.stanford.bmir.protege.web.shared.user.UserId)1