Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ModelActionX instance #850

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,88 @@ public void apply(T target) {
val configurer = target.getExtensions().getByType(ModelConfigurer.class);

// Rules to execute actions
configurer.configure(ModelActionWithInputs.of(ModelTags.referenceOf(ModelActionTag.class), this::trackActions));
configurer.configure(ModelActionWithInputs.of(ModelTags.referenceOf(ConfigurableTag.class), this::trackConfigurableEntities));
configurer.configure(ModelActionWithInputs.of(ModelComponentReference.of(ActionSelectorComponent.class), this::onIdentityChanged));
configurer.configure(ModelActionWithInputs.of(ModelComponentReference.of(ModelSpecComponent.class), ModelComponentReference.of(ModelActionComponent.class), this::onActionAdded));
configurer.configure(/*trackActions*/new ModelActionWithInputs.ModelAction1<ModelComponentTag<ModelActionTag>>() {
protected void execute(ModelNode entity, ModelComponentTag<ModelActionTag> tag) {
allActionEntities.add(entity);
}
});
configurer.configure(/*trackConfigurableEntities*/new ModelActionWithInputs.ModelAction1<ModelComponentTag<ConfigurableTag>>() {
protected void execute(ModelNode entity, ModelComponentTag<ConfigurableTag> tag) {
allConfigurableEntities.add(entity);
}
});
configurer.configure(/*onIdentityChanged*/new ModelActionWithInputs.ModelAction1<ActionSelectorComponent>() {
// ComponentFromEntity<MatchingSpecificationComponent> (readonly) all
// ComponentFromEntity<ActionComponent> (readonly) all
// ComponentFromEntity<ExecutedActionComponent> (read-write) self
protected void execute(ModelNode entity, ActionSelectorComponent component) {
allActions(reentrant.andDeferredActions(entity, filter(actionMatching(component),
whileIgnoringExecuted(entity, executeAction(entity)))));
}
});
configurer.configure(/*onActionAdded*/new ModelActionWithInputs.ModelAction2<ModelSpecComponent, ModelActionComponent>() {
// ComponentFromEntity<ActionSelectorComponent> (readonly) all
// ComponentFromEntity<ExecutedActionComponent> (read-write) all
protected void execute(ModelNode entity, ModelSpecComponent identity, ModelActionComponent component) {
allEntities(filter(onlyMatching(identity),
it -> it.forEach(reentrant.ifPossible(entity, updateExecutedAfter(entity, executeAction(component))))));
}
});

// Rules to keep identity up-to-date
configurer.configure(ModelActionWithInputs.of(ModelTags.referenceOf(ConfigurableTag.class), ModelComponentReference.of(ModelProjection.class), this::updateSelectorForProjection));
configurer.configure(ModelActionWithInputs.of(ModelTags.referenceOf(ConfigurableTag.class), ModelComponentReference.of(ParentComponent.class), this::updateSelectorForParent));
configurer.configure(ModelActionWithInputs.of(ModelTags.referenceOf(ConfigurableTag.class), ModelComponentReference.of(ModelState.class), this::updateSelectorForState));
configurer.configure(ModelActionWithInputs.of(ModelTags.referenceOf(ConfigurableTag.class), ModelComponentReference.of(ParentComponent.class), this::updateSelectorForAncestors));
configurer.configure(ModelActionWithInputs.of(ModelTags.referenceOf(ConfigurableTag.class), this::updateSelectorForSelf));
}
configurer.configure(/*updateSelectorForProjection*/new ModelActionWithInputs.ModelAction2<ModelComponentTag<ConfigurableTag>, ModelProjection>() {
// ComponentFromEntity<ActionSelectorComponent> read-write self
protected void execute(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, ModelProjection projection) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.plus(projection.getType()))
.orElseGet(() -> DomainObjectIdentity.of(ImmutableSet.of(projection.getType())))));
}
});
configurer.configure(/*updateSelectorForParent*/new ModelActionWithInputs.ModelAction2<ModelComponentTag<ConfigurableTag>, ParentComponent>() {
// ComponentFromEntity<ActionSelectorComponent> read-write self
protected void execute(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, ParentComponent parent) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(new ParentRef(parent.get().getId())))
.orElseGet(() -> DomainObjectIdentity.of(new ParentRef(parent.get().getId())))));
}
});
configurer.configure(/*updateSelectorForState*/new ModelActionWithInputs.ModelAction2<ModelComponentTag<ConfigurableTag>, ModelState>() {
// ComponentFromEntity<ActionSelectorComponent> read-write self
protected void execute(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, ModelState state) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(state))
.orElseGet(() -> DomainObjectIdentity.of(state))));
}
});
configurer.configure(/*updateSelectorForAncestors*/new ModelActionWithInputs.ModelAction2<ModelComponentTag<ConfigurableTag>, ParentComponent>() {
// ComponentFromEntity<ParentComponent> read-only all
// ComponentFromEntity<ActionSelectorComponent> read-write self
protected void execute(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, ParentComponent parent) {
val ancestors = ImmutableSet.<AncestorRef>builder();
Optional<ParentComponent> parentComponent = Optional.of(parent);
while(parentComponent.isPresent()) {
ancestors.add(new AncestorRef(parentComponent.get().get().getId()));
parentComponent = parentComponent.flatMap(it -> it.get().findComponent(componentOf(ParentComponent.class)));
}

// ComponentFromEntity<MatchingSpecificationComponent> (readonly) all
// ComponentFromEntity<ActionComponent> (readonly) all
// ComponentFromEntity<ExecutedActionComponent> (read-write) self
private void onIdentityChanged(ModelNode entity, ActionSelectorComponent component) {
allActions(reentrant.andDeferredActions(entity, filter(actionMatching(component),
whileIgnoringExecuted(entity, executeAction(entity)))));
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(ancestors.build()))
.orElseGet(() -> DomainObjectIdentity.of(ancestors.build()))));
}
});
configurer.configure(/*updateSelectorForSelf*/new ModelActionWithInputs.ModelAction1<ModelComponentTag<ConfigurableTag>>() {
// ComponentFromEntity<ActionSelectorComponent> read-write self
protected void execute(ModelNode entity, ModelComponentTag<ConfigurableTag> tag) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(new SelfRef(entity.getId())))
.orElseGet(() -> DomainObjectIdentity.of(new SelfRef(entity.getId())))));
}
});
}

private static Consumer<ModelNode> executeAction(ModelNode entity) {
Expand Down Expand Up @@ -136,69 +199,6 @@ private static Predicate<ModelNode> actionMatching(ActionSelectorComponent compo
};
}

private void trackActions(ModelNode entity, ModelComponentTag<ModelActionTag> tag) {
allActionEntities.add(entity);
}

private void trackConfigurableEntities(ModelNode entity, ModelComponentTag<ConfigurableTag> tag) {
allConfigurableEntities.add(entity);
}

// ComponentFromEntity<ActionSelectorComponent> read-write self
private void updateSelectorForState(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, ModelState state) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(state))
.orElseGet(() -> DomainObjectIdentity.of(state))));
}

// ComponentFromEntity<ActionSelectorComponent> read-write self
private void updateSelectorForParent(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, ParentComponent parent) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(new ParentRef(parent.get().getId())))
.orElseGet(() -> DomainObjectIdentity.of(new ParentRef(parent.get().getId())))));
}

// ComponentFromEntity<ActionSelectorComponent> read-write self
private void updateSelectorForProjection(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, ModelProjection projection) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.plus(projection.getType()))
.orElseGet(() -> DomainObjectIdentity.of(ImmutableSet.of(projection.getType())))));
}

// ComponentFromEntity<ParentComponent> read-only all
// ComponentFromEntity<ActionSelectorComponent> read-write self
private void updateSelectorForAncestors(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, ParentComponent parent) {
val ancestors = ImmutableSet.<AncestorRef>builder();
Optional<ParentComponent> parentComponent = Optional.of(parent);
while(parentComponent.isPresent()) {
ancestors.add(new AncestorRef(parentComponent.get().get().getId()));
parentComponent = parentComponent.flatMap(it -> it.get().findComponent(componentOf(ParentComponent.class)));
}

entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(ancestors.build()))
.orElseGet(() -> DomainObjectIdentity.of(ancestors.build()))));
}

