All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cdc.applic.expressions.ast.visitors.ConvertToBinary Maven / Gradle / Ivy

The newest version!
package cdc.applic.expressions.ast.visitors;

import cdc.applic.expressions.ast.AbstractNaryNode;
import cdc.applic.expressions.ast.AndNode;
import cdc.applic.expressions.ast.NaryAndNode;
import cdc.applic.expressions.ast.Node;
import cdc.applic.expressions.ast.OrNode;

/**
 * Utility class used to recursively replace nary nodes by appropriate binary nodes.
 * 
    *
  • α1 ∧ α2 ∧ α3 * ∧ ... ≡ (...((α1 ∧ α2) ∧ * α3) ∧ ...)
  • *
  • α1 ∨ α2 ∨ α3 * ∨ ... ≡ (...((α1 ∨ α2) ∨ * α3) ∨ ...)
  • *
* * @author Damien Carbonne */ public final class ConvertToBinary extends AbstractConverter { private static final ConvertToBinary CONVERTER = new ConvertToBinary(); private ConvertToBinary() { } public static Node execute(Node node) { return node.accept(CONVERTER); } @Override public Node visitNary(AbstractNaryNode node) { final Node[] children = node.getChildren().clone(); for (int index = 0; index < children.length; index++) { children[index] = children[index].accept(this); } Node result = children[0]; for (int index = 1; index < children.length; index++) { if (node instanceof NaryAndNode) { result = new AndNode(result, children[index]); } else { // It must be an nary or node result = new OrNode(result, children[index]); } } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy