// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/utils/Utils.java
// public static String normalizeSignature(final String signature) {
//   if (signature == null) {
//     return null;
//   }
//   String sig = signature.trim();
// 
//   if (sig.length() > 0 && sig.charAt(0) == '\'') {
//     sig = sig.substring(1);
//     final int lastIndex = sig.lastIndexOf('/');
//     if (lastIndex < 0) {
//       sig = null;
//     } else {
//       final String arity = sig.substring(lastIndex + 1).trim();
//       String name = sig.substring(0, lastIndex - 1).trim();
//       if (name.length() > 0 && name.charAt(name.length() - 1) == '\'') {
//         name = name.substring(0, name.length() - 1);
//         sig = name + '/' + arity;
//       } else {
//         sig = null;
//       }
//     }
//   }
//   return sig;
// }
// 
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/utils/Utils.java
// public static String validateSignature(final String signature) {
//   final String[] parsed =
//       Objects.requireNonNull(signature, "Null signature not allowed").split("/");
//   if (parsed.length == 2) {
//     String str = parsed[0].trim();
//     boolean quoted = false;
//     if (str.length() != 0) {
//       if (str.charAt(0) == '\'') {
//         if (str.length() > 1 && str.charAt(str.length() - 1) == '\'') {
//           str = str.substring(1, str.length() - 1);
//           if (str.length() == 0) {
//             return null;
//           }
//           quoted = true;
//         } else {
//           // wrong name, it must not contain '\'' as the only symbol
//           return null;
//         }
//       }
// 
//       final char firstChar = str.charAt(0);
//       if (!quoted && (Character.isDigit(firstChar) || Character.isUpperCase(firstChar) ||
//           Character.isWhitespace(firstChar) || firstChar == '.')) {
//         return null;
//       }
// 
//       // ok. the first part is ok, check the second part
//       final int arity;
//       try {
//         arity = Integer.parseInt(parsed[1].trim());
//         if (arity < 0) {
//           throw new NumberFormatException("Negate number is not supported as arity");
//         }
//       } catch (NumberFormatException ex) {
//         return null;
//       }
// 
//       final StringBuilder builder = new StringBuilder(signature.length());
// 
//       if (quoted) {
//         builder.append('\'').append(str).append('\'').append('/').append(arity);
//       } else {
//         builder.append(str).append('/').append(arity);
//       }
// 
//       return builder.toString();
//     }
//   }
//   return null;
// }


import static com.igormaznitsa.jprol.utils.Utils.normalizeSignature;
import static com.igormaznitsa.jprol.utils.Utils.validateSignature;
import static java.util.Objects.requireNonNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/*
 * Copyright 2014 Igor Maznitsa (http://www.igormaznitsa.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.igormaznitsa.jprol.logic.triggers;




public abstract class AbstractJProlTrigger implements JProlTrigger {

  private final Map<String, JProlTriggerType> signatureMap;

  public AbstractJProlTrigger() {
    this.signatureMap = Collections.synchronizedMap(new HashMap<>());
  }

  public AbstractJProlTrigger addSignature(final String signature,
                                           final JProlTriggerType observedEvent) {
    String processedsignature = validateSignature(requireNonNull(signature));

    if (processedsignature == null) {
      throw new IllegalArgumentException("Wrong signature format [" + signature + ']');
    } else {
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/utils/Utils.java
// public static String normalizeSignature(final String signature) {
//   if (signature == null) {
//     return null;
//   }
//   String sig = signature.trim();
// 
//   if (sig.length() > 0 && sig.charAt(0) == '\'') {
//     sig = sig.substring(1);
//     final int lastIndex = sig.lastIndexOf('/');
//     if (lastIndex < 0) {
//       sig = null;
//     } else {
//       final String arity = sig.substring(lastIndex + 1).trim();
//       String name = sig.substring(0, lastIndex - 1).trim();
//       if (name.length() > 0 && name.charAt(name.length() - 1) == '\'') {
//         name = name.substring(0, name.length() - 1);
//         sig = name + '/' + arity;
//       } else {
//         sig = null;
//       }
//     }
//   }
//   return sig;
// }
// 
// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/utils/Utils.java
// public static String validateSignature(final String signature) {
//   final String[] parsed =
//       Objects.requireNonNull(signature, "Null signature not allowed").split("/");
//   if (parsed.length == 2) {
//     String str = parsed[0].trim();
//     boolean quoted = false;
//     if (str.length() != 0) {
//       if (str.charAt(0) == '\'') {
//         if (str.length() > 1 && str.charAt(str.length() - 1) == '\'') {
//           str = str.substring(1, str.length() - 1);
//           if (str.length() == 0) {
//             return null;
//           }
//           quoted = true;
//         } else {
//           // wrong name, it must not contain '\'' as the only symbol
//           return null;
//         }
//       }
// 
//       final char firstChar = str.charAt(0);
//       if (!quoted && (Character.isDigit(firstChar) || Character.isUpperCase(firstChar) ||
//           Character.isWhitespace(firstChar) || firstChar == '.')) {
//         return null;
//       }
// 
//       // ok. the first part is ok, check the second part
//       final int arity;
//       try {
//         arity = Integer.parseInt(parsed[1].trim());
//         if (arity < 0) {
//           throw new NumberFormatException("Negate number is not supported as arity");
//         }
//       } catch (NumberFormatException ex) {
//         return null;
//       }
// 
//       final StringBuilder builder = new StringBuilder(signature.length());
// 
//       if (quoted) {
//         builder.append('\'').append(str).append('\'').append('/').append(arity);
//       } else {
//         builder.append(str).append('/').append(arity);
//       }
// 
//       return builder.toString();
//     }
//   }
//   return null;
// }


// Path: engine/jprol-core/src/main/java/com/igormaznitsa/jprol/logic/triggers/AbstractJProlTrigger.java
import static com.igormaznitsa.jprol.utils.Utils.normalizeSignature;
import static com.igormaznitsa.jprol.utils.Utils.validateSignature;
import static java.util.Objects.requireNonNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/*
 * Copyright 2014 Igor Maznitsa (http://www.igormaznitsa.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.igormaznitsa.jprol.logic.triggers;




public abstract class AbstractJProlTrigger implements JProlTrigger {

  private final Map<String, JProlTriggerType> signatureMap;

  public AbstractJProlTrigger() {
    this.signatureMap = Collections.synchronizedMap(new HashMap<>());
  }

  public AbstractJProlTrigger addSignature(final String signature,
                                           final JProlTriggerType observedEvent) {
    String processedsignature = validateSignature(requireNonNull(signature));

    if (processedsignature == null) {
      throw new IllegalArgumentException("Wrong signature format [" + signature + ']');
    } else {
      processedsignature = normalizeSignature(processedsignature);