⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpncontext.java

📁 Rakiura JFern是一个非常轻型的带有模拟器的Petri网络框架
💻 JAVA
字号:
// This is copyrighted source file, part of Rakiura JFern package. // See the file LICENSE for copyright information and the terms and conditions// for copying, distributing and modifications of Rakiura JFern package.// Copyright (C) 1999-2002 by Mariusz Nowostawski and others  [http://www.rakiura.org]package org.rakiura.cpn;/**/import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;/** * Represents a CPN context. * Implements a basic context for inscription evaluations. * Current context can be accessed from  any CPN net element,  * and provide basic mechanism for obtaining token references,  * manipulating tokens in expressions and guards, unification  * mechanism, and variable handling. *  *<br><br> * CpnContext.java<br> * Created: Fri Apr 19 11:23:01 2002<br> * *@author  <a href="mariusz@rakiura.org">Mariusz Nowostawski</a> *@version 2.1.0 $Revision: 1.11 $ *@since 2.0 */public class CpnContext implements Context {  /** Parent context. */  private CpnContext parent = null;  /**/  private Multiset multiset;   /** Set of all declared variables. */  private Set variables = new HashSet();  /**   * Mapping between variable name and its values.   */  private Map varPool = null;    /**   */  private int anonymousVariables = 0;  /**   * Stores the mapping for this context. Used by transtion firing   * machinery.   */  private Map binding;  /**   * Creates an empty top level context.   */  public CpnContext() {    this.varPool = new HashMap();  }  public CpnContext(final CpnContext aParent) {    this.parent = aParent;  }    public Multiset getMultiset() {    return this.multiset;  }  /**   * Sets the input multiset to the given value. This method also resets   * the local variables pool for this context, so it can be reused as    * "fresh".   *@param aMultiset a multiset.   */  public void setMultiset(final Multiset aMultiset) {    this.multiset = aMultiset;    this.anonymousVariables = 0;  }  public void var(final String aVariable) {    if (this.parent != null) {      this.parent.var(aVariable);    }     this.variables.add(aVariable);  }  public void var(final int aNumber) {    this.anonymousVariables += aNumber;  }    /**   * Returns all declared variables in this context.   *@return variables declared in this context   */  public Set variables() {    return this.variables;  }  public Object get(final String aVariable) {    if (this.parent != null) {      return this.parent.get(aVariable);    }    return this.varPool.get(aVariable);  }    /**   * Returns the varPool of this context.   *@return the variables pool map.   */  public Map getVarPool() {    return this.varPool;  }  public List getPossibleBindings() {    final Object[] values = this.getMultiset().toArray();    final String[] variables = (String[]) this.variables.toArray(new String[this.variables.size()]);    if (values.length == 0         || (variables.length == 0 && this.anonymousVariables == 0)        || (values.length < variables.length + this.anonymousVariables)) {      return new ArrayList();    }    if (variables.length == 0) {      final Map m = new HashMap();      final List pr = new ArrayList(this.multiset.getAny(this.anonymousVariables));      for (int i = 0; i < this.anonymousVariables; i++) {        m.put("__anonVar_" + getUniqueString(), pr.get(i));      }      final List result = new ArrayList();      result.add(m);      return result;    }    final List result = new ArrayList();    final List list = new ArrayList();    kpermutations(variables, values, this.anonymousVariables, list);    return list;  }  public void setBinding(Map aBinding) {    this.binding = aBinding;  }  public Map getBinding() {    return this.binding;  }  private static long COUNTER = 0;  /**   * Generates a new unique identifier. */  private final static String getUniqueString() {    return "" + System.currentTimeMillis() + COUNTER++;  }  /**   * The implementation to calculate kpermutations is based on   * final static private methods for better performance.   */  private static final void kpermutations(final String[] variables, final Object[] values,                                            final int anonymousVariables,                                           final List result) {    permutation(combination(values.length, variables.length),                variables,                values,                 anonymousVariables,                result);  }  /**   * Calculates a factorial of given integer.   */  private static final long factorial(long from, long to) {    if (from == to) return to;    return (from * factorial(from-1, to));  }  /**   * Private  function used for generating combination states.   */  private static final void check_state(int[] state, int max) {    int imax = max, isize = state.length-1;    while (isize > 0) {      if(state[isize] == imax) {        state[isize-1]++;        int i = isize;        while (i < state.length) {          state[i] = state[i-1]+1;          i++;        }      }      imax--;      isize--;    }  }  /**   * Function generating all possible combinations with permutations   * from a sequence of numbers 0..(size-1)   * input   *   size - int max value, number of elements   *   howmany elements we are selecting   * output   *   return - an array with all possible combinations without repeating   *                  result_array[result_size][howmany]   */  private static final int[][] combination(int size, int howmany) {    final long long_result_size = factorial(size, size - howmany + 1) / factorial(howmany, 1);    final int result_size = (int) long_result_size;    if (long_result_size != result_size) throw new RuntimeException("Limit of combinations too big to fit into memory");    final int[][] result = new int[result_size][howmany];    int[] current_state = new int[howmany];        for(int i = 0; i < howmany; i++) {      current_state[i] = i;    }        for(int i1 = 0; i1 < result_size; i1++) {      for(int i2 = 0; i2 < howmany; i2++) {        result[i1][i2] = current_state[i2];      }      current_state[howmany-1]++;      check_state(current_state, size);    }        return result;  }  private static final void permutation(final int[][] s,                                        final String[] variables, final Object[] values,  final int anonymousVariables,                                         final List result) {    for (int i = 0; i < s.length; i++) {      rpermute(s[i], 0, variables, values, anonymousVariables, result);    }  }  private static final void rpermute(final int[] c, final int k,                                      final String[] variables, final Object[] values, final int anonymousVariables, final List result) {    if (k == c.length - 1) {      final Map m = new HashMap();      for (int i = 0; i < c.length; i++) {        m.put(variables[i], values[c[i]]);      }      if (anonymousVariables > 0) {        List pr = java.util.Arrays.asList(values);        pr.retainAll(m.values());        for (int i = 0; i < anonymousVariables; i++) {          m.put("__anonVar_" + i, pr.get(i));        }      }      result.add(m);    } else {      int temp;      for (int i = k; i < c.length; i++) {        temp = c [i];        c [i] = c [k];        c [k] = temp;        rpermute((int[]) c.clone(), k + 1, variables, values, anonymousVariables, result);      } // for    } // else  }   /** Test. */  public static void main(String[] args) {    System.out.println("Test for variables: X,Y and values 1,2,3");    String[] variables1 = new String[] { "X", "Y" };    Object[] values1 = new String[] { "1", "2", "3" };    List list1 = new ArrayList();    kpermutations(variables1, values1, 0, list1);    System.out.println(list1 + "\n\n");    list1.clear();    System.out.println("Test for variables: X,Y,Z and values 1,2,3,4,5");    String[] variables = new String[] { "X", "Y", "Z" };    Object[] values = new String[] { "1", "2", "3", "4", "5" };    kpermutations(variables, values, 0, list1);    System.out.println(list1 + "\n\n");    for (int i = 0; i < list1.size(); i++) {      Map m = (Map) list1.get(i);      System.out.println("" + m.get("X") + m.get("Y") + m.get("Z"));    }  }} // CpnContext//////////////////// end of file ////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -