Package statechum.DeterministicDirectedSparseGraph

Examples of statechum.DeterministicDirectedSparseGraph.DeterministicVertex


      Map<CmpVertex,DeterministicVertex> oldToNew = new HashMapWithSearch<CmpVertex,DeterministicVertex>(coregraph.getStateNumber());
      // add states
      for(Entry<CmpVertex,Map<CmpVertex,Set<Label>>> entry:flowgraph.entrySet())
      {
        CmpVertex source = entry.getKey();
        DeterministicVertex vert = new DeterministicVertex(source);
        if (coregraph.getInit() == source)
          vert.addUserDatum(JUConstants.INITIAL, true, UserData.SHARED);
        vert.setAccept(source.isAccept());
        vert.setColour(source.getColour());
        vert.setHighlight(source.isHighlight());
        result.addVertex(vert);
        oldToNew.put(source,vert);
      }
     
      // now add transitions
      for(Entry<CmpVertex,Map<CmpVertex,Set<Label>>> entry:flowgraph.entrySet())
      {
        DeterministicVertex source = oldToNew.get(entry.getKey());
        for(Entry<CmpVertex,Set<Label>> tgtEntry:entry.getValue().entrySet())
        {
          DeterministicVertex target = oldToNew.get(tgtEntry.getKey());
          DeterministicEdge e = new DeterministicEdge(source,target);
          e.addUserDatum(JUConstants.LABEL, tgtEntry.getValue(), UserData.CLONE);
          result.addEdge(e);
        }
      }
