尝试在足球数据库中获取最大连续胜利数

时间:2018-11-25 16:02:23

标签: neo4j cypher

所以我试图从我的足球数据库中获得最长的连胜纪录。我知道在堆栈溢出方面有很多类似的问题,但是大多数解决方案最终只是增加每个团队的总胜利,这不是我想要获得的。

我的目标是获得球队有史以来最长的连胜纪录。本质上,如果team1玩了10场比赛,并且在这10场比赛中连续赢了6次,然后输了1次,又连续赢了3场,那么连续的获胜命令应该返回team1的最大连胜数为6。

我得到的代码虽然显示了每个玩家的获胜总数,但我不是想要得到的。

public class MainActivity extends AppCompatActivity {
    Button downloadPDF;
    DownloadingClass downPDF;
    private static final String TAG = "omar.asynctaskdemo;";
    String urlExample = "https://doc.lagout.org/programmation/Actionscript%20-%20Flash%20-%20Flex%20-%20Air/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29/Flash%20Development%20for%20Android%20Cookbook%20-%20Labrecque%20-%20Packt%20%282011%29.pdf";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        downloadPDF = findViewById(R.id.download_pdf);
        downPDF = new DownloadingClass();

        downloadPDF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downPDF.execute();
            }
        });

        permissionCheck();
    }

    private void permissionCheck() {
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
                return;
            }
        }
    }

    private class DownloadingClass extends AsyncTask<String, Integer, Void>{

        @Override
        protected Void doInBackground(String... strings) {
            URL url;
            try {
                url = new URL(urlExample);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                InputStream inputStream = httpURLConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String line = bufferedReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

上面的代码的结果是:

MATCH (T1:Team)-[m:Match]-(:Team)
with T1, m
ORDER BY m.round ASC
WITH T1,
REDUCE(initial = {wins: 0, max_wins: 0}, matches IN COLLECT(m) | 
CASE WHEN (T1 = startNode(matches) AND (matches.ft_home_score > matches.ft_away_score))
OR (T1 = endNode(matches) AND (matches.ft_away_score > matches.ft_home_score))
THEN {wins: initial.wins + 1, max_wins: CASE WHEN initial.wins + 1 > initial.max_wins THEN initial.wins + 1 ELSE initial.max_wins END}
ELSE initial
END).max_wins AS successive_wins
return T1.name AS Team, successive_wins ORDER BY successive_wins DESC

我认为我在REDUCE语句中做错了什么,但还不完全确定:D

1 个答案:

答案 0 :(得分:1)

如果您有APOC程序,则可以使用集合拆分程序来提供帮助。

MATCH (T1:Team)-[m:Match]-()
with T1, m
ORDER BY m.round ASC
WITH T1, collect(NOT (T1 = startNode(m) XOR (m.ft_home_score > m.ft_away_score)) AND NOT m.ft_home_score = m.ft_away_score) as results
CALL apoc.coll.split(results, false) YIELD value
WITH T1, max(size(value)) as successive_wins
RETURN T1.name AS Team, successive_wins 
ORDER BY successive_wins DESC

关于这一点的一些注释。

我正在使用NOT ... XOR来实现XNOR,尽管这确实需要进行最后一项检查来确保不算关系。

通过收集布尔值以获得有序的结果(如果获胜则为true,否则为false)并在false上拆分,我们在每一行上获得一个连续的true或连续获胜的列表,并通过获得最大的列表来获得每个团队的最大连续获胜次数。