function checkAddNode($pid, $isConnector, $link1, $link2, $link3)
{
    $count = getCountByPid($pid);
    if ($count >= 6) {
        return "Pattern is full";
    }
    if ($isConnector == 1) {
        if (getConnectorByPid($pid) != null) {
            return "already exist a connector in this pattern";
        }
    } elseif ($isConnector == 0) {
        if (getConnectorByPid($pid) == null) {
            return "This pattern do not exist,please create a connector for it";
        }
        if ($link1 == null && $link2 == null && $link3 == null) {
            return "A normal node must need to connect to the other node(s)";
        }
    }
    if (getLinkCountByNid($link1) >= 3) {
        return "Can not connect to Node" . $link1 . " because it has already connected with three nodes";
    }
    if (getLinkCountByNid($link2) >= 3) {
        return "Can not connect to Node" . $link2 . " because it has already connected with three nodes";
    }
    if (getLinkCountByNid($link3) >= 3) {
        return "Can not connect to Node" . $link3 . " because it has already connected with three nodes";
    }
    return "true";
}
function delete($nid)
{
    $row = getNodeByNid($nid);
    // $row['isActive']="no";
    //delete the whole pattern
    $list = array();
    $list[0] = $row['nid'];
    iniRecordList($list);
    if ($row['isConnector'] == 2) {
        return "domainFail";
    } else {
        if ($row['isConnector'] == 1) {
            if (countPatternNodes($row['pid']) > 1) {
                return "patternFail";
            }
            $neighbourList = findNodeNeighbour($row['nid']);
            $did = getDidByNid($nid);
            $domainId = getDomainByDid($did)['nid'];
            if (count($neighbourList) > 1) {
                for ($i = 0; $i < count($neighbourList); $i++) {
                    if ($neighbourList[$i] != $domainId) {
                        if (count(sendMessageIgnoreInactive($neighbourList[$i], $domainId, $list)) == 0) {
                            return "fail";
                        }
                    }
                }
                deleteNodesByNid($row['nid']);
                deleteLinksByNid($row['nid']);
                return "connectorSuccess";
            } else {
                //get all neighbors of this domain, includes one connector node
                $tempDid = $neighbourList[0];
                //echo "did ".$tempDid."</br>";
                $tempNeighbourList = findNodeNeighbour($tempDid);
                $tempCount = 0;
                for ($i = 0; $i < count($tempNeighbourList); $i++) {
                    $tempNode = $tempNeighbourList[$i];
                    if ($tempDid == getDidByNid($tempNode)) {
                        $tempCount++;
                    }
                }
                //echo "count ".$tempCount;
                if ($tempCount >= 1) {
                    deleteNodesByNid($row['nid']);
                    deleteLinksByNid($row['nid']);
                    return "connectorSuccess";
                } else {
                    $domainNeighborList = findNodeNeighbour($domainId);
                    $domainNeighborsCount = count($domainNeighborList);
                    if ($domainNeighborsCount >= 2) {
                        $list[0] = $domainId;
                        iniRecordList($list);
                        for ($i = 0; $i < $domainNeighborsCount; $i++) {
                            for ($j = 0; $j < $domainNeighborsCount; $j++) {
                                if ($domainNeighborList[$i] != $domainNeighborList[$j]) {
                                    if (count(sendMessageIgnoreInactive($domainNeighborList[$i], $domainNeighborList[$j], $list)) == 0) {
                                        return "fail";
                                    }
                                }
                            }
                        }
                    }
                    deleteNodesByNid($domainId);
                    deleteLinksByNid($domainId);
                    deleteNodesByNid($row['nid']);
                    deleteLinksByNid($row['nid']);
                    return $domainId;
                }
            }
        } else {
            $neighbourList = findNodeNeighbour($row['nid']);
            $pid = getPidByNid($nid);
            //  echo $pid;
            $connectorId = getConnectorByPid($pid)['nid'];
            // echo $connectorId;
            for ($i = 0; $i < count($neighbourList); $i++) {
                // echo $neighbourList[$i];
                if ($neighbourList[$i] == $connectorId) {
                } else {
                    if (count(sendMessageIgnoreInactive($neighbourList[$i], $connectorId, $list)) == 0) {
                        return "fail";
                    }
                }
            }
            deleteLinksByNid($row['nid']);
            deleteNodesByNid($row['nid']);
            return "normalSuccess";
        }
    }
}