greedydev

Untitled

Apr 18th, 2022 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package regul;
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5.  
  6. public class Algorithm {
  7.     private ArrayList <Rule> base = new ArrayList<>();
  8.  
  9.     public Algorithm (String allRules) {
  10.         // Removing all whitespaces
  11.         String allRulesFmt = allRules.replaceAll("\s", "");
  12.         // Splitting each rule
  13.         String[] splitRules = allRulesFmt.split(";");
  14.         // Regex to validate rules
  15.         Pattern ruleRegex = Pattern.compile("^([A-Za-z0-9#]*)->\\.?([A-Za-z0-9#]*)$");
  16.         for (var rule : splitRules) {
  17.             // Checking if the rule is valid
  18.             Matcher matcher = ruleRegex.matcher(rule);
  19.             if (matcher.matches()) {
  20.                 // Checking if the rule is terminating
  21.                 boolean terminating = rule.contains(".");
  22.                 // Getting left and right part
  23.                 String left = matcher.group(1);
  24.                 String right = matcher.group(2);
  25.                 // Adding the rule to the base
  26.                 base.add(new Rule(Pattern.compile(left), right, terminating));
  27.             // If the rule is invalid
  28.             } else {
  29.                 // Invalidate the entire set
  30.                 base = null;
  31.                 return;
  32.             }
  33.  
  34.         }
  35.  
  36.     }
  37.    
  38.     public String eval(String begin, int cnt) {
  39.         // Creating res variable to populate later
  40.         String res = begin;
  41.  
  42.         for (var i = 0; i < cnt;) {
  43.             // Variable to check if any of the rules was applied in the cycle
  44.             boolean didWork = false;
  45.             // Looping through each rule
  46.             for (var rule : base) {
  47.                 // If rule adds a character
  48.                 if (rule.left.toString().equals("")) {
  49.                     // Add it to the result
  50.                     res += rule.right;
  51.                     // If the rule is terminating
  52.                     if (rule.end) {
  53.                         // Return the result
  54.                         return res;
  55.                     }
  56.                     // Increment the counter and mark the done work
  57.                     i++;
  58.                     didWork = true;
  59.                 // Else if the rule can be applied to the string
  60.                 } else if (rule.left.matcher(res).results().findAny().isPresent()) {
  61.                     // Apply the rule
  62.                     Matcher matcher = rule.left.matcher(res);
  63.                     res = matcher.replaceAll(rule.right);
  64.                     // If the rule is terminating
  65.                     if (rule.end) {
  66.                         // Return the result
  67.                         return res;
  68.                     }
  69.                     // Increment the counter and mark the done work
  70.                     i++;
  71.                     didWork = true;
  72.                 }
  73.             }
  74.             // Checking if any of the rules was applied to the string
  75.             if (!didWork) {
  76.                 return res;
  77.             }
  78.         }
  79.  
  80.         return "undefined";
  81.     }
  82.    
  83.     public boolean isGood() {
  84.         return base != null;
  85.     }
  86.    
  87.     private class Rule {
  88.         Pattern left;
  89.         String right;
  90.         boolean end;
  91.         Rule (Pattern sl, String sr, boolean end) {
  92.             this.left = sl;
  93.             this.right = sr;
  94.             this.end = end;
  95.         }
  96.         @Override
  97.         public String toString() {
  98.             return left + " ->" + (end?".":"") + " " + right;
  99.         }
  100.     }
  101.     // .... methods
  102.     // ......
  103.     // ......
  104.     @Override
  105.     public String toString() {
  106.         StringBuilder res;
  107.         if (base == null) res = new StringBuilder("Error Algorithm");
  108.         else {
  109.             res = new StringBuilder("Algorithm = [");
  110.             for (Rule rule : base) {
  111.                 res.append("  \n ").append(rule.toString());
  112.             }
  113.             res.append("\n]");
  114.         }
  115.         return res.toString();
  116.     }
  117. }
  118.  
Add Comment
Please, Sign In to add comment