// ComponentFromEntity<ActionSelectorComponent> read-write self
private void updateSelectorForSelf(ModelNode entity, ModelComponentTag<ConfigurableTag> tag) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(new SelfRef(entity.getId())))
.orElseGet(() -> DomainObjectIdentity.of(new SelfRef(entity.getId())))));
}

// ComponentFromEntity<ActionSelectorComponent> (readonly) all
// ComponentFromEntity<ExecutedActionComponent> (read-write) all
private void onActionAdded(ModelNode entity, ModelSpecComponent identity, ModelActionComponent component) {
allEntities(filter(onlyMatching(identity),
it -> it.forEach(reentrant.ifPossible(entity, updateExecutedAfter(entity, executeAction(component))))));
}

private static Consumer<ModelNode> executeAction(ModelActionComponent component) {
return it -> component.get().execute(it);
}
Expand All @@ -220,9 +220,8 @@ private static Predicate<ModelNode> onlyMatching(ModelSpecComponent component) {

// ComponentFromEntity<ActionSelectorComponent> read-write self
public static <T extends ModelComponent> ModelAction updateSelectorForTag(Class<T> componentType) {
return ModelActionWithInputs.of(ModelTags.referenceOf(ConfigurableTag.class), ModelComponentReference.of(componentType), new ModelActionWithInputs.A2<ModelComponentTag<ConfigurableTag>, T>() {
@Override
public void execute(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, T component) {
return new ModelActionWithInputs.ModelAction2<ModelComponentTag<ConfigurableTag>, T>(ModelTags.referenceOf(ConfigurableTag.class), ModelComponentReference.of(componentType)) {
protected void execute(ModelNode entity, ModelComponentTag<ConfigurableTag> tag, T component) {
entity.addComponent(new ActionSelectorComponent(entity.findComponent(componentOf(ActionSelectorComponent.class))
.map(ActionSelectorComponent::get)
.map(it -> it.with(valueOf(component)))
Expand All @@ -237,6 +236,6 @@ private Object valueOf(T component) {
return unpacked;
}
}
});
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.common.collect.ImmutableSet;
import dev.nokee.model.internal.core.ModelActionWithInputs;
import dev.nokee.model.internal.core.ModelComponentReference;
import dev.nokee.model.internal.core.ModelNode;
import dev.nokee.model.internal.core.ParentComponent;
import dev.nokee.model.internal.core.ParentUtils;
Expand All @@ -29,10 +28,10 @@
public abstract class AncestryCapabilityPlugin<T extends ExtensionAware & PluginAware> implements Plugin<T> {
@Override
public void apply(T target) {
target.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionWithInputs.of(ModelComponentReference.of(ParentComponent.class), AncestryCapabilityPlugin::calculateAncestorsFromParent));
}

private static void calculateAncestorsFromParent(ModelNode entity, ParentComponent parent) {
entity.addComponent(new AncestorsComponent(new Ancestors(ParentUtils.stream(parent).map(AncestorRef::of).collect(ImmutableSet.toImmutableSet()))));
target.getExtensions().getByType(ModelConfigurer.class).configure(/*calculateAncestorsFromParent*/new ModelActionWithInputs.ModelAction1<ParentComponent>() {
protected void execute(ModelNode entity, ParentComponent parent) {
entity.addComponent(new AncestorsComponent(new Ancestors(ParentUtils.stream(parent).map(AncestorRef::of).collect(ImmutableSet.toImmutableSet()))));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import dev.nokee.model.internal.actions.ModelActionSystem;
import dev.nokee.model.internal.core.ModelActionWithInputs;
import dev.nokee.model.internal.core.ModelComponentReference;
import dev.nokee.model.internal.core.ModelNode;
import dev.nokee.model.internal.core.ParentComponent;
import dev.nokee.model.internal.core.ParentUtils;
Expand All @@ -33,22 +32,22 @@
public abstract class NamesCapabilityPlugin<T extends ExtensionAware & PluginAware> implements Plugin<T> {
@Override
public void apply(T target) {
target.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionWithInputs.of(ModelComponentReference.of(ParentComponent.class), ModelComponentReference.of(ElementNameComponent.class), ModelComponentReference.of(ModelState.IsAtLeastCreated.class), NamesCapabilityPlugin::computeRelativelyQualifiedName));
target.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionWithInputs.of(ModelComponentReference.of(ParentComponent.class), ModelComponentReference.of(ElementNameComponent.class), ModelComponentReference.of(ModelState.IsAtLeastCreated.class), NamesCapabilityPlugin::computeFullyQualifiedName));
target.getExtensions().getByType(ModelConfigurer.class).configure(/*computeRelativelyQualifiedName*/new ModelActionWithInputs.ModelAction3<ParentComponent, ElementNameComponent, ModelState.IsAtLeastCreated>() {
// ComponentFromEntity<MainComponentTag> read-only ancestors
// ComponentFromEntity<ElementNameComponent> read-only ancestors
protected void execute(ModelNode entity, ParentComponent parent, ElementNameComponent elementName, ModelState.IsAtLeastCreated ignored1) {
entity.addComponent(new RelativeNamesComponent(ParentUtils.stream(parent).collect(toRelativeNames(elementName.get()))));
}
});
target.getExtensions().getByType(ModelConfigurer.class).configure(/*computeFullyQualifiedName*/new ModelActionWithInputs.ModelAction3<ParentComponent, ElementNameComponent, ModelState.IsAtLeastCreated>() {
// ComponentFromEntity<MainComponentTag> read-only ancestors
// ComponentFromEntity<ElementNameComponent> read-only ancestors
protected void execute(ModelNode entity, ParentComponent parent, ElementNameComponent elementName, ModelState.IsAtLeastCreated ignored1) {
entity.addComponent(new FullyQualifiedNameComponent(ParentUtils.stream(parent).collect(toFullyQualifiedName(elementName.get()))));
}
});
target.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionSystem.updateSelectorForTag(RelativeNamesComponent.class));
target.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionSystem.updateSelectorForTag(FullyQualifiedNameComponent.class));
target.getExtensions().getByType(ModelConfigurer.class).configure(ModelActionSystem.updateSelectorForTag(ElementNameComponent.class));
}

// ComponentFromEntity<MainComponentTag> read-only ancestors
// ComponentFromEntity<ElementNameComponent> read-only ancestors
private static void computeRelativelyQualifiedName(ModelNode entity, ParentComponent parent, ElementNameComponent elementName, ModelState.IsAtLeastCreated ignored1) {
entity.addComponent(new RelativeNamesComponent(ParentUtils.stream(parent).collect(toRelativeNames(elementName.get()))));
}

// ComponentFromEntity<MainComponentTag> read-only ancestors
// ComponentFromEntity<ElementNameComponent> read-only ancestors
private static void computeFullyQualifiedName(ModelNode entity, ParentComponent parent, ElementNameComponent elementName, ModelState.IsAtLeastCreated ignored1) {
entity.addComponent(new FullyQualifiedNameComponent(ParentUtils.stream(parent).collect(toFullyQualifiedName(elementName.get()))));
}
}
Loading
Loading