Urban tunnels is crucial to the function of modern society. The proper defect diagnosis and maintenance of tunnels are critical to the performance of tunnel infrastructure. Recent years, the data-driven causation analysis method is becoming popular, especially the ontology and rule reasoning method. However most exsisting research creat the ontology and reasoning rules mannully and lack of automatic implementation method. The paper use owlready2 package of python to realize the automatic generation of ontology and the automatic retrieval of ontology rules. Based on the methodology, the paper takes the defect causation analysis as the example, automatically generate the defect causation ontology, and automatically called reasoning rules to analyze the causes of tunnel defects. The proposed methodology provides practical value to defect causation analysis of urban infrastructure.