static Web web_ifThen2(Web inputWeb, Web web) { Web patternWeb, Map patternMap = unpair cloneWebWithMap(web); web_dropRelationsStartingWith(patternWeb, "Then:"); Collection adders = mapUsingMap(reversedMap(patternMap), web_singleNodes(patternWeb)); web_dropSingleNodes(patternWeb); // To remove things only linked through "then" relations Web thenWeb, Map thenMap = unpair cloneWebWithReverseMap(web); web_unpackRelationsStartingWith(thenWeb, "Then:"); Map match = web_matchAllPerms_partial(patternWeb, inputWeb); if (match != null) { print("Have match. Adders: " + adders); // Add new nodes first new HashMap adderMap; for (WebNode n : adders) { // adders are in "web" space WebNode inputNode; WebNode thenNode = reverseLookup(thenMap, n); web_setLabels(inputNode = inputWeb.newNode(), n.labels); adderMap.put(thenNode, inputNode); } // Then add relations from thenWeb for (WebRelation rel : web_relationObjects(thenWeb)) { WebNode a = or(mapGet(adderMap, rel.a), mapGet(match, mapGet(patternMap, mapGet(thenMap, rel.a)))); WebNode b = or(mapGet(adderMap, rel.b), mapGet(match, mapGet(patternMap, mapGet(thenMap, rel.b)))); if (a != null && b != null) { WebRelation rel2 = inputWeb.getRelation(a, b); web_addLabelsFromNodeToNode(rel, rel2); } } } ret inputWeb; }