View Full Code Here


  }
 
  public static DirectedSparseGraph mergeAndDeterminize(Graph graphToMerge, StatePair pair, Configuration conf)
  {
      DirectedSparseGraph g = (DirectedSparseGraph)graphToMerge.copy();
      DeterministicVertex newBlue = DeterministicDirectedSparseGraph.findVertexNamed(pair.getQ().getID(),g);
      DeterministicVertex newRed = DeterministicDirectedSparseGraph.findVertexNamed(pair.getR().getID(),g);
      Map<CmpVertex,List<CmpVertex>> mergedVertices = new HashMap<CmpVertex,List<CmpVertex>>();
     
      // Special configuration is necessary to ensure that computePairCompatibilityScore_internal
      // builds mergedVertices using g's vertices rather than StringVertices or clones of g's vertices.
      Configuration VertexCloneConf = (Configuration)conf.clone();VertexCloneConf.setLearnerUseStrings(false);VertexCloneConf.setLearnerCloneGraph(false);
      LearnerGraph s=new LearnerGraph(g,VertexCloneConf);
      if (s.pairscores.computePairCompatibilityScore_internal(new StatePair(s.findVertex(pair.getQ().getID()),s.findVertex(pair.getR().getID())),mergedVertices) < 0)
        throw new IllegalArgumentException("elements of the pair are incompatible");

      // make a loop
      Set<String> usedInputs = new HashSet<String>();
      for(DirectedSparseEdge e:(Set<DirectedSparseEdge>)newBlue.getInEdges())
      {
        Vertex source = e.getSource();
        Collection<String> existingLabels = (Collection<String>)e.getUserDatum(JUConstants.LABEL);
        g.removeEdge(e);

        // It is possible that there is already an edge between g.getSource Blue and newRed
        Iterator<DirectedSparseEdge> sourceOutIt = source.getOutEdges().iterator();
        Edge fromSourceToNewRed = null;
        while(sourceOutIt.hasNext() && fromSourceToNewRed == null)
        {
          DirectedSparseEdge out = sourceOutIt.next();if (out.getDest() == newRed) fromSourceToNewRed = out;
        }
        if (fromSourceToNewRed == null)
        {
          fromSourceToNewRed = new DirectedSparseEdge(source,newRed);
          fromSourceToNewRed.setUserDatum(JUConstants.LABEL, existingLabels, UserData.CLONE);// no need to clone this one since I'll delete the edge in a bit
          g.addEdge(fromSourceToNewRed);
        }
        else
          // there is already a transition from source to newRed, hence all we have to do is merge the new labels into it.
          ((Collection<String>)fromSourceToNewRed.getUserDatum(JUConstants.LABEL)).addAll( existingLabels );
         
      }

      // now the elements of mergedVertices are in terms of the copied graph.
      for(Vertex vert:(Set<Vertex>)g.getVertices())
        if (mergedVertices.containsKey(vert))
        {// there are some vertices to merge with this one.
          usedInputs.clear();usedInputs.addAll(s.transitionMatrix.get(vert).keySet());
          for(CmpVertex toMerge:mergedVertices.get(vert))
          {// for every input, I'll have a unique target state - this is a feature of PTA
           // For this reason, every if multiple branches of PTA get merged, there will be no loops or parallel edges.
          // As a consequence, it is safe to assume that each input/target state combination will lead to a new state.
            Set<String> inputsFrom_toMerge = s.transitionMatrix.get(toMerge).keySet();
            for(String input:inputsFrom_toMerge)
              if (!usedInputs.contains(input))
              {
                Set<String> labels = new HashSet<String>();labels.add(input);
                DeterministicVertex targetVert = (DeterministicVertex)s.transitionMatrix.get(toMerge).get(input);
                DirectedSparseEdge newEdge = new DirectedSparseEdge(vert,targetVert);
                newEdge.addUserDatum(JUConstants.LABEL, labels, UserData.CLONE);
                g.removeEdges(targetVert.getInEdges());g.addEdge(newEdge);
              }
            usedInputs.addAll(inputsFrom_toMerge);
          }
        }
     
View Full Code Here

 
  @SuppressWarnings("unchecked")
  public static DirectedSparseGraph mergeAndDeterminize(Graph graphToMerge, StatePair pair, Configuration conf)
  {
      DirectedSparseGraph g = (DirectedSparseGraph)graphToMerge.copy();
      DeterministicVertex newBlue = DeterministicDirectedSparseGraph.findVertexNamed(pair.getQ(),g);
      DeterministicVertex newRed = DeterministicDirectedSparseGraph.findVertexNamed(pair.getR(),g);
      Map<CmpVertex,List<CmpVertex>> mergedVertices = conf.getTransitionMatrixImplType() == STATETREE.STATETREE_ARRAY?
          new ArrayMapWithSearch<CmpVertex,List<CmpVertex>>(g.numVertices()):
          new HashMapWithSearch<CmpVertex,List<CmpVertex>>(g.numVertices());
     
      // Special configuration is necessary to ensure that computePairCompatibilityScore_internal
      // builds mergedVertices using g's vertices rather than StringVertices or clones of g's vertices.
      Configuration VertexCloneConf = conf.copy();VertexCloneConf.setLearnerUseStrings(false);VertexCloneConf.setLearnerCloneGraph(false);
      LearnerGraph s=new LearnerGraph(g,VertexCloneConf);
      if (s.pairscores.computePairCompatibilityScore_internal(new StatePair(s.findVertex(pair.getQ()),s.findVertex(pair.getR())),mergedVertices) < 0)
        throw new IllegalArgumentException("elements of the pair are incompatible");

      // make a loop
      Set<Label> usedInputs = new HashSet<Label>();
      for(DirectedSparseEdge e:(Set<DirectedSparseEdge>)newBlue.getInEdges())
      {
        Vertex source = e.getSource();
        Collection<Label> existingLabels = (Collection<Label>)e.getUserDatum(JUConstants.LABEL);
        g.removeEdge(e);

        // It is possible that there is already an edge between g.getSource Blue and newRed
        Iterator<DirectedSparseEdge> sourceOutIt = source.getOutEdges().iterator();
        Edge fromSourceToNewRed = null;
        while(sourceOutIt.hasNext() && fromSourceToNewRed == null)
        {
          DirectedSparseEdge out = sourceOutIt.next();if (out.getDest() == newRed) fromSourceToNewRed = out;
        }
        if (fromSourceToNewRed == null)
        {
          fromSourceToNewRed = new DirectedSparseEdge(source,newRed);
          fromSourceToNewRed.setUserDatum(JUConstants.LABEL, existingLabels, UserData.CLONE);// no need to clone this one since I'll delete the edge in a bit
          g.addEdge(fromSourceToNewRed);
        }
        else
          // there is already a transition from source to newRed, hence all we have to do is merge the new labels into it.
          ((Collection<Label>)fromSourceToNewRed.getUserDatum(JUConstants.LABEL)).addAll( existingLabels );
         
      }

      // now the elements of mergedVertices are in terms of the copied graph.
      for(Vertex vert:(Set<Vertex>)g.getVertices())
        if (mergedVertices.containsKey(vert))
        {// there are some vertices to merge with this one.
          usedInputs.clear();usedInputs.addAll(s.transitionMatrix.get(vert).keySet());
          for(CmpVertex toMerge:mergedVertices.get(vert))
          {// for every input, I'll have a unique target state - this is a feature of PTA
           // For this reason, every if multiple branches of PTA get merged, there will be no loops or parallel edges.
          // As a consequence, it is safe to assume that each input/target state combination will lead to a new state.
            Set<Label> inputsFrom_toMerge = s.transitionMatrix.get(toMerge).keySet();
            for(Label input:inputsFrom_toMerge)
              if (!usedInputs.contains(input))
              {
                Set<Label> labels = new HashSet<Label>();
                                                                labels.add(input);
                DeterministicVertex targetVert = (DeterministicVertex)s.transitionMatrix.get(toMerge).get(input);
                DirectedSparseEdge newEdge = new DirectedSparseEdge(vert,targetVert);
                newEdge.addUserDatum(JUConstants.LABEL, labels, UserData.CLONE);
                g.removeEdges(targetVert.getInEdges());g.addEdge(newEdge);
              }
            usedInputs.addAll(inputsFrom_toMerge);
          }
        }
     
View Full Code Here

      Map<CmpVertex,DeterministicVertex> oldToNew = new HashMap<CmpVertex,DeterministicVertex>();
      // add states
      for(Entry<CmpVertex,Map<CmpVertex,Set<String>>> entry:flowgraph.entrySet())
      {
        CmpVertex source = entry.getKey();
        DeterministicVertex vert = new DeterministicVertex(source.getID());
        if (coregraph.getInit() == source)
          vert.addUserDatum(JUConstants.INITIAL, true, UserData.SHARED);
        vert.setAccept(source.isAccept());
        vert.setColour(source.getColour());
        vert.setHighlight(source.isHighlight());
        result.addVertex(vert);
        oldToNew.put(source,vert);
      }
     
      // now add transitions
      for(Entry<CmpVertex,Map<CmpVertex,Set<String>>> entry:flowgraph.entrySet())
      {
        DeterministicVertex source = oldToNew.get(entry.getKey());
        for(Entry<CmpVertex,Set<String>> tgtEntry:entry.getValue().entrySet())
        {
          DeterministicVertex target = oldToNew.get(tgtEntry.getKey());
          DeterministicEdge e = new DeterministicEdge(source,target);
          e.addUserDatum(JUConstants.LABEL, tgtEntry.getValue(), UserData.CLONE);
          result.addEdge(e);
        }
      }
View Full Code Here

  }
 
  public static DirectedSparseGraph mergeAndDeterminize(Graph graphToMerge, StatePair pair, Configuration conf)
  {
      DirectedSparseGraph g = (DirectedSparseGraph)graphToMerge.copy();
      DeterministicVertex newBlue = DeterministicDirectedSparseGraph.findVertexNamed(pair.getQ().getID(),g);
      DeterministicVertex newRed = DeterministicDirectedSparseGraph.findVertexNamed(pair.getR().getID(),g);
      Map<CmpVertex,List<CmpVertex>> mergedVertices = new HashMap<CmpVertex,List<CmpVertex>>();
     
      // Special configuration is necessary to ensure that computePairCompatibilityScore_internal
      // builds mergedVertices using g's vertices rather than StringVertices or clones of g's vertices.
      Configuration VertexCloneConf = conf.copy();VertexCloneConf.setLearnerUseStrings(false);VertexCloneConf.setLearnerCloneGraph(false);
      LearnerGraph s=new LearnerGraph(g,VertexCloneConf);
      if (s.pairscores.computePairCompatibilityScore_internal(new StatePair(s.findVertex(pair.getQ().getID()),s.findVertex(pair.getR().getID())),mergedVertices) < 0)
        throw new IllegalArgumentException("elements of the pair are incompatible");

      // make a loop
      Set<String> usedInputs = new HashSet<String>();
      for(DirectedSparseEdge e:(Set<DirectedSparseEdge>)newBlue.getInEdges())
      {
        Vertex source = e.getSource();
        Collection<String> existingLabels = (Collection<String>)e.getUserDatum(JUConstants.LABEL);
        g.removeEdge(e);

        // It is possible that there is already an edge between g.getSource Blue and newRed
        Iterator<DirectedSparseEdge> sourceOutIt = source.getOutEdges().iterator();
        Edge fromSourceToNewRed = null;
        while(sourceOutIt.hasNext() && fromSourceToNewRed == null)
        {
          DirectedSparseEdge out = sourceOutIt.next();if (out.getDest() == newRed) fromSourceToNewRed = out;
        }
        if (fromSourceToNewRed == null)
        {
          fromSourceToNewRed = new DirectedSparseEdge(source,newRed);
          fromSourceToNewRed.setUserDatum(JUConstants.LABEL, existingLabels, UserData.CLONE);// no need to clone this one since I'll delete the edge in a bit
          g.addEdge(fromSourceToNewRed);
        }
        else
          // there is already a transition from source to newRed, hence all we have to do is merge the new labels into it.
          ((Collection<String>)fromSourceToNewRed.getUserDatum(JUConstants.LABEL)).addAll( existingLabels );
         
      }

      // now the elements of mergedVertices are in terms of the copied graph.
      for(Vertex vert:(Set<Vertex>)g.getVertices())
        if (mergedVertices.containsKey(vert))
        {// there are some vertices to merge with this one.
          usedInputs.clear();usedInputs.addAll(s.transitionMatrix.get(vert).keySet());
          for(CmpVertex toMerge:mergedVertices.get(vert))
          {// for every input, I'll have a unique target state - this is a feature of PTA
           // For this reason, every if multiple branches of PTA get merged, there will be no loops or parallel edges.
          // As a consequence, it is safe to assume that each input/target state combination will lead to a new state.
            Set<String> inputsFrom_toMerge = s.transitionMatrix.get(toMerge).keySet();
            for(String input:inputsFrom_toMerge)
              if (!usedInputs.contains(input))
              {
                Set<String> labels = new HashSet<String>();labels.add(input);
                DeterministicVertex targetVert = (DeterministicVertex)s.transitionMatrix.get(toMerge).get(input);
                DirectedSparseEdge newEdge = new DirectedSparseEdge(vert,targetVert);
                newEdge.addUserDatum(JUConstants.LABEL, labels, UserData.CLONE);
                g.removeEdges(targetVert.getInEdges());g.addEdge(newEdge);
              }
            usedInputs.addAll(inputsFrom_toMerge);
          }
        }
     
View Full Code Here

  public static final String PTA1 = "\nA-p->I-q->B"+"\nB-a->B1-a-#B2\nB1-b->B3-b->B4\n";
 
  @Test(expected = IllegalArgumentException.class)
  public final void testLearnerFailsWhenRedNotFound()
  {
    ComputeQuestions.computeQS_orig(new StatePair(null,new DeterministicVertex("non-existing")), new LearnerGraph(testConfig), new LearnerGraph(testConfig));
  }
View Full Code Here

    }
    long lastScore = -1;
    for(StatePair elem:pairs)
    {
      doneEdges = new HashSet<DirectedSparseEdge>();
      DeterministicVertex origBlue = DeterministicDirectedSparseGraph.findVertexNamed(elem.getQ(), g);
      DeterministicVertex origRed = DeterministicDirectedSparseGraph.findVertexNamed(elem.getR(), g);
      long currentScore = computeScore(g, new OrigStatePair(origBlue,origRed));// This one returns vertices from g, but elem may easily contain StringVertices and such, hence convert elem to Vertex-pair.
      PairScore elA = constructPairScore(elem.getQ().getStringId(),elem.getR().getStringId(),currentScore, testConfig);
      PairScore elB = constructPairScore(elem.getR().getStringId(),elem.getQ().getStringId(),currentScore, testConfig);
      Assert.assertTrue(elem.getR().getColour() == JUConstants.RED);
      Assert.assertTrue(elem.getQ().getColour() == JUConstants.BLUE);
View Full Code Here

     * @param graphToUpdate where to add
     * @param vertex what to add.
     */
    protected DeterministicVertex addVertex(DirectedSparseGraph graphToUpdate, CmpVertex vertex)
    {
      DeterministicVertex vert = new DeterministicVertex(vertex.getStringId());
     
      vert.setAccept(vertex.isAccept());
      vert.setColour(vertex.getColour());
      vert.setHighlight(vertex.isHighlight());
      graphToUpdate.addVertex(vert);return vert;
    }
View Full Code Here

      Map<StatePair,DeterministicEdge> pairsToNewEdges = new HashMap<StatePair,DeterministicEdge>();
     
      for(Entry<CmpVertex,Map<Label,List<CmpVertex>>> entry:graph.transitionMatrix.entrySet())
      {
        CmpVertex from = entry.getKey();
        DeterministicVertex fromVertex = DeterministicDirectedSparseGraph.findVertexNamed(from,graphToUpdate);
        if (fromVertex == null) fromVertex = addVertex(graphToUpdate, from);
       
        Map<Label,Map<String,Color>> lbl = transitionAnnotation.get(from.getStringId());
        if (lbl == null)
        {
          lbl = new TreeMap<Label,Map<String,Color>>();transitionAnnotation.put(from.getStringId(), lbl);
        }

        for(Entry<Label,List<CmpVertex>> transition:entry.getValue().entrySet())
          for(CmpVertex target:transition.getValue())
          {
            DeterministicVertex targetVertex = DeterministicDirectedSparseGraph.findVertexNamed(target,graphToUpdate);
            if (targetVertex == null) targetVertex = addVertex(graphToUpdate, target);
            DeterministicEdge e = pairsToNewEdges.get(new StatePair(from,target));
            if (e == null)
            {
                e = new DeterministicEdge(fromVertex,targetVertex);
View Full Code Here

    protected void removeFromGraph(DirectedSparseGraph graphToUpdate, LearnerGraphND graph)
    {
      for(Entry<CmpVertex,Map<Label,List<CmpVertex>>> entry:graph.transitionMatrix.entrySet())
      {
        CmpVertex from = entry.getKey();
        DeterministicVertex fromVertex = DeterministicDirectedSparseGraph.findVertexNamed(from,graphToUpdate);
        if (fromVertex == null)
          throw new IllegalArgumentException("source state in diff is not known, looking at "+from);
       
        for(Entry<Label,List<CmpVertex>> transition:entry.getValue().entrySet())
          for(CmpVertex target:transition.getValue())
          {
            DeterministicVertex targetVertex = DeterministicDirectedSparseGraph.findVertexNamed(target,graphToUpdate);
            if (targetVertex == null)
              throw new IllegalArgumentException("target state in diff is not known, looking at "+target);
            Edge e=DeterministicDirectedSparseGraph.findEdge(fromVertex, targetVertex);
            if (e == null)
              throw new IllegalArgumentException("edge in diff was not found");
View Full Code Here

TOP

Related Classes of statechum.DeterministicDirectedSparseGraph.DeterministicVertex

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.