示例#1
0
        private List <double[]> extractEdgeInfos(String transitionLabel, String[][] edgeInfoTemplates)
        {
            List <double[]> edgeInfos = new List <double[]>(1);

            transitionLabel = transitionLabel.Trim(new char[] { '[', ']' }); // for hidden events
            String[] labels = transitionLabel.Split('.');
            Int32    index  = mapTranslabelIndex.get(labels[0]);

            if (0 == index)
            {
                index = transitionLabelIndex++;
                mapTranslabelIndex.put(labels[0], index);
            }
            edgeInfos.Add(new double[] { index });

            double[] values = new double[labels.Length - 1];
            for (int i = 1; i < labels.Length; i++)
            {
                values[i - 1] = double.Parse(labels[i]);
            }
            edgeInfos.Add(values);

            return(edgeInfos);
        }
示例#2
0
        public void parse(List <object[]> nodesData, List <string[]> edgesData, List <List <double[]> > nodesInfo, List <List <double[]> > edgesInfo, DictionaryJ <string, GraphDataConfiguration> graphDataConfigs)
        {
            try
            {
                DictionaryJ <String, object[]> nodesMap = new DictionaryJ <String, object[]>();
                foreach (Edge edge in Graph.Edges)
                {
                    String sourceNodeId = edge.SourceNode.LabelText;
                    String targetNodeId = edge.TargetNode.LabelText;

                    object[] sourceNodeData; // length=4 [1:type, 2:id, 3;label, 4:additionalinfo]
                    if (!string.IsNullOrEmpty(edge.LabelText) && !nodesMap.ContainsKey(sourceNodeId))
                    {
                        EventStepSim userData = edge.SourceNode.UserData as EventStepSim;
                        sourceNodeData    = new object[4];
                        sourceNodeData[0] = "REGULAR";
                        sourceNodeData[1] = sourceNodeId;
                        sourceNodeData[2] = String.Format("[{0}] {1}", sourceNodeId, userData.ToString());
                        PAT.GenericDiff.graph.Graph configGraph = configGraphBuilder.execute(this.side, userData.Config, sourceNodeId);
                        sourceNodeData[3] = configGraph;
                        nodesData.Add(sourceNodeData);

                        nodesInfo.Add(extractNodeInfos(userData.Config.GlobalEnv, configGraph, graphDataConfigs["REGULAR"].elementInfoTemplates));

                        nodesMap.put(sourceNodeId, sourceNodeData);
                    }

                    object[] targetNodeData; // length=4 [1:type, 2:id, 3;label, 4:additionalinfo]
                    if (!nodesMap.ContainsKey(targetNodeId))
                    {
                        EventStepSim userData = edge.TargetNode.UserData as EventStepSim;
                        targetNodeData    = new object[4];
                        targetNodeData[0] = !string.IsNullOrEmpty(edge.LabelText) ? "REGULAR" : "INIT";
                        targetNodeData[1] = targetNodeId;
                        targetNodeData[2] = String.Format("[{0}] {1}", targetNodeId, userData.ToString());
                        PAT.GenericDiff.graph.Graph configGraph = configGraphBuilder.execute(this.side, userData.Config, targetNodeId);
                        targetNodeData[3] = configGraph;
                        nodesData.Add(targetNodeData);

                        nodesInfo.Add(extractNodeInfos(userData.Config.GlobalEnv, configGraph, graphDataConfigs[!string.IsNullOrEmpty(edge.LabelText) ? "REGULAR" : "INIT"].elementInfoTemplates));

                        nodesMap.put(targetNodeId, targetNodeData);
                    }

                    if (!string.IsNullOrEmpty(edge.LabelText))
                    {
                        String[] edgeData = new String[5];
                        edgeData[0] = "TRANSITION";
                        edgeData[1] = edge.LabelText;
                        edgeData[2] = sourceNodeId;
                        edgeData[3] = targetNodeId;
                        edgesData.Add(edgeData);
                        edgesInfo.Add(extractEdgeInfos(edge.LabelText, graphDataConfigs["TRANSITION"].elementInfoTemplates));
                    }
                }
            }
            catch (Exception e)
            {
                //e.printStackTrace();
            }

            if (!compareConfigGraph)
            {
                if (compareParameterizedSystem)
                {
                    adjustNodeInfosForParameterizedSystems(nodesInfo, graphDataConfigs);
                }
                else
                {
                    adjustNodeInfosRegular(nodesInfo, graphDataConfigs);
                }
            }
        }
