Repository: RDFLib/rdflib

Issue:
can't get rid of default prefix definition in trig serialization
I have a trig file with just one graph like this:

```trig
@prefix adm: <http://purl.bdrc.io/ontology/admin/> .
@prefix bda: <http://purl.bdrc.io/admindata/> .
@prefix bdg: <http://purl.bdrc.io/graph/> .

bdg:W1NLM5228 {
    bda:W1NLM5228 a adm:AdminData ;
        adm:status bda:StatusReleased .
}
```

In my use case I need to be able to read it, modify it and then write it with the same prefix definitions, but when I do something very simple like

```python
import rdflib
from rdflib import Literal, Graph, Dataset, URIRef
import os

ds = Dataset()
ds.parse("bug.trig", format="trig", publicID=URIRef("http://purl.bdrc.io/graph/W1NLM5228"))
ds.remove_graph(URIRef("urn:x-rdflib:default")) # doesn't work
ds.serialize(filepath+"-bug", format="trig")
```

the output I get is

```trig
@prefix adm: <http://purl.bdrc.io/ontology/admin/> .
@prefix bda: <http://purl.bdrc.io/admindata/> .
@prefix bdg: <http://purl.bdrc.io/graph/> .
@prefix ns1: <urn:x-rdflib:> .

bdg:W1NLM5228 {
    bda:W1NLM5228 a adm:AdminData ;
        adm:status bda:StatusReleased .
}
```

and I seem unable to remove the `ns1: <urn:x-rdflib:>` prefix definition. Is there any way to do so? Thanks!

Discussion:
eroux: Thanks for the merge!
aucampia: Sorry for the delay in review, I will try check by the weekend.
coveralls: 
[![Coverage Status](https://coveralls.io/builds/55536390/badge)](https://coveralls.io/builds/55536390)

Coverage: 90.63% (-0.004%) from 90.634% when pulling **aca39df5f20c0a7e26f4a17f708977b26b31bece on eroux:main** into **8213b7465783e636a394c86fd2387a44523d9c2f on RDFLib:main**.


Patch:
diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py
index 0fa62b71..d4052b29 100644
--- a/rdflib/plugins/serializers/trig.py
+++ b/rdflib/plugins/serializers/trig.py
@@ -34,6 +34,9 @@ class TrigSerializer(TurtleSerializer):
 
     def preprocess(self):
         for context in self.contexts:
+            # do not write unnecessary prefix (ex: for an empty default graph)
+            if len(context) == 0:
+                continue
             self.store = context
             self.getQName(context.identifier)
             self._subjects = {}


Test patch:
diff --git a/test/test_trig.py b/test/test_trig.py
index dbb1adbf..30bff634 100644
--- a/test/test_trig.py
+++ b/test/test_trig.py
@@ -192,3 +192,9 @@ def test_prefixes():
     assert "ns2: <http://ex.org/docs/".encode("latin-1") in data, data
     assert "<ns2:document1>".encode("latin-1") not in data, data
     assert "ns2:document1".encode("latin-1") in data, data
+
+
+def test_issue_2154():
+    ds = rdflib.Dataset()
+    sg = ds.serialize(format="trig")
+    assert "prefix" not in sg, sg