CollectorGem listCollector = new CollectorGem();
listCollector.setName("list");
gemGraph.addGem(listCollector);
// Construct the test for checking whether the 'list' value is the empty list.
Gem isEmptyGem = gemFactory.makeFunctionalAgentGem(CAL_Prelude.Functions.isEmpty);
gemGraph.addGem(isEmptyGem);
ReflectorGem listReflector1 = new ReflectorGem(listCollector);
gemGraph.addGem(listReflector1);
// Construct the gem tree for the case when 'list' is not empty.
// The head of the returned Cons is: mapFunction (List.head list)
// which uses the apply gem to apply the mapFunction to its argument.
gemGraph.connectGems(listReflector1.getOutputPart(), isEmptyGem.getInputPart(0));
Gem headGem = gemFactory.makeFunctionalAgentGem(CAL_List.Functions.head);
gemGraph.addGem(headGem);
ReflectorGem listReflector2 = new ReflectorGem(listCollector);
gemGraph.addGem(listReflector2);
gemGraph.connectGems(listReflector2.getOutputPart(), headGem.getInputPart(0));
Gem applyGem = gemFactory.makeFunctionalAgentGem(CAL_Prelude.Functions.apply);
gemGraph.addGem(applyGem);
ReflectorGem mapFunctionReflector1 = new ReflectorGem(mapFunctionCollector);
gemGraph.addGem(mapFunctionReflector1);
gemGraph.connectGems(mapFunctionReflector1.getOutputPart(), applyGem.getInputPart(0));
gemGraph.connectGems(headGem.getOutputPart(), applyGem.getInputPart(1));
// The tail of the returned Cons is: demoMap mapFunction (List.tail list)
Gem tailGem = gemFactory.makeFunctionalAgentGem(CAL_List.Functions.tail);
gemGraph.addGem(tailGem);
ReflectorGem listReflector3 = new ReflectorGem(listCollector);
gemGraph.addGem(listReflector3);
gemGraph.connectGems(listReflector3.getOutputPart(), tailGem.getInputPart(0));
ReflectorGem demoMapReflector = new ReflectorGem(gemGraph.getTargetCollector());
gemGraph.addGem(demoMapReflector);
ReflectorGem mapFunctionReflector2 = new ReflectorGem(mapFunctionCollector);
gemGraph.addGem(mapFunctionReflector2);
Gem consGem = gemFactory.makeFunctionalAgentGem(CAL_Prelude.DataConstructors.Cons);
gemGraph.addGem(consGem);
gemGraph.connectGems(applyGem.getOutputPart(), consGem.getInputPart(0));
gemGraph.connectGems(demoMapReflector.getOutputPart(), consGem.getInputPart(1));
// Construct the conditional branch (using the iff gem). The false case returns the value
// generated by the gem tree defined above. In the true case, Nil (the empty list) is returned.
Gem iffGem = gemFactory.makeFunctionalAgentGem(CAL_Prelude.Functions.iff);
gemGraph.addGem(iffGem);
Gem nilGem = gemFactory.makeFunctionalAgentGem(CAL_Prelude.DataConstructors.Nil);
gemGraph.addGem(nilGem);
gemGraph.connectGems(isEmptyGem.getOutputPart(), iffGem.getInputPart(0));
gemGraph.connectGems(nilGem.getOutputPart(), iffGem.getInputPart(1));
gemGraph.connectGems(consGem.getOutputPart(), iffGem.getInputPart(2));
// Connect the gems to the target collector
gemGraph.connectGems(iffGem.getOutputPart(), gemGraph.getTargetCollector().getInputPart(0));