├── .gitignore ├── License.txt ├── README.md ├── configs.json ├── examples ├── citybench │ ├── AarhusTrafficData182955.stream │ ├── SensorRepository.n3 │ ├── SensorRepository.ttl │ └── ssnExtended.ttl ├── influenza │ ├── catalog-v001.xml │ ├── influenza.owl │ ├── observation_example.xml │ ├── static.owl │ └── stream.xml └── sensors │ ├── input.stream │ └── query.json ├── figs └── StreamingMassifPrototypesDemo.png ├── logback.xml ├── pom.xml ├── src ├── main │ ├── java │ │ └── idlab │ │ │ ├── examples │ │ │ └── generators │ │ │ │ ├── HTTPGetGenerator.java │ │ │ │ ├── HTTPPostGenerator.java │ │ │ │ ├── HTTPPostSinkTest.java │ │ │ │ ├── SimpleEventSocket.java │ │ │ │ ├── Test.java │ │ │ │ ├── WebSocketClientGenerator.java │ │ │ │ └── WebSocketServerGenerator.java │ │ │ └── massif │ │ │ ├── abstraction │ │ │ └── hermit │ │ │ │ ├── HermitAbstractionComp.java │ │ │ │ ├── HermitAbstractionImpl.java │ │ │ │ └── utils │ │ │ │ └── DLQueryParser.java │ │ │ ├── cep │ │ │ └── esper │ │ │ │ ├── EsperCEPComp.java │ │ │ │ └── EsperCEPImpl.java │ │ │ ├── core │ │ │ ├── HTTPMonitoringSingleTon.java │ │ │ ├── PipeLine.java │ │ │ ├── PipeLineComponent.java │ │ │ └── PipeLineGraph.java │ │ │ ├── exceptions │ │ │ └── QueryRegistrationException.java │ │ │ ├── filter │ │ │ └── jena │ │ │ │ └── JenaFilter.java │ │ │ ├── functions │ │ │ ├── FunctionManyParTest.java │ │ │ └── FunctionTest.java │ │ │ ├── interfaces │ │ │ └── core │ │ │ │ ├── AbstractionInf.java │ │ │ │ ├── AbstractionListenerInf.java │ │ │ │ ├── CEPInf.java │ │ │ │ ├── CEPListener.java │ │ │ │ ├── FilterInf.java │ │ │ │ ├── ListenerInf.java │ │ │ │ ├── MapperInf.java │ │ │ │ ├── MonitorInf.java │ │ │ │ ├── PipeLineElement.java │ │ │ │ ├── SelectionInf.java │ │ │ │ ├── SelectionListenerInf.java │ │ │ │ ├── SinkInf.java │ │ │ │ ├── SourceInf.java │ │ │ │ └── WindowInf.java │ │ │ ├── listeners │ │ │ └── AbstractionListener.java │ │ │ ├── mapping │ │ │ ├── JSONMapper.java │ │ │ └── SimpleMapper.java │ │ │ ├── run │ │ │ ├── QueryParser.java │ │ │ └── Run.java │ │ │ ├── selection │ │ │ └── csparql_basic │ │ │ │ ├── CSparqlSelectionImpl.java │ │ │ │ ├── DBPediaTest.java │ │ │ │ ├── JenaReasoner.java │ │ │ │ ├── JenaReasonerOWL2RL.java │ │ │ │ ├── StreamingJena.java │ │ │ │ └── utils │ │ │ │ ├── EventReader.java │ │ │ │ ├── GraphEvent.java │ │ │ │ └── OWLUtils.java │ │ │ ├── sinks │ │ │ ├── HTTPGetCombinedSink.java │ │ │ ├── HTTPGetSink.java │ │ │ ├── HTTPPostSink.java │ │ │ ├── PrintSink.java │ │ │ ├── SparqlEPInsertSink.java │ │ │ ├── WebSocketClientSink.java │ │ │ ├── WebSocketServerSink.java │ │ │ └── utils │ │ │ │ └── WebSocketOutputStream.java │ │ │ ├── sources │ │ │ ├── FileSource.java │ │ │ ├── HTTPGetSource.java │ │ │ ├── HTTPPostCombinedSource.java │ │ │ ├── HTTPPostSource.java │ │ │ ├── KafkaConsumerSource.java │ │ │ ├── KafkaSource.java │ │ │ ├── WebSocketClientSource.java │ │ │ ├── WebSocketServerSource.java │ │ │ ├── kafkautils │ │ │ │ ├── JsonPOJODeserializer.java │ │ │ │ └── JsonPOJOSerializer.java │ │ │ └── util │ │ │ │ └── WebSocketInputStream.java │ │ │ ├── timeout │ │ │ ├── KeyedTimeout.java │ │ │ └── Timeout.java │ │ │ ├── utils │ │ │ ├── FormatUtils.java │ │ │ ├── RDFUtils.java │ │ │ └── TableUtils.java │ │ │ └── window │ │ │ └── esper │ │ │ └── EsperWindow.java │ └── resources │ │ └── web │ │ ├── icon.png │ │ ├── index.html │ │ ├── index_vue.html │ │ ├── mockup.html │ │ └── style.css └── test │ └── java │ └── idlab │ └── massif │ ├── AppTest.java │ ├── MASSIFTest.java │ ├── PipelineTest.java │ ├── QueryTest.java │ ├── SinkTest.java │ ├── SourceTest.java │ ├── abstraction │ └── hermit │ │ └── HermitAbstractionTest.java │ ├── cep │ └── esper │ │ └── EsperCEPTest.java │ ├── pipeline │ ├── KafkaTest.java │ ├── PipeLineTest.java │ └── StreamingMASSIFTest.java │ ├── selection │ └── csparql_basic │ │ └── CSparqlBasicTest.java │ ├── timeout │ └── KeyedTimeoutTest.java │ └── utils │ └── TableUtilsTest.java ├── web_old ├── icon.png ├── index.html └── style.css └── webfiles ├── .gitignore └── log4j2.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .classpath 3 | .project 4 | target/* 5 | 6 | # Compiled class file 7 | *.class 8 | 9 | # Log file 10 | *.log 11 | 12 | # BlueJ files 13 | *.ctxt 14 | 15 | # Mobile Tools for Java (J2ME) 16 | .mtj.tmp/ 17 | 18 | # Package Files # 19 | *.jar 20 | *.war 21 | *.nar 22 | *.ear 23 | *.zip 24 | *.tar.gz 25 | *.rar 26 | 27 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 28 | hs_err_pid* 29 | /target/ 30 | .DS_Store 31 | .settings/ 32 | .idea/ 33 | /web/ 34 | -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Ghent University and IMEC vzw with offices at Technologiepark 15, 9052 Ghent, Belgium - Email: info@imec.be. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software for non-commercial educational and research use, including without limitation the rights to use, copy, modify, merge, publish, distribute and/or sublicense copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | 1. The above copyright notice and this permission notice shall be included in all copies of the Software. 6 | 7 | 2. Permission is restricted to non-commercial educational and research use: the use of the Software is allowed for teaching purposes and academic research. Usage by non-academic parties is allowed in a strict research environment only. The use of the results of the research for commercial purposes or inclusion in commercial activities requires the permission of Ghent University and IMEC vzw. 8 | 9 | 3. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StreamingMASSIF 2 | 3 | This is the implementation of the *StreamingMASSIF* platform, a streaming extension of the MASSIF platform. 4 | 5 | StreamingMASSIF allows to perform cascading reasoning by combining various components. In its standard configuration it allows to filter meaningful events from a datastream through RDF Stream Processing, abstract the selection through DL reasoning and perform Complex Event Processing ontop of these abstraction. 6 | 7 | Check the [wikipage](https://github.com/IBCNServices/StreamingMASSIF/wiki) for a more in depth explanation on how to use Streaming MASSIF! 8 | 9 | How to cite [Streaming MASSIF](https://www.mdpi.com/1424-8220/18/11/3832): 10 | ``` 11 | @article{bonte2018streaming, 12 | title={Streaming MASSIF: Cascading Reasoning for Efficient Processing of IoT Data Streams}, 13 | author={Bonte, Pieter and Tommasini, Riccardo and Della Valle, Emanuele and De Turck, Filip and Ongenae, Femke}, 14 | journal={Sensors}, 15 | volume={18}, 16 | number={11}, 17 | pages={3832}, 18 | year={2018}, 19 | publisher={Multidisciplinary Digital Publishing Institute} 20 | } 21 | ``` 22 | 23 | How to cite [MASSIF](https://link.springer.com/article/10.1007/s10115-016-0969-1): 24 | ``` 25 | @article{bonte2017massif, 26 | title={The MASSIF platform: a modular and semantic platform for the development of flexible IoT services}, 27 | author={Bonte, Pieter and Ongenae, Femke and De Backere, Femke and Schaballie, Jeroen and Arndt, D{\"o}rthe and Verstichel, Stijn and Mannens, Erik and Van de Walle, Rik and De Turck, Filip}, 28 | journal={Knowledge and Information Systems}, 29 | volume={51}, 30 | number={1}, 31 | pages={89--126}, 32 | year={2017}, 33 | publisher={Springer} 34 | } 35 | ``` 36 | 37 | ## Building and running MASSIF 38 | 39 | ### Requirements: 40 | 41 | - Java 9+ 42 | - Maven2 43 | 44 | ### Build 45 | To build the MASSIF app, call `mvn` and get the compiled `.jar`. 46 | ```shell 47 | mvn clean compile assembly:single 48 | mv target/massif-jar-with-dependencies.jar . 49 | ``` 50 | 51 | To build the MASSIF classes (e.g. for usage in higher level apps), call `mvn` to compile and install the project in the local repository, then add the package to the higher level app dependencies (see `pom.xml` snippet below): 52 | ```shell 53 | mvn install -Dmaven.test.skip=true 54 | ``` 55 | ```xml 56 | 57 | be.ugent.idlab 58 | massif 59 | 0.0.1 60 | 61 | ``` 62 | 63 | ### Run 64 | To run MASSIF, call the compiled `.jar` from the command line as follows: 65 | ```shell 66 | java -jar -Dlog4j.configurationFile=webfiles/log4j2.xml massif-jar-with-dependencies.jar 67 | ``` 68 | Calling this command will return something like this on the CLI: 69 | ```shell 70 | 21:27:32.313 [main] INFO idlab.massif.run.Run - MASSIF STARTING 71 | 21:27:32.403 [main] INFO idlab.massif.run.Run - MASSIF Listening on port 9000 72 | 21:27:32.403 [main] INFO idlab.massif.run.Run - Access the MASSIF GUI on localhost:9000 or register a configuration on localhost:9000/register 73 | 21:27:32.414 [main] INFO idlab.massif.run.Run - MASSIF is ONLINE 74 | ``` 75 | Run options: 76 | * `-p` : TCP port on which massif listens, default: `9000` 77 | 78 | GUI: 79 | * The MASSIF GUI is available on http://localhost:9000 80 | 81 | ### REST API 82 | The MASSIF allow for direct management through GET/POST calls. 83 | Here are the most important path: 84 | * `/register`: register a configuration 85 | * `/stop` : stop a certain query 86 | * `/configs` : get all registered configs 87 | 88 | ### REST API call examples with the [cURL](https://linuxize.com/post/curl-post-request/) tool 89 | List of active configs: 90 | ``` 91 | curl -X GET --verbose \ 92 | --url "http://127.0.0.1:9000/configs" 93 | ``` 94 | * Note: returns `{}` if there are no active config. 95 | 96 | Stop a config (pre-requisite: config ID, see previous command): 97 | ``` 98 | curl -X POST --verbose \ 99 | -H "Content-Type: application/json" \ 100 | -d '19' \ 101 | --url "http://127.0.0.1:9000/stop" 102 | ``` 103 | * Note: returns `ok` 104 | 105 | Send a config: 106 | ``` 107 | curl -X POST --verbose \ 108 | -H "Content-Type: application/json" \ 109 | -d '{"configuration":{"0":[1],"1":[]},"components":{"0":{"type":"Source","impl":"kafkaSource","kafkaServer":"127.0.0.1:9092","kafkaTopic":"backblaze_smart"},"1":{"type":"Sink","impl":"httpGetSinkCombined","path":"1","config":""}}}' \ 110 | --url "http://127.0.0.1:9000/register" 111 | ``` 112 | * Note: returns the config ID 113 | 114 | Get a component running metrics (pre-requisite: component ID, see previous commands): 115 | ``` 116 | curl -X GET --verbose \ 117 | --url "http://127.0.0.1:9000/monitor/1" 118 | ``` 119 | 120 | 121 | -------------------------------------------------------------------------------- /configs.json: -------------------------------------------------------------------------------- 1 | 2 | 3 | { 4 | "components": { 5 | "comp1": { 6 | "type": "Sink", 7 | "impl": "PrintSink" 8 | }, 9 | "sink2": { 10 | "type": "Sink", 11 | "impl": "HTTPGetSinkCombined", 12 | "path":"sink", 13 | "config":"last" 14 | }, 15 | "comp2": { 16 | "type": "window", 17 | "size": 1, 18 | "slide": 1 19 | }, 20 | "comp3": { 21 | "type": "Filter", 22 | "queries": ["CONSTRUCT{?obs ?p ?o.} WHERE {?obs ; [ ]. ?obs ?p ?o}"], 23 | "ontology":"http://aaa-master.aaadev.wall2-ilabt-iminds-be.wall2.ilabt.iminds.be:30081/sensors.ttl" 24 | }, 25 | "comp4": { 26 | "type": "Abstract", 27 | "expressions": [ 28 | { 29 | "head": "http://massif.test/EventA", 30 | "tail": "Observation" 31 | } 32 | ] 33 | }, 34 | "comp5": { 35 | "type": "Source", 36 | "impl": "HTTPGetSource", 37 | "url": "http://aaa-master.aaadev.wall2-ilabt-iminds-be.wall2.ilabt.iminds.be:30080/httpgetsink/sink", 38 | "timeout": 1000 39 | } 40 | }, 41 | "configuration": { 42 | "comp1": [], 43 | "comp3": [ 44 | "comp1" 45 | ], 46 | "comp2": [ 47 | "comp3" 48 | ], 49 | "comp5": [ 50 | "comp2", 51 | "sink2" 52 | ], 53 | "comp4": [ 54 | "comp1" 55 | ] 56 | } 57 | } 58 | 59 | { 60 | "components": { 61 | "print": { 62 | "type": "Sink", 63 | "impl": "PrintSink" 64 | }, 65 | "window": { 66 | "type": "window", 67 | "size": 1, 68 | "slide": 1 69 | }, 70 | "filter": { 71 | "type": "Filter", 72 | "queries": ["CONSTRUCT{?s ?p ?p.} WHERE {?s ?p ?o}"] 73 | }, 74 | "abstract": { 75 | "type": "Abstract", 76 | "expressions": [ 77 | { 78 | "head": "http://massif.test/EventA", 79 | "tail": "Observation" 80 | } 81 | ] 82 | }, 83 | "kafka": { 84 | "type": "Source", 85 | "impl": "KafkaSource", 86 | "kafkaServer": "kafka-headless.kafka:9092", 87 | "kafkaTopic": "idlab.homelab.semantic" 88 | }, 89 | "sink": { 90 | "type": "Sink", 91 | "impl": "HTTPGetSinkCombined", 92 | "path":"sink", 93 | "config":"last" 94 | }, 95 | }, 96 | "configuration": { 97 | "print": [], 98 | "abstract": [ 99 | "print" 100 | ], 101 | "window": [ 102 | "filter" 103 | ], 104 | "kafka": [ 105 | "sink" 106 | ], 107 | "filter": [ 108 | "abstract" 109 | ] 110 | } 111 | } -------------------------------------------------------------------------------- /examples/influenza/catalog-v001.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /examples/influenza/observation_example.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 30.0 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 37.9 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /examples/sensors/input.stream: -------------------------------------------------------------------------------- 1 | . 2 | . 3 | . 4 | . 5 | . 6 | . 7 | . 8 | "465.92" . -------------------------------------------------------------------------------- /examples/sensors/query.json: -------------------------------------------------------------------------------- 1 | { 2 | "components": { 3 | "file":{"type":"Source","impl":"FileSource","fileName":"examples/sensors/input.stream","timeout":1000}, 4 | "print":{"type":"Sink","impl":"PrintSink"}, 5 | "window": {"type": "window","size": 10,"slide": 1}, 6 | "filter": {"type": "Filter","queries": ["CONSTRUCT{?s ?p ?p.} WHERE {?s ?p ?o}"]} 7 | }, 8 | "configuration": { 9 | "file": ["window"], 10 | "window":["filter"], 11 | "filter":["print"] 12 | 13 | } 14 | } 15 | 16 | { 17 | "components": { 18 | "0":{"type":"Source","impl":"FileSource","fileName":"examples/sensors/input.stream","timeout":1000}, 19 | "2": {"type": "window","size": 10,"slide": 1}, 20 | "3": {"type": "Filter","queries": ["CONSTRUCT{?s ?p ?p.} WHERE {?s ?p ?o}"]}, 21 | "1":{"type":"Sink","impl":"PrintSink"} 22 | }, 23 | "configuration": { 24 | "0": ["2"], 25 | "2":["3"], 26 | "3":["1"] 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /figs/StreamingMassifPrototypesDemo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KNowledgeOnWebScale/StreamingMASSIF/c84fcaf251f8dffbb32bb188821cb2159b1646fd/figs/StreamingMassifPrototypesDemo.png -------------------------------------------------------------------------------- /logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | c:/tmp/output.log 5 | 6 | 7 | %date %level [%thread] %logger{10} [%file:%line] %msg%n 8 | 9 | 10 | 11 | 12 | 13 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 14 | 15 | 16 | 17 | 20 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | 6 | be.ugent.idlab 7 | massif 8 | 0.0.1 9 | jar 10 | 11 | massif 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.9 17 | 1.9 18 | 19 | 20 | 21 | massif 22 | 23 | 24 | maven-assembly-plugin 25 | 26 | 27 | 28 | idlab.massif.run.Run 29 | 30 | 31 | 32 | jar-with-dependencies 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.apache.jena 43 | jena-arq 44 | 3.16.0 45 | 46 | 47 | com.github.jsonld-java 48 | jsonld-java 49 | 50 | 51 | 52 | 53 | 54 | net.sourceforge.owlapi 55 | org.semanticweb.hermit 56 | 1.4.3.517 57 | 58 | 59 | com.github.jsonld-java 60 | jsonld-java 61 | 62 | 63 | 64 | 65 | 66 | com.github.jsonld-java 67 | jsonld-java 68 | 0.12.5 69 | 70 | 71 | 72 | com.espertech 73 | esper 74 | 7.1.0 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | junit 85 | junit 86 | 4.12 87 | test 88 | 89 | 90 | com.sparkjava 91 | spark-core 92 | 2.9.1 93 | 94 | 95 | org.apache.logging.log4j 96 | log4j-api 97 | 2.11.1 98 | 99 | 100 | org.apache.logging.log4j 101 | log4j-core 102 | 2.11.1 103 | 104 | 105 | org.apache.logging.log4j 106 | log4j-slf4j-impl 107 | 2.11.1 108 | 109 | 110 | 111 | org.json 112 | json 113 | 20200518 114 | 115 | 116 | 117 | org.apache.kafka 118 | kafka-streams 119 | 2.5.0 120 | 121 | 122 | commons-codec 123 | commons-codec 124 | 1.14 125 | 126 | 127 | com.jsoniter 128 | jsoniter 129 | 0.9.19 130 | 131 | 132 | info.picocli 133 | picocli 134 | 4.6.1 135 | 136 | 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /src/main/java/idlab/examples/generators/HTTPGetGenerator.java: -------------------------------------------------------------------------------- 1 | package idlab.examples.generators; 2 | 3 | import spark.Spark; 4 | 5 | public class HTTPGetGenerator { 6 | public static void main(String[] args) { 7 | new HTTPGetGenerator(); 8 | } 9 | 10 | public HTTPGetGenerator() { 11 | Spark.get("/test", (req, res) -> ONT_EVENT); 12 | 13 | } 14 | private static String ONT_EVENT="\n" + 15 | "\n" + 23 | " \n" + 24 | " \n" + 25 | " \n" + 26 | " \n" + 27 | "\n" + 28 | "\n" + 29 | " \n" + 36 | "\n" + 37 | " \n" + 38 | "\n" + 39 | "\n" + 40 | " \n" + 41 | "\n" + 42 | " \n" + 43 | " \n" + 44 | " \n" + 45 | " \n" + 46 | "\n" + 47 | "\n" + 48 | " \n" + 49 | "\n" + 50 | " \n" + 51 | " \n" + 52 | " \n" + 53 | " \n" + 54 | "\n" + 55 | "\n" + 56 | " \n" + 57 | "\n" + 58 | " \n" + 59 | " \n" + 60 | " \n" + 61 | " \n" + 62 | "\n" + 63 | "\n" + 64 | " \n" + 65 | "\n" + 66 | " \n" + 67 | " \n" + 68 | " \n" + 69 | " \n" + 70 | ""; 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/idlab/examples/generators/HTTPPostGenerator.java: -------------------------------------------------------------------------------- 1 | package idlab.examples.generators; 2 | 3 | import java.io.IOException; 4 | import java.io.UnsupportedEncodingException; 5 | 6 | import org.apache.http.client.methods.CloseableHttpResponse; 7 | import org.apache.http.client.methods.HttpPost; 8 | import org.apache.http.client.methods.HttpPut; 9 | import org.apache.http.entity.StringEntity; 10 | import org.apache.http.impl.client.CloseableHttpClient; 11 | import org.apache.http.impl.client.HttpClients; 12 | 13 | public class HTTPPostGenerator { 14 | public static void main(String[] args) { 15 | new HTTPPostGenerator(); 16 | } 17 | 18 | public HTTPPostGenerator() { 19 | 20 | 21 | while(true) { 22 | 23 | 24 | try { 25 | 26 | CloseableHttpClient client = HttpClients.createDefault(); 27 | HttpPost httpPost = new HttpPost("http://localhost:8080/test"); 28 | 29 | StringEntity entity = new StringEntity(ONT_EVENT); 30 | httpPost.setEntity(entity); 31 | 32 | 33 | CloseableHttpResponse response = client.execute(httpPost); 34 | client.close(); 35 | try { 36 | Thread.sleep(1000); 37 | } catch (InterruptedException e) { 38 | // TODO Auto-generated catch block 39 | e.printStackTrace(); 40 | } 41 | 42 | } catch (UnsupportedEncodingException e) { 43 | // TODO Auto-generated catch block 44 | e.printStackTrace(); 45 | }catch( IOException e) { 46 | e.printStackTrace(); 47 | } 48 | } 49 | 50 | } 51 | private static String ONT_EVENT="\n" + 52 | "\n" + 60 | " \n" + 61 | " \n" + 62 | " \n" + 63 | " \n" + 64 | "\n" + 65 | "\n" + 66 | " \n" + 73 | "\n" + 74 | " \n" + 75 | "\n" + 76 | "\n" + 77 | " \n" + 78 | "\n" + 79 | " \n" + 80 | " \n" + 81 | " \n" + 82 | " \n" + 83 | "\n" + 84 | "\n" + 85 | " \n" + 86 | "\n" + 87 | " \n" + 88 | " \n" + 89 | " \n" + 90 | " \n" + 91 | "\n" + 92 | "\n" + 93 | " \n" + 94 | "\n" + 95 | " \n" + 96 | " \n" + 97 | " \n" + 98 | " \n" + 99 | "\n" + 100 | "\n" + 101 | " \n" + 102 | "\n" + 103 | " \n" + 104 | " \n" + 105 | " \n" + 106 | " \n" + 107 | ""; 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/idlab/examples/generators/HTTPPostSinkTest.java: -------------------------------------------------------------------------------- 1 | package idlab.examples.generators; 2 | 3 | import spark.Spark; 4 | 5 | public class HTTPPostSinkTest { 6 | 7 | public static void main(String[] args) { 8 | // TODO Auto-generated method stub 9 | String path = "test"; 10 | Spark.port(9090); 11 | Spark.post("/" + path, (req, res) -> { 12 | System.out.println(req.body()); 13 | 14 | return "ok"; 15 | }); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/idlab/examples/generators/Test.java: -------------------------------------------------------------------------------- 1 | package idlab.examples.generators; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.ArrayList; 6 | import java.util.HashSet; 7 | import java.util.List; 8 | import java.util.Set; 9 | 10 | import org.apache.jena.query.Query; 11 | import org.apache.jena.query.QueryExecution; 12 | import org.apache.jena.query.QueryExecutionFactory; 13 | import org.apache.jena.query.QueryFactory; 14 | import org.apache.jena.query.QuerySolution; 15 | import org.apache.jena.query.ResultSet; 16 | import org.apache.jena.rdf.model.Model; 17 | import org.apache.jena.rdf.model.ModelFactory; 18 | import org.apache.jena.rdf.model.ResourceFactory; 19 | import org.apache.jena.rdf.model.StmtIterator; 20 | 21 | public class Test { 22 | 23 | public static void main(String[] args) { 24 | System.out.println(ResourceFactory.createResource("http://IBCNServices.github.io/homelab.owl#lightIntensity"));; 25 | } 26 | private static String stripFilterName(String longName) { 27 | if (longName.contains("#")) { 28 | return longName.substring(longName.lastIndexOf('#') + 1); 29 | 30 | } else { 31 | return longName.substring(longName.lastIndexOf('/') + 1); 32 | } 33 | } 34 | private static String ONT_EVENT="\n" + 35 | "\n" + 43 | " \n" + 44 | " \n" + 45 | " \n" + 46 | " \n" + 47 | "\n" + 48 | "\n" + 49 | " \n" + 56 | "\n" + 57 | " \n" + 58 | "\n" + 59 | "\n" + 60 | " \n" + 61 | "\n" + 62 | " \n" + 63 | " \n" + 64 | " \n" + 65 | " \n" + 66 | "\n" + 67 | "\n" + 68 | " \n" + 69 | "\n" + 70 | " \n" + 71 | " \n" + 72 | " \n" + 73 | " \n" + 74 | "\n" + 75 | "\n" + 76 | " \n" + 77 | "\n" + 78 | " \n" + 79 | " \n" + 80 | " \n" + 81 | " \n" + 82 | "\n" + 83 | "\n" + 84 | " \n" + 85 | "\n" + 86 | " \n" + 87 | " \n" + 88 | " \n" + 89 | 90 | " \n" + 91 | " \n" + 92 | ""; 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/idlab/examples/generators/WebSocketClientGenerator.java: -------------------------------------------------------------------------------- 1 | package idlab.examples.generators; 2 | 3 | import java.net.URI; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; 7 | import org.eclipse.jetty.websocket.client.WebSocketClient; 8 | 9 | 10 | public class WebSocketClientGenerator { 11 | public static void main(String[] args) { 12 | WebSocketClient client = new WebSocketClient(); 13 | 14 | SimpleEventSocket socket = new SimpleEventSocket(); 15 | try 16 | { 17 | client.start(); 18 | 19 | URI echoUri = new URI("ws://localhost:4000/ws"); 20 | ClientUpgradeRequest request = new ClientUpgradeRequest(); 21 | client.connect(socket,echoUri,request); 22 | System.out.printf("Connecting to : %s%n",echoUri); 23 | 24 | // wait for closed socket connection. 25 | socket.awaitClose(5,TimeUnit.SECONDS); 26 | 27 | } 28 | catch (Throwable t) 29 | { 30 | t.printStackTrace(); 31 | } 32 | finally 33 | { 34 | try 35 | { 36 | client.stop(); 37 | } 38 | catch (Exception e) 39 | { 40 | e.printStackTrace(); 41 | } 42 | } 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/idlab/examples/generators/WebSocketServerGenerator.java: -------------------------------------------------------------------------------- 1 | package idlab.examples.generators; 2 | 3 | import idlab.massif.sources.util.WebSocketInputStream; 4 | import spark.Spark; 5 | 6 | public class WebSocketServerGenerator { 7 | 8 | public static void main(String[] args) { 9 | Spark.port(4000); 10 | Spark.webSocket("/ws", new SimpleEventSocket()); 11 | Spark.init(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/abstraction/hermit/HermitAbstractionComp.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.abstraction.hermit; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.util.HashMap; 6 | import java.util.HashSet; 7 | import java.util.Map; 8 | import java.util.Map.Entry; 9 | import java.util.Set; 10 | import java.util.stream.Collectors; 11 | 12 | import org.semanticweb.HermiT.Configuration; 13 | import org.semanticweb.HermiT.Reasoner; 14 | import org.semanticweb.owlapi.apibinding.OWLManager; 15 | import org.semanticweb.owlapi.io.StringDocumentSource; 16 | import org.semanticweb.owlapi.io.StringDocumentTarget; 17 | import org.semanticweb.owlapi.model.IRI; 18 | import org.semanticweb.owlapi.model.OWLAxiom; 19 | import org.semanticweb.owlapi.model.OWLClass; 20 | import org.semanticweb.owlapi.model.OWLClassExpression; 21 | import org.semanticweb.owlapi.model.OWLDataFactory; 22 | import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; 23 | import org.semanticweb.owlapi.model.OWLNamedIndividual; 24 | import org.semanticweb.owlapi.model.OWLOntology; 25 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; 26 | import org.semanticweb.owlapi.model.OWLOntologyManager; 27 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; 28 | import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; 29 | import org.semanticweb.owlapi.reasoner.NodeSet; 30 | import org.slf4j.Logger; 31 | import org.slf4j.LoggerFactory; 32 | 33 | import idlab.massif.abstraction.hermit.utils.DLQueryParser; 34 | import idlab.massif.interfaces.core.AbstractionInf; 35 | import idlab.massif.interfaces.core.AbstractionListenerInf; 36 | import idlab.massif.interfaces.core.ListenerInf; 37 | 38 | public class HermitAbstractionComp implements AbstractionInf { 39 | private OWLOntology ontology; 40 | private OWLOntologyManager manager; 41 | private Reasoner reasoner; 42 | private OWLDataFactory factory; 43 | private static String EVENT_IRI = "http://idlab.massif.be/EVENT"; 44 | private static String ONTOLOGY_IRI = "http://idlab.massif.be/abstraction.owl#"; 45 | 46 | private ListenerInf listener; 47 | private DLQueryParser parser; 48 | private int queryCounter = 0; 49 | private Map queryIDMapper; 50 | private Set queries; 51 | 52 | private final Logger logger = LoggerFactory.getLogger(this.getClass()); 53 | 54 | public HermitAbstractionComp() { 55 | this.queryIDMapper = new HashMap(); 56 | this.queries = new HashSet(); 57 | } 58 | 59 | public HermitAbstractionComp(OWLOntology ontology) { 60 | this(); 61 | setOntology(ontology); 62 | } 63 | 64 | public HermitAbstractionComp(String ontologyIRI) { 65 | // load ontology 66 | this.manager = OWLManager.createOWLOntologyManager(); 67 | try { 68 | this.ontology = manager.loadOntologyFromOntologyDocument(IRI.create(ontologyIRI)); 69 | } catch (OWLOntologyCreationException e) { 70 | // TODO Auto-generated catch block 71 | e.printStackTrace(); 72 | } 73 | initiateReasoner(); 74 | } 75 | 76 | private void initiateReasoner() { 77 | // initiate the reasoner 78 | Configuration conf = new Configuration(); 79 | conf.ignoreUnsupportedDatatypes = true; 80 | // conf.prepareReasonerInferences = new PrepareReasonerInferences(); 81 | // conf.prepareReasonerInferences.realisationRequired = true; 82 | this.reasoner = new Reasoner(conf, this.ontology); 83 | this.factory = manager.getOWLDataFactory(); 84 | } 85 | 86 | @Deprecated 87 | public boolean registerDLQuery(String newClass, String classExpression, AbstractionListenerInf listener) { 88 | 89 | return false; 90 | } 91 | 92 | public void addEvent(Set event) { 93 | 94 | System.out.println(event); 95 | Set> activatedQueries = new HashSet>(); 96 | synchronized (ontology) { 97 | manager.addAxioms(ontology, event); 98 | try { 99 | File file = new File("/tmp/newfile.owl"); 100 | FileOutputStream fop = new FileOutputStream(file); 101 | manager.saveOntology(ontology, fop); 102 | }catch(Exception e) { 103 | 104 | } 105 | initiateReasoner(); 106 | 107 | reasoner.flush(); 108 | OWLClass messageClass = factory.getOWLClass(EVENT_IRI); 109 | NodeSet eventIndividuals = reasoner.getInstances(messageClass, false); 110 | logger.debug("Found abstract events: " + eventIndividuals); 111 | for (OWLNamedIndividual eventInd : eventIndividuals.getFlattened()) { 112 | if (event.toString().contains(eventInd.toString())) { 113 | 114 | // ask reasoner for the types of the arriving event 115 | NodeSet inferedClasses = reasoner.getTypes(eventInd, false); 116 | for (OWLClass owlclss : inferedClasses.getFlattened()) { 117 | String clss = owlclss.getIRI().toString(); 118 | if (queries.contains(clss)) { 119 | // add abstract class to event 120 | event.add(factory.getOWLClassAssertionAxiom(owlclss, eventInd)); 121 | Map eventMapping = new HashMap(1); 122 | eventMapping.put(queryIDMapper.get(clss), clss); 123 | activatedQueries.add(eventMapping); 124 | 125 | } 126 | } 127 | 128 | } 129 | } 130 | manager.removeAxioms(ontology, event); 131 | 132 | } 133 | if (!activatedQueries.isEmpty()) { 134 | String eventStr = ""; 135 | try { 136 | OWLOntology eventOnt = manager.createOntology(); 137 | manager.addAxioms(eventOnt, event); 138 | StringDocumentTarget target = new StringDocumentTarget(); 139 | manager.saveOntology(eventOnt, target); 140 | 141 | eventStr = target.toString(); 142 | } catch (OWLOntologyCreationException | OWLOntologyStorageException e) { 143 | // TODO Auto-generated catch block 144 | e.printStackTrace(); 145 | } 146 | 147 | for (Map queryMap : activatedQueries) { 148 | for (Entry ent : queryMap.entrySet()) { 149 | listener.notify(ent.getKey(), eventStr); 150 | } 151 | } 152 | } 153 | 154 | } 155 | 156 | @Override 157 | public boolean setOntology(OWLOntology ontology) { 158 | // load ontology 159 | this.manager = ontology.getOWLOntologyManager(); 160 | this.ontology = ontology; 161 | if(!this.ontology.getOntologyID().getOntologyIRI().isPresent()) { 162 | try { 163 | this.ontology = manager.createOntology(IRI.create(ONTOLOGY_IRI)); 164 | manager.addAxioms(this.ontology, ontology.getAxioms()); 165 | } catch (OWLOntologyCreationException e) { 166 | // TODO Auto-generated catch block 167 | e.printStackTrace(); 168 | } 169 | } 170 | // initiate the reasoner 171 | initiateReasoner(); 172 | this.parser = new DLQueryParser(ontology); 173 | return true; 174 | } 175 | 176 | @Override 177 | public boolean addEvent(String triples) { 178 | // add import to event 179 | if (ontology.getOntologyID().getOntologyIRI().isPresent()) { 180 | triples += "\n[ rdf:type owl:Ontology ;\n" + " owl:imports <" 181 | + ontology.getOntologyID().getOntologyIRI().get() + ">\n" + " ] ."; 182 | } 183 | logger.debug("Received message: " + triples); 184 | // load event 185 | OWLOntology eventOnt; 186 | try { 187 | eventOnt = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(triples)); 188 | Set event = eventOnt.axioms().collect(Collectors.toSet()); 189 | this.addEvent(event); 190 | manager.removeOntology(eventOnt.getOntologyID()); 191 | return true; 192 | 193 | } catch (OWLOntologyCreationException e) { 194 | // TODO Auto-generated catch block 195 | e.printStackTrace(); 196 | return false; 197 | } 198 | 199 | } 200 | 201 | @Override 202 | public boolean addListener(ListenerInf listener) { 203 | this.listener = listener; 204 | return true; 205 | } 206 | 207 | @Override 208 | public void start() { 209 | // TODO Auto-generated method stub 210 | 211 | } 212 | 213 | @Override 214 | public int registerDLQuery(String newClass, String classExpression) { 215 | if (!queries.contains(newClass)) { 216 | OWLClass messageClass = factory.getOWLClass(EVENT_IRI); 217 | OWLClass newClassAxiom = factory.getOWLClass(newClass); 218 | OWLSubClassOfAxiom subclassAxiom = factory.getOWLSubClassOfAxiom(newClassAxiom, messageClass); 219 | OWLClassExpression dlQuery = parser.parseClassExpression(classExpression); 220 | OWLEquivalentClassesAxiom eqclassAxiom = factory.getOWLEquivalentClassesAxiom(newClassAxiom, dlQuery); 221 | 222 | synchronized (ontology) { 223 | manager.addAxiom(ontology, subclassAxiom); 224 | manager.addAxiom(ontology, eqclassAxiom); 225 | reasoner.flush(); 226 | queryIDMapper.put(newClass, queryCounter); 227 | queryCounter++; 228 | queries.add(newClass); 229 | } 230 | return queryCounter - 1; 231 | } else { 232 | return -1; 233 | } 234 | } 235 | 236 | @Override 237 | public void stop() { 238 | // TODO Auto-generated method stub 239 | 240 | } 241 | 242 | } 243 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/abstraction/hermit/HermitAbstractionImpl.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.abstraction.hermit; 2 | 3 | import java.util.HashMap; 4 | import java.util.HashSet; 5 | import java.util.Map; 6 | import java.util.Map.Entry; 7 | import java.util.Set; 8 | import java.util.stream.Collectors; 9 | 10 | import org.semanticweb.HermiT.Configuration; 11 | import org.semanticweb.HermiT.Reasoner; 12 | import org.semanticweb.owlapi.apibinding.OWLManager; 13 | import org.semanticweb.owlapi.formats.TurtleDocumentFormat; 14 | import org.semanticweb.owlapi.io.StringDocumentSource; 15 | import org.semanticweb.owlapi.io.StringDocumentTarget; 16 | import org.semanticweb.owlapi.model.IRI; 17 | import org.semanticweb.owlapi.model.OWLAxiom; 18 | import org.semanticweb.owlapi.model.OWLClass; 19 | import org.semanticweb.owlapi.model.OWLClassExpression; 20 | import org.semanticweb.owlapi.model.OWLDataFactory; 21 | import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; 22 | import org.semanticweb.owlapi.model.OWLNamedIndividual; 23 | import org.semanticweb.owlapi.model.OWLOntology; 24 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; 25 | import org.semanticweb.owlapi.model.OWLOntologyManager; 26 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; 27 | import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; 28 | import org.semanticweb.owlapi.reasoner.NodeSet; 29 | 30 | import idlab.massif.abstraction.hermit.utils.DLQueryParser; 31 | import idlab.massif.interfaces.core.AbstractionInf; 32 | import idlab.massif.interfaces.core.AbstractionListenerInf; 33 | import idlab.massif.interfaces.core.ListenerInf; 34 | 35 | public class HermitAbstractionImpl implements AbstractionInf { 36 | private OWLOntology ontology; 37 | private OWLOntologyManager manager; 38 | private Reasoner reasoner; 39 | private OWLDataFactory factory; 40 | private static String EVENT_IRI = "http://idlab.massif.be/EVENT"; 41 | private Map listenerMapping; 42 | private DLQueryParser parser; 43 | private ListenerInf listener; 44 | 45 | public HermitAbstractionImpl() { 46 | this.manager = OWLManager.createOWLOntologyManager(); 47 | try { 48 | this.ontology = manager.createOntology(); 49 | initiateReasoner(); 50 | } catch (OWLOntologyCreationException e) { 51 | // TODO Auto-generated catch block 52 | e.printStackTrace(); 53 | } 54 | } 55 | 56 | public HermitAbstractionImpl(OWLOntology ontology) { 57 | // load ontology 58 | this.manager = ontology.getOWLOntologyManager(); 59 | this.ontology = ontology; 60 | // initiate the reasoner 61 | initiateReasoner(); 62 | this.parser = new DLQueryParser(ontology); 63 | this.listenerMapping = new HashMap(); 64 | 65 | } 66 | 67 | public HermitAbstractionImpl(String ontologyIRI) { 68 | // load ontology 69 | this.manager = OWLManager.createOWLOntologyManager(); 70 | try { 71 | this.ontology = manager.loadOntologyFromOntologyDocument(IRI.create(ontologyIRI)); 72 | } catch (OWLOntologyCreationException e) { 73 | // TODO Auto-generated catch block 74 | e.printStackTrace(); 75 | } 76 | initiateReasoner(); 77 | } 78 | 79 | private void initiateReasoner() { 80 | // initiate the reasoner 81 | Configuration conf = new Configuration(); 82 | conf.ignoreUnsupportedDatatypes = true; 83 | // conf.prepareReasonerInferences = new PrepareReasonerInferences(); 84 | // conf.prepareReasonerInferences.realisationRequired = true; 85 | this.reasoner = new Reasoner(conf, this.ontology); 86 | this.factory = manager.getOWLDataFactory(); 87 | } 88 | 89 | public boolean registerDLQuery(String newClass, String classExpression, ListenerInf listener) { 90 | OWLClass messageClass = factory.getOWLClass(EVENT_IRI); 91 | OWLClass newClassAxiom = factory.getOWLClass(newClass); 92 | OWLSubClassOfAxiom subclassAxiom = factory.getOWLSubClassOfAxiom(newClassAxiom, messageClass); 93 | OWLClassExpression dlQuery = parser.parseClassExpression(classExpression); 94 | OWLEquivalentClassesAxiom eqclassAxiom = factory.getOWLEquivalentClassesAxiom(newClassAxiom, dlQuery); 95 | 96 | synchronized (ontology) { 97 | manager.addAxiom(ontology, subclassAxiom); 98 | manager.addAxiom(ontology, eqclassAxiom); 99 | reasoner.flush(); 100 | // TODO check if this is correct!! 101 | listenerMapping.put(newClass, listener); 102 | } 103 | return true; 104 | } 105 | 106 | public void addEvent(Set event) { 107 | Set> activatedLsitener = new HashSet>(); 108 | synchronized (ontology) { 109 | manager.addAxioms(ontology, event); 110 | // TODO: fix reinitation!!! 111 | // initiateReasoner(); 112 | 113 | reasoner.flush(); 114 | OWLClass messageClass = factory.getOWLClass(EVENT_IRI); 115 | NodeSet eventIndividuals = reasoner.getInstances(messageClass, false); 116 | for (OWLNamedIndividual eventInd : eventIndividuals.getFlattened()) { 117 | if (event.toString().contains(eventInd.toString())) { 118 | 119 | // ask reasoner for the types of the arriving event 120 | NodeSet inferedClasses = reasoner.getTypes(eventInd, false); 121 | for (OWLClass owlclss : inferedClasses.getFlattened()) { 122 | String clss = owlclss.getIRI().toString(); 123 | if (listenerMapping.containsKey(clss)) { 124 | // add abstract class to event 125 | event.add(factory.getOWLClassAssertionAxiom(owlclss, eventInd)); 126 | Map eventMapping = new HashMap( 127 | 1); 128 | eventMapping.put(listenerMapping.get(clss), clss); 129 | activatedLsitener.add(eventMapping); 130 | 131 | } 132 | } 133 | 134 | } 135 | } 136 | } 137 | if (!activatedLsitener.isEmpty()) { 138 | String eventStr=""; 139 | try { 140 | OWLOntology eventOnt = manager.createOntology(); 141 | manager.addAxioms(eventOnt, event); 142 | StringDocumentTarget target = new StringDocumentTarget(); 143 | TurtleDocumentFormat turtleFormat = new TurtleDocumentFormat(); 144 | manager.saveOntology(eventOnt,turtleFormat, target); 145 | eventStr = target.toString(); 146 | } catch (OWLOntologyCreationException | OWLOntologyStorageException e) { 147 | // TODO Auto-generated catch block 148 | e.printStackTrace(); 149 | } 150 | 151 | for (Map listenerMap : activatedLsitener) { 152 | for (Entry ent : listenerMap.entrySet()) { 153 | //ent.getKey().notify(eventStr, ent.getValue()); 154 | ent.getKey().notify(0,eventStr); 155 | } 156 | } 157 | } 158 | 159 | } 160 | 161 | @Override 162 | public boolean setOntology(OWLOntology ontology) { 163 | // load ontology 164 | this.manager = ontology.getOWLOntologyManager(); 165 | this.ontology = ontology; 166 | // initiate the reasoner 167 | initiateReasoner(); 168 | this.parser = new DLQueryParser(ontology); 169 | this.listenerMapping = new HashMap(); 170 | return true; 171 | } 172 | 173 | @Override 174 | public boolean addEvent(String triples) { 175 | // load event 176 | OWLOntology eventOnt; 177 | try { 178 | eventOnt = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(triples)); 179 | Set event = eventOnt.axioms().collect(Collectors.toSet()); 180 | this.addEvent(event); 181 | manager.removeOntology(eventOnt.getOntologyID()); 182 | return true; 183 | 184 | } catch (OWLOntologyCreationException e) { 185 | // TODO Auto-generated catch block 186 | e.printStackTrace(); 187 | return false; 188 | } 189 | 190 | } 191 | 192 | @Override 193 | public boolean addListener(ListenerInf listener) { 194 | // TODO Auto-generated method stub 195 | this.listener = listener; 196 | return false; 197 | } 198 | 199 | @Override 200 | public void start() { 201 | // TODO Auto-generated method stub 202 | 203 | } 204 | 205 | @Override 206 | public int registerDLQuery(String newClass, String classExpression) { 207 | // TODO Auto-generated method stub 208 | this.registerDLQuery(newClass, classExpression, this.listener); 209 | return 0; 210 | } 211 | 212 | @Override 213 | public void stop() { 214 | // TODO Auto-generated method stub 215 | 216 | } 217 | 218 | @Override 219 | public boolean registerDLQuery(String newClass, String classExpression, AbstractionListenerInf listener) { 220 | // TODO Auto-generated method stub 221 | return false; 222 | } 223 | 224 | 225 | 226 | 227 | } 228 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/abstraction/hermit/utils/DLQueryParser.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.abstraction.hermit.utils; 2 | 3 | import java.util.Set; 4 | 5 | import org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxEditorParser; 6 | import org.semanticweb.owlapi.expression.OWLEntityChecker; 7 | import org.semanticweb.owlapi.expression.ShortFormEntityChecker; 8 | import org.semanticweb.owlapi.model.OWLClassExpression; 9 | import org.semanticweb.owlapi.model.OWLDataFactory; 10 | import org.semanticweb.owlapi.model.OWLOntology; 11 | import org.semanticweb.owlapi.model.OWLOntologyManager; 12 | import org.semanticweb.owlapi.util.BidirectionalShortFormProvider; 13 | import org.semanticweb.owlapi.util.BidirectionalShortFormProviderAdapter; 14 | import org.semanticweb.owlapi.util.ShortFormProvider; 15 | import org.semanticweb.owlapi.util.SimpleShortFormProvider; 16 | 17 | public class DLQueryParser { 18 | private final OWLOntology rootOntology; 19 | private final BidirectionalShortFormProvider bidiShortFormProvider; 20 | 21 | public DLQueryParser(OWLOntology rootOntology) { 22 | ShortFormProvider shortFormProvider = new SimpleShortFormProvider(); 23 | 24 | this.rootOntology = rootOntology; 25 | OWLOntologyManager manager = rootOntology.getOWLOntologyManager(); 26 | Set importsClosure = rootOntology.getImportsClosure(); 27 | // Create a bidirectional short form provider to do the actual mapping. 28 | // It will generate names using the input 29 | // short form provider. 30 | bidiShortFormProvider = new BidirectionalShortFormProviderAdapter(manager, 31 | importsClosure, shortFormProvider); 32 | } 33 | 34 | public OWLClassExpression parseClassExpression(String classExpressionString) { 35 | OWLDataFactory dataFactory = rootOntology.getOWLOntologyManager() 36 | .getOWLDataFactory(); 37 | ManchesterOWLSyntaxEditorParser parser = new ManchesterOWLSyntaxEditorParser( 38 | dataFactory, classExpressionString); 39 | parser.setDefaultOntology(rootOntology); 40 | OWLEntityChecker entityChecker = new ShortFormEntityChecker(bidiShortFormProvider); 41 | parser.setOWLEntityChecker(entityChecker); 42 | return parser.parseClassExpression(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/cep/esper/EsperCEPComp.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.cep.esper; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.io.StringWriter; 6 | import java.util.ArrayList; 7 | import java.util.HashMap; 8 | import java.util.HashSet; 9 | import java.util.List; 10 | import java.util.Map; 11 | import java.util.Map.Entry; 12 | import java.util.Set; 13 | 14 | import org.apache.jena.query.Query; 15 | import org.apache.jena.query.QueryExecution; 16 | import org.apache.jena.query.QueryExecutionFactory; 17 | import org.apache.jena.query.QueryFactory; 18 | import org.apache.jena.query.QuerySolution; 19 | import org.apache.jena.query.ResultSet; 20 | import org.apache.jena.rdf.model.Model; 21 | import org.apache.jena.rdf.model.ModelFactory; 22 | import org.apache.jena.rdf.model.Property; 23 | import org.apache.jena.rdf.model.Resource; 24 | import org.apache.jena.rdf.model.ResourceFactory; 25 | import org.apache.jena.rdf.model.StmtIterator; 26 | import org.apache.jena.vocabulary.RDF; 27 | import org.slf4j.Logger; 28 | import org.slf4j.LoggerFactory; 29 | 30 | import com.espertech.esper.client.Configuration; 31 | import com.espertech.esper.client.EPServiceProvider; 32 | import com.espertech.esper.client.EPServiceProviderManager; 33 | import com.espertech.esper.client.EPStatement; 34 | import com.espertech.esper.client.EventBean; 35 | import com.espertech.esper.client.UpdateListener; 36 | import com.espertech.esper.event.map.MapEventBean; 37 | 38 | import idlab.massif.interfaces.core.CEPInf; 39 | import idlab.massif.interfaces.core.CEPListener; 40 | import idlab.massif.interfaces.core.ListenerInf; 41 | import idlab.massif.selection.csparql_basic.StreamingJena; 42 | 43 | public class EsperCEPComp implements CEPInf { 44 | 45 | private EPServiceProvider epService; 46 | 47 | private Set allEventTypes; 48 | private List queries; 49 | private List complexNames; 50 | private List> indComplexPropMappings; 51 | private int queryCounter; 52 | 53 | private ListenerInf listener; 54 | 55 | private Query typeQuery; 56 | 57 | private final Logger logger = LoggerFactory.getLogger(this.getClass()); 58 | 59 | public EsperCEPComp() { 60 | this.allEventTypes = new HashSet(); 61 | this.queryCounter = 0; 62 | this.typeQuery= QueryFactory.create("Select DISTINCT ?ind ?someType WHERE {?ind ?someType}" ); 63 | this.queries = new ArrayList(); 64 | this.complexNames = new ArrayList(); 65 | indComplexPropMappings = new ArrayList>(); 66 | } 67 | 68 | public int registerQuery(String complexName,String query, Set eventTypes,Map indComplexPropMapping) { 69 | allEventTypes.addAll(eventTypes); 70 | queries.add(query); 71 | complexNames.add(complexName); 72 | indComplexPropMappings.add(indComplexPropMapping); 73 | queryCounter++; 74 | return queryCounter - 1; 75 | 76 | } 77 | 78 | @Deprecated 79 | public boolean registerQuery(String query, Set eventTypes, CEPListener listener) { 80 | return false; 81 | } 82 | @Deprecated 83 | public void addEvent(String event, String eventNameFull) { 84 | 85 | } 86 | 87 | private String stripFilterName(String longName) { 88 | if (longName.contains("#")) { 89 | return longName.substring(longName.lastIndexOf('#') + 1); 90 | 91 | } else { 92 | return longName.substring(longName.lastIndexOf('/') + 1); 93 | } 94 | } 95 | 96 | @Override 97 | public boolean addEvent(String event) { 98 | logger.debug("Received message" + event); 99 | List foundTypes = new ArrayList(); 100 | List foundInds = new ArrayList(); 101 | 102 | Model dataModel = ModelFactory.createDefaultModel(); 103 | try { 104 | InputStream targetStream = new ByteArrayInputStream(event.getBytes()); 105 | dataModel.read(targetStream,"TTL"); 106 | StmtIterator it = dataModel.listStatements(); 107 | try (QueryExecution qexec = QueryExecutionFactory.create(typeQuery, dataModel)) { 108 | ResultSet result = qexec.execSelect(); 109 | while(result.hasNext()){ 110 | 111 | QuerySolution sol = result.nextSolution(); 112 | String stripped =stripFilterName(sol.get("someType").toString()); 113 | if(allEventTypes.contains(stripped)) { 114 | foundTypes.add(stripped); 115 | foundInds.add(sol.get("ind").toString()); 116 | } 117 | } 118 | }catch(Exception e) { 119 | e.printStackTrace(); 120 | } 121 | }catch(Exception e) { 122 | e.printStackTrace(); 123 | } 124 | logger.debug("Found types: " + foundTypes); 125 | for(int i = 0 ; i eventMap = new HashMap(); 127 | eventMap.put("ts", System.currentTimeMillis()); 128 | eventMap.put("content", dataModel); 129 | eventMap.put("ind", foundInds.get(i)); 130 | epService.getEPRuntime().sendEvent(eventMap, foundTypes.get(i)); 131 | } 132 | return true; 133 | } 134 | 135 | 136 | @Override 137 | public boolean addListener(ListenerInf listener) { 138 | this.listener = listener; 139 | return true; 140 | } 141 | 142 | @Override 143 | public void start() { 144 | Map properties = new HashMap(); 145 | properties.put("packedId", "string"); 146 | properties.put("ts", "long"); 147 | properties.put("content", Model.class); 148 | properties.put("ind", "string"); 149 | 150 | ClassLoader classLoader = StreamingJena.class.getClassLoader(); 151 | 152 | Configuration configuration = new Configuration(); 153 | for (String eventType : allEventTypes) { 154 | configuration.addEventType(eventType, properties); 155 | } 156 | // disable internal clock 157 | configuration.getEngineDefaults().getThreading().setInternalTimerEnabled(true); 158 | // this.epService = EPServiceProviderManager.getDefaultProvider(configuration); 159 | this.epService = EPServiceProviderManager.getProvider("cep", configuration); 160 | for(int i = 0 ; i < queries.size();i++) { 161 | // register query 162 | String query = queries.get(i); 163 | String eplQuery = "select * from pattern [ " + query + "]"; 164 | EPStatement statement = epService.getEPAdministrator().createEPL(eplQuery); 165 | EsperListener esperListener = new EsperListener(i,complexNames.get(i),indComplexPropMappings.get(i)); 166 | statement.addListener(esperListener); 167 | } 168 | } 169 | 170 | private class EsperListener implements UpdateListener { 171 | private int queryID; 172 | private String complexName; 173 | private Map indMapping; 174 | public EsperListener(int queryID,String complexName,Map indMapping) { 175 | this.queryID = queryID; 176 | this.complexName=complexName; 177 | this.indMapping =indMapping; 178 | 179 | } 180 | 181 | public void update(EventBean[] newEvents, EventBean[] oldEvents) { 182 | try { 183 | if (newEvents != null) { 184 | for (EventBean e : newEvents) { 185 | if (e instanceof MapEventBean) { 186 | MapEventBean meb = (MapEventBean) e; 187 | Model cepResult = ModelFactory.createDefaultModel(); 188 | //add the complex class to the model 189 | Resource blank = cepResult.createResource(); 190 | cepResult.add(ResourceFactory.createStatement(blank, RDF.type, ResourceFactory.createResource(complexName))); 191 | for (Entry entry : meb.getProperties().entrySet()) { 192 | 193 | if (entry.getValue() instanceof MapEventBean) { 194 | MapEventBean mapEvent = (MapEventBean) entry.getValue(); 195 | String eventName = mapEvent.getEventType().getName(); 196 | String eventIdentifier = entry.getKey(); 197 | if (mapEvent.getProperties().containsKey("content")) { 198 | cepResult.add(ResourceFactory.createStatement(blank, 199 | ResourceFactory.createProperty(indMapping.get(eventIdentifier)), 200 | ResourceFactory.createResource((String)mapEvent.getProperties().get("ind")))); 201 | 202 | Model eventModel = (Model) mapEvent.getProperties().get("content"); 203 | cepResult.add(eventModel); 204 | } 205 | } 206 | } 207 | String syntax = "RDF/XML"; // also try "N-TRIPLE" and "TURTLE" 208 | StringWriter out = new StringWriter(); 209 | cepResult.write(out, syntax); 210 | String resultString = out.toString(); 211 | listener.notify(this.queryID,resultString); 212 | } 213 | } 214 | } 215 | } catch (Exception e) { 216 | e.printStackTrace(); 217 | } 218 | } 219 | } 220 | 221 | @Override 222 | public void stop() { 223 | // TODO Auto-generated method stub 224 | this.epService.destroy(); 225 | } 226 | 227 | } 228 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/cep/esper/EsperCEPImpl.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.cep.esper; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Set; 6 | import java.util.Map.Entry; 7 | 8 | import org.semanticweb.owlapi.model.OWLAxiom; 9 | 10 | import com.espertech.esper.client.Configuration; 11 | import com.espertech.esper.client.EPServiceProvider; 12 | import com.espertech.esper.client.EPServiceProviderManager; 13 | import com.espertech.esper.client.EPStatement; 14 | import com.espertech.esper.client.EventBean; 15 | import com.espertech.esper.client.UpdateListener; 16 | import com.espertech.esper.event.map.MapEventBean; 17 | 18 | import idlab.massif.interfaces.core.CEPInf; 19 | import idlab.massif.interfaces.core.CEPListener; 20 | import idlab.massif.interfaces.core.ListenerInf; 21 | import idlab.massif.selection.csparql_basic.StreamingJena; 22 | 23 | public class EsperCEPImpl implements CEPInf{ 24 | 25 | private EPServiceProvider epService; 26 | private Set acceptedEvents; 27 | 28 | public EsperCEPImpl() { 29 | 30 | } 31 | 32 | public boolean registerQuery(String query, Set eventTypes, ListenerInf listener) { 33 | this.acceptedEvents = eventTypes; 34 | Map properties = new HashMap(); 35 | properties.put("packedId", "string"); 36 | properties.put("ts", "long"); 37 | properties.put("content", Set.class); 38 | ClassLoader classLoader = StreamingJena.class.getClassLoader(); 39 | 40 | Configuration configuration = new Configuration(); 41 | for (String eventType : eventTypes) { 42 | configuration.addEventType(eventType, properties); 43 | } 44 | // disable internal clock 45 | configuration.getEngineDefaults().getThreading().setInternalTimerEnabled(true); 46 | //this.epService = EPServiceProviderManager.getDefaultProvider(configuration); 47 | this.epService = EPServiceProviderManager.getProvider("cep", configuration); 48 | 49 | // register query 50 | String eplQuery = "select * from pattern [ " + query + "]"; 51 | EPStatement statement = epService.getEPAdministrator().createEPL(eplQuery); 52 | EsperListener esperListener = new EsperListener(listener); 53 | statement.addListener(esperListener); 54 | return true; 55 | } 56 | 57 | public void addEvent(String event, String eventNameFull) { 58 | String eventName=stripFilterName(eventNameFull); 59 | if (acceptedEvents.contains(eventName)) { 60 | Map eventMap = new HashMap(); 61 | eventMap.put("ts", System.currentTimeMillis()); 62 | eventMap.put("content", event); 63 | 64 | epService.getEPRuntime().sendEvent(eventMap, eventName); 65 | } else { 66 | System.out.println("Unknown type "+eventName); 67 | } 68 | } 69 | 70 | private String stripFilterName(String longName) { 71 | if (longName.contains("#")) { 72 | return longName.substring(longName.lastIndexOf('#') + 1); 73 | 74 | } else if(longName.contains("/")){ 75 | return longName.substring(longName.lastIndexOf('/') + 1); 76 | }else { 77 | return longName; 78 | } 79 | } 80 | 81 | private class EsperListener implements UpdateListener { 82 | private ListenerInf cepListener; 83 | 84 | public EsperListener(ListenerInf cepListener) { 85 | this.cepListener = cepListener; 86 | } 87 | 88 | public void update(EventBean[] newEvents, EventBean[] oldEvents) { 89 | try { 90 | if (newEvents != null) { 91 | for (EventBean e : newEvents) { 92 | if (e instanceof MapEventBean) { 93 | MapEventBean meb = (MapEventBean) e; 94 | Map cepResult = new HashMap(); 95 | for (Entry entry : meb.getProperties().entrySet()) { 96 | Map subResult = new HashMap(); 97 | cepResult.put(entry.getKey(), subResult); 98 | if (entry.getValue() instanceof MapEventBean) { 99 | MapEventBean mapEvent = (MapEventBean) entry.getValue(); 100 | String eventName = mapEvent.getEventType().getName(); 101 | subResult.put("eventName", eventName); 102 | if (mapEvent.getProperties().containsKey("content")) { 103 | String message = (String) mapEvent.getProperties().get("content"); 104 | subResult.put("event", message); 105 | } 106 | } 107 | } 108 | cepListener.notify(0,cepResult.toString()); 109 | } 110 | } 111 | } 112 | } catch (Exception e) { 113 | e.printStackTrace(); 114 | } 115 | } 116 | } 117 | 118 | @Override 119 | public boolean addEvent(String event) { 120 | // TODO Auto-generated method stub 121 | for(String eventType: acceptedEvents) { 122 | if(event.contains(eventType)) { 123 | this.addEvent(event, eventType); 124 | } 125 | } 126 | return false; 127 | } 128 | 129 | @Override 130 | public boolean addListener(ListenerInf listener) { 131 | // TODO Auto-generated method stub 132 | return false; 133 | } 134 | 135 | @Override 136 | public void start() { 137 | // TODO Auto-generated method stub 138 | 139 | } 140 | 141 | @Override 142 | public int registerQuery(String complexName, String CEPquery, Set eventTypes, 143 | Map indComplexPropMapping) { 144 | // TODO Auto-generated method stub 145 | return 0; 146 | } 147 | @Override 148 | public void stop() { 149 | // TODO Auto-generated method stub 150 | this.epService.destroy(); 151 | } 152 | 153 | @Override 154 | public boolean registerQuery(String CEPquery, Set eventTypes, CEPListener listener) { 155 | // TODO Auto-generated method stub 156 | return false; 157 | } 158 | 159 | 160 | } 161 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/core/HTTPMonitoringSingleTon.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.core; 2 | 3 | import static spark.Spark.get; 4 | 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | 10 | import org.json.JSONObject; 11 | 12 | import idlab.massif.interfaces.core.MonitorInf; 13 | 14 | public class HTTPMonitoringSingleTon { 15 | 16 | private static HTTPMonitoringSingleTon single_instance = null; 17 | 18 | private Map eventSourceMap; 19 | 20 | // private constructor restricted to this class itself 21 | private HTTPMonitoringSingleTon() { 22 | get("/monitor/:id", (req, res) -> prepData(req.params("id"))); 23 | eventSourceMap = new HashMap(); 24 | } 25 | 26 | // static method to create instance of Singleton class 27 | public static HTTPMonitoringSingleTon getInstance() { 28 | if (single_instance == null) 29 | single_instance = new HTTPMonitoringSingleTon(); 30 | 31 | return single_instance; 32 | } 33 | 34 | private String prepData(String id) { 35 | Map results = new HashMap(); 36 | if (eventSourceMap.containsKey(id)) { 37 | results.put("queueSize", eventSourceMap.get(id).getQueueSize()); 38 | results.put("eventsIn", eventSourceMap.get(id).getEventsIn()); 39 | results.put("eventsOut", eventSourceMap.get(id).getEventsOut()); 40 | results.put("throughput", eventSourceMap.get(id).getThroughput()); 41 | results.put("avgTime", eventSourceMap.get(id).getAvgTime()); 42 | } 43 | ; 44 | return new JSONObject(results).toString(); 45 | } 46 | 47 | public void registerMonitor(String id, MonitorInf monitor) { 48 | eventSourceMap.put(id, monitor); 49 | } 50 | 51 | public void removeMonitor(String id) { 52 | eventSourceMap.remove(id); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/core/PipeLine.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.core; 2 | 3 | import java.util.HashSet; 4 | import java.util.Map; 5 | import java.util.Set; 6 | import java.util.concurrent.ExecutorService; 7 | import java.util.concurrent.Executors; 8 | 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import idlab.massif.interfaces.core.AbstractionInf; 13 | import idlab.massif.interfaces.core.AbstractionListenerInf; 14 | import idlab.massif.interfaces.core.CEPInf; 15 | import idlab.massif.interfaces.core.CEPListener; 16 | import idlab.massif.interfaces.core.SelectionInf; 17 | import idlab.massif.interfaces.core.SelectionListenerInf; 18 | import idlab.massif.interfaces.core.SinkInf; 19 | 20 | 21 | public class PipeLine implements SelectionListenerInf, AbstractionListenerInf, CEPListener{ 22 | 23 | private SelectionInf selection; 24 | private AbstractionInf abstraction; 25 | private CEPInf cep; 26 | private final Logger logger = LoggerFactory.getLogger(this.getClass()); 27 | private ExecutorService abstractionQueue = Executors.newFixedThreadPool(1); 28 | private ExecutorService cepQueue = Executors.newFixedThreadPool(1); 29 | private Set sinks; 30 | 31 | public PipeLine(SelectionInf selection, AbstractionInf abstraction, CEPInf cep) { 32 | this.selection = selection; 33 | this.abstraction = abstraction; 34 | this.cep = cep; 35 | this.sinks = new HashSet(); 36 | } 37 | public void addEvent(String event) { 38 | selection.addEvent(event); 39 | } 40 | public void registerSink(SinkInf sink) { 41 | this.sinks.add(sink); 42 | } 43 | public void unregisterSink(SinkInf sink) { 44 | if(this.sinks.contains(sink)) { 45 | this.sinks.remove(sink); 46 | logger.debug("Removing sink"); 47 | }else { 48 | logger.error("Trying to unregister unknown sink"); 49 | } 50 | 51 | } 52 | //CEP listener 53 | @Override 54 | public void notify(Map events) { 55 | for(SinkInf sink: sinks) { 56 | sink.addEvent(events.toString()); 57 | } 58 | } 59 | //abstraction listener 60 | @Override 61 | public void notify(String event, String eventName) { 62 | logger.debug("Abstraction Listener received " + eventName); 63 | 64 | cepQueue.execute(new Runnable() { 65 | 66 | @Override 67 | public void run() { 68 | cep.addEvent(event, eventName); 69 | } 70 | 71 | }); 72 | } 73 | //Selection listener 74 | @Override 75 | public void notify(String triples) { 76 | logger.debug("Selection Listener received " + triples); 77 | 78 | abstractionQueue.execute(new Runnable() { 79 | 80 | @Override 81 | public void run() { 82 | abstraction.addEvent(triples); 83 | } 84 | 85 | }); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/core/PipeLineComponent.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.core; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.concurrent.Executors; 7 | import java.util.concurrent.ThreadPoolExecutor; 8 | import java.util.concurrent.atomic.AtomicLong; 9 | 10 | import idlab.massif.interfaces.core.ListenerInf; 11 | import idlab.massif.interfaces.core.MonitorInf; 12 | import idlab.massif.interfaces.core.PipeLineElement; 13 | 14 | public class PipeLineComponent implements ListenerInf, MonitorInf { 15 | 16 | private List output; 17 | Map> outputMap; 18 | private PipeLineElement element; 19 | private ThreadPoolExecutor queue = (ThreadPoolExecutor)Executors.newFixedThreadPool(1); 20 | private boolean isQueryComponentLinked = false; 21 | 22 | private AtomicLong inEvents= new AtomicLong(0); 23 | private AtomicLong outEvents= new AtomicLong(0); 24 | private long totalTime = 0; 25 | public PipeLineComponent(String id,PipeLineElement element, List output) { 26 | this(element,output); 27 | HTTPMonitoringSingleTon.getInstance().registerMonitor(id, this); 28 | } 29 | public PipeLineComponent(PipeLineElement element, List output) { 30 | this.output = output; 31 | this.element = element; 32 | element.addListener(this); 33 | 34 | } 35 | public PipeLineComponent(PipeLineElement element, Map> output) { 36 | this.outputMap = output; 37 | this.element = element; 38 | element.addListener(this); 39 | isQueryComponentLinked=true; 40 | 41 | } 42 | 43 | public void setOutput(List output) { 44 | this.output = output; 45 | } 46 | public List getOutputs(){ 47 | return this.output; 48 | } 49 | public void addEvent(String event) { 50 | inEvents.getAndIncrement(); 51 | // add all arriving events to the queue 52 | queue.execute(new Runnable() { 53 | 54 | @Override 55 | public void run() { 56 | long time1 = System.currentTimeMillis(); 57 | element.addEvent(event); 58 | totalTime+=(System.currentTimeMillis()-time1); 59 | 60 | } 61 | 62 | }); 63 | } 64 | 65 | public void setQueryComponentLinked(boolean queryCompLinked) { 66 | this.isQueryComponentLinked = queryCompLinked; 67 | } 68 | 69 | @Override 70 | public void notify(int queryID, String event) { 71 | // send all the response from this component to all its output components 72 | outEvents.getAndIncrement(); 73 | if (!isQueryComponentLinked) { 74 | for (PipeLineComponent comp : output) { 75 | comp.addEvent(event); 76 | } 77 | } else { 78 | for (PipeLineComponent comp : outputMap.get(queryID)) { 79 | comp.addEvent(event); 80 | } 81 | } 82 | } 83 | public PipeLineElement getElement() { 84 | return element; 85 | } 86 | @Override 87 | public long getQueueSize() { 88 | return queue.getQueue().size(); 89 | } 90 | @Override 91 | public long getEventsIn() { 92 | return inEvents.get(); 93 | } 94 | @Override 95 | public long getEventsOut() { 96 | return outEvents.get(); 97 | } 98 | @Override 99 | public float getThroughput() { 100 | return totalTime>0?(float)(((float)1000.0*outEvents.get())/(float)totalTime):0; 101 | } 102 | 103 | public String getConfig() { 104 | return element.toString(); 105 | } 106 | @Override 107 | public float getAvgTime() { 108 | return totalTime>0?(float)(((float)totalTime)/(float)outEvents.get()):0; 109 | } 110 | 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/core/PipeLineGraph.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.core; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Map.Entry; 6 | 7 | public class PipeLineGraph { 8 | 9 | private Map pipecomps; 10 | 11 | public PipeLineGraph(Map pipecomps) { 12 | this.pipecomps = pipecomps; 13 | } 14 | public void stop() { 15 | pipecomps.values().stream().forEach(p->p.getElement().stop()); 16 | } 17 | @Override 18 | public String toString() { 19 | Map reverseMap = new HashMap(); 20 | StringBuilder str = new StringBuilder(); 21 | //Add components definitions 22 | str.append("{\"components\":{"); 23 | for (Entry entry:pipecomps.entrySet()) { 24 | str.append("\"").append(entry.getKey()).append("\":").append(entry.getValue().getConfig()).append(","); 25 | reverseMap.put(entry.getValue(), entry.getKey()); 26 | } 27 | //remove trailing comma fix 28 | str.deleteCharAt(str.length()-1); 29 | str.append("},"); 30 | //Add configuration 31 | str.append("\"configuration\":{"); 32 | for (Entry entry:pipecomps.entrySet()) { 33 | reverseMap.put(entry.getValue(), entry.getKey()); 34 | str.append("\"").append(entry.getKey()).append("\":["); 35 | if(!entry.getValue().getOutputs().isEmpty()) { 36 | for (PipeLineComponent out : entry.getValue().getOutputs()) { 37 | str.append("\"").append(reverseMap.get(out)).append("\","); 38 | } 39 | //remove trailing comma fix 40 | str.deleteCharAt(str.length() - 1); 41 | } 42 | str.append("],"); 43 | } 44 | //remove trailing comma fix 45 | str.deleteCharAt(str.length()-1); 46 | str.append("}}"); 47 | return str.toString(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/exceptions/QueryRegistrationException.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.exceptions; 2 | 3 | /** 4 | * The QueryRegistrationException wraps all the exception that can occur during query registration. 5 | * Note that various types of subqueries need to be parsed and registered to their handeling implemention. 6 | * @author psbonte 7 | * 8 | */ 9 | 10 | public class QueryRegistrationException extends Exception{ 11 | 12 | /** 13 | * 14 | */ 15 | private static final long serialVersionUID = -1512081969345761510L; 16 | 17 | public QueryRegistrationException(String message) { 18 | super(message); 19 | } 20 | 21 | public QueryRegistrationException(String message, Throwable cause) { 22 | super(message,cause); 23 | } 24 | public QueryRegistrationException(Throwable cause) { 25 | super(cause); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/filter/jena/JenaFilter.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.filter.jena; 2 | 3 | import java.io.StringWriter; 4 | import java.util.ArrayList; 5 | import java.util.Iterator; 6 | import java.util.List; 7 | 8 | import org.apache.jena.query.Dataset; 9 | import org.apache.jena.query.Query; 10 | import org.apache.jena.query.QueryExecution; 11 | import org.apache.jena.query.QueryExecutionFactory; 12 | import org.apache.jena.query.QueryFactory; 13 | import org.apache.jena.query.QuerySolution; 14 | import org.apache.jena.query.ResultSet; 15 | import org.apache.jena.query.Syntax; 16 | import org.apache.jena.rdf.model.InfModel; 17 | import org.apache.jena.rdf.model.Model; 18 | import org.apache.jena.rdf.model.ModelFactory; 19 | import org.apache.jena.rdf.model.Statement; 20 | import org.apache.jena.reasoner.Reasoner; 21 | import org.apache.jena.reasoner.rulesys.GenericRuleReasoner; 22 | import org.apache.jena.reasoner.rulesys.Rule; 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | 26 | import idlab.massif.interfaces.core.FilterInf; 27 | import idlab.massif.interfaces.core.ListenerInf; 28 | import idlab.massif.utils.FormatUtils; 29 | import idlab.massif.utils.RDFUtils; 30 | 31 | public class JenaFilter implements FilterInf { 32 | 33 | protected Model infModel; 34 | protected List queries; 35 | protected List queryStrings; 36 | private ListenerInf listener; 37 | protected String rules; 38 | protected List dataSources; 39 | private final Logger logger = LoggerFactory.getLogger(this.getClass()); 40 | 41 | public JenaFilter() { 42 | this.infModel = ModelFactory.createDefaultModel(); 43 | this.queries = new ArrayList(); 44 | this.queryStrings = new ArrayList(); 45 | this.dataSources = new ArrayList(); 46 | } 47 | 48 | 49 | @Override 50 | public boolean addEvent(String event) { 51 | logger.debug("Received message: " + event); 52 | // add the data 53 | try { 54 | 55 | List statements = RDFUtils.parseTriples(event); 56 | 57 | infModel.add(statements); 58 | System.out.println("Query model size: " + infModel.size()); 59 | 60 | // execute the query 61 | int queryId = 0; 62 | for (Query query : queries) { 63 | 64 | try (QueryExecution qexec = QueryExecutionFactory.create(query, infModel)) { 65 | 66 | if (!query.isSelectType()) { 67 | // check if quad construct or normal construct 68 | if (query.isConstructQuad()) { 69 | Dataset results = qexec.execConstructDataset(); 70 | Iterator names = results.listNames(); 71 | while (names.hasNext()) { 72 | String name = names.next(); 73 | Model m = results.getNamedModel(name); 74 | String resultString = RDFUtils.modelToString(m); 75 | // notify the listener 76 | notifyListener(queryId, resultString); 77 | 78 | } 79 | 80 | } else { 81 | Model result = qexec.execConstruct(); 82 | if (!result.isEmpty()) { 83 | String resultString = RDFUtils.modelToString(result); 84 | // notify the listener 85 | notifyListener(queryId, resultString); 86 | } 87 | } 88 | } else { 89 | 90 | ResultSet results = qexec.execSelect(); 91 | StringBuilder builder = new StringBuilder(); 92 | List vars = results.getResultVars(); 93 | for (String var : vars) { 94 | 95 | builder.append(var).append(","); 96 | } 97 | builder.append("\n"); 98 | for (; results.hasNext(); ) { 99 | QuerySolution soln = results.nextSolution(); 100 | for (String var : vars) { 101 | builder.append(soln.get(var)).append(","); 102 | } 103 | builder.append("\n"); 104 | 105 | } 106 | notifyListener(queryId, builder.toString()); 107 | } 108 | } 109 | queryId++; 110 | 111 | } 112 | 113 | // remove the data 114 | infModel.remove(statements); 115 | return true; 116 | 117 | } catch (Exception e) { 118 | // TODO Auto-generated catch block 119 | e.printStackTrace(); 120 | return false; 121 | } 122 | } 123 | 124 | private void notifyListener(int queryID, String result) { 125 | if (listener != null) { 126 | listener.notify(queryID, result); 127 | } 128 | } 129 | 130 | @Override 131 | public boolean addListener(ListenerInf listener) { 132 | this.listener = listener; 133 | return true; 134 | } 135 | 136 | @Override 137 | public int registerContinuousQuery(String queryString) { 138 | if (queries == null) { 139 | queries = new ArrayList(); 140 | queryStrings = new ArrayList(); 141 | } 142 | Query query = QueryFactory.create(queryString, Syntax.syntaxARQ); 143 | // if (!query.isConstructType()) { 144 | // logger.error("Only construct queries allowed"); 145 | // return -1; 146 | // } 147 | queries.add(query); 148 | queryStrings.add(queryString); 149 | return queries.size() - 1; 150 | } 151 | 152 | @Override 153 | public boolean setStaticData(String dataSource) { 154 | this.dataSources.add(dataSource); 155 | return false; 156 | } 157 | 158 | @Override 159 | public boolean setRules(String rules) { 160 | this.rules = rules; 161 | return true; 162 | } 163 | 164 | public void start() { 165 | this.infModel = ModelFactory.createDefaultModel(); 166 | 167 | try { 168 | for (String dataSource : dataSources) { 169 | if (!dataSource.isEmpty() && dataSource.endsWith("sparql")) { 170 | // fetch remote data 171 | Model remote = getRemoteData(dataSource); 172 | System.out.println("Remote size: " + remote.size()); 173 | this.infModel.add(remote); 174 | } else if (!dataSource.isEmpty()) { 175 | this.infModel.read(dataSource, "TTL"); 176 | System.out.println("local size: " + infModel.size()); 177 | } 178 | } 179 | if (rules != null) { 180 | 181 | Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(rules)); 182 | InfModel model = ModelFactory.createInfModel(reasoner, this.infModel); 183 | model.prepare(); 184 | this.infModel = model; 185 | System.out.println("With rules size: " + infModel.size()); 186 | } 187 | System.out.println("final size: " + infModel.size()); 188 | } catch (Exception e) { 189 | // TODO Auto-generated catch block 190 | e.printStackTrace(); 191 | } 192 | } 193 | 194 | private Model getRemoteData(String dataSource) { 195 | QueryExecution q = QueryExecutionFactory.sparqlService(dataSource, "CONSTRUCT {?s ?p ?o } WHERE {?s ?p ?o .} "); 196 | Model result = q.execConstruct(); 197 | return result; 198 | } 199 | 200 | @Override 201 | public void stop() { 202 | // TODO Auto-generated method stub 203 | this.infModel.removeAll(); 204 | } 205 | 206 | @Override 207 | public String toString() { 208 | StringBuilder str = new StringBuilder(); 209 | 210 | str.append("{\"type\":\"Filter\",\"impl\":\"jena\",\"ontology\":\"" + String.join(",", this.dataSources) 211 | + "\",\"queries\":["); 212 | for (int i = 0; i < queryStrings.size(); i++) { 213 | str.append("\"").append( 214 | FormatUtils.encodeQuery(queryStrings.get(i).toString())) 215 | .append("\""); 216 | if (i < queryStrings.size() - 1) { 217 | str.append(","); 218 | } 219 | } 220 | str.append("]}"); 221 | return str.toString(); 222 | } 223 | 224 | } 225 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/functions/FunctionManyParTest.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.functions; 2 | 3 | import java.util.Collections; 4 | import java.util.List; 5 | 6 | import org.apache.jena.sparql.expr.ExprList; 7 | import org.apache.jena.sparql.expr.NodeValue; 8 | import org.apache.jena.sparql.function.Function; 9 | import org.apache.jena.sparql.function.FunctionBase; 10 | import org.apache.jena.sparql.function.FunctionFactory; 11 | import org.apache.jena.sparql.function.FunctionRegistry; 12 | 13 | import idlab.massif.core.PipeLineComponent; 14 | import idlab.massif.filter.jena.JenaFilter; 15 | import idlab.massif.sinks.PrintSink; 16 | 17 | public class FunctionManyParTest extends FunctionBase { 18 | 19 | public static void main(String[] args) { 20 | 21 | 22 | FunctionRegistry.get().put("http://example.org/function#myFunction", FunctionManyParTest.class) ; 23 | PrintSink sink = new PrintSink(); 24 | JenaFilter filter = new JenaFilter(); 25 | filter.registerContinuousQuery("PREFIX f: \n" + 26 | "Select ?s (f:myFunction(?s, ?o,?p,?s, ?o,?p) as ?t) WHERE {?s ?p ?o. }"); 27 | PipeLineComponent filterComp = new PipeLineComponent(filter, Collections.emptyList()); 28 | 29 | filterComp.setOutput(Collections.singletonList(new PipeLineComponent(sink, Collections.emptyList()))); 30 | 31 | filter.start(); 32 | filter.addEvent(input); 33 | 34 | } 35 | 36 | @Override 37 | public NodeValue exec(List args) { 38 | // TODO Auto-generated method stub 39 | System.out.println(args); 40 | return NodeValue.FALSE; 41 | } 42 | 43 | @Override 44 | public void checkBuild(String uri, ExprList args) { 45 | // TODO Auto-generated method stub 46 | 47 | } 48 | static String input = " .\n" + 49 | " .\n" + 50 | " .\n" + 51 | " .\n" + 52 | " .\n" + 53 | " .\n" + 54 | " .\n" + 55 | " \"465.92\" ."; 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/functions/FunctionTest.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.functions; 2 | 3 | import java.util.Collections; 4 | 5 | import org.apache.jena.sparql.expr.NodeValue; 6 | import org.apache.jena.sparql.function.Function; 7 | import org.apache.jena.sparql.function.FunctionBase2; 8 | import org.apache.jena.sparql.function.FunctionFactory; 9 | import org.apache.jena.sparql.function.FunctionRegistry; 10 | 11 | import idlab.massif.core.PipeLineComponent; 12 | import idlab.massif.filter.jena.JenaFilter; 13 | import idlab.massif.sinks.PrintSink; 14 | 15 | public class FunctionTest extends FunctionBase2{ 16 | 17 | public static void main(String[] args) { 18 | 19 | FunctionFactory test = new FunctionFactory() { 20 | 21 | @Override 22 | public Function create(String uri) { 23 | // TODO Auto-generated method stub 24 | return new FunctionTest(); 25 | }}; 26 | 27 | // TODO Auto-generated method stub 28 | FunctionRegistry.get().put("http://example.org/function#myFunction", test) ; 29 | FunctionRegistry.get().put("http://example.org/function#myFunction2", test) ; 30 | PrintSink sink = new PrintSink(); 31 | JenaFilter filter = new JenaFilter(); 32 | filter.registerContinuousQuery("PREFIX f: \n" + 33 | "Select ?s (f:myFunction2(?s, ?o) as ?t) WHERE {?s ?p ?o. }"); 34 | PipeLineComponent filterComp = new PipeLineComponent(filter, Collections.emptyList()); 35 | 36 | filterComp.setOutput(Collections.singletonList(new PipeLineComponent(sink, Collections.emptyList()))); 37 | 38 | filter.start(); 39 | filter.addEvent(input); 40 | 41 | } 42 | public FunctionTest() { super() ; } 43 | public NodeValue exec(NodeValue nv1, NodeValue nv2) 44 | { 45 | long start = System.nanoTime(); 46 | System.out.println("called" +start); 47 | return NodeValue.makeDecimal(System.currentTimeMillis()); 48 | } 49 | static String input = " .\n" + 50 | " .\n" + 51 | " .\n" + 52 | " .\n" + 53 | " .\n" + 54 | " .\n" + 55 | " .\n" + 56 | " \"465.92\" ."; 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/AbstractionInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | 4 | 5 | import org.semanticweb.owlapi.model.OWLOntology; 6 | 7 | public interface AbstractionInf extends PipeLineElement{ 8 | 9 | public boolean setOntology(OWLOntology ontology); 10 | @Deprecated 11 | public boolean registerDLQuery(String newClass, String classExpression, AbstractionListenerInf listener); 12 | public int registerDLQuery(String newClass, String classExpression); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/AbstractionListenerInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | 4 | 5 | public interface AbstractionListenerInf { 6 | 7 | public void notify(String event,String eventName); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/CEPInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | import java.util.Map; 4 | import java.util.Set; 5 | 6 | 7 | public interface CEPInf extends PipeLineElement{ 8 | 9 | public boolean registerQuery(String CEPquery, Set eventTypes, CEPListener listener); 10 | public void addEvent(String event, String eventName); 11 | 12 | public int registerQuery(String complexName,String CEPquery, Set eventTypes,Map indComplexPropMapping); 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/CEPListener.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | import java.util.Map; 4 | 5 | public interface CEPListener { 6 | 7 | public void notify(Map events); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/FilterInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | public interface FilterInf extends PipeLineElement{ 4 | /** 5 | * Registers a query to the filter component. 6 | * @param query: the query to be executed. 7 | * @return query id: the id of the query 8 | */ 9 | public int registerContinuousQuery(String query); 10 | public boolean setStaticData(String dataSource); 11 | public boolean setRules(String rules); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/ListenerInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | public interface ListenerInf { 4 | 5 | public void notify(int queryID,String event); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/MapperInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | public interface MapperInf extends PipeLineElement{ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/MonitorInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | public interface MonitorInf { 4 | 5 | public long getQueueSize(); 6 | public long getEventsIn(); 7 | public long getEventsOut(); 8 | public float getThroughput(); 9 | public float getAvgTime(); 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/PipeLineElement.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | public interface PipeLineElement { 4 | public boolean addEvent(String event); 5 | public boolean addListener(ListenerInf listener); 6 | public void start(); 7 | public void stop(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/SelectionInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | import java.util.List; 4 | import java.util.Set; 5 | 6 | public interface SelectionInf { 7 | 8 | public boolean addListener(SelectionListenerInf listener); 9 | public boolean registerContinuousQuery(String query,int window, int windowSlide); 10 | public boolean setStaticData(String dataSource); 11 | public boolean setRules(String rules); 12 | public SelectionInf builtEngine(); 13 | public boolean addEvent(Set> triples); 14 | public boolean addEvent(String triples); 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/SelectionListenerInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | 4 | public interface SelectionListenerInf { 5 | 6 | public void notify(String triples); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/SinkInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | public interface SinkInf extends PipeLineElement{ 4 | 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/SourceInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | public interface SourceInf extends PipeLineElement{ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/interfaces/core/WindowInf.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.interfaces.core; 2 | 3 | public interface WindowInf extends PipeLineElement { 4 | 5 | public void setWindowSize(int window); 6 | 7 | public void setWindowSize(int windowSize, int windowSlide); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/listeners/AbstractionListener.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.listeners; 2 | 3 | import java.util.Set; 4 | 5 | import org.semanticweb.owlapi.model.OWLAxiom; 6 | 7 | import idlab.massif.interfaces.core.AbstractionListenerInf; 8 | 9 | public class AbstractionListener implements AbstractionListenerInf{ 10 | 11 | @Override 12 | public void notify(String event,String eventName) { 13 | // TODO Auto-generated method stub 14 | System.out.println(event); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/mapping/SimpleMapper.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.mapping; 2 | 3 | import idlab.massif.interfaces.core.ListenerInf; 4 | import idlab.massif.interfaces.core.MapperInf; 5 | 6 | public class SimpleMapper implements MapperInf { 7 | private String mapping; 8 | private ListenerInf listener; 9 | private boolean keepHeader = false; 10 | private boolean isFirst = true; 11 | private String header; 12 | 13 | public SimpleMapper(String mapping) { 14 | this.mapping = mapping; 15 | } 16 | 17 | public SimpleMapper(String mapping, boolean keepHeader) { 18 | this(mapping); 19 | this.keepHeader = keepHeader; 20 | } 21 | 22 | public static void main(String args[]) { 23 | String mapping = "{loc} {avg}."; 24 | String input = "loc,avg,\n" 25 | + "https://igentprojectLBD#space_d6ea3a02-082e-4966-bcbf-563e69393f96,1^^http://www.w3.org/2001/XMLSchema#integer,\n" 26 | + "https://igentprojectLBD#space_21b36f84-98e4-4689-924f-112fb8dd0925,1^^http://www.w3.org/2001/XMLSchema#integer,\n" 27 | + "https://igentprojectLBD#space_21b36f84-98e4-4689-924f-112fb8dd0558,6^^http://www.w3.org/2001/XMLSchema#integer,\n" 28 | + "https://igentprojectLBD#space_21b36f84-98e4-4689-924f-112fb8dd0cf0,1^^http://www.w3.org/2001/XMLSchema#integer,\n" 29 | + "https://igentprojectLBD#space_a00c84ce-475e-4b66-98b3-72fbdf61761e,1^^http://www.w3.org/2001/XMLSchema#integer,"; 30 | SimpleMapper mapper = new SimpleMapper(mapping); 31 | String result = mapper.map(input); 32 | System.out.println(result); 33 | } 34 | 35 | public String map(String input) { 36 | if (keepHeader) { 37 | StringBuilder builder = new StringBuilder(); 38 | input = builder.append(header).append("\n").append(input).toString(); 39 | } 40 | String[] lines = input.split("\n"); 41 | if (lines.length == 1) { 42 | lines = input.split("\\\\n"); 43 | } 44 | String result = ""; 45 | if (lines.length > 1) { 46 | String[] vars = lines[0].split(","); 47 | for (int i = 1; i < lines.length; i++) { 48 | 49 | String currentMap = new String(mapping); 50 | String[] variables = lines[i].split(","); 51 | if (variables.length == vars.length) { 52 | for (int j = 0; j < vars.length; j++) { 53 | String var = vars[j]; 54 | if (!var.equals("")) { 55 | currentMap = currentMap.replaceAll("\\{" + var +"\\}", variables[j]); 56 | } 57 | } 58 | 59 | result += currentMap; 60 | } 61 | if (i < lines.length - 1) { 62 | result += "\n"; 63 | } 64 | } 65 | } 66 | return result; 67 | } 68 | 69 | @Override 70 | public boolean addEvent(String event) { 71 | if (keepHeader && isFirst) { 72 | isFirst = false; 73 | header = event; 74 | } else { 75 | String mappedResult = this.map(event); 76 | if (listener != null) { 77 | listener.notify(0, mappedResult); 78 | } 79 | } 80 | return false; 81 | } 82 | 83 | @Override 84 | public boolean addListener(ListenerInf listener) { 85 | // TODO Auto-generated method stub 86 | this.listener = listener; 87 | return true; 88 | } 89 | 90 | @Override 91 | public void start() { 92 | // TODO Auto-generated method stub 93 | 94 | } 95 | 96 | @Override 97 | public void stop() { 98 | // TODO Auto-generated method stub 99 | 100 | } 101 | 102 | @Override 103 | public String toString() { 104 | return String.format("{\"type\":\"mapper\",\"keepHeader\":%b,\"mapping\":%s}", keepHeader, mapping); 105 | 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/selection/csparql_basic/CSparqlSelectionImpl.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.selection.csparql_basic; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.ArrayList; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Set; 9 | 10 | import org.apache.jena.query.Query; 11 | import org.apache.jena.rdf.model.InfModel; 12 | import org.apache.jena.rdf.model.Model; 13 | import org.apache.jena.rdf.model.ModelFactory; 14 | import org.apache.jena.rdf.model.ResourceFactory; 15 | import org.apache.jena.rdf.model.Statement; 16 | import org.apache.jena.rdf.model.StmtIterator; 17 | import org.apache.jena.reasoner.Reasoner; 18 | import org.apache.jena.reasoner.rulesys.GenericRuleReasoner; 19 | import org.apache.jena.reasoner.rulesys.Rule; 20 | import org.apache.jena.vocabulary.RDF; 21 | 22 | import idlab.massif.interfaces.core.SelectionInf; 23 | import idlab.massif.interfaces.core.SelectionListenerInf; 24 | 25 | public class CSparqlSelectionImpl implements SelectionInf { 26 | private SelectionListenerInf listner; 27 | private int window, windowSlide; 28 | private String dataSource; 29 | private String rules; 30 | private String query; 31 | private StreamingJena engine; 32 | 33 | public CSparqlSelectionImpl() { 34 | 35 | } 36 | 37 | @Override 38 | public boolean addListener(SelectionListenerInf listener) { 39 | this.listner = listener; 40 | return false; 41 | } 42 | 43 | @Override 44 | public boolean registerContinuousQuery(String query, int window, int windowSlide) { 45 | this.query = query; 46 | this.window = window; 47 | this.windowSlide = windowSlide; 48 | return false; 49 | } 50 | 51 | @Override 52 | public boolean setStaticData(String dataSource) { 53 | this.dataSource = dataSource; 54 | return false; 55 | } 56 | 57 | @Override 58 | public boolean setRules(String rules) { 59 | this.rules = rules; 60 | return false; 61 | } 62 | 63 | @Override 64 | public SelectionInf builtEngine() { 65 | if (dataSource != null && rules != null) { 66 | this.engine = new StreamingJena(dataSource, window + "", windowSlide + "", rules); 67 | } else { 68 | this.engine = new StreamingJena(null, window + "", windowSlide + ""); 69 | 70 | } 71 | engine.addContinuousQuery(query, listner); 72 | return this; 73 | } 74 | 75 | @Override 76 | public boolean addEvent(Set> triples) { 77 | engine.advanceTime(System.currentTimeMillis()); 78 | List statements = new ArrayList(); 79 | for (List triple : triples) { 80 | statements.add(ResourceFactory.createStatement(ResourceFactory.createResource(triple.get(0)), 81 | ResourceFactory.createProperty(triple.get(1)), ResourceFactory.createResource(triple.get(2)))); 82 | } 83 | engine.addEvent(statements); 84 | return false; 85 | } 86 | 87 | @Override 88 | public boolean addEvent(String triples) { 89 | engine.advanceTime(System.currentTimeMillis()); 90 | Model dataModel = ModelFactory.createDefaultModel(); 91 | try { 92 | InputStream targetStream = new ByteArrayInputStream(triples.getBytes()); 93 | dataModel.read(targetStream,"TTL"); 94 | StmtIterator it = dataModel.listStatements(); 95 | List statements = new ArrayList(); 96 | while (it.hasNext()) { 97 | statements.add(it.next()); 98 | } 99 | engine.addEvent(statements); 100 | return true; 101 | 102 | } catch (Exception e) { 103 | // TODO Auto-generated catch block 104 | e.printStackTrace(); 105 | return false; 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/selection/csparql_basic/DBPediaTest.java: -------------------------------------------------------------------------------- 1 | // JRDFox(c) Copyright University of Oxford, 2013. All Rights Reserved. 2 | 3 | package idlab.massif.selection.csparql_basic; 4 | 5 | import java.io.BufferedReader; 6 | import java.io.InputStream; 7 | import java.io.InputStreamReader; 8 | import java.net.URI; 9 | import java.util.stream.Collectors; 10 | 11 | 12 | import org.semanticweb.owlapi.apibinding.OWLManager; 13 | import org.semanticweb.owlapi.model.OWLOntology; 14 | import org.semanticweb.owlapi.model.OWLOntologyManager; 15 | 16 | 17 | 18 | public class DBPediaTest { 19 | 20 | public static void main(String[] args) throws Exception { 21 | 22 | String windowSize = args[0]; 23 | String dataset = args[1]; 24 | String url = args[2]; 25 | String rules =args[3]; 26 | 27 | String query = "PREFIX : " 28 | + "Select * " 29 | //+ "{?work .} " 30 | + "WHERE { ?work . }"; 31 | //"/tmp/jasper2/rdfs-rules-rhodf.rules" 32 | if(args.length>4){ 33 | query = "PREFIX : " 34 | + "Select * " 35 | //+ "{?work .} " 36 | + "WHERE { ?work . }"; 37 | } 38 | if(args.length>5){ 39 | query = args[5]; 40 | } 41 | 42 | StreamingJena reasoner = new StreamingJena(dataset,windowSize,windowSize,rules); 43 | reasoner.addContinuousQuery(query); 44 | 45 | 46 | } 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/selection/csparql_basic/JenaReasoner.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.selection.csparql_basic; 2 | 3 | 4 | import java.io.StringWriter; 5 | import java.util.ArrayList; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | import org.apache.jena.query.Query; 11 | import org.apache.jena.query.QueryExecution; 12 | import org.apache.jena.query.QueryExecutionFactory; 13 | import org.apache.jena.query.QueryFactory; 14 | import org.apache.jena.query.QuerySolution; 15 | import org.apache.jena.query.ResultSet; 16 | import org.apache.jena.rdf.model.InfModel; 17 | import org.apache.jena.rdf.model.Model; 18 | import org.apache.jena.rdf.model.ModelFactory; 19 | import org.apache.jena.rdf.model.Statement; 20 | import org.apache.jena.reasoner.Reasoner; 21 | import org.apache.jena.reasoner.rulesys.GenericRuleReasoner; 22 | import org.apache.jena.reasoner.rulesys.Rule; 23 | 24 | import idlab.massif.interfaces.core.SelectionListenerInf; 25 | 26 | 27 | 28 | /** 29 | * @author pbonte 30 | * 31 | */ 32 | public class JenaReasoner { 33 | private Model infModel; 34 | private List queries; 35 | private Map listenerMap; 36 | 37 | public JenaReasoner() { 38 | this.infModel = ModelFactory.createDefaultModel(); 39 | this.listenerMap= new HashMap(); 40 | 41 | } 42 | public JenaReasoner(String staticOntFile){ 43 | Model dataModel = ModelFactory.createDefaultModel(); 44 | try { 45 | ClassLoader classloader = Thread.currentThread().getContextClassLoader(); 46 | 47 | dataModel.read(staticOntFile, "RDF/XML"); 48 | } catch (Exception e) { 49 | // TODO Auto-generated catch block 50 | e.printStackTrace(); 51 | } 52 | this.infModel = dataModel; 53 | this.listenerMap= new HashMap(); 54 | 55 | } 56 | public JenaReasoner(String staticOntFile,String rules){ 57 | Model dataModel = ModelFactory.createDefaultModel(); 58 | try { 59 | ClassLoader classloader = Thread.currentThread().getContextClassLoader(); 60 | 61 | dataModel.read(staticOntFile, "RDF/XML"); 62 | Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(rules)); 63 | InfModel model= ModelFactory.createInfModel(reasoner, dataModel); 64 | model.prepare(); 65 | this.infModel=model; 66 | this.listenerMap= new HashMap(); 67 | 68 | } catch (Exception e) { 69 | // TODO Auto-generated catch block 70 | e.printStackTrace(); 71 | } 72 | 73 | 74 | } 75 | public Query addContinuousQuery(String queryString){ 76 | if(queries == null){ 77 | queries = new ArrayList(); 78 | } 79 | Query query = QueryFactory.create(queryString); 80 | queries.add(query); 81 | return query; 82 | } 83 | public void addContinuousQuery(String queryString, SelectionListenerInf listener){ 84 | Query query = addContinuousQuery(queryString); 85 | listenerMap.put(query,listener); 86 | } 87 | 88 | 89 | 90 | public void setupSimpleAdd(List add) { 91 | infModel.add(add); 92 | } 93 | public void setupSimpleDelete(List delete) { 94 | infModel.remove(delete); 95 | 96 | } 97 | 98 | 99 | 100 | public List query() { 101 | long counter = 0; 102 | List results = new ArrayList(); 103 | for (Query query : queries) { 104 | if(query.isSelectType()){ 105 | try (QueryExecution qexec = QueryExecutionFactory.create(query, infModel)) { 106 | ResultSet result = qexec.execSelect(); 107 | while(result.hasNext()){ 108 | counter++; 109 | QuerySolution sol = result.nextSolution(); 110 | System.out.println(sol); 111 | } 112 | } 113 | }else{ 114 | try (QueryExecution qexec = QueryExecutionFactory.create(query, infModel)) { 115 | Model result = qexec.execConstruct(); 116 | String syntax = "TURTLE"; // also try "N-TRIPLE" and "TURTLE" 117 | StringWriter out = new StringWriter(); 118 | result.write(out, syntax); 119 | String resultString = out.toString(); 120 | if(listenerMap.containsKey(query)) { 121 | listenerMap.get(query).notify(resultString); 122 | } 123 | } 124 | } 125 | } 126 | System.out.println("#Results:\t"+ counter); 127 | return results; 128 | } 129 | 130 | 131 | } 132 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/selection/csparql_basic/JenaReasonerOWL2RL.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.selection.csparql_basic; 2 | 3 | 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.FileNotFoundException; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | import org.apache.jena.query.Query; 11 | import org.apache.jena.query.QueryExecution; 12 | import org.apache.jena.query.QueryExecutionFactory; 13 | import org.apache.jena.query.QueryFactory; 14 | import org.apache.jena.query.QuerySolution; 15 | import org.apache.jena.query.ResultSet; 16 | import org.apache.jena.rdf.model.InfModel; 17 | import org.apache.jena.rdf.model.Model; 18 | import org.apache.jena.rdf.model.ModelFactory; 19 | import org.apache.jena.rdf.model.Statement; 20 | import org.apache.jena.reasoner.rulesys.GenericRuleReasoner; 21 | import org.apache.jena.reasoner.rulesys.Rule; 22 | 23 | 24 | 25 | /** 26 | * @author pbonte 27 | * 28 | */ 29 | public class JenaReasonerOWL2RL { 30 | private InfModel infModel; 31 | private List queries; 32 | 33 | 34 | public JenaReasonerOWL2RL(String staticOntFile,String ontRules){ 35 | GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(ontRules)); 36 | reasoner.setMode(GenericRuleReasoner.FORWARD_RETE); 37 | reasoner.setDerivationLogging(true); 38 | Model dataModel = ModelFactory.createDefaultModel(); 39 | try { 40 | dataModel.read(new FileInputStream(new File(staticOntFile)), "RDF/XML"); 41 | System.out.println(dataModel.size()); 42 | } catch (FileNotFoundException e) { 43 | // TODO Auto-generated catch block 44 | e.printStackTrace(); 45 | } 46 | this.infModel = ModelFactory.createInfModel(reasoner, dataModel); 47 | System.out.println(infModel.size()); 48 | infModel.prepare(); 49 | System.out.println(infModel.size()); 50 | 51 | } 52 | public void addContinuousQuery(String queryString){ 53 | if(queries == null){ 54 | queries = new ArrayList(); 55 | } 56 | Query query = QueryFactory.create(queryString); 57 | queries.add(query); 58 | } 59 | 60 | 61 | 62 | public void setupSimpleAdd(List add) { 63 | 64 | infModel.add(add); 65 | } 66 | public void setupSimpleDelete(List delete) { 67 | infModel.remove(delete); 68 | 69 | } 70 | 71 | 72 | 73 | public List query() { 74 | long counter = 0; 75 | List results = new ArrayList(); 76 | for (Query query : queries) { 77 | try (QueryExecution qexec = QueryExecutionFactory.create(query, infModel)) { 78 | ResultSet result = qexec.execSelect(); 79 | while(result.hasNext()){ 80 | counter++; 81 | QuerySolution sol = result.nextSolution(); 82 | //System.out.println(sol); 83 | } 84 | } 85 | } 86 | System.out.println("#Results:\t"+ counter); 87 | return results; 88 | } 89 | 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/selection/csparql_basic/StreamingJena.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package idlab.massif.selection.csparql_basic; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.apache.jena.rdf.model.Model; 10 | import org.apache.jena.rdf.model.Statement; 11 | 12 | import com.espertech.esper.client.Configuration; 13 | import com.espertech.esper.client.EPAdministrator; 14 | import com.espertech.esper.client.EPRuntime; 15 | import com.espertech.esper.client.EPServiceProvider; 16 | import com.espertech.esper.client.EPServiceProviderManager; 17 | import com.espertech.esper.client.EPStatement; 18 | import com.espertech.esper.client.EventBean; 19 | import com.espertech.esper.client.UpdateListener; 20 | import com.espertech.esper.client.deploy.EPDeploymentAdmin; 21 | import com.espertech.esper.client.time.CurrentTimeEvent; 22 | 23 | import idlab.massif.interfaces.core.SelectionListenerInf; 24 | import idlab.massif.selection.csparql_basic.utils.GraphEvent; 25 | 26 | 27 | /** 28 | * @author pbonte 29 | * 30 | */ 31 | public class StreamingJena { 32 | 33 | private EPRuntime cepRT; 34 | private long counter = 0; 35 | private JenaReasoner reasoner; 36 | private boolean isDone=false; 37 | 38 | public StreamingJena(String ontologyFile, String windowSize, String windowSlide,String rules) { 39 | this.reasoner = new JenaReasoner(ontologyFile,rules); 40 | Configuration cep_config = new Configuration(); 41 | 42 | cep_config.getEngineDefaults().getThreading().setInternalTimerEnabled(false); 43 | cep_config.getEngineDefaults().getLogging().setEnableExecutionDebug(true); 44 | cep_config.getEngineDefaults().getLogging().setEnableTimerDebug(true); 45 | cep_config.getEngineDefaults().getLogging().setEnableQueryPlan(true); 46 | cep_config.getEngineDefaults().getMetricsReporting().setEnableMetricsReporting(true); 47 | cep_config.getEngineDefaults().getLogging().setEnableQueryPlan(true); 48 | 49 | EPServiceProvider cep = EPServiceProviderManager.getDefaultProvider(cep_config); 50 | this.cepRT = cep.getEPRuntime(); 51 | EPAdministrator cepAdm = cep.getEPAdministrator(); 52 | EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(); 53 | EPDeploymentAdmin deployAdmin = cepAdm.getDeploymentAdmin(); 54 | ClassLoader classLoader = StreamingJena.class.getClassLoader(); 55 | cep.getEPAdministrator().getConfiguration().addEventType(GraphEvent.class); 56 | 57 | String eplStatement = String.format("select * from GraphEvent#time(%s sec) output snapshot every %s seconds", 58 | windowSize, windowSlide); 59 | 60 | try { 61 | EPStatement statement = cepAdm.createEPL(eplStatement); 62 | 63 | statement.addListener(new UpdateListener() { 64 | public void update(EventBean[] newEvents, EventBean[] oldEvents) { 65 | String fullMsg = ""; 66 | if (newEvents != null) { 67 | try { 68 | long time1 = System.currentTimeMillis(); 69 | StringBuilder builder = new StringBuilder(); 70 | System.out.println("IN:\t" + System.currentTimeMillis()); 71 | System.out.println("#events:\t" + newEvents.length); 72 | int counter = 0; 73 | List add = new ArrayList(); 74 | for (EventBean e : newEvents) { 75 | counter++; 76 | add.addAll((List) e.get("triples")); 77 | 78 | 79 | } 80 | reasoner.setupSimpleAdd(add); 81 | System.out.println("Added"); 82 | System.out.println("Start query"); 83 | reasoner.query(); 84 | System.out.println("done query"); 85 | // String result = builder.toString(); 86 | // System.out.println("Result lenght:" + 87 | // result.length()); 88 | // reasoner.setupAdd(result); 89 | counter = 0; 90 | builder = new StringBuilder(); 91 | 92 | reasoner.setupSimpleDelete(add); 93 | 94 | System.out.println("Processing Time:\t" + (System.currentTimeMillis() - time1)); 95 | System.out.println("Setup complete"); 96 | System.out.println("OUT:\t" + System.currentTimeMillis()); 97 | System.out.println("OUTNANO:\t" + System.nanoTime()); 98 | if(add.size()==0&&isDone){ 99 | System.out.println("Done at:\t"+System.nanoTime()); 100 | System.exit(0); 101 | } 102 | } catch (Exception e) { 103 | e.printStackTrace(); 104 | } 105 | } 106 | 107 | } 108 | }); 109 | } catch (Exception e1) { 110 | // TODO Auto-generated catch block 111 | e1.printStackTrace(); 112 | } 113 | // cepRT.sendEvent(new CurrentTimeEvent(System.currentTimeMillis())); 114 | 115 | long time1 = System.currentTimeMillis(); 116 | 117 | } 118 | public StreamingJena(String ontologyFile, String windowSize, String windowSlide) { 119 | if(ontologyFile!=null) { 120 | this.reasoner = new JenaReasoner(ontologyFile); 121 | 122 | }else { 123 | this.reasoner = new JenaReasoner(); 124 | } 125 | Configuration cep_config = new Configuration(); 126 | 127 | cep_config.getEngineDefaults().getThreading().setInternalTimerEnabled(false); 128 | cep_config.getEngineDefaults().getLogging().setEnableExecutionDebug(true); 129 | cep_config.getEngineDefaults().getLogging().setEnableTimerDebug(true); 130 | cep_config.getEngineDefaults().getLogging().setEnableQueryPlan(true); 131 | cep_config.getEngineDefaults().getMetricsReporting().setEnableMetricsReporting(true); 132 | cep_config.getEngineDefaults().getLogging().setEnableQueryPlan(true); 133 | 134 | EPServiceProvider cep = EPServiceProviderManager.getDefaultProvider(cep_config); 135 | this.cepRT = cep.getEPRuntime(); 136 | EPAdministrator cepAdm = cep.getEPAdministrator(); 137 | EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(); 138 | EPDeploymentAdmin deployAdmin = cepAdm.getDeploymentAdmin(); 139 | ClassLoader classLoader = StreamingJena.class.getClassLoader(); 140 | cep.getEPAdministrator().getConfiguration().addEventType(GraphEvent.class); 141 | 142 | String eplStatement = String.format("select * from GraphEvent#time(%s sec) output snapshot every %s seconds", 143 | windowSize, windowSlide); 144 | 145 | try { 146 | EPStatement statement = cepAdm.createEPL(eplStatement); 147 | 148 | statement.addListener(new UpdateListener() { 149 | public void update(EventBean[] newEvents, EventBean[] oldEvents) { 150 | String fullMsg = ""; 151 | System.out.println("triggering"); 152 | if (newEvents != null) { 153 | try { 154 | long time1 = System.currentTimeMillis(); 155 | StringBuilder builder = new StringBuilder(); 156 | System.out.println("IN:\t" + System.currentTimeMillis()); 157 | System.out.println("#events:\t" + newEvents.length); 158 | int counter = 0; 159 | List add = new ArrayList(); 160 | for (EventBean e : newEvents) { 161 | counter++; 162 | add.addAll((List) e.get("triples")); 163 | 164 | } 165 | reasoner.setupSimpleAdd(add); 166 | System.out.println("Added"); 167 | System.out.println("Start query"); 168 | reasoner.query(); 169 | System.out.println("done query"); 170 | // String result = builder.toString(); 171 | // System.out.println("Result lenght:" + 172 | // result.length()); 173 | // reasoner.setupAdd(result); 174 | counter = 0; 175 | builder = new StringBuilder(); 176 | 177 | reasoner.setupSimpleDelete(add); 178 | 179 | System.out.println("Processing Time:\t" + (System.currentTimeMillis() - time1)); 180 | System.out.println("Setup complete"); 181 | System.out.println("OUT:\t" + System.currentTimeMillis()); 182 | System.out.println("OUTNANO:\t" + System.nanoTime()); 183 | if(add.size()==0&&isDone){ 184 | System.out.println("Done at:\t"+System.nanoTime()); 185 | System.exit(0); 186 | } 187 | } catch (Exception e) { 188 | e.printStackTrace(); 189 | } 190 | } 191 | 192 | } 193 | }); 194 | } catch (Exception e1) { 195 | // TODO Auto-generated catch block 196 | e1.printStackTrace(); 197 | } 198 | // cepRT.sendEvent(new CurrentTimeEvent(System.currentTimeMillis())); 199 | 200 | long time1 = System.currentTimeMillis(); 201 | 202 | } 203 | public void setDone(){ 204 | this.isDone=true; 205 | } 206 | 207 | public void addEvent(List event) { 208 | cepRT.sendEvent(new GraphEvent(counter++, event)); 209 | } 210 | 211 | public void advanceTime(long time) { 212 | cepRT.sendEvent(new CurrentTimeEvent(time)); 213 | } 214 | 215 | public void addContinuousQuery(String query) { 216 | reasoner.addContinuousQuery(query); 217 | } 218 | public void addContinuousQuery(String query,SelectionListenerInf listener) { 219 | reasoner.addContinuousQuery(query,listener); 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/selection/csparql_basic/utils/EventReader.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package idlab.massif.selection.csparql_basic.utils; 5 | 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | import java.nio.file.Files; 9 | import java.nio.file.Paths; 10 | import java.util.ArrayList; 11 | import java.util.HashMap; 12 | import java.util.List; 13 | import java.util.Map; 14 | import java.util.stream.Stream; 15 | 16 | import org.apache.jena.rdf.model.ResourceFactory; 17 | import org.apache.jena.rdf.model.Statement; 18 | 19 | import com.github.jsonldjava.core.JsonLdOptions; 20 | import com.github.jsonldjava.core.JsonLdProcessor; 21 | import com.github.jsonldjava.utils.JsonUtils; 22 | 23 | /** 24 | * @author pbonte 25 | * 26 | */ 27 | public class EventReader { 28 | 29 | 30 | private List jsonld; 31 | public EventReader(String fileLocation) { 32 | this.jsonld = new ArrayList(); 33 | try (Stream stream = Files.lines(Paths.get(fileLocation))) { 34 | 35 | stream.forEach(a -> jsonld.add(a)); 36 | 37 | } catch (IOException e) { 38 | e.printStackTrace(); 39 | } 40 | } 41 | public List getStringStatements(int numEvents){ 42 | List stmnts = new ArrayList(); 43 | for(int i=0;i>>) ((Map) compact).get("@default")); 80 | } catch (Exception e) { 81 | // TODO Auto-generated catch block 82 | e.printStackTrace(); 83 | } 84 | return ""; 85 | } 86 | 87 | private String toTTL(List>> tripleList) { 88 | StringBuilder builder = new StringBuilder(); 89 | int i = 0; 90 | for (Map> tripleMap : tripleList) { 91 | builder.append(tripleMap.get("subject").get("value")).append(" ") 92 | .append(tripleMap.get("predicate").get("value")).append(" "); 93 | 94 | if (tripleMap.get("object").get("type").equals("literal")) { 95 | builder.append("\"").append(tripleMap.get("object").get("value")).append("\"") 96 | // .append("^^") 97 | // .append("") 98 | 99 | // append("<").append(tripleMap.get("object").get("datatype")).append(">") 100 | ; 101 | 102 | } else { 103 | builder.append(tripleMap.get("object").get("value")); 104 | } 105 | builder.append(" .\n"); 106 | 107 | } 108 | 109 | return builder.toString(); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/selection/csparql_basic/utils/GraphEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package idlab.massif.selection.csparql_basic.utils; 5 | 6 | import java.util.List; 7 | 8 | import org.apache.jena.rdf.model.Model; 9 | import org.apache.jena.rdf.model.Statement; 10 | 11 | /** 12 | * @author pbonte 13 | * 14 | */ 15 | public class GraphEvent { 16 | 17 | private long id; 18 | private List triples; 19 | 20 | public GraphEvent(long id, List triples){ 21 | this.id = id; 22 | this.triples=triples; 23 | } 24 | public List getTriples(){ 25 | return triples; 26 | } 27 | public long getId(){ 28 | return id; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/selection/csparql_basic/utils/OWLUtils.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.selection.csparql_basic.utils; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.util.Collections; 7 | import java.util.HashSet; 8 | import java.util.Set; 9 | import java.util.stream.Collectors; 10 | import java.util.stream.Stream; 11 | 12 | import org.semanticweb.owlapi.model.AddImport; 13 | import org.semanticweb.owlapi.model.IRI; 14 | import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; 15 | import org.semanticweb.owlapi.model.OWLAxiom; 16 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; 17 | import org.semanticweb.owlapi.model.OWLDataFactory; 18 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; 19 | import org.semanticweb.owlapi.model.OWLNamedIndividual; 20 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; 21 | import org.semanticweb.owlapi.model.OWLOntology; 22 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; 23 | import org.semanticweb.owlapi.model.OWLOntologyManager; 24 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; 25 | import org.semanticweb.owlapi.util.OWLEntityRemover; 26 | 27 | public class OWLUtils { 28 | 29 | /** 30 | * Finds the overlapping axioms in an axiom set and an ontology 31 | * 32 | * @param ontology 33 | * @param newEvent 34 | * @return 35 | */ 36 | public static Set getOverlappingAxioms(OWLOntology ontology, Set newEvent) { 37 | // Find updated object and data properties. 38 | Set overlappingAxioms = new HashSet(); 39 | for (OWLAxiom ax : newEvent) { 40 | if (ontology.containsAxiom(ax)) { 41 | overlappingAxioms.add(ax); 42 | } 43 | } 44 | return overlappingAxioms; 45 | } 46 | 47 | /** 48 | * Finds the axioms in an axiom set that are not present in an ontology 49 | * 50 | * @param ontology 51 | * @param newEvent 52 | * @return 53 | */ 54 | public static Set getUniqueAxioms(OWLOntology ontology, Set newEvent) { 55 | // Find updated object and data properties. 56 | Set uniqueAxioms = new HashSet(); 57 | for (OWLAxiom ax : newEvent) { 58 | if (!ontology.containsAxiom(ax)) { 59 | uniqueAxioms.add(ax); 60 | } 61 | } 62 | return uniqueAxioms; 63 | } 64 | 65 | public static OWLOntology copyOntology(OWLOntology baseOntology) { 66 | OWLOntologyManager manger = baseOntology.getOWLOntologyManager(); 67 | IRI ontIRI = IRI.create(baseOntology.getOntologyID().getOntologyIRI().toString() + "_copy"); 68 | try { 69 | OWLOntology ontology = manger.createOntology(ontIRI); 70 | 71 | // add imports 72 | OWLDataFactory factory = manger.getOWLDataFactory(); 73 | baseOntology.getImportsDeclarations().forEach(i -> manger.applyChange(new AddImport(ontology, i))); 74 | for (OWLOntology importOnt : baseOntology.getImports()) { 75 | // OWLImportsDeclaration id = 76 | // factory.getOWLImportsDeclaration(importOnt.getOntologyID().getOntologyIRI().get()); 77 | // AddImport ai = new AddImport(ontology, id); 78 | // manger.applyChange(ai); 79 | manger.addAxioms(ontology, importOnt.getAxioms()); 80 | } 81 | manger.applyChange(new AddImport(ontology, manger.getOWLDataFactory() 82 | .getOWLImportsDeclaration(IRI.create("http://orca.test/ontology/TaskAccio.owl")))); 83 | // add defined axioms 84 | // manger.addAxioms(ontology, baseOntology.getAxioms()); 85 | return ontology; 86 | } catch (OWLOntologyCreationException e) { 87 | e.printStackTrace(); 88 | } 89 | return null; 90 | 91 | } 92 | 93 | public static OWLOntology removeABox(OWLOntology ontology) { 94 | OWLOntologyManager manger = ontology.getOWLOntologyManager(); 95 | 96 | OWLDataFactory factory = manger.getOWLDataFactory(); 97 | OWLEntityRemover remover = new OWLEntityRemover(ontology); 98 | for (OWLNamedIndividual ind : ontology.getIndividualsInSignature()) { 99 | remover.visit(ind); 100 | // or ind.accept(remover); 101 | } 102 | manger.applyChanges(remover.getChanges()); 103 | return ontology; 104 | } 105 | 106 | public static void saveOntology(OWLOntology ontology, String name) { 107 | if (ontology != null) { 108 | String location = "/tmp/massif/"; 109 | try { 110 | File file = new File(location + name); 111 | if (!file.canExecute()) { 112 | File mkdir = new File(location); 113 | mkdir.mkdirs(); 114 | } 115 | file.createNewFile(); 116 | ontology.getOWLOntologyManager().saveOntology(ontology, 117 | new FileOutputStream(file)); 118 | } catch (OWLOntologyStorageException | IOException e) { 119 | e.printStackTrace(); 120 | } 121 | } 122 | 123 | } 124 | public static void removeOnlyAbox(OWLOntology ontology,Set axioms){ 125 | Set filtered = axioms.stream().filter(a-> (a instanceof OWLClassAssertionAxiom || 126 | a instanceof OWLDataPropertyAssertionAxiom|| 127 | a instanceof OWLObjectPropertyAssertionAxiom|| 128 | a instanceof OWLAnnotationAssertionAxiom)).collect(Collectors.toSet()); 129 | ontology.getOWLOntologyManager().removeAxioms(ontology, filtered); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sinks/HTTPGetCombinedSink.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sinks; 2 | 3 | import static spark.Spark.get; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Collections; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | import org.json.JSONArray; 12 | 13 | import idlab.massif.interfaces.core.ListenerInf; 14 | import idlab.massif.interfaces.core.SinkInf; 15 | import spark.route.Routes; 16 | 17 | class HTTPHandlerSingleton { 18 | private static HTTPHandlerSingleton single_instance = null; 19 | 20 | private Map> eventSourceMap; 21 | private Map sourceConfigMap; 22 | 23 | // private constructor restricted to this class itself 24 | private HTTPHandlerSingleton() { 25 | get("/httpgetsink/:id", (req, res) -> prepData(req.params("id"))); 26 | sourceConfigMap = new HashMap(); 27 | eventSourceMap = new HashMap>(); 28 | } 29 | 30 | // static method to create instance of Singleton class 31 | public static HTTPHandlerSingleton getInstance() { 32 | if (single_instance == null) 33 | single_instance = new HTTPHandlerSingleton(); 34 | 35 | return single_instance; 36 | } 37 | 38 | private synchronized String prepData(String id) { 39 | List lastEvents = null; 40 | if (eventSourceMap.containsKey(id)) { 41 | lastEvents = eventSourceMap.get(id); 42 | } else { 43 | lastEvents = Collections.emptyList(); 44 | } 45 | String jsonString = new JSONArray(lastEvents).toString(); 46 | lastEvents.clear(); 47 | return jsonString; 48 | } 49 | 50 | public void registerRouter(String id) { 51 | eventSourceMap.put(id, new ArrayList()); 52 | } 53 | 54 | public void setConfig(String id, String config) { 55 | sourceConfigMap.put(id, config); 56 | } 57 | 58 | public synchronized void add(String id, String event) { 59 | if (eventSourceMap.containsKey(id)) { 60 | if (sourceConfigMap.get(id).equals("last")) { 61 | eventSourceMap.get(id).clear(); 62 | } 63 | eventSourceMap.get(id).add(event); 64 | } 65 | } 66 | 67 | public void removeRoute(String id) { 68 | eventSourceMap.remove(id); 69 | sourceConfigMap.remove(id); 70 | } 71 | } 72 | 73 | public class HTTPGetCombinedSink implements SinkInf { 74 | private String path; 75 | private HTTPHandlerSingleton handler; 76 | private String config; 77 | public HTTPGetCombinedSink(String path) { 78 | handler = HTTPHandlerSingleton.getInstance(); 79 | this.path = path; 80 | handler.registerRouter(path); 81 | handler.setConfig(path, "last"); 82 | this.config =""; 83 | } 84 | 85 | public HTTPGetCombinedSink(String path, String config) { 86 | this(path); 87 | handler.setConfig(path, config); 88 | this.config = config; 89 | } 90 | 91 | @Override 92 | public boolean addEvent(String event) { 93 | handler.add(path, event); 94 | 95 | return true; 96 | } 97 | 98 | @Override 99 | public boolean addListener(ListenerInf listener) { 100 | // TODO Auto-generated method stub 101 | return false; 102 | } 103 | 104 | @Override 105 | public void start() { 106 | // TODO Auto-generated method stub 107 | 108 | } 109 | 110 | @Override 111 | public void stop() { 112 | // TODO Auto-generated method stub 113 | handler.removeRoute(path); 114 | } 115 | @Override 116 | public String toString() { 117 | return String.format("{\"type\":\"Sink\",\"impl\":\"httpGetSinkCombined\",\"path\":\"%s\",\"config\":\"%s\"}",path,config); 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sinks/HTTPGetSink.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sinks; 2 | 3 | import static spark.Spark.get; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import org.json.JSONArray; 9 | 10 | import idlab.massif.interfaces.core.ListenerInf; 11 | import idlab.massif.interfaces.core.SinkInf; 12 | import spark.route.Routes; 13 | 14 | public class HTTPGetSink implements SinkInf{ 15 | private List lastEvents; 16 | private String config ="last"; 17 | private String path; 18 | 19 | public HTTPGetSink(String path) { 20 | lastEvents = new ArrayList(); 21 | get("/"+path, (req, res) -> prepData()); 22 | this.path = path; 23 | } 24 | public HTTPGetSink(String path,String config) { 25 | this(path); 26 | if(!config.isEmpty()) { 27 | this.config = config; 28 | } 29 | } 30 | 31 | private String prepData() { 32 | String jsonString = new JSONArray(lastEvents).toString(); 33 | lastEvents.clear(); 34 | return jsonString; 35 | } 36 | @Override 37 | public boolean addEvent(String event) { 38 | if(config.equals("last")) { 39 | lastEvents.clear(); 40 | } 41 | lastEvents.add( event); 42 | 43 | return true; 44 | } 45 | 46 | @Override 47 | public boolean addListener(ListenerInf listener) { 48 | // TODO Auto-generated method stub 49 | return false; 50 | } 51 | 52 | @Override 53 | public void start() { 54 | // TODO Auto-generated method stub 55 | 56 | } 57 | @Override 58 | public void stop() { 59 | // TODO Auto-generated method stub 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sinks/HTTPPostSink.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sinks; 2 | 3 | import java.io.IOException; 4 | import java.io.UnsupportedEncodingException; 5 | 6 | import org.apache.http.client.methods.CloseableHttpResponse; 7 | import org.apache.http.client.methods.HttpPost; 8 | import org.apache.http.entity.StringEntity; 9 | import org.apache.http.impl.client.CloseableHttpClient; 10 | import org.apache.http.impl.client.HttpClients; 11 | 12 | import idlab.massif.interfaces.core.ListenerInf; 13 | import idlab.massif.interfaces.core.SinkInf; 14 | 15 | public class HTTPPostSink implements SinkInf { 16 | 17 | private String url; 18 | 19 | public HTTPPostSink(String url) { 20 | this.url = url; 21 | } 22 | 23 | @Override 24 | public boolean addEvent(String result) { 25 | try { 26 | 27 | CloseableHttpClient client = HttpClients.createDefault(); 28 | HttpPost httpPost = new HttpPost(url); 29 | 30 | StringEntity entity = new StringEntity(result); 31 | httpPost.setEntity(entity); 32 | 33 | CloseableHttpResponse response = client.execute(httpPost); 34 | client.close(); 35 | 36 | } catch (UnsupportedEncodingException e) { 37 | // TODO Auto-generated catch block 38 | e.printStackTrace(); 39 | } catch (IOException e) { 40 | 41 | } 42 | return true; 43 | } 44 | 45 | @Override 46 | public boolean addListener(ListenerInf listener) { 47 | // TODO Auto-generated method stub 48 | return false; 49 | } 50 | 51 | @Override 52 | public void start() { 53 | // TODO Auto-generated method stub 54 | 55 | } 56 | 57 | @Override 58 | public void stop() { 59 | // TODO Auto-generated method stub 60 | 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sinks/PrintSink.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sinks; 2 | 3 | import idlab.massif.interfaces.core.ListenerInf; 4 | import idlab.massif.interfaces.core.SinkInf; 5 | 6 | public class PrintSink implements SinkInf{ 7 | 8 | 9 | 10 | @Override 11 | public boolean addEvent(String event) { 12 | System.out.println("PrintSink received:"); 13 | System.out.println(event); 14 | return true; 15 | } 16 | 17 | @Override 18 | public boolean addListener(ListenerInf listener) { 19 | // TODO Auto-generated method stub 20 | return false; 21 | } 22 | 23 | @Override 24 | public void start() { 25 | // TODO Auto-generated method stub 26 | 27 | } 28 | @Override 29 | public void stop() { 30 | // TODO Auto-generated method stub 31 | 32 | } 33 | @Override 34 | public String toString() { 35 | return "{\"type\":\"Sink\",\"impl\":\"printSink\"}"; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sinks/SparqlEPInsertSink.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sinks; 2 | // Initial release: L. Tailhardat - 2021.06.15 3 | 4 | import idlab.massif.interfaces.core.ListenerInf; 5 | import idlab.massif.interfaces.core.SinkInf; 6 | 7 | // DOC: https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/update/UpdateExecutionFactory.html 8 | // DOC: https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/update/UpdateFactory.html 9 | 10 | import org.apache.jena.update.UpdateRequest; 11 | import org.apache.jena.update.UpdateFactory; 12 | import org.apache.jena.update.UpdateExecutionFactory; 13 | import org.apache.jena.update.UpdateProcessor; 14 | import org.apache.jena.update.UpdateException; 15 | 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | public class SparqlEPInsertSink implements SinkInf { 20 | 21 | private String serviceUrl; 22 | private String graphName; 23 | 24 | private static final Logger logger = LoggerFactory.getLogger(SparqlEPInsertSink.class); 25 | 26 | public SparqlEPInsertSink(String serviceUrl, String graphName) { 27 | this.serviceUrl = serviceUrl; 28 | this.graphName = graphName; 29 | } 30 | 31 | @Override 32 | public boolean addEvent(String event) { 33 | try { 34 | 35 | // TODO: send to debug 36 | logger.debug( 37 | "Received event: {}", 38 | event); 39 | 40 | String query = String.format( 41 | "INSERT DATA { GRAPH %s { %s } }", 42 | graphName, 43 | event); 44 | 45 | UpdateRequest ur = UpdateFactory.create(query); 46 | UpdateProcessor up = UpdateExecutionFactory.createRemote(ur, serviceUrl); 47 | 48 | logger.debug( 49 | "Executing SPARQL UPDATE query: {}", 50 | query); 51 | up.execute(); 52 | 53 | } catch (UpdateException e) { 54 | // TODO Auto-generated catch block 55 | e.printStackTrace(); 56 | } 57 | 58 | return true; 59 | } 60 | 61 | @Override 62 | public boolean addListener(ListenerInf listener) { 63 | // TODO Auto-generated method stub 64 | return false; 65 | } 66 | 67 | @Override 68 | public void start() { 69 | // TODO Auto-generated method stub 70 | 71 | } 72 | 73 | @Override 74 | public void stop() { 75 | // TODO Auto-generated method stub 76 | 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sinks/WebSocketClientSink.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sinks; 2 | 3 | import java.net.URI; 4 | 5 | import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; 6 | import org.eclipse.jetty.websocket.client.WebSocketClient; 7 | 8 | import idlab.massif.interfaces.core.ListenerInf; 9 | import idlab.massif.interfaces.core.SinkInf; 10 | import idlab.massif.sinks.utils.WebSocketOutputStream; 11 | import idlab.massif.sources.util.WebSocketInputStream; 12 | 13 | public class WebSocketClientSink implements SinkInf { 14 | 15 | private String wsURL; 16 | private WebSocketOutputStream socket; 17 | public WebSocketClientSink(String wsURL) { 18 | this.wsURL = wsURL; 19 | WebSocketClient client = new WebSocketClient(); 20 | 21 | this.socket = new WebSocketOutputStream(); 22 | try { 23 | client.start(); 24 | 25 | URI echoUri = new URI(wsURL); 26 | ClientUpgradeRequest request = new ClientUpgradeRequest(); 27 | client.connect(socket, echoUri, request); 28 | System.out.printf("Connecting to : %s%n", echoUri); 29 | 30 | } catch (Throwable t) { 31 | t.printStackTrace(); 32 | } finally { 33 | try { 34 | //client.stop(); 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | } 38 | } 39 | } 40 | @Override 41 | public boolean addEvent(String result) { 42 | // TODO Auto-generated method stub 43 | socket.notify(result); 44 | return true; 45 | } 46 | @Override 47 | public boolean addListener(ListenerInf listener) { 48 | // TODO Auto-generated method stub 49 | return false; 50 | } 51 | @Override 52 | public void start() { 53 | // TODO Auto-generated method stub 54 | 55 | } 56 | @Override 57 | public void stop() { 58 | // TODO Auto-generated method stub 59 | this.socket.close(); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sinks/WebSocketServerSink.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sinks; 2 | 3 | import idlab.massif.interfaces.core.ListenerInf; 4 | 5 | import idlab.massif.interfaces.core.SinkInf; 6 | import idlab.massif.sinks.utils.WebSocketOutputStream; 7 | import spark.Service; 8 | import spark.Spark; 9 | 10 | 11 | public class WebSocketServerSink implements SinkInf{ 12 | 13 | private WebSocketOutputStream socket; 14 | 15 | public WebSocketServerSink(int port, String wsURL) { 16 | Service ws = Service.ignite() 17 | .port(port); 18 | 19 | this.socket = new WebSocketOutputStream(); 20 | ws.webSocket("/"+wsURL, socket); 21 | ws.init(); 22 | } 23 | 24 | 25 | @Override 26 | public boolean addEvent(String event) { 27 | this.socket.notify(event); 28 | return true; 29 | } 30 | 31 | @Override 32 | public boolean addListener(ListenerInf listener) { 33 | // TODO Auto-generated method stub 34 | return false; 35 | } 36 | 37 | @Override 38 | public void start() { 39 | // TODO Auto-generated method stub 40 | 41 | } 42 | 43 | 44 | @Override 45 | public void stop() { 46 | // TODO Auto-generated method stub 47 | this.socket.close(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sinks/utils/WebSocketOutputStream.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sinks.utils; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import org.eclipse.jetty.websocket.api.Session; 8 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; 9 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; 10 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; 11 | import org.eclipse.jetty.websocket.api.annotations.WebSocket; 12 | 13 | 14 | @WebSocket 15 | public class WebSocketOutputStream { 16 | 17 | private List sessions; 18 | public WebSocketOutputStream() { 19 | sessions = new ArrayList(); 20 | } 21 | @OnWebSocketConnect 22 | public void connected(Session session) { 23 | 24 | System.out.println("connecting"); 25 | if(!sessions.contains(session)) { 26 | sessions.add(session); 27 | } 28 | } 29 | 30 | @OnWebSocketClose 31 | public void closed(Session session, int statusCode, String reason) { 32 | System.out.println("closing"); 33 | if(sessions.contains(session)) { 34 | sessions.remove(session); 35 | } 36 | } 37 | 38 | @OnWebSocketMessage 39 | public void message(Session session, String message) throws IOException { 40 | 41 | } 42 | public void notify(String event) { 43 | System.out.println("sending"); 44 | for(Session session: sessions) { 45 | try { 46 | 47 | session.getRemote().sendString(event); 48 | } catch (IOException e) { 49 | // TODO Auto-generated catch block 50 | e.printStackTrace(); 51 | } 52 | } 53 | } 54 | public void close() { 55 | for(Session session: sessions) { 56 | session.close(); 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/FileSource.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileReader; 5 | import java.io.IOException; 6 | 7 | import idlab.massif.core.PipeLine; 8 | import idlab.massif.interfaces.core.ListenerInf; 9 | import idlab.massif.interfaces.core.SourceInf; 10 | 11 | /*** 12 | * Reads a provided file line by line and streams each line to the pipeline. If 13 | * timeout equals -1, it read the whole file in one go and then streams the 14 | * result. 15 | * 16 | * @author psbonte 17 | * 18 | */ 19 | public class FileSource implements SourceInf { 20 | 21 | private String fileName; 22 | private PipeLine pipeline; 23 | private long timeout; 24 | private ListenerInf listener; 25 | private boolean streaming = true; 26 | 27 | public FileSource(String fileName, long timeout) { 28 | this.fileName = fileName.trim(); 29 | this.timeout = timeout; 30 | } 31 | 32 | public void registerPipeline(PipeLine pipeline) { 33 | this.pipeline = pipeline; 34 | } 35 | 36 | public void stream() { 37 | 38 | try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { 39 | String line; 40 | if (timeout >= 0) { 41 | 42 | while ((line = br.readLine()) != null && streaming) { 43 | 44 | line += "\n"; 45 | if (listener != null) { 46 | listener.notify(0, line); 47 | } 48 | if (pipeline != null) { 49 | pipeline.addEvent(line); 50 | } 51 | try { 52 | Thread.sleep(timeout); 53 | } catch (InterruptedException e) { 54 | // TODO Auto-generated catch block 55 | e.printStackTrace(); 56 | } 57 | 58 | } 59 | } else { 60 | //read the file as one and then stream the results 61 | StringBuilder result = new StringBuilder(); 62 | while ((line = br.readLine()) != null && streaming) { 63 | result.append(line).append("\n"); 64 | } 65 | if (listener != null) { 66 | listener.notify(0, result.toString()); 67 | } 68 | if (pipeline != null) { 69 | pipeline.addEvent(result.toString()); 70 | } 71 | 72 | } 73 | 74 | } catch (IOException e) { 75 | e.printStackTrace(); 76 | } 77 | } 78 | 79 | @Override 80 | public boolean addEvent(String event) { 81 | // TODO Auto-generated method stub 82 | return false; 83 | } 84 | 85 | @Override 86 | public boolean addListener(ListenerInf listener) { 87 | this.listener = listener; 88 | return true; 89 | } 90 | 91 | @Override 92 | public void start() { 93 | // TODO Auto-generated method stub 94 | streaming = true; 95 | new Thread(() -> this.stream()).start(); 96 | 97 | } 98 | 99 | @Override 100 | public void stop() { 101 | // TODO Auto-generated method stub 102 | streaming = false; 103 | } 104 | 105 | @Override 106 | public String toString() { 107 | return String.format("{\"type\":\"Source\",\"impl\":\"fileSource\",\"fileName\":\"%s\",\"timeout\":%d}", 108 | fileName, timeout); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/HTTPGetSource.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.net.MalformedURLException; 8 | import java.net.URL; 9 | import java.net.URLConnection; 10 | import java.nio.charset.Charset; 11 | import java.util.stream.Collectors; 12 | 13 | import org.json.JSONArray; 14 | import org.json.JSONObject; 15 | 16 | import idlab.massif.core.PipeLine; 17 | import idlab.massif.interfaces.core.ListenerInf; 18 | import idlab.massif.interfaces.core.SourceInf; 19 | 20 | /*** 21 | * Allows to continuously pull a HTTP source through a HTTP get request. 22 | * 23 | * @author psbonte 24 | * 25 | */ 26 | public class HTTPGetSource implements SourceInf { 27 | 28 | private String url; 29 | private PipeLine pipeline; 30 | private long pullTimeOut; 31 | private ListenerInf listener; 32 | private boolean streaming = true; 33 | 34 | public HTTPGetSource(String url, long pullTimeOut) { 35 | this.url = url; 36 | this.pullTimeOut = pullTimeOut; 37 | } 38 | 39 | public void registerPipeline(PipeLine pipeline) { 40 | this.pipeline = pipeline; 41 | } 42 | 43 | public void stream() { 44 | while (streaming) { 45 | URL urlCon; 46 | try { 47 | urlCon = new URL(url); 48 | URLConnection conn = urlCon.openConnection(); 49 | InputStream is = conn.getInputStream(); 50 | String result = ""; 51 | try (BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.defaultCharset()))) { 52 | result = br.lines().collect(Collectors.joining(System.lineSeparator())); 53 | JSONArray jsonRes = new JSONArray(result); 54 | for (int i = 0; i < jsonRes.length(); i++) { 55 | 56 | if (pipeline != null) { 57 | pipeline.addEvent(jsonRes.getString(i)); 58 | } 59 | if (listener != null) { 60 | listener.notify(0, jsonRes.getString(i)); 61 | } 62 | } 63 | } 64 | Thread.sleep(pullTimeOut); 65 | } catch (MalformedURLException e) { 66 | // TODO Auto-generated catch block 67 | e.printStackTrace(); 68 | } catch (IOException e) { 69 | // TODO Auto-generated catch block 70 | e.printStackTrace(); 71 | } catch (InterruptedException e) { 72 | // TODO Auto-generated catch block 73 | e.printStackTrace(); 74 | } 75 | 76 | } 77 | } 78 | 79 | @Override 80 | public boolean addEvent(String event) { 81 | // TODO Auto-generated method stub 82 | return false; 83 | } 84 | 85 | @Override 86 | public boolean addListener(ListenerInf listener) { 87 | this.listener = listener; 88 | return true; 89 | } 90 | 91 | @Override 92 | public void start() { 93 | // TODO Auto-generated method stub 94 | streaming = true; 95 | new Thread(() -> this.stream()).start(); 96 | 97 | } 98 | 99 | @Override 100 | public void stop() { 101 | // TODO Auto-generated method stub 102 | streaming = false; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/HTTPPostCombinedSource.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources; 2 | 3 | import static spark.Spark.get; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Collections; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | import org.json.JSONArray; 12 | 13 | import idlab.massif.interfaces.core.ListenerInf; 14 | import idlab.massif.interfaces.core.SourceInf; 15 | import spark.Response; 16 | 17 | import static spark.Spark.post; 18 | 19 | class HTTPHandlerSingleton { 20 | private static HTTPHandlerSingleton single_instance = null; 21 | 22 | private Map> listenerMap; 23 | 24 | // private constructor restricted to this class itself 25 | private HTTPHandlerSingleton() { 26 | post("/httppostsource/:id", (req, res) -> prepData(req.params("id"), req.body(), res)); 27 | listenerMap = new HashMap>(); 28 | } 29 | 30 | // static method to create instance of Singleton class 31 | public static HTTPHandlerSingleton getInstance() { 32 | if (single_instance == null) 33 | single_instance = new HTTPHandlerSingleton(); 34 | 35 | return single_instance; 36 | } 37 | 38 | private String prepData(String id, String body, Response response) { 39 | if (listenerMap.containsKey(id)) { 40 | listenerMap.get(id).forEach(l -> l.notify(0, body)); 41 | response.status(200); 42 | return "found"; 43 | } else { 44 | response.status(200); 45 | return "not found"; 46 | } 47 | } 48 | 49 | 50 | public void removeRoute(String id) { 51 | listenerMap.remove(id); 52 | } 53 | 54 | public boolean addListener(String id, ListenerInf listener) { 55 | if (!listenerMap.containsKey(id)) { 56 | listenerMap.put(id,new ArrayList()); 57 | } 58 | listenerMap.get(id).add(listener); 59 | return true; 60 | } 61 | } 62 | 63 | public class HTTPPostCombinedSource implements SourceInf { 64 | private String path; 65 | private HTTPHandlerSingleton handler; 66 | 67 | public HTTPPostCombinedSource(String path) { 68 | handler = HTTPHandlerSingleton.getInstance(); 69 | this.path = path; 70 | 71 | 72 | } 73 | 74 | @Override 75 | public boolean addEvent(String event) { 76 | 77 | return false; 78 | } 79 | 80 | @Override 81 | public boolean addListener(ListenerInf listener) { 82 | // TODO Auto-generated method stub 83 | handler.addListener(path, listener); 84 | return true; 85 | } 86 | 87 | @Override 88 | public void start() { 89 | // TODO Auto-generated method stub 90 | 91 | } 92 | 93 | @Override 94 | public void stop() { 95 | // TODO Auto-generated method stub 96 | handler.removeRoute(path); 97 | } 98 | 99 | @Override 100 | public String toString() { 101 | return String.format("{\"type\":\"Source\",\"impl\":\"httpPostCombinedSource\",\"path\":\"%s\"}", 102 | path); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/HTTPPostSource.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources; 2 | 3 | 4 | 5 | import idlab.massif.core.PipeLine; 6 | import idlab.massif.interfaces.core.ListenerInf; 7 | import idlab.massif.interfaces.core.SourceInf; 8 | import spark.Spark; 9 | 10 | /*** 11 | * Provides a Source that can be externally call through the provided url through a HTTP Post request. 12 | * 13 | * @author psbonte 14 | * 15 | */ 16 | public class HTTPPostSource implements SourceInf { 17 | 18 | private String path; 19 | private PipeLine pipeline; 20 | private ListenerInf listener; 21 | private int port; 22 | 23 | public HTTPPostSource(String path,int port) { 24 | this.path = path; 25 | this.port = port; 26 | } 27 | 28 | public void registerPipeline(PipeLine pipeline) { 29 | this.pipeline = pipeline; 30 | } 31 | 32 | public void stream() { 33 | //Spark.port(this.port); 34 | Spark.post("/" + path, (req, res) -> { 35 | if(pipeline!=null) { 36 | this.pipeline.addEvent(req.body()); 37 | } 38 | if(listener!=null) { 39 | this.listener.notify(0, req.body()); 40 | } 41 | return "ok"; 42 | }); 43 | 44 | } 45 | 46 | @Override 47 | public boolean addEvent(String event) { 48 | // TODO Auto-generated method stub 49 | return false; 50 | } 51 | 52 | @Override 53 | public boolean addListener(ListenerInf listener) { 54 | this.listener = listener; 55 | return true; 56 | } 57 | 58 | @Override 59 | public void start() { 60 | // TODO Auto-generated method stub 61 | this.stream(); 62 | } 63 | @Override 64 | public void stop() { 65 | // TODO Auto-generated method stub 66 | Spark.stop(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/KafkaConsumerSource.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources; 2 | 3 | import idlab.massif.core.PipeLine; 4 | import idlab.massif.interfaces.core.ListenerInf; 5 | import idlab.massif.interfaces.core.SourceInf; 6 | import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; 7 | import org.apache.kafka.clients.consumer.ConsumerRecord; 8 | import org.apache.kafka.clients.consumer.ConsumerRecords; 9 | import org.apache.kafka.clients.consumer.KafkaConsumer; 10 | import org.apache.kafka.common.TopicPartition; 11 | import org.json.JSONObject; 12 | 13 | import java.util.Collection; 14 | import java.util.Collections; 15 | import java.util.Properties; 16 | 17 | public class KafkaConsumerSource implements SourceInf { 18 | 19 | private final String kafkaServer; 20 | private final String kafkaTopic; 21 | private final Properties props; 22 | private PipeLine pipeline; 23 | private ListenerInf listener; 24 | 25 | public KafkaConsumerSource(String kafkaServer, String kafkaTopic) { 26 | this.kafkaServer = kafkaServer; 27 | this.kafkaTopic = kafkaTopic; 28 | this.props = new Properties(); 29 | props.put("bootstrap.servers", kafkaServer); 30 | props.put("group.id", "MASSIF_"+System.currentTimeMillis()); 31 | props.put("key.deserializer", 32 | "org.apache.kafka.common.serialization.StringDeserializer"); 33 | props.put("value.deserializer", 34 | "org.apache.kafka.common.serialization.StringDeserializer"); 35 | } 36 | 37 | public static void main(String[] args){ 38 | KafkaConsumerSource source = new KafkaConsumerSource("localhost:9092","quickstart-events"); 39 | source.stream(); 40 | 41 | 42 | 43 | 44 | } 45 | public void registerPipeline(PipeLine pipeline) { 46 | this.pipeline = pipeline; 47 | } 48 | public void stream(){ 49 | KafkaConsumer consumer = 50 | new KafkaConsumer(props); 51 | 52 | consumer.subscribe(Collections.singletonList(this.kafkaTopic),new SeekToEnd(consumer)); 53 | try { 54 | while (true) { 55 | ConsumerRecords records = consumer.poll(1000); 56 | if(!records.isEmpty()) { 57 | System.out.println(System.currentTimeMillis()+ ": # kafka messages received: " + records.count()); 58 | } 59 | for (ConsumerRecord record : records) 60 | { 61 | 62 | if (pipeline != null) { 63 | this.pipeline.addEvent(record.value()); 64 | } 65 | if (listener != null) { 66 | this.listener.notify(0, record.value()); 67 | } 68 | 69 | } 70 | } 71 | } catch(Exception e){ 72 | e.printStackTrace(); 73 | } 74 | finally { 75 | consumer.close(); 76 | } 77 | } 78 | 79 | @Override 80 | public boolean addEvent(String event) { 81 | return false; 82 | } 83 | 84 | @Override 85 | public boolean addListener(ListenerInf listener) { 86 | this.listener = listener; 87 | return false; 88 | } 89 | 90 | @Override 91 | public void start() { 92 | Runnable task = () -> { 93 | this.stream(); 94 | }; 95 | Thread thread = new Thread(task); 96 | thread.start(); 97 | } 98 | 99 | @Override 100 | public void stop() { 101 | 102 | } 103 | 104 | public static class SeekToEnd implements ConsumerRebalanceListener { 105 | private final KafkaConsumer consumer; 106 | 107 | public SeekToEnd(KafkaConsumer consumer) { 108 | this.consumer = consumer; 109 | } 110 | 111 | public void onPartitionsRevoked(Collection partitions) { 112 | 113 | } 114 | 115 | public void onPartitionsAssigned(Collection partitions) { 116 | consumer.seekToEnd(partitions); 117 | 118 | } 119 | } 120 | @Override 121 | public String toString() { 122 | return String.format("{\"type\":\"Source\",\"impl\":\"KafkaConsumerSource\",\"option1\":\"%s\",\"option2\":\"%s\"}", kafkaServer,kafkaTopic); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/KafkaSource.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources; 2 | 3 | 4 | import java.util.Properties; 5 | 6 | import org.apache.kafka.clients.consumer.ConsumerConfig; 7 | 8 | import org.apache.kafka.common.serialization.Serdes; 9 | import org.apache.kafka.streams.KafkaStreams; 10 | import org.apache.kafka.streams.StreamsBuilder; 11 | import org.apache.kafka.streams.StreamsConfig; 12 | import org.apache.kafka.streams.Topology; 13 | import org.apache.kafka.streams.kstream.Consumed; 14 | import org.apache.kafka.streams.kstream.KStream; 15 | 16 | 17 | import idlab.massif.core.PipeLine; 18 | import idlab.massif.interfaces.core.ListenerInf; 19 | import idlab.massif.interfaces.core.SourceInf; 20 | 21 | 22 | 23 | 24 | 25 | public class KafkaSource implements SourceInf { 26 | 27 | 28 | 29 | 30 | 31 | 32 | private PipeLine pipeline; 33 | private String kafkaServer; 34 | private String kafkaTopic; 35 | private Properties props; 36 | private ListenerInf listener; 37 | private KafkaStreams streams; 38 | private long timeOutTime; 39 | private long startTime; 40 | 41 | public KafkaSource(String kafkaServer, String kafkaTopic) { 42 | this.kafkaServer = kafkaServer; 43 | this.kafkaTopic = kafkaTopic; 44 | this.props = new Properties(); 45 | props.put(StreamsConfig.APPLICATION_ID_CONFIG, "StreamingMassif-KafkaSink" + kafkaTopic); 46 | props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServer); 47 | props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass()); 48 | props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); 49 | props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest"); //latest? 50 | this.timeOutTime=30000; 51 | 52 | 53 | } 54 | public void registerPipeline(PipeLine pipeline) { 55 | this.pipeline = pipeline; 56 | } 57 | public void stream() { 58 | try { 59 | this.startTime = System.currentTimeMillis(); 60 | StreamsBuilder builder = new StreamsBuilder(); 61 | 62 | KStream sensors = builder.stream(this.kafkaTopic, Consumed.with(Serdes.String(), Serdes.String())); 63 | sensors 64 | .filter((key, value) -> (System.currentTimeMillis() - startTime) > timeOutTime) 65 | .foreach((key, value) -> { 66 | if (pipeline != null) { 67 | this.pipeline.addEvent(value); 68 | } 69 | if (listener != null) { 70 | this.listener.notify(0, value); 71 | } 72 | }); 73 | 74 | 75 | final Topology topology = builder.build(); 76 | final KafkaStreams streams = new KafkaStreams(topology, this.props); 77 | this.streams = streams; 78 | 79 | streams.cleanUp(); 80 | 81 | streams.start(); 82 | }catch(Exception e){ 83 | e.printStackTrace(); 84 | } 85 | } 86 | 87 | @Override 88 | public boolean addEvent(String event) { 89 | // TODO Auto-generated method stub 90 | return false; 91 | } 92 | 93 | 94 | @Override 95 | public boolean addListener(ListenerInf listener) { 96 | this.listener = listener; 97 | return false; 98 | } 99 | 100 | 101 | @Override 102 | public void start() { 103 | // TODO Auto-generated method stub 104 | this.stream(); 105 | } 106 | @Override 107 | public void stop() { 108 | streams.close(); 109 | } 110 | @Override 111 | public String toString() { 112 | return String.format("{\"type\":\"Source\",\"impl\":\"kafkaSource\",\"kafkaServer\":\"%s\",\"kafkaTopic\":\"%s\"}", kafkaServer,kafkaTopic); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/WebSocketClientSource.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources; 2 | 3 | import java.net.URI; 4 | 5 | import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; 6 | import org.eclipse.jetty.websocket.client.WebSocketClient; 7 | 8 | import idlab.massif.core.PipeLine; 9 | import idlab.massif.sources.util.WebSocketInputStream; 10 | 11 | public class WebSocketClientSource { 12 | 13 | private PipeLine pipeline; 14 | private String wsURL; 15 | 16 | public WebSocketClientSource(String wsURL) { 17 | this.wsURL = wsURL; 18 | } 19 | 20 | public void registerPipeline(PipeLine pipeline) { 21 | this.pipeline = pipeline; 22 | } 23 | 24 | public void stream() { 25 | WebSocketClient client = new WebSocketClient(); 26 | 27 | WebSocketInputStream socket = new WebSocketInputStream(this.pipeline); 28 | try { 29 | client.start(); 30 | 31 | URI echoUri = new URI(wsURL); 32 | ClientUpgradeRequest request = new ClientUpgradeRequest(); 33 | client.connect(socket, echoUri, request); 34 | System.out.printf("Connecting to : %s%n", echoUri); 35 | 36 | } catch (Throwable t) { 37 | t.printStackTrace(); 38 | } finally { 39 | try { 40 | //client.stop(); 41 | } catch (Exception e) { 42 | e.printStackTrace(); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/WebSocketServerSource.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources; 2 | 3 | 4 | 5 | import idlab.massif.core.PipeLine; 6 | import idlab.massif.sources.util.WebSocketInputStream; 7 | import spark.Spark; 8 | 9 | public class WebSocketServerSource { 10 | private String wsURL; 11 | private PipeLine pipeline; 12 | private int port; 13 | 14 | public WebSocketServerSource(int port,String wsURL) { 15 | this.wsURL = wsURL; 16 | this.port = port; 17 | } 18 | public void registerPipeline(PipeLine pipeline) { 19 | this.pipeline = pipeline; 20 | } 21 | 22 | public void stream() { 23 | Spark.port(port); 24 | Spark.webSocket("/"+wsURL, new WebSocketInputStream(pipeline)); 25 | Spark.init(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/kafkautils/JsonPOJODeserializer.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources.kafkautils; 2 | /* 3 | * Licensed to the Apache Software Foundation (ASF) under one or more 4 | * contributor license agreements. See the NOTICE file distributed with 5 | * this work for additional information regarding copyright ownership. 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 7 | * (the "License"); you may not use this file except in compliance with 8 | * the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import org.apache.kafka.common.errors.SerializationException; 21 | import org.apache.kafka.common.serialization.Deserializer; 22 | 23 | import java.util.Map; 24 | 25 | public class JsonPOJODeserializer implements Deserializer { 26 | private ObjectMapper objectMapper = new ObjectMapper(); 27 | 28 | private Class tClass; 29 | 30 | /** 31 | * Default constructor needed by Kafka 32 | */ 33 | public JsonPOJODeserializer() { 34 | } 35 | 36 | @SuppressWarnings("unchecked") 37 | @Override 38 | public void configure(Map props, boolean isKey) { 39 | tClass = (Class) props.get("JsonPOJOClass"); 40 | } 41 | 42 | @Override 43 | public T deserialize(String topic, byte[] bytes) { 44 | if (bytes == null) 45 | return null; 46 | 47 | T data; 48 | try { 49 | data = objectMapper.readValue(bytes, tClass); 50 | } catch (Exception e) { 51 | throw new SerializationException(e); 52 | } 53 | 54 | return data; 55 | } 56 | 57 | @Override 58 | public void close() { 59 | 60 | } 61 | } -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/kafkautils/JsonPOJOSerializer.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources.kafkautils; 2 | 3 | /* 4 | * Licensed to the Apache Software Foundation (ASF) under one or more 5 | * contributor license agreements. See the NOTICE file distributed with 6 | * this work for additional information regarding copyright ownership. 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 8 | * (the "License"); you may not use this file except in compliance with 9 | * the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import org.apache.kafka.common.errors.SerializationException; 21 | import org.apache.kafka.common.serialization.Serializer; 22 | 23 | import java.util.Map; 24 | 25 | public class JsonPOJOSerializer implements Serializer { 26 | private final ObjectMapper objectMapper = new ObjectMapper(); 27 | 28 | /** 29 | * Default constructor needed by Kafka 30 | */ 31 | public JsonPOJOSerializer() { 32 | } 33 | 34 | @Override 35 | public void configure(Map props, boolean isKey) { 36 | } 37 | 38 | @Override 39 | public byte[] serialize(String topic, T data) { 40 | if (data == null) 41 | return null; 42 | 43 | try { 44 | return objectMapper.writeValueAsBytes(data); 45 | } catch (Exception e) { 46 | throw new SerializationException("Error serializing JSON message", e); 47 | } 48 | } 49 | 50 | @Override 51 | public void close() { 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/sources/util/WebSocketInputStream.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.sources.util; 2 | 3 | import java.io.IOException; 4 | 5 | import org.eclipse.jetty.websocket.api.Session; 6 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; 7 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; 8 | import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; 9 | import org.eclipse.jetty.websocket.api.annotations.WebSocket; 10 | 11 | import idlab.massif.core.PipeLine; 12 | 13 | @WebSocket 14 | 15 | public class WebSocketInputStream { 16 | private PipeLine pipeline; 17 | 18 | public WebSocketInputStream(PipeLine pipeline) { 19 | this.pipeline=pipeline; 20 | } 21 | @OnWebSocketConnect 22 | public void connected(Session session) { 23 | System.out.println("connecting"); 24 | } 25 | 26 | @OnWebSocketClose 27 | public void closed(Session session, int statusCode, String reason) { 28 | } 29 | 30 | @OnWebSocketMessage 31 | public void message(Session session, String message) throws IOException { 32 | pipeline.addEvent(message); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/timeout/KeyedTimeout.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.timeout; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import idlab.massif.filter.jena.JenaFilter; 8 | import idlab.massif.interfaces.core.ListenerInf; 9 | import idlab.massif.interfaces.core.PipeLineElement; 10 | import idlab.massif.utils.FormatUtils; 11 | import idlab.massif.utils.TableUtils; 12 | 13 | public class KeyedTimeout implements PipeLineElement, ListenerInf { 14 | 15 | private ListenerInf listener; 16 | private double timeout; 17 | private double prevMessage; 18 | private Map keyedTimeout; 19 | private JenaFilter filter; 20 | private String query; 21 | private String currentEvent; 22 | 23 | public KeyedTimeout(String timeout, String query) { 24 | this.timeout = Double.parseDouble(timeout); 25 | this.prevMessage = 0; 26 | this.keyedTimeout = new HashMap(); 27 | this.filter = new JenaFilter(); 28 | this.query = query; 29 | filter.registerContinuousQuery(query); 30 | filter.addListener(this); 31 | 32 | } 33 | 34 | @Override 35 | public boolean addEvent(String event) { 36 | this.currentEvent = event; 37 | // extract key 38 | filter.addEvent(event); 39 | 40 | return true; 41 | } 42 | 43 | @Override 44 | public boolean addListener(ListenerInf listener) { 45 | this.listener = listener; 46 | return true; 47 | } 48 | 49 | @Override 50 | public void start() { 51 | // TODO Auto-generated method stub 52 | 53 | } 54 | 55 | @Override 56 | public void stop() { 57 | // TODO Auto-generated method stub 58 | 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return String.format("{\"type\":\"TimeOut\",\"impl\":\"timeout\",\"option1\":\"%s\",\"option2\":\"%s\"}", timeout + "", 64 | FormatUtils.encodeQuery(query)); 65 | 66 | } 67 | 68 | @Override 69 | public void notify(int queryID, String event) { 70 | Map> table = TableUtils.convertStringToTable(event); 71 | if (table.containsKey("key") && !table.get("key").isEmpty()) { 72 | String key = table.get("key").get(0); 73 | double prevMessage = keyedTimeout.getOrDefault(key, (double) 0); 74 | if (System.currentTimeMillis() - prevMessage > timeout) { 75 | listener.notify(0, this.currentEvent); 76 | keyedTimeout.put(key,(double)System.currentTimeMillis()); 77 | } 78 | } 79 | 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/timeout/Timeout.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.timeout; 2 | 3 | import idlab.massif.interfaces.core.ListenerInf; 4 | import idlab.massif.interfaces.core.PipeLineElement; 5 | 6 | public class Timeout implements PipeLineElement { 7 | 8 | private ListenerInf listener; 9 | private double timeout; 10 | private double prevMessage; 11 | 12 | public Timeout(String timeout) { 13 | this.timeout = Double.parseDouble(timeout); 14 | this.prevMessage = 0; 15 | 16 | } 17 | 18 | @Override 19 | public boolean addEvent(String event) { 20 | if (System.currentTimeMillis() - prevMessage > timeout) { 21 | listener.notify(0, event); 22 | prevMessage = System.currentTimeMillis(); 23 | } 24 | 25 | return false; 26 | } 27 | 28 | @Override 29 | public boolean addListener(ListenerInf listener) { 30 | this.listener = listener; 31 | return true; 32 | } 33 | 34 | @Override 35 | public void start() { 36 | // TODO Auto-generated method stub 37 | 38 | } 39 | 40 | @Override 41 | public void stop() { 42 | // TODO Auto-generated method stub 43 | 44 | } 45 | @Override 46 | public String toString() { 47 | return String.format("{\"type\":\"TimeOut\",\"impl\":\"timeout\",\"option1\":%s}",timeout +""); 48 | 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/utils/FormatUtils.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.utils; 2 | 3 | public class FormatUtils { 4 | 5 | public static String encodeQuery(String query) { 6 | return query.replace("\t", "\\t").replace("\n", "\\n").replace("\"", "\\\""); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/utils/RDFUtils.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.utils; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.io.StringWriter; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.apache.jena.rdf.model.Model; 10 | import org.apache.jena.rdf.model.ModelFactory; 11 | import org.apache.jena.rdf.model.Statement; 12 | import org.apache.jena.rdf.model.StmtIterator; 13 | import org.apache.jena.riot.RiotException; 14 | 15 | public class RDFUtils { 16 | private static String[] syntaxes = new String[]{"TTL", "RDF/XML"}; 17 | private static int currentSyntaxIndex = 0; 18 | public static List parseTriples(String event){ 19 | Model dataModel = ModelFactory.createDefaultModel(); 20 | readAndtryVariousSyntaxes(dataModel,event); 21 | 22 | StmtIterator it = dataModel.listStatements(); 23 | List statements = new ArrayList(); 24 | while (it.hasNext()) { 25 | statements.add(it.next()); 26 | } 27 | return statements; 28 | } 29 | private static void readAndtryVariousSyntaxes(Model dataModel, String event){ 30 | if(currentSyntaxIndex < syntaxes.length) { 31 | InputStream targetStream = new ByteArrayInputStream(event.getBytes()); 32 | try { 33 | dataModel.read(targetStream, null, syntaxes[currentSyntaxIndex]); 34 | } catch (RiotException e) { 35 | currentSyntaxIndex++; 36 | readAndtryVariousSyntaxes(dataModel,event); 37 | } 38 | }else { 39 | currentSyntaxIndex = 0; 40 | } 41 | } 42 | public static String modelToString(Model m) { 43 | String syntax = "TURTLE"; // also try "N-TRIPLE" and "TURTLE" 44 | StringWriter out = new StringWriter(); 45 | m.write(out, syntax); 46 | return out.toString(); 47 | 48 | } 49 | public static String RDFStatementsToString(List statements) { 50 | Model dataModel = ModelFactory.createDefaultModel(); 51 | dataModel.add(statements); 52 | return modelToString(dataModel); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/utils/TableUtils.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.utils; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | public class TableUtils { 9 | 10 | public static Map> convertStringToTable(String event){ 11 | Map> table = new HashMap>(); 12 | String[] lines = event.split(System.lineSeparator()); 13 | String[] vars = lines[0].split(","); 14 | List keys = new ArrayList(); 15 | //Add the keys 16 | for(String var: vars) { 17 | if(!var.trim().equals("")) { 18 | table.put(var.trim(), new ArrayList()); 19 | keys.add(var.trim()); 20 | } 21 | } 22 | 23 | //Add the data 24 | for(int lineCounter = 1; lineCounter < lines.length; lineCounter++) { 25 | String[] bindings = lines[lineCounter].split(","); 26 | for(int keyIndex = 0 ; keyIndex < keys.size(); keyIndex++) { 27 | table.get(keys.get(keyIndex).trim()).add(bindings[keyIndex].trim()); 28 | } 29 | 30 | } 31 | return table; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/idlab/massif/window/esper/EsperWindow.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.window.esper; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.io.StringWriter; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import org.apache.jena.rdf.model.Model; 10 | import org.apache.jena.rdf.model.ModelFactory; 11 | import org.apache.jena.rdf.model.Statement; 12 | import org.apache.jena.rdf.model.StmtIterator; 13 | 14 | import com.espertech.esper.client.Configuration; 15 | import com.espertech.esper.client.EPAdministrator; 16 | import com.espertech.esper.client.EPRuntime; 17 | import com.espertech.esper.client.EPServiceProvider; 18 | import com.espertech.esper.client.EPServiceProviderManager; 19 | import com.espertech.esper.client.EPStatement; 20 | import com.espertech.esper.client.EventBean; 21 | import com.espertech.esper.client.UpdateListener; 22 | import com.espertech.esper.client.deploy.EPDeploymentAdmin; 23 | import com.espertech.esper.client.time.CurrentTimeEvent; 24 | 25 | import idlab.massif.interfaces.core.ListenerInf; 26 | import idlab.massif.interfaces.core.WindowInf; 27 | import idlab.massif.selection.csparql_basic.utils.GraphEvent; 28 | 29 | public class EsperWindow implements WindowInf { 30 | 31 | private int windowSize; 32 | private int windowSlide; 33 | private EPRuntime cepRT; 34 | private ListenerInf listener; 35 | private int counter; 36 | private EPServiceProvider cep; 37 | @Override 38 | public boolean addEvent(String event) { 39 | this.advanceTime(System.currentTimeMillis()); 40 | Model dataModel = ModelFactory.createDefaultModel(); 41 | try { 42 | InputStream targetStream = new ByteArrayInputStream(event.getBytes()); 43 | dataModel.read(targetStream,null,"TTL"); 44 | StmtIterator it = dataModel.listStatements(); 45 | List statements = new ArrayList(); 46 | while (it.hasNext()) { 47 | statements.add(it.next()); 48 | } 49 | this.addEvent(statements); 50 | return true; 51 | 52 | } catch (Exception e) { 53 | // TODO Auto-generated catch block 54 | e.printStackTrace(); 55 | return false; 56 | } 57 | } 58 | 59 | @Override 60 | public boolean addListener(ListenerInf listener) { 61 | // TODO Auto-generated method stub 62 | this.listener=listener; 63 | return true; 64 | } 65 | 66 | @Override 67 | public void setWindowSize(int window) { 68 | this.windowSize=window; 69 | this.windowSlide=window; 70 | 71 | } 72 | 73 | @Override 74 | public void setWindowSize(int windowSize, int windowSlide) { 75 | this.windowSize=windowSize; 76 | this.windowSlide=windowSlide; 77 | 78 | } 79 | public void addEvent(List event) { 80 | cepRT.sendEvent(new GraphEvent(counter++, event)); 81 | } 82 | 83 | public void advanceTime(long time) { 84 | cepRT.sendEvent(new CurrentTimeEvent(time)); 85 | } 86 | public void start() { 87 | Configuration cep_config = new Configuration(); 88 | 89 | cep_config.getEngineDefaults().getThreading().setInternalTimerEnabled(false); 90 | cep_config.getEngineDefaults().getLogging().setEnableExecutionDebug(true); 91 | cep_config.getEngineDefaults().getLogging().setEnableTimerDebug(true); 92 | cep_config.getEngineDefaults().getLogging().setEnableQueryPlan(true); 93 | cep_config.getEngineDefaults().getMetricsReporting().setEnableMetricsReporting(true); 94 | cep_config.getEngineDefaults().getLogging().setEnableQueryPlan(true); 95 | 96 | this.cep = EPServiceProviderManager.getDefaultProvider(cep_config); 97 | this.cepRT = cep.getEPRuntime(); 98 | EPAdministrator cepAdm = cep.getEPAdministrator(); 99 | EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(); 100 | EPDeploymentAdmin deployAdmin = cepAdm.getDeploymentAdmin(); 101 | cep.getEPAdministrator().getConfiguration().addEventType(GraphEvent.class); 102 | 103 | String eplStatement = String.format("select * from GraphEvent#time(%s sec) output snapshot every %s seconds", 104 | windowSize, windowSlide); 105 | 106 | try { 107 | EPStatement statement = cepAdm.createEPL(eplStatement); 108 | 109 | statement.addListener(new UpdateListener() { 110 | public void update(EventBean[] newEvents, EventBean[] oldEvents) { 111 | if (newEvents != null) { 112 | try { 113 | 114 | List add = new ArrayList(); 115 | for (EventBean e : newEvents) { 116 | add.addAll((List) e.get("triples")); 117 | 118 | } 119 | String event = convertToStringEvent(add); 120 | //notify the listener 121 | listener.notify(0,event); 122 | 123 | } catch (Exception e) { 124 | e.printStackTrace(); 125 | } 126 | } 127 | 128 | } 129 | }); 130 | } catch (Exception e1) { 131 | // TODO Auto-generated catch block 132 | e1.printStackTrace(); 133 | } 134 | } 135 | private String convertToStringEvent(List statements) { 136 | Model result = ModelFactory.createDefaultModel(); 137 | result.add(statements); 138 | String syntax = "TTL"; // also try "N-TRIPLE" and "TURTLE" 139 | StringWriter out = new StringWriter(); 140 | result.write(out, syntax); 141 | String resultString = out.toString(); 142 | return resultString; 143 | } 144 | 145 | @Override 146 | public void stop() { 147 | // TODO Auto-generated method stub 148 | this.cep.getEPAdministrator().destroyAllStatements(); 149 | } 150 | @Override 151 | public String toString() { 152 | return String.format("{\"type\":\"window\",\"size\":%d,\"slide\":%d}",windowSize,windowSlide); 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /src/main/resources/web/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KNowledgeOnWebScale/StreamingMASSIF/c84fcaf251f8dffbb32bb188821cb2159b1646fd/src/main/resources/web/icon.png -------------------------------------------------------------------------------- /src/main/resources/web/index_vue.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | {{ message }} 13 |
14 |
15 | 20 |
21 |
22 |
23 | 24 | 88 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /src/main/resources/web/style.css: -------------------------------------------------------------------------------- 1 | #gate { 2 | cursor: pointer; 3 | margin-bottom: 100px; 4 | width: 83px; 5 | height: 46px; 6 | } 7 | 8 | #ball { 9 | cursor: pointer; 10 | width: 40px; 11 | height: 40px; 12 | } -------------------------------------------------------------------------------- /src/test/java/idlab/massif/AppTest.java: -------------------------------------------------------------------------------- 1 | package idlab.massif; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/idlab/massif/cep/esper/EsperCEPTest.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.cep.esper; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.HashMap; 6 | import java.util.HashSet; 7 | import java.util.Map; 8 | import java.util.Set; 9 | import java.util.Map.Entry; 10 | import java.util.stream.Collectors; 11 | 12 | import org.junit.Test; 13 | import org.semanticweb.owlapi.apibinding.OWLManager; 14 | import org.semanticweb.owlapi.io.StringDocumentSource; 15 | import org.semanticweb.owlapi.model.OWLAxiom; 16 | import org.semanticweb.owlapi.model.OWLOntology; 17 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; 18 | import org.semanticweb.owlapi.model.OWLOntologyManager; 19 | 20 | import com.espertech.esper.client.Configuration; 21 | import com.espertech.esper.client.EPServiceProvider; 22 | import com.espertech.esper.client.EPServiceProviderManager; 23 | import com.espertech.esper.client.EPStatement; 24 | import com.espertech.esper.client.EventBean; 25 | import com.espertech.esper.client.UpdateListener; 26 | import com.espertech.esper.event.map.MapEventBean; 27 | 28 | import idlab.massif.interfaces.core.CEPListener; 29 | import junit.framework.Assert; 30 | 31 | 32 | 33 | 34 | public class EsperCEPTest { 35 | 36 | @Test 37 | public void filtertest() throws OWLOntologyCreationException { 38 | EsperCEPImpl cepEngine = new EsperCEPImpl(); 39 | //register query 40 | String querySub= "a=EventA -> b=EventB"; 41 | Set eventTypes=new HashSet(); 42 | eventTypes.add("EventA"); 43 | eventTypes.add("EventB"); 44 | CEPListenerTest listener = new CEPListenerTest(); 45 | cepEngine.registerQuery(querySub, eventTypes, listener); 46 | //CEPListenerTest listener2 = new CEPListenerTest(); 47 | //cepEngine.registerQuery("a=EventA or b=EventB", eventTypes, listener2); 48 | 49 | //add event 50 | cepEngine.addEvent(ONT_EVENT, "EventA"); 51 | cepEngine.addEvent(ONT_EVENT, "EventB"); 52 | System.out.println(listener.getResults()); 53 | Assert.assertEquals( eventTypes.size(),listener.getResults().size()); 54 | 55 | } 56 | @Test 57 | public void test() throws OWLOntologyCreationException { 58 | EsperCEPImpl cepEngine = new EsperCEPImpl(); 59 | //register query 60 | String querySub= "a=EventA -> b=EventB"; 61 | Set eventTypes=new HashSet(); 62 | eventTypes.add("EventA"); 63 | eventTypes.add("EventB"); 64 | CEPListenerTest listener = new CEPListenerTest(); 65 | cepEngine.registerQuery(querySub, eventTypes, listener); 66 | //load event 67 | 68 | //add event 69 | cepEngine.addEvent(ONT_EVENT, "EventA"); 70 | cepEngine.addEvent(ONT_EVENT, "EventB"); 71 | System.out.println(listener.getResults()); 72 | Assert.assertEquals( eventTypes.size(),listener.getResults().size()); 73 | 74 | } 75 | 76 | // TODO: class provided by template 77 | private class CEPListenerTest implements CEPListener { 78 | private Map events; 79 | @Override 80 | public void notify(Map events) { 81 | this.events = events; 82 | 83 | } 84 | public Map getResults(){ 85 | return events; 86 | } 87 | 88 | } 89 | private static String ONT_EVENT="@prefix : .\n" + 90 | "@prefix owl: .\n" + 91 | "@prefix rdf: .\n" + 92 | "@prefix xml: .\n" + 93 | "@prefix xsd: .\n" + 94 | "@prefix rdfs: .\n" + 95 | "@base .\n" + 96 | "\n" + 97 | " rdf:type owl:Ontology ;\n" + 98 | " owl:imports .\n" + 99 | "\n" + 100 | "\n" + 101 | "#################################################################\n" + 102 | "# Individuals\n" + 103 | "#################################################################\n" + 104 | "\n" + 105 | "### http://IBCNServices.github.io/homelab.owl#lightIntensity\n" + 106 | " rdf:type owl:NamedIndividual ,\n" + 107 | " .\n" + 108 | "\n" + 109 | "\n" + 110 | "### http://IBCNServices.github.io/homelab.owl#motionIntensity\n" + 111 | " rdf:type owl:NamedIndividual ,\n" + 112 | " .\n" + 113 | "\n" + 114 | "\n" + 115 | "### http://IBCNServices.github.io/homelab.owl#soundIntensity\n" + 116 | " rdf:type owl:NamedIndividual ,\n" + 117 | " .\n" + 118 | "\n" + 119 | "\n" + 120 | "### http://IBCNServices.github.io/homelabPlus.owl#obs\n" + 121 | ":obs rdf:type owl:NamedIndividual ,\n" + 122 | " ;\n" + 123 | " .\n" + 124 | "\n" + 125 | "\n" + 126 | "### Generated by the OWL API (version 4.5.9.2019-02-01T07:24:44Z) https://github.com/owlcs/owlapi\n" + 127 | ""; 128 | } 129 | -------------------------------------------------------------------------------- /src/test/java/idlab/massif/pipeline/KafkaTest.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.pipeline; 2 | 3 | import java.util.Collections; 4 | import java.util.HashMap; 5 | import java.util.HashSet; 6 | import java.util.Map; 7 | import java.util.Set; 8 | 9 | import org.junit.Test; 10 | import org.semanticweb.owlapi.apibinding.OWLManager; 11 | import org.semanticweb.owlapi.io.StringDocumentSource; 12 | import org.semanticweb.owlapi.model.OWLOntology; 13 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; 14 | import org.semanticweb.owlapi.model.OWLOntologyManager; 15 | 16 | import idlab.massif.abstraction.hermit.HermitAbstractionComp; 17 | import idlab.massif.cep.esper.EsperCEPComp; 18 | import idlab.massif.core.PipeLineComponent; 19 | import idlab.massif.filter.jena.JenaFilter; 20 | import idlab.massif.interfaces.core.AbstractionInf; 21 | import idlab.massif.interfaces.core.CEPInf; 22 | import idlab.massif.interfaces.core.FilterInf; 23 | import idlab.massif.interfaces.core.SinkInf; 24 | import idlab.massif.interfaces.core.WindowInf; 25 | import idlab.massif.sinks.PrintSink; 26 | import idlab.massif.sources.FileSource; 27 | import idlab.massif.sources.KafkaSource; 28 | import idlab.massif.window.esper.EsperWindow; 29 | 30 | public class KafkaTest { 31 | @Test 32 | public void test() throws InterruptedException, OWLOntologyCreationException { 33 | //define window 34 | WindowInf window = new EsperWindow(); 35 | window.setWindowSize(1); 36 | window.start(); 37 | 38 | //define filter 39 | String query = "PREFIX : " 40 | + "CONSTRUCT{?work ?pred ?type.} " 41 | 42 | + "WHERE { ?work ?pred ?type. }"; 43 | FilterInf filter = new JenaFilter(); 44 | int filterQueryID=filter.registerContinuousQuery(query); 45 | filter.start(); 46 | //define the abstraction layer 47 | 48 | //define sink component 49 | SinkInf printSink = new PrintSink(); 50 | 51 | 52 | PipeLineComponent sinkComp = new PipeLineComponent(printSink,Collections.EMPTY_LIST); 53 | 54 | 55 | PipeLineComponent filterComp = new PipeLineComponent(filter,Collections.singletonList(sinkComp)); 56 | 57 | PipeLineComponent windowComp = new PipeLineComponent(window,Collections.singletonList(filterComp)); 58 | 59 | // for(int i = 0;i<10;i++) { 60 | // windowComp.addEvent(ONT_EVENTa); 61 | // Thread.sleep(1000); 62 | // windowComp.addEvent(ONT_EVENTb); 63 | // 64 | // } 65 | KafkaSource kafkaSource = new KafkaSource("kafka-headless.kafka:9092","idlab.homelab"); 66 | kafkaSource = new KafkaSource("localhost:9092","semantic-input"); 67 | 68 | PipeLineComponent sourceComp = new PipeLineComponent(kafkaSource,Collections.singletonList(windowComp)); 69 | kafkaSource.stream(); 70 | Thread.sleep(1000000); 71 | 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/idlab/massif/selection/csparql_basic/CSparqlBasicTest.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.selection.csparql_basic; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Set; 8 | 9 | import org.junit.Test; 10 | 11 | import idlab.massif.interfaces.core.SelectionInf; 12 | import idlab.massif.interfaces.core.SelectionListenerInf; 13 | import junit.framework.Assert; 14 | 15 | public class CSparqlBasicTest { 16 | 17 | @Test 18 | public void test() { 19 | int windowSize = 1; 20 | String dataset = null; 21 | 22 | CSparqlSelectionImpl rsp = new CSparqlSelectionImpl(); 23 | String query = "PREFIX : " 24 | + "CONSTRUCT{?work ?pred ?type.} " 25 | //+ "{?work .} " 26 | + "WHERE { ?work ?pred ?type. }"; 27 | MyListener listener = new MyListener(); 28 | rsp.registerContinuousQuery(query, windowSize, windowSize); 29 | rsp.addListener(listener); 30 | rsp.setStaticData(dataset); 31 | SelectionInf engine = rsp.builtEngine(); 32 | //Add the data 33 | String[] statements = new String[] {"http://massif.test/pbonte","http://www.w3.org/1999/02/22-rdf-syntax-ns#type","http://massif.test/esearcher"}; 34 | String[] statements2 = new String[] {"http://massif.test/rictom","http://www.w3.org/1999/02/22-rdf-syntax-ns#type","http://massif.test/researcher"}; 35 | Set> event1 = new HashSet>(); 36 | event1.add(Arrays.asList(statements)); 37 | Set> event2 = new HashSet>(); 38 | event2.add(Arrays.asList(statements2)); 39 | rsp.addEvent(event1); 40 | 41 | try { 42 | Thread.sleep(3000); 43 | } catch (InterruptedException e) { 44 | // TODO Auto-generated catch block 45 | e.printStackTrace(); 46 | } 47 | rsp.addEvent(event2); 48 | try { 49 | Thread.sleep(3000); 50 | } catch (InterruptedException e) { 51 | // TODO Auto-generated catch block 52 | e.printStackTrace(); 53 | } 54 | rsp.addEvent(event2); 55 | //System.out.println(listener.getResult()); 56 | 57 | Assert.assertTrue(listener.getResult().length()>0); 58 | 59 | } 60 | private class MyListener implements SelectionListenerInf{ 61 | 62 | private String result; 63 | public MyListener() { 64 | this.result=""; 65 | } 66 | @Override 67 | public void notify(String triples) { 68 | // TODO Auto-generated method stub 69 | this.result=triples; 70 | } 71 | public String getResult() { 72 | return result; 73 | } 74 | 75 | } 76 | // @Test 77 | // public void test() { 78 | // String windowSize = "1"; 79 | // String dataset = null; 80 | // 81 | // 82 | // String query = "PREFIX : " 83 | // + "Select * " 84 | // //+ "{?work .} " 85 | // + "WHERE { ?work ?pred ?type. }"; 86 | // 87 | // 88 | // StreamingJena reasoner = new StreamingJena(dataset,windowSize,windowSize); 89 | // reasoner.addContinuousQuery(query); 90 | // //Add the data 91 | // List statements = new ArrayList(); 92 | // 93 | // statements.add(ResourceFactory.createStatement(ResourceFactory.createResource("pbonte"), RDF.type, ResourceFactory.createResource("researcher"))); 94 | // List statements2 = new ArrayList(); 95 | // statements2.add(ResourceFactory.createStatement(ResourceFactory.createResource("rictom"), RDF.type, ResourceFactory.createResource("researcher"))); 96 | // reasoner.advanceTime(0); 97 | // reasoner.addEvent(statements); 98 | // reasoner.advanceTime(1001); 99 | // reasoner.addEvent(statements2); 100 | // reasoner.advanceTime(2001); 101 | // reasoner.addEvent(statements); 102 | // reasoner.advanceTime(3001); 103 | // // 104 | // 105 | // try { 106 | // Thread.sleep(3000); 107 | // } catch (InterruptedException e) { 108 | // // TODO Auto-generated catch block 109 | // e.printStackTrace(); 110 | // } 111 | // } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /src/test/java/idlab/massif/timeout/KeyedTimeoutTest.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.timeout; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import idlab.massif.interfaces.core.ListenerInf; 10 | 11 | public class KeyedTimeoutTest { 12 | 13 | @Test 14 | public void singleKeyTest() { 15 | long timeOut = 1000; 16 | KeyedTimeout ktimeout = new KeyedTimeout(timeOut+"", "Select ?key WHERE{?key a }"); 17 | TestListener listener = new TestListener(); 18 | ktimeout.addListener(listener); 19 | String event1 = " a "; 20 | ktimeout.addEvent(event1); 21 | Assert.assertEquals(listener.events.size(), 1); 22 | Assert.assertEquals(event1, listener.events.get(0)); 23 | //we send same event directly after, which should not be forwarded 24 | ktimeout.addEvent(event1); 25 | Assert.assertEquals(listener.events.size(), 1); 26 | } 27 | @Test 28 | public void waitKeyTest() { 29 | long timeOut = 1000; 30 | KeyedTimeout ktimeout = new KeyedTimeout(timeOut+"", "Select ?key WHERE{?key a }"); 31 | TestListener listener = new TestListener(); 32 | ktimeout.addListener(listener); 33 | String event1 = " a "; 34 | ktimeout.addEvent(event1); 35 | Assert.assertEquals(listener.events.size(), 1); 36 | Assert.assertEquals(event1, listener.events.get(0)); 37 | try { 38 | Thread.sleep(2*timeOut); 39 | } catch (InterruptedException e) { 40 | // TODO Auto-generated catch block 41 | e.printStackTrace(); 42 | } 43 | //we send another event directly after, which should not be forwarded 44 | ktimeout.addEvent(event1); 45 | Assert.assertEquals(listener.events.size(), 2); 46 | } 47 | @Test 48 | public void multipleKeyTest() { 49 | KeyedTimeout ktimeout = new KeyedTimeout("1000", "Select ?key WHERE{?key a }"); 50 | TestListener listener = new TestListener(); 51 | ktimeout.addListener(listener); 52 | String event1 = " a "; 53 | ktimeout.addEvent(event1); 54 | Assert.assertEquals(listener.events.size(), 1); 55 | Assert.assertEquals(event1, listener.events.get(0)); 56 | //we send another event directly after, which should be forwarded 57 | String event2 = " a "; 58 | ktimeout.addEvent(event2); 59 | Assert.assertEquals(listener.events.size(), 2); 60 | Assert.assertEquals(event2, listener.events.get(1)); 61 | 62 | } 63 | 64 | 65 | private class TestListener implements ListenerInf{ 66 | 67 | public List events = new ArrayList(); 68 | 69 | @Override 70 | public void notify(int queryID, String event) { 71 | this.events.add(event); 72 | 73 | } 74 | 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/test/java/idlab/massif/utils/TableUtilsTest.java: -------------------------------------------------------------------------------- 1 | package idlab.massif.utils; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.junit.Test; 9 | 10 | import junit.framework.Assert; 11 | 12 | public class TableUtilsTest { 13 | 14 | @Test 15 | public void singleKeyResultTableTest() { 16 | String queryResult = "key,\n" 17 | + "http://test/event1,"; 18 | Map> result = TableUtils.convertStringToTable(queryResult); 19 | Map> expected = new HashMap>(); 20 | expected.put("key", new ArrayList()); 21 | expected.get("key").add("http://test/event1"); 22 | Assert.assertEquals(expected, result); 23 | 24 | } 25 | @Test 26 | public void whiteSpaceTableTest() { 27 | String queryResult = "key , \n" 28 | + "http://test/event1 , "; 29 | Map> result = TableUtils.convertStringToTable(queryResult); 30 | Map> expected = new HashMap>(); 31 | expected.put("key", new ArrayList()); 32 | expected.get("key").add("http://test/event1"); 33 | Assert.assertEquals(expected, result); 34 | 35 | } 36 | @Test 37 | public void multipleLinesTableTest() { 38 | String queryResult = "key , \n" 39 | + "http://test/event3 , \n" 40 | + "http://test/event2 , \n" 41 | + "http://test/event1 , "; 42 | Map> result = TableUtils.convertStringToTable(queryResult); 43 | Map> expected = new HashMap>(); 44 | expected.put("key", new ArrayList()); 45 | expected.get("key").add("http://test/event3"); 46 | expected.get("key").add("http://test/event2"); 47 | expected.get("key").add("http://test/event1"); 48 | Assert.assertEquals(expected, result); 49 | 50 | } 51 | @Test 52 | public void multipleKeysTableTest() { 53 | String queryResult = "key1, key2\n" 54 | 55 | + "http://test/event1 , http://test/event2"; 56 | Map> result = TableUtils.convertStringToTable(queryResult); 57 | Map> expected = new HashMap>(); 58 | expected.put("key1", new ArrayList()); 59 | expected.put("key2", new ArrayList()); 60 | expected.get("key2").add("http://test/event2"); 61 | expected.get("key1").add("http://test/event1"); 62 | Assert.assertEquals(expected, result); 63 | 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /web_old/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KNowledgeOnWebScale/StreamingMASSIF/c84fcaf251f8dffbb32bb188821cb2159b1646fd/web_old/icon.png -------------------------------------------------------------------------------- /web_old/style.css: -------------------------------------------------------------------------------- 1 | #gate { 2 | cursor: pointer; 3 | margin-bottom: 100px; 4 | width: 83px; 5 | height: 46px; 6 | } 7 | 8 | #ball { 9 | cursor: pointer; 10 | width: 40px; 11 | height: 40px; 12 | } -------------------------------------------------------------------------------- /webfiles/.gitignore: -------------------------------------------------------------------------------- 1 | /web/ 2 | -------------------------------------------------------------------------------- /webfiles/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | --------------------------------------------------------------------------------