Search in sources :

Example 11 with ItemDefinition

use of org.kie.dmn.model.v1_1.ItemDefinition in project drools by kiegroup.

the class ItemDefinitionDependenciesTest method testOrdering4.

@Test
public void testOrdering4() {
    ItemDefinition _TypeDecisionA1 = build("TypeDecisionA1");
    ItemDefinition _TypeDecisionA2_x = build("TypeDecisionA2.x", _TypeDecisionA1);
    ItemDefinition _TypeDecisionA3 = build("TypeDecisionA3", _TypeDecisionA2_x);
    ItemDefinition _TypeDecisionB1 = build("TypeDecisionB1");
    ItemDefinition _TypeDecisionB2_x = build("TypeDecisionB2.x", _TypeDecisionB1);
    ItemDefinition _TypeDecisionB3 = build("TypeDecisionB3", _TypeDecisionB2_x, _TypeDecisionA3);
    ItemDefinition _TypeDecisionC1 = build("TypeDecisionC1", _TypeDecisionA3, _TypeDecisionB3);
    ItemDefinition _TypeDecisionC4 = build("TypeDecisionC4");
    List<ItemDefinition> originalList = Arrays.asList(new ItemDefinition[] { _TypeDecisionA1, _TypeDecisionA2_x, _TypeDecisionA3, _TypeDecisionB1, _TypeDecisionB2_x, _TypeDecisionB3, _TypeDecisionC1, _TypeDecisionC4 });
    List<ItemDefinition> orderedList = orderingStrategy(originalList);
    assertTrue("Index of _TypeDecisionA1 < _TypeDecisionA2_x", orderedList.indexOf(_TypeDecisionA1) < orderedList.indexOf(_TypeDecisionA2_x));
    assertTrue("Index of _TypeDecisionA2_x < _TypeDecisionA3", orderedList.indexOf(_TypeDecisionA2_x) < orderedList.indexOf(_TypeDecisionA3));
    assertTrue("Index of _TypeDecisionA3 < _TypeDecisionB3", orderedList.indexOf(_TypeDecisionA3) < orderedList.indexOf(_TypeDecisionB3));
    assertTrue("Index of _TypeDecisionA3 < _TypeDecisionC1", orderedList.indexOf(_TypeDecisionA3) < orderedList.indexOf(_TypeDecisionC1));
    assertTrue("Index of _TypeDecisionB1 < _TypeDecisionB2_x", orderedList.indexOf(_TypeDecisionB1) < orderedList.indexOf(_TypeDecisionB2_x));
    assertTrue("Index of _TypeDecisionB2_x < _TypeDecisionB3", orderedList.indexOf(_TypeDecisionB2_x) < orderedList.indexOf(_TypeDecisionB3));
    assertTrue("Index of _TypeDecisionB3 < _TypeDecisionC1", orderedList.indexOf(_TypeDecisionB3) < orderedList.indexOf(_TypeDecisionC1));
}
Also used : ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) Test(org.junit.Test)

Example 12 with ItemDefinition

use of org.kie.dmn.model.v1_1.ItemDefinition in project drools by kiegroup.

the class ItemDefinitionDependenciesTest method testGeneric.

@Test
public void testGeneric() {
    ItemDefinition a = build("a");
    ItemDefinition b = build("b");
    ItemDefinition c = build("c", a, b);
    ItemDefinition d = build("d", c);
    List<ItemDefinition> originalList = Arrays.asList(new ItemDefinition[] { d, c, b, a });
    List<ItemDefinition> orderedList = orderingStrategy(originalList);
    assertThat(orderedList.subList(0, 2), containsInAnyOrder(a, b));
    assertThat(orderedList.subList(2, 4), contains(c, d));
}
Also used : ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) Test(org.junit.Test)

Example 13 with ItemDefinition

use of org.kie.dmn.model.v1_1.ItemDefinition in project drools by kiegroup.

the class ItemDefinitionDependenciesSorter method sort.

/**
 * Return a new list of ItemDefinition sorted by dependencies (required dependencies comes first)
 */
public List<ItemDefinition> sort(List<ItemDefinition> ins) {
    // In a graph A -> B -> {C, D}
    // showing that A requires B, and B requires C,D
    // then a depth-first visit would satisfy required ordering, for example a valid depth first visit is also a valid sort here: C, D, B, A.
    Collection<ItemDefinition> visited = new ArrayList<>(ins.size());
    List<ItemDefinition> dfv = new ArrayList<>(ins.size());
    for (ItemDefinition node : ins) {
        if (!visited.contains(node)) {
            dfVisit(node, ins, visited, dfv);
        }
    }
    return dfv;
}
Also used : ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) ArrayList(java.util.ArrayList)

Example 14 with ItemDefinition

use of org.kie.dmn.model.v1_1.ItemDefinition in project drools by kiegroup.

the class ItemDefinitionDependenciesSorter method displayDependencies.

public static void displayDependencies(List<ItemDefinition> ins, String namespaceURI) {
    for (ItemDefinition in : ins) {
        System.out.println(in.getName());
        List<ItemDefinition> others = new ArrayList<>(ins);
        others.remove(in);
        for (ItemDefinition other : others) {
            QName otherQName = new QName(namespaceURI, other.getName());
            if (directFind(in, otherQName)) {
                System.out.println(" direct depends on: " + other.getName());
            } else if (recurseFind(in, otherQName)) {
                System.out.println(" indir. depends on: " + other.getName());
            }
        }
    }
}
Also used : QName(javax.xml.namespace.QName) ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) ArrayList(java.util.ArrayList)

Example 15 with ItemDefinition

use of org.kie.dmn.model.v1_1.ItemDefinition in project drools by kiegroup.

the class ItemDefinitionDependenciesSorter method dfVisit.

/**
 * Performs a depth first visit, but keeping a separate reference of visited/visiting nodes, _also_ to avoid potential issues of circularities.
 */
private void dfVisit(ItemDefinition node, List<ItemDefinition> allNodes, Collection<ItemDefinition> visited, List<ItemDefinition> dfv) {
    visited.add(node);
    List<ItemDefinition> neighbours = allNodes.stream().filter(// filter out `node`
    n -> !n.getName().equals(node.getName())).filter(// I pick from allNodes, those referenced by this `node`. Only neighbours of `node`, because N is referenced by NODE.
    n -> recurseFind(node, new QName(modelNamespace, n.getName()))).collect(Collectors.toList());
    for (ItemDefinition n : neighbours) {
        if (!visited.contains(n)) {
            dfVisit(n, allNodes, visited, dfv);
        }
    }
    dfv.add(node);
}
Also used : List(java.util.List) Collection(java.util.Collection) QName(javax.xml.namespace.QName) ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) QName(javax.xml.namespace.QName) ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition)

Aggregations

ItemDefinition (org.kie.dmn.model.v1_1.ItemDefinition)22 Test (org.junit.Test)10 QName (javax.xml.namespace.QName)7 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 RunWith (org.junit.runner.RunWith)2 Parameterized (org.junit.runners.Parameterized)2 DMNType (org.kie.dmn.api.core.DMNType)2 Definitions (org.kie.dmn.model.v1_1.Definitions)2 XStream (com.thoughtworks.xstream.XStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1