use of org.kie.workbench.common.stunner.core.api.DefinitionManager in project kie-wb-common by kiegroup.
the class ConnectorParentsMatchContainmentHandler method evaluateSingleContainment.
@SuppressWarnings("unchecked")
private void evaluateSingleContainment(final DefaultRuleViolations result, final RuleExtension rule, final NodeContainmentContext context, final Node<? extends Definition<?>, ? extends Edge> candidate) {
final GraphEvaluationState state = context.getState();
final Graph<?, ? extends Node> graph = context.getState().getGraph();
final String connectorId = rule.getId();
// Walk throw the graph and evaluate connector source and target nodes parent match.
treeWalkTraverseProcessor.traverse(graph, candidate, new AbstractTreeTraverseCallback<Graph, Node, Edge>() {
@Override
public boolean startNodeTraversal(final Node node) {
// Process incoming edges into the node as well.
final List<? extends Edge> inEdges = node.getInEdges();
if (null != inEdges) {
inEdges.stream().forEach(this::process);
}
return true;
}
@Override
public boolean startEdgeTraversal(final Edge edge) {
return process(edge);
}
private boolean process(final Edge edge) {
final Optional<String> eId = getId(definitionManager, edge);
if (eId.isPresent() && connectorId.equals(eId.get())) {
final Node sourceNode = state.getConnectionState().getSource(edge);
final Node targetNode = state.getConnectionState().getTarget(edge);
final boolean valid = new ParentTypesMatcher(() -> definitionManager, e -> getParent(context, e), rule.getTypeArguments()).matcher().test(sourceNode, targetNode);
if (!valid) {
addViolation(edge.getUUID(), rule, result);
}
}
return true;
}
});
}
use of org.kie.workbench.common.stunner.core.api.DefinitionManager in project kie-wb-common by kiegroup.
the class BPMNDiagramMarshallerBase method init.
@SuppressWarnings("unchecked")
protected void init() {
initMocks(this);
// Work Items.
workItemDefinitionMockRegistry = new WorkItemDefinitionMockRegistry();
definitionManager = mock(DefinitionManager.class);
adapterManager = mock(AdapterManager.class);
adapterRegistry = mock(AdapterRegistry.class);
rulesManager = mock(RuleManager.class);
cloneManager = mock(CloneManager.class);
// Graph utils.
when(definitionManager.adapters()).thenReturn(adapterManager);
when(adapterManager.registry()).thenReturn(adapterRegistry);
DefinitionUtils definitionUtils1 = new DefinitionUtils(definitionManager, applicationFactoryManager);
testScopeModelFactory = new TestScopeModelFactory(new BPMNDefinitionSet.BPMNDefinitionSetBuilder().build());
// Definition manager.
final BackendDefinitionAdapter definitionAdapter = new BackendDefinitionAdapter(definitionUtils1);
final BackendDefinitionSetAdapter definitionSetAdapter = new BackendDefinitionSetAdapter(definitionAdapter);
final BackendPropertySetAdapter propertySetAdapter = new BackendPropertySetAdapter();
final BackendPropertyAdapter propertyAdapter = new BackendPropertyAdapter();
when(adapterManager.forDefinitionSet()).thenReturn(definitionSetAdapter);
when(adapterManager.forDefinition()).thenReturn(definitionAdapter);
when(adapterManager.forPropertySet()).thenReturn(propertySetAdapter);
when(adapterManager.forProperty()).thenReturn(propertyAdapter);
when(adapterRegistry.getDefinitionSetAdapter(any(Class.class))).thenReturn(definitionSetAdapter);
when(adapterRegistry.getDefinitionAdapter(any(Class.class))).thenReturn(definitionAdapter);
when(adapterRegistry.getPropertySetAdapter(any(Class.class))).thenReturn(propertySetAdapter);
when(adapterRegistry.getPropertyAdapter(any(Class.class))).thenReturn(propertyAdapter);
GraphCommandManager commandManager1 = new GraphCommandManagerImpl(null, null, null);
GraphCommandFactory commandFactory1 = new GraphCommandFactory();
connectionEdgeFactory = new EdgeFactoryImpl(definitionManager);
viewNodeFactory = new NodeFactoryImpl(definitionUtils1);
bpmnGraphFactory = new GraphFactoryImpl(definitionManager);
doAnswer(invocationOnMock -> {
String id = (String) invocationOnMock.getArguments()[0];
return testScopeModelFactory.build(id);
}).when(applicationFactoryManager).newDefinition(anyString());
doAnswer(invocationOnMock -> {
String uuid = (String) invocationOnMock.getArguments()[0];
String id = (String) invocationOnMock.getArguments()[1];
if (BPMNDefinitionSet.class.getName().equals(id)) {
return bpmnGraphFactory.build(uuid, BPMN_DEF_SET_ID);
}
Object model = testScopeModelFactory.accepts(id) ? testScopeModelFactory.build(id) : null;
if (null != model) {
Class<? extends ElementFactory> element = BackendDefinitionAdapter.getGraphFactory(model.getClass());
if (element.isAssignableFrom(NodeFactory.class)) {
return viewNodeFactory.build(uuid, model);
} else if (element.isAssignableFrom(EdgeFactory.class)) {
return connectionEdgeFactory.build(uuid, model);
}
}
return null;
}).when(applicationFactoryManager).newElement(anyString(), anyString());
doAnswer(invocationOnMock -> {
String uuid = (String) invocationOnMock.getArguments()[0];
Class type = (Class) invocationOnMock.getArguments()[1];
String id = BindableAdapterUtils.getGenericClassName(type);
if (BPMNDefinitionSet.class.equals(type)) {
return bpmnGraphFactory.build(uuid, BPMN_DEF_SET_ID);
}
Object model = testScopeModelFactory.accepts(id) ? testScopeModelFactory.build(id) : null;
if (null != model) {
Class<? extends ElementFactory> element = BackendDefinitionAdapter.getGraphFactory(model.getClass());
if (element.isAssignableFrom(NodeFactory.class)) {
return viewNodeFactory.build(uuid, model);
} else if (element.isAssignableFrom(EdgeFactory.class)) {
return connectionEdgeFactory.build(uuid, model);
}
}
return null;
}).when(applicationFactoryManager).newElement(anyString(), any(Class.class));
doAnswer(invocationOnMock -> {
String uuid = (String) invocationOnMock.getArguments()[0];
String defSetId = (String) invocationOnMock.getArguments()[1];
final Graph graph = (Graph) applicationFactoryManager.newElement(uuid, defSetId);
final DiagramImpl result = new DiagramImpl(uuid, new MetadataImpl.MetadataImplBuilder(defSetId).build());
result.setGraph(graph);
return result;
}).when(applicationFactoryManager).newDiagram(anyString(), anyString(), any(Metadata.class));
// Bpmn 2 oryx stuff.
Bpmn2OryxIdMappings oryxIdMappings = new Bpmn2OryxIdMappings(definitionManager, () -> workItemDefinitionMockRegistry);
StringTypeSerializer stringTypeSerializer = new StringTypeSerializer();
BooleanTypeSerializer booleanTypeSerializer = new BooleanTypeSerializer();
ColorTypeSerializer colorTypeSerializer = new ColorTypeSerializer();
DoubleTypeSerializer doubleTypeSerializer = new DoubleTypeSerializer();
IntegerTypeSerializer integerTypeSerializer = new IntegerTypeSerializer();
EnumTypeSerializer enumTypeSerializer = new EnumTypeSerializer(definitionUtils1);
AssignmentsTypeSerializer assignmentsTypeSerializer = new AssignmentsTypeSerializer();
VariablesTypeSerializer variablesTypeSerializer = new VariablesTypeSerializer();
TimerSettingsTypeSerializer timerSettingsTypeSerializer = new TimerSettingsTypeSerializer();
ScriptTypeTypeSerializer scriptTypeTypeSerializer = new ScriptTypeTypeSerializer();
ScriptTypeListTypeSerializer scriptTypeListTypeSerializer = new ScriptTypeListTypeSerializer();
List<Bpmn2OryxPropertySerializer<?>> propertySerializers = new LinkedList<>();
propertySerializers.add(stringTypeSerializer);
propertySerializers.add(booleanTypeSerializer);
propertySerializers.add(colorTypeSerializer);
propertySerializers.add(doubleTypeSerializer);
propertySerializers.add(integerTypeSerializer);
propertySerializers.add(enumTypeSerializer);
propertySerializers.add(assignmentsTypeSerializer);
propertySerializers.add(variablesTypeSerializer);
propertySerializers.add(timerSettingsTypeSerializer);
propertySerializers.add(scriptTypeTypeSerializer);
propertySerializers.add(scriptTypeListTypeSerializer);
Bpmn2OryxPropertyManager oryxPropertyManager = new Bpmn2OryxPropertyManager(propertySerializers);
Bpmn2OryxManager oryxManager = new Bpmn2OryxManager(oryxIdMappings, oryxPropertyManager);
oryxManager.init();
// Marshalling factories.
BPMNGraphObjectBuilderFactory objectBuilderFactory = new BPMNGraphObjectBuilderFactory(definitionManager, oryxManager, () -> workItemDefinitionMockRegistry);
taskMorphDefinition = new TaskTypeMorphDefinition();
Collection<MorphDefinition> morphDefinitions = new ArrayList<MorphDefinition>() {
{
add(taskMorphDefinition);
}
};
BackendBindableMorphAdapter<Object> morphAdapter = new BackendBindableMorphAdapter(definitionUtils1, applicationFactoryManager, cloneManager, morphDefinitions);
when(adapterRegistry.getMorphAdapter(eq(UserTask.class))).thenReturn(morphAdapter);
when(adapterRegistry.getMorphAdapter(eq(NoneTask.class))).thenReturn(morphAdapter);
when(adapterRegistry.getMorphAdapter(eq(ScriptTask.class))).thenReturn(morphAdapter);
when(adapterRegistry.getMorphAdapter(eq(BusinessRuleTask.class))).thenReturn(morphAdapter);
GraphIndexBuilder<?> indexBuilder = new MapIndexBuilder();
when(rulesManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(new DefaultRuleViolations());
// The work item definition registry.
WorkItemDefinitionBackendRegistry widRegistry = mock(WorkItemDefinitionBackendRegistry.class);
when(widRegistry.getRegistry()).thenReturn(workItemDefinitionMockRegistry);
when(widRegistry.items()).thenReturn(workItemDefinitionMockRegistry.items());
when(widRegistry.load(any(Metadata.class))).thenReturn(widRegistry);
doAnswer(invocationOnMock -> workItemDefinitionMockRegistry.get((String) invocationOnMock.getArguments()[0])).when(widRegistry).get(anyString());
// The tested BPMN marshaller.
oldMarshaller = new BPMNDiagramMarshaller(new XMLEncoderDiagramMetadataMarshaller(), objectBuilderFactory, definitionManager, indexBuilder, oryxManager, applicationFactoryManager, rulesManager, commandManager1, commandFactory1, widRegistry);
// Graph utils.
when(definitionManager.adapters()).thenReturn(adapterManager);
when(adapterManager.registry()).thenReturn(adapterRegistry);
// initApplicationFactoryManagerAlt();
when(rulesManager.evaluate(any(RuleSet.class), any(RuleEvaluationContext.class))).thenReturn(new DefaultRuleViolations());
DefinitionUtils definitionUtils = new DefinitionUtils(definitionManager, applicationFactoryManager);
TestScopeModelFactory testScopeModelFactory = new TestScopeModelFactory(new BPMNDefinitionSet.BPMNDefinitionSetBuilder().build());
// Definition manager.
mockAdapterManager(definitionAdapter, definitionSetAdapter, propertySetAdapter, propertyAdapter);
mockAdapterRegistry(definitionAdapter, definitionSetAdapter, propertySetAdapter, propertyAdapter);
applicationFactoryManager = new MockApplicationFactoryManager(definitionManager, new GraphFactoryImpl(definitionManager), testScopeModelFactory, new EdgeFactoryImpl(definitionManager), new NodeFactoryImpl(definitionUtils));
GraphCommandManagerImpl commandManager = new GraphCommandManagerImpl(null, null, null);
GraphCommandFactory commandFactory = new GraphCommandFactory();
// The tested BPMN marshaller.
newMarshaller = new BPMNDirectDiagramMarshaller(new XMLEncoderDiagramMetadataMarshaller(), definitionManager, rulesManager, applicationFactoryManager, commandFactory, commandManager);
}
use of org.kie.workbench.common.stunner.core.api.DefinitionManager in project kie-wb-common by kiegroup.
the class CloneNodeCommandTest method testCloneNodeContentWithProperties.
@Test
public void testCloneNodeContentWithProperties() {
final View cloneContent = mock(View.class);
final DefinitionManager definitionManager = mock(DefinitionManager.class);
final CloneManager cloneManager = mock(CloneManager.class);
final Object clonedDefinition = mock(Object.class);
cloneNodeCommand.setClone(clone);
when(clone.getContent()).thenReturn(cloneContent);
when(graphCommandExecutionContext.getDefinitionManager()).thenReturn(definitionManager);
when(definitionManager.cloneManager()).thenReturn(cloneManager);
when(cloneManager.clone(candidateContent.getDefinition(), ClonePolicy.ALL)).thenReturn(clonedDefinition);
cloneNodeCommand.cloneNodeContentWithProperties(graphCommandExecutionContext);
verify(cloneContent).setBounds(boundsArgumentCaptor.capture());
verify(cloneContent).setDefinition(clonedDefinition);
final Bounds clonedBounds = boundsArgumentCaptor.getValue();
assertEquals(candidateBounds, clonedBounds);
assertNotSame(candidateBounds, clonedBounds);
}
use of org.kie.workbench.common.stunner.core.api.DefinitionManager in project kie-wb-common by kiegroup.
the class UpdateDomainObjectPropertyValueCommand method execute.
@Override
@SuppressWarnings("unchecked")
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
final DefinitionManager definitionManager = context.getDefinitionManager();
final Object p = definitionManager.adapters().forDefinition().getProperty(domainObject, field).get();
final AdapterManager adapterManager = definitionManager.adapters();
final AdapterRegistry adapterRegistry = adapterManager.registry();
final PropertyAdapter<Object, Object> adapter = (PropertyAdapter<Object, Object>) adapterRegistry.getPropertyAdapter(p.getClass());
oldValue = adapter.getValue(p);
adapter.setValue(p, value);
return GraphCommandResultBuilder.SUCCESS;
}
use of org.kie.workbench.common.stunner.core.api.DefinitionManager in project kie-wb-common by kiegroup.
the class MorphNodeCommand method execute.
@Override
@SuppressWarnings("unchecked")
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
checkSafeCandidate(context);
final CommandResult<RuleViolation> results = check(context);
final DefinitionManager definitionManager = context.getDefinitionManager();
final Object currentDef = candidate.getContent().getDefinition();
final String currentDefId = definitionManager.adapters().forDefinition().getId(currentDef).value();
this.oldMorphTarget = currentDefId;
final MorphAdapter<Object> morphAdapter = context.getDefinitionManager().adapters().registry().getMorphAdapter(currentDef.getClass());
if (null == morphAdapter) {
throw new RuntimeException("No morph adapter found for definition [" + currentDef.toString() + "] " + "and target morph [" + morphTarget + "]");
}
// Morph the bean instance.
final Object targetDef = morphAdapter.morph(currentDef, morphDefinition, morphTarget);
if (null == targetDef) {
throw new RuntimeException("No morph resulting Definition. [ morphSource=" + currentDefId + ", " + "morphTarget=" + morphTarget + "]");
}
// Assign the resulting instance to the node,
candidate.getContent().setDefinition(targetDef);
// Update the node's labels.
final DefinitionAdapter<Object> adapter = definitionManager.adapters().registry().getDefinitionAdapter(targetDef.getClass());
candidate.getLabels().clear();
final String[] labels = AbstractElementFactory.computeLabels(adapter, targetDef);
for (String label : labels) {
candidate.getLabels().add(label);
}
return results;
}
Aggregations