示例#3
0
        private List <double[]> extractNodeInfos(Valuation globalVars, PAT.GenericDiff.graph.Graph configGraph, String[][] nodeInfoTemplates)
        {
            List <double[]> nodeInfos = new List <double[]>(nodeInfoTemplates.Length);

            for (int i = 0; i < nodeInfoTemplates.Length; i++)
            {
                nodeInfos.Add(null);
            }

            if (globalVars != null)
            {
                if (globalVars.Variables != null)
                {
                    foreach (StringDictionaryEntryWithKey <ExpressionValue> pair in globalVars.Variables._entries)
                    {
                        if (pair != null)
                        {
                            string varName = pair.Key;

                            int i = 0;
                            int j = 0;
                            for (; i < nodeInfoTemplates.Length; i++)
                            {
                                String[] keys = nodeInfoTemplates[i];
                                for (j = 0; j < keys.Length; j++)
                                {
                                    if (keys[j].Equals(varName))
                                    {
                                        break;
                                    }
                                }
                                if (j < keys.Length)
                                {
                                    break;
                                }
                            }

                            if (i < nodeInfoTemplates.Length)
                            {
                                double          value    = Double.NaN;
                                ExpressionValue varValue = pair.Value;
                                if (varValue is RecordValue)
                                {
                                    // todo: xingzc. need to get array length and values in the array
                                    RecordValue       array  = varValue as RecordValue;
                                    ExpressionValue[] values = array.Associations;
                                    nodeInfos[i] = new double[values.Length];
                                    for (int valueIndex = 0; valueIndex < values.Length; valueIndex++)
                                    {
                                        if (values[valueIndex] is IntConstant)
                                        {
                                            nodeInfos[i][valueIndex] = (values[valueIndex] as IntConstant).Value;
                                        }
                                        else if (values[valueIndex] is BoolConstant)
                                        {
                                            nodeInfos[i][valueIndex] = (values[valueIndex] as BoolConstant).Value ? 1.0 : 0.0;
                                        }
                                        else
                                        {
                                            // todo: xingzc. process other primitive types
                                        }
                                    }
                                }
                                else if (false)
                                {
                                    // todo: xingzc. process other complex data type
                                }
                                else
                                {
                                    if (varValue is IntConstant)
                                    {
                                        value = (varValue as IntConstant).Value;
                                    }
                                    else if (varValue is BoolConstant)
                                    {
                                        value = (varValue as BoolConstant).Value ? 1.0 : 0.0;
                                    }
                                    else
                                    {
                                        // todo: xingzc. process other primitive types
                                    }

                                    if (nodeInfos[i] == null)
                                    {
                                        nodeInfos[i] = new double[nodeInfoTemplates[i].Length];
                                    }

                                    if (!Double.IsNaN(value))
                                    {
                                        nodeInfos[i][j] = value;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (!compareConfigGraph)
            {
                DictionaryJ <int, int[]> mapLabelindexCount = new DictionaryJ <int, int[]>();
                List <String>            nodeLabels         = configGraph.getNodelabels();
                foreach (String nodeLabel in nodeLabels)
                {
                    int index = CSPConfigurationParser.mapLabelIndex.get(nodeLabel);
                    if (0 != index) // index should not be 0 according to the way to build configGraph
                    {
                        int[] count = mapLabelindexCount.get(index);
                        if (null == count)
                        {
                            count = new int[1];
                            mapLabelindexCount.put(index, count);
                        }
                        int[] labelIndexCount = CSPConfigurationParser.mapLabelIndexCount.get(index); // shold never be null
                        if (labelIndexCount != null)
                        {
                            count[0] = count[0] + labelIndexCount[0];
                        }
                        else
                        {
                            count[0]++;
                        }
                    }
                }

                int      infoIndex       = 0;
                double[] labelindexCount = new double[mapLabelindexCount.Count * 2];
                foreach (int labelindex in mapLabelindexCount.Keys)
                {
                    int[] count = mapLabelindexCount.get(labelindex);
                    if (null != count) // count should not be null
                    {
                        labelindexCount[infoIndex]     = labelindex;
                        labelindexCount[infoIndex + 1] = count[0];
                    }
                    infoIndex = infoIndex + 2;
                }

                nodeInfos[nodeInfoTemplates.Length - 1] = labelindexCount;
            }

            return(nodeInfos);
        }
示例#4
0
        protected Graph createGraph(Side side, Parser parser, DictionaryJ <String, GraphDataConfiguration> graphDataConfigs, String graphName)
        {
            Graph graph = new Graph(graphName);

            List <String[]>         nodesData = new List <String[]>();
            List <String[]>         edgesData = new List <String[]>();
            List <List <double[]> > nodesInfo = new List <List <double[]> >();
            List <List <double[]> > edgesInfo = new List <List <double[]> >();

            parser.parse(nodesData, edgesData, nodesInfo, edgesInfo, graphDataConfigs);

            if (nodesData.Count != nodesInfo.Count)
            {
                throw new IncomparableException(nodesData.Count, nodesInfo.Count);
            }

            if (edgesData.Count != edgesInfo.Count)
            {
                throw new IncomparableException(edgesData.Count, edgesInfo.Count);
            }

            DictionaryJ <String, GraphNode> nodes = new DictionaryJ <String, GraphNode>();

            for (int nodeIndex = 0; nodeIndex < nodesData.Count; nodeIndex++)
            {
                String[]  nodeData = nodesData[nodeIndex];
                String    label    = nodeData[2] != null ? nodeData[2] : nodeData[1];
                GraphNode node;
                if (graphCreationParameters.createQualifiedGraphNode)
                {
                    node = new QualifiedGraphNode(label, nodeData[0], side, diffParameters.noPairupIdenticalNodesWithOthers);
                }
                else
                {
                    node = new ConcreteGraphNode(label, nodeData[0], side, diffParameters.noPairupIdenticalNodesWithOthers);
                }
                ((AbstractGraphElement)node).setAdditionalInfo(nodeData[3]);

                List <double[]>        nodeInfos  = nodesInfo[nodeIndex];
                GraphDataConfiguration nodeConfig = graphDataConfigs.get(nodeData[0]);
                if (nodeInfos.Count != nodeConfig.elementInfoTemplates.Length)
                {
                    throw new IncomparableException(nodeInfos.Count, nodeConfig.elementInfoTemplates.Length);
                }

                if (nodeInfos.Count > 0)
                {
                    InfoDescriptor[] descriptors = new InfoDescriptor[nodeInfos.Count];
                    for (int infoIndex = 0; infoIndex < nodeInfos.Count; infoIndex++)
                    {
                        double[] characteristicVector         = nodeInfos[infoIndex];
                        DistanceCalculatorKind calculatorkind = (DistanceCalculatorKind)Enum.Parse(typeof(DistanceCalculatorKind), nodeConfig.elementInfoCalculatorKind[infoIndex][0]);
                        if (nodeConfig.elementInfoCalculatorKind[infoIndex][1].Equals("vector"))  // VectorBasedLeafInfoDescriptor
                        {
                            double[] noneVector = nodeConfig.elementInfoNone[infoIndex];
                            if (characteristicVector.Length != noneVector.Length)
                            {
                                throw new IncomparableException(characteristicVector.Length, noneVector.Length);
                            }
                            descriptors[infoIndex] = VectorBasedLeafInfoDescriptor.create(characteristicVector, noneVector, calculatorkind);
                        }
                        else if (nodeConfig.elementInfoCalculatorKind[infoIndex][1].Equals("set"))    // SetBasedLeafInfoDescriptor
                        {
                            descriptors[infoIndex] = new SetBasedLeafInfoDescriptor(characteristicVector, calculatorkind);
                        }
                        else
                        {
                            descriptors[infoIndex] = null; // should not happen. this column is ignore.
                        }
                    }
                    ((ConcreteGraphNode)node).attachInfoDescriptor(new CompositeInfoDescriptor(descriptors));
                }

                if (graphCreationParameters.encapsulateInMatchedGraphNode)
                {
                    node = new MatchedGraphNode((ConcreteGraphNode)node, diffParameters.noPairupIdenticalNodesWithOthers);
                }

                nodes.put(nodeData[1], node);
            }

            List <GraphEdge> edges = new List <GraphEdge>(edgesData.Count);

            for (int edgeIndex = 0; edgeIndex < edgesData.Count; edgeIndex++)
            {
                String[]  edgeData = edgesData[edgeIndex];
                GraphNode source   = nodes.get(edgeData[2]);
                GraphNode target   = nodes.get(edgeData[3]);
                if (source == null || target == null)
                {
                    continue;
                }

                ConcreteGraphEdge edge = new ConcreteGraphEdge(edgeData[1], edgeData[0], source, target);
                edge.setAdditionalInfo(edgeData[4]);

                List <double[]>        edgeInfos  = edgesInfo[edgeIndex];
                GraphDataConfiguration edgeConfig = graphDataConfigs.get(edgeData[0]);
                if (edgeInfos.Count != edgeConfig.elementInfoTemplates.Length)
                {
                    throw new IncomparableException(edgeInfos.Count, edgeConfig.elementInfoTemplates.Length);
                }
                if (edgeInfos.Count > 0)
                {
                    InfoDescriptor[] descriptors = new InfoDescriptor[edgeInfos.Count];
                    for (int infoIndex = 0; infoIndex < edgeInfos.Count; infoIndex++)
                    {
                        double[] characteristicVector         = edgeInfos[infoIndex];
                        DistanceCalculatorKind calculatorkind = (DistanceCalculatorKind)Enum.Parse(typeof(DistanceCalculatorKind), edgeConfig.elementInfoCalculatorKind[infoIndex][0]);
                        if (edgeConfig.elementInfoCalculatorKind[infoIndex][1].Equals("vector"))  // VectorBasedLeafInfoDescriptor
                        {
                            double[] noneVector = edgeConfig.elementInfoNone[infoIndex];
                            if (characteristicVector.Length != noneVector.Length)
                            {
                                throw new IncomparableException(characteristicVector.Length, noneVector.Length);
                            }
                            descriptors[infoIndex] = VectorBasedLeafInfoDescriptor.create(characteristicVector, noneVector, calculatorkind);
                        }
                        else if (edgeConfig.elementInfoCalculatorKind[infoIndex][1].Equals("set"))
                        { // SetBasedLeafInfoDescriptor
                            descriptors[infoIndex] = new SetBasedLeafInfoDescriptor(characteristicVector, calculatorkind);
                        }
                        else     // should not happen. this InfoDescriptor column is ignored
                        {
                            descriptors[infoIndex] = null;
                        }
                    }
                    edge.attachInfoDescriptor(new CompositeInfoDescriptor(descriptors));
                }

                edges.Add(edge);

                // TODO Should find a better way
                if (graphCreationParameters.createQualifiedGraphNode && graphCreationParameters.containmentRelation.Equals(edgeData[0]))
                {
                    QualifiedGraphNode node;
                    if (graphCreationParameters.encapsulateInMatchedGraphNode)
                    {
                        node = (QualifiedGraphNode)((MatchedGraphNode)target).getNode();
                    }
                    else
                    {
                        node = (QualifiedGraphNode)target;
                    }
                    node.setParent(source);
                }
            }


            //for(Iterator<GraphNode> iter = nodes.values().iterator(); iter.hasNext();) {
            foreach (KeyValuePair <string, GraphNode> pair in nodes)
            {
                graph.addNode(pair.Value);
            }

            foreach (GraphEdge list in edges)
            {
                graph.addEdge(list);
            }


            //for(Iterator<Set<GraphNode>> iter = graph.getNodes().iterator(); iter.hasNext();) {
            foreach (HashSet <GraphNode> nodesSet in graph.getNodes())
            {
                foreach (GraphNode node in nodesSet)
                {
                    if (!node.hasBackwardEdges() && !node.hasForwardEdges())
                    {
                        //todo:liuyang
                        //nodesIter.remove();
                    }
                }


                if (nodesSet.Count == 0)
                {
                    //todo:liuyang
                    //iter.remove();
                }
            }

            return(graph);